所以我试着在两个数组之间做简单的乘法,然后把每个乘法的结果加起来,我真的被减法搞糊涂了,这是我的代码:
#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不是一个局部变量,并初始化到每个线程,然后在所有线程都完成时,缩减会将其相加吗?
以下是+=求和结果的结果
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的结果
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发布于 2019-03-16 03:08:22
在得出Sum的最终结果之前,每个线程都会经过两次迭代。因为您不是在每次迭代中添加Sum,而是赋值它,所以对于上次在该线程上运行的任何i,最终结果都将是Result[i]。这是最终与所有其他线程的结果相加的值。您需要Sum += Result[i],以便每个线程保持其自己的运行Sum,直到它们遇到备份并将不同的Sum添加在一起。
https://stackoverflow.com/questions/55188918
复制相似问题