因此,我试图在两个数组之间进行简单的乘法,然后将每个乘法的结果相加,而归约让我很困惑,这是我的代码:
'Access-Control-Allow-Methods': 'POST,GET,OPTIONS',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
问题是,如果我将“ Sum = Result [i]”更改为“ Sum + = Result [i]”,则会得到正确的结果。 为什么会这样? 难道不是对每个线程都创建并初始化了Sum的局部变量,然后在所有线程完成后,归约化将其加起来吗?
这是Sum + = Result [i]的结果:
#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 = 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
答案 0 :(得分:1)
每个线程运行两次迭代,然后得出Sum
的最终结果。因为您不是在每个迭代中都添加Sum,而是对其进行分配,所以对于该线程上次运行的Result[i]
而言,最终结果将只是i
。那是最终与所有其他线程的结果相加的值。您需要Sum += Result[i]
,以便每个线程保持自己的运行状态Sum
,直到它们见面并把不同的Sum
加在一起。