When is the reduction needed?

余生长醉 提交于 2020-01-09 11:50:00

问题


I've written this code which reads a Matrix and it basically sums the values of the matrix... But my question would be, since I've tried doing the pragma in different ways, I found that the reduction (+:sum) wouldn't be necessary, but, I just don't know why, I might have missed the actual sense of the reduction system in this case. This would be the alternative: #pragma omp parallel for private(i, j) reduction (+:sum)

And this would be the code:

#include <stdio.h>
#include <math.h>
#include <omp.h>
#include <unistd.h>


int main ()
{

    printf("===MATRIX SUM===\n");
    printf("N ROWS: ");
    int i1; scanf("%d",&i1);
    printf("M COLUMNS: ");
    int j1; scanf("%d",&j1);
    int matrixA[i1][j1];

    int i, j;

    for(i = 0; i < i1; i++){
        for (j = 0; j < j1; j++){
            scanf("%d",&matriuA[i][j]);
        }
    }

    printf("\nMATRIX A: \n");
    for (i = 0; i < i1; i++){
        for (j = 0; j < j1; j++){
            printf("%d ", matrixA[i][j]);
        }
        printf("\n");
    }
    int sum = 0;
    #pragma omp parallel for private(i, j)
        for (i = 0; i < i1; i++)
            for (j = 0; j < j1; j++){
                sum += matrixA[i][j];
           }


    printf("\nTHE RESULT IS: %d", sum);

    return 0;
}

And, I would like to ask, if there would be like, a better solution for the pragma reduction since I read that's the most efficient way.


回答1:


The code you posted is not correct without the reduction clause.

sum += matrixA[i][j];

Will cause a classic race condition when executed by multiple threads in parallel. Sum is a shared variable, but sum += ... is not an atomic operation.

(sum is initially 0, all matrix elements 1)
Thread 1                     |  Thread 2
-----------------------------------------------------------
tmp = sum + matrix[0][0] = 1 |
                             | tmp = sum + matrix[1][0] = 1
sum = tmp = 1                |
                             | sum = tmp = 1 (instead of 2)

The reduction fixes exactly this. With reduction, the loop will work on an implicit thread-local copy of the sum variable. At the end of the region, the original sum variable will be set to the sum of all thread-local copies (in a correct way without race-conditions).

Another solution would be to mark the sum += ... as atomic operation or critical section. That, however has a significant performance penalty.



来源:https://stackoverflow.com/questions/40819136/when-is-the-reduction-needed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!