- C/C++ code
static long num_steps = 10000;double step;double pi;void main(){ int i; double x,sum = 0.0; step = 1.0/(double) num_steps;#pragma omp parallel {#pragma omp for reduction(+:pi) for (i=0;i<num_steps;i++) { x = (i+0.5)*step; sum = sum + 4.0/(1.0 +x*x); } pi = step * sum; } cout <<pi<<endl;}
输出结果是2.48922。我想把pi设为 private(pi)这样会报错。我这个归约错在哪里?
------解决方案--------------------------------------------------------
改成这样 #pragma omp for reduction(+:sum)
被归约的变量搞错了