标签:
<pre name="code" class="cpp">#include<stdio.h> #include<time.h> #include<omp.h> static long num_steps = 100000; double step,pi; void main() { clock_t t1=clock(); int i; double x,sum = 0.0; step = 1.0/(double)num_steps; #pragma omp parallel for reduction(+:sum) for(i = 0;i<num_steps;i++) { x = (i+0.5)*step; sum = sum + 4.0/(1.0+x*x); printf("%d",omp_get_thread_num()); } pi = step *sum; clock_t t2 =clock(); printf("pi=%f,用时:%d\n",pi,t2-t1); }上述代码使用了规约rduction(op:list);这样每个线程会有一个私有的list,当所有线程的list计算完成之后,每个线程的私有list值将被op规约为一个list值。如果不进行归约的话可能会导致每次的运行结果都不一样
标签:
原文地址:http://blog.csdn.net/linscut/article/details/42739989