gcc openmp thread reuse

后端 未结 1 1892
甜味超标
甜味超标 2020-12-21 03:50

I am using gcc\'s implementation of openmp to try to parallelize a program. Basically the assignment is to add omp pragmas to obtain speedup on a program that finds amicable

相关标签:
1条回答
  • 2020-12-21 04:08

    OpenMP thread teams are instantiated on entering the parallel section. This means, indeed, that the thread creation is repeated every time the inner loop is starting.

    To enable reuse of threads, use a larger parallel section (to control the lifetime of the team) and specificly control the parallellism for the outer/inner loops, like so:

    Execution time for test.exe 1 1000000 has gone down from 43s to 22s using this fix (and the number of threads reflects the numThreads defined value + 1

    PS Perhaps stating the obvious, it would not appear that parallelizing the inner loop is a sound performance measure. But that is likely the whole point to this exercise, and I won't critique the question for that.

    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include <omp.h>
    
    #define numThread 2
    int main(int argc, char* argv[]) {
        int ser[29], end, i, j, a, limit, als;
        als = atoi(argv[1]);
        limit = atoi(argv[2]);
    #pragma omp parallel num_threads(numThread)
        {
    #pragma omp single
            for (i = 2; i < limit; i++) {
                ser[0] = i;
                for (a = 1; a <= als; a++) {
                    ser[a] = 1;
                    int prev = ser[a-1];
                    if ((prev > i) || (a == 1)) {
                        end = sqrt(prev);
                        int sum = 0;//added this
    #pragma omp parallel for reduction(+:sum) //added this
                        for (j = 2; j <= end; j++) {
                            if (prev % j == 0) {
                                sum += j;
                                sum += prev / j;
                            }
                        }
                        ser[a] = sum + 1;//added this
                    }
                }
                if (ser[als] == i) {
                    printf("%d", i);
                    for (j = 1; j < als; j++) {
                        printf(", %d", ser[j]);
                    }
                    printf("\n");
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题