简体   繁体   English

打开 mp 减少令人困惑

[英]open mp reduction confusing

So I was trying to do simple multiplication between two arrays then add up the result of each multiplication and I am really confused by the reduction, here is my code:所以我试图在两个数组之间进行简单的乘法,然后将每个乘法的结果相加,我真的对减少感到困惑,这是我的代码:

#include <omp.h>
#include <stdio.h>
#define SizeOfVector 8
#define NumberOfThreads 4
int main(){
    const int X[SizeOfVector] = {0,2,3,4,5,6,7,8};
    const int Y[SizeOfVector] = {1,2,4,8,16,32,64,128};
    int Result[SizeOfVector] = {0};
    int Sum = 0;
    unsigned short id;

    omp_set_num_threads(NumberOfThreads);

    #pragma omp parallel private(id)
    {
        id = omp_get_thread_num();

        #pragma omp for reduction(+:Sum)
        for(unsigned short i = 0; i < SizeOfVector; i++)
        {
            Result[i] = X[i] * Y[i];
            Sum = Result[i];    //Problem Here
            printf("Partial result by thread[%d]= %d\n", id, Result[i]);
        }
    }
    printf("Final result= %d\n", Sum);
    return 0;
}

The thing is, if i change the "Sum = Result[i]" to "Sum += Result[i]" I get the correct result.问题是,如果我将“Sum = Result[i]”更改为“Sum += Result[i]”,我会得到正确的结果。 Why does this happen?为什么会发生这种情况? Isn't a local variable of Sum made and initialized to each thread, then the reduction adds it all up when all the threads are done?不是为每个线程创建并初始化了 Sum 的局部变量,然后在所有线程完成后将其全部加起来吗?

Here is the result with Sum += Result[i]:这是 Sum += Result[i] 的结果:

Partial result by thread[2]= 80
Partial result by thread[2]= 192
Partial result by thread[0]= 0
Partial result by thread[0]= 4
Partial result by thread[1]= 12
Partial result by thread[1]= 32
Partial result by thread[3]= 448
Partial result by thread[3]= 1024
Final result= 1792

And Here is the result with Sum = Result[i]:这是 Sum = Result[i] 的结果:

Partial result by thread[2]= 80
Partial result by thread[2]= 192
Partial result by thread[0]= 0
Partial result by thread[0]= 4
Partial result by thread[3]= 448
Partial result by thread[3]= 1024
Partial result by thread[1]= 12
Partial result by thread[1]= 32
Final result= 1252

Each thread is running through two iterations before coming to a final result for Sum .Sum的最终结果之前,每个线程都经过两次迭代。 Because you are not adding to Sum each iteration, but rather assigning it, the final result will simply be Result[i] for whatever i was the last run on that thread.因为您不是在每次迭代中添加 Sum ,而是分配它,最终结果将只是Result[i] ,无论i在该线程上最后一次运行。 That is the value that is ultimately summed up with the results of all the other threads.这是最终与所有其他线程的结果相加的值。 You need Sum += Result[i] so that each thread keeps its own running Sum until they meet back up and add the different Sum s together.您需要Sum += Result[i]以便每个线程保持自己的运行Sum直到它们相遇并将不同的Sum添加在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM