#include<time.h>#include<stdio.h>#include<stdlib.h>#include"omp.h"#defineNUM_THREADS5#definePAD8 //跟CPU内存分配有关double step;staticlong num_steps;intmain() {clock_tstart_t,end_t;doubletotal_t;start_t=clock(); num_steps =100000000;double pi; pi=0.0; step =1.0/ (double)num_steps; //将1平分成100000步omp_set_num_threads(NUM_THREADS); //设置要使用的线程数目#pragmaompparallel//开始并发执行 {double x,sum;int id , i; id =omp_get_thread_num(); //获取每个线程的id编号 sum=0.0;for (i=id; i < num_steps; i = i + NUM_THREADS) { x = (i -0.5) * step; sum +=4.0/ (1.0+ x * x); }#pragmaompcritical { pi+=sum*step; } }end_t=clock();total_t= (double)(end_t-start_t) / CLOCKS_PER_SEC;printf("运行时间为%fs\t%f\n",total_t, pi);return0; }
for reduction循环规约
#pragmaompparallel//开始并发执行 {double x,sum;int id , i; id =omp_get_thread_num(); //获取每个线程的id编号 sum=0.0;#pragmaompparallelforreduction(+:sum) //for循环规约,多个sum最后会加到最外层的sumfor (i=id; i < num_steps; i = i + NUM_THREADS) { x = (i -0.5) * step; sum +=4.0/ (1.0+ x * x); } pi+=sum*step; }
for 循环 lastprivate(sum)规约
double sum=0.0;#pragmaompparallel//开始并发执行 {double x,sum;int id , i; id =omp_get_thread_num(); //获取每个线程的id编号#pragmaompparallelforlastprivate(sum) //获取每个初始化的sum=0.0for (i=id; i < num_steps; i = i + NUM_THREADS) { x = (i -0.5) * step; sum +=4.0/ (1.0+ x * x); }// pi+=sum*step; //这里的sum等于最后一个退出的sum的值,虽然能算出结果,但只是其中一个线程的结果,精度会有所下降 }
for循环 private(sum)规约
#pragmaompparallel//开始并发执行 {double x,sum=1.0;int id , i; id =omp_get_thread_num(); //获取每个线程的id编号#pragmaompparallelforlastprivate(sum)for (i=id; i < num_steps; i = i + NUM_THREADS) { x = (i -0.5) * step; sum +=4.0/ (1.0+ x * x); }printf("%f\n",sum); pi+=sum*step; }//最后输出4个1.0