首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >open mp reduction令人困惑

open mp reduction令人困惑
EN

Stack Overflow用户
提问于 2019-03-16 02:42:49
回答 1查看 63关注 0票数 3

所以我试着在两个数组之间做简单的乘法,然后把每个乘法的结果加起来,我真的被减法搞糊涂了,这是我的代码:

代码语言:javascript
复制
#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;
}

问题是,如果我将"Sum = Resulti“更改为"Sum += Resulti”,我会得到正确的结果。这一切为什么要发生?Sum不是一个局部变量,并初始化到每个线程,然后在所有线程都完成时,缩减会将其相加吗?

以下是+=求和结果的结果

代码语言:javascript
复制
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

下面是Sum = Resulti的结果

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-16 03:08:22

在得出Sum的最终结果之前,每个线程都会经过两次迭代。因为您不是在每次迭代中添加Sum,而是赋值它,所以对于上次在该线程上运行的任何i,最终结果都将是Result[i]。这是最终与所有其他线程的结果相加的值。您需要Sum += Result[i],以便每个线程保持其自己的运行Sum,直到它们遇到备份并将不同的Sum添加在一起。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55188918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档