算例
对的值通过级数进行计算,采用的公式为如下。
假设需要计算前N项的级数和,则的表达式为:
计算
分别使用OpenMP中的共享任务结构、private和critical语句、并行归约与串行程序进行计算精度和时间的对比,计算中N取值为10000000,对比结果如下:
pi : 3.14159Seriel Process Total Time: 0.069spi : 2.63998Parallel Process 1 Total Time: 0.309spi : 2.82509Parallel Process 2 Total Time: 0.044spi : 3.14159Parallel Process 3 Total Time: 0.035s
其中:Parallel Process 1为共享任务结构,Parallel Process 2为private和critical语句、Parallel Process 3为并行归约。
可以看出P1和P2与的值相差较远,P3算的最精确;P1所使用的时间比串行还要多,P2和P3所使用的时间都比串行少。
对于此问题,P3是最优的选择方案。
代码
// test case of OpenMPusing namespace std;static long num_steps = 10000000;double step;int SerielProcess(){ clock_t startTime, endTime; startTime = clock(); int i; double x, pi, sum = 0.0; step = 1.0 / (double)num_steps; for (i = 0; i < num_steps; i++) { x = (i + 0.5)*step; sum += 4.0 / (1.0 + x * x); } pi = step * sum; cout << "pi : " << pi << endl; endTime = clock(); cout << "Seriel Process Total Time: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl; return 0;}// 使用共享任务结构并行化的程序int ParallelProcess1(){ clock_t startTime, endTime; startTime = clock(); int i; double x, pi, sum[NUM_THREADS]; step = 1.0 / (double)num_steps; omp_set_num_threads(NUM_THREADS); { double x; int id; id = omp_get_thread_num(); for (i = id, sum[id] = 0.0; i < num_steps; i = i + NUM_THREADS) { x = (i + 0.5)*step; sum[id] += 4.0 / (1.0 + x * x); } } for (i = 0, pi = 0.0; i < NUM_THREADS; i++) pi += sum[i] * step; cout << "pi : " << pi << endl; endTime = clock(); cout << "Parallel Process 1 Total Time: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl; return 0;}// 使用private子句和critical部分并行化的程序int ParallelProcess2(){ clock_t startTime, endTime; startTime = clock(); int i; double x, pi = 0.0, sum; step = 1.0 / (double)num_steps; omp_set_num_threads(NUM_THREADS); { int id; id = omp_get_thread_num(); for (i = id, sum = 0.0; i < num_steps; i = i + NUM_THREADS) { x = (i + 0.5)*step; sum+= 4.0 / (1.0 + x * x); } pi += sum * step; } cout << "pi : " << pi << endl; endTime = clock(); cout << "Parallel Process 2 Total Time: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl; return 0;}// 使用并行归约得出的并行程序int ParallelProcess3(){ clock_t startTime, endTime; startTime = clock(); int i; double x, pi, sum = 0.0; step = 1.0 / (double)num_steps; omp_set_num_threads(NUM_THREADS); for (i = 0; i < num_steps; i++) { x = (i + 0.5)*step; sum += 4.0 / (1.0 + x * x); } pi = sum * step; cout << "pi : " << pi << endl; endTime = clock(); cout << "Parallel Process 3 Total Time: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl; return 0;}int main(){ SerielProcess(); ParallelProcess1(); ParallelProcess2(); ParallelProcess3(); system("pause"); return 0;}
Comments
Post a Comment