找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4326|回复: 0

【程序语言】并行编程——openMPI初探

[复制链接]
发表于 2012-1-17 10:06:28 | 显示全部楼层 |阅读模式
(1)openMPI的配置(windows平台+vs2010)。在Visul Studio中配置openMPI十分简单,只需打开“项目 - > 属性 - > C/C++ - > 语言”中将“OpenMPI支持”选为"是" 如下图所示:        
     
这样你就可以开始OpenMPI之旅了。
(2)下面开始我们最简单的OpenMPI语句,hello world!#include "stdafx.h"
  1. #include <omp.h>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.         #pragma omp parallel num_threads(8)
  7.         cout<<"hello world! "<<"thread numbers: "<<omp_get_thread_num()<<endl;
  8. }
复制代码
    其中#pragma omp parallel是一句编译指导语句,告诉编译器后面的语句需要并行处理.num_threads(8)给出线程数为8,可以不给出线程数,一般会有一个默认值,我的机子上是2.

(3)OpenMPI的循环并行化
最基础和典型的并行部分应该就是循环,我们先从循环的并行开始#include "stdafx.h"
  1. #include <omp.h>
  2. #include <iostream>
  3. #include <time.h>
  4. using namespace std;
  5. const int core   = 2;
  6. const int thread = 4;
  7. void test()
  8. {
  9.         int sum = 0;       
  10.         for(int i=0; i<10000000; ++i)
  11.         {        sum *= i;        }
  12. }
  13. int main()
  14. {
  15.         clock_t start = clock();
  16.        
  17.                 #pragma omp parallel for
  18.                 for(int i=0; i<core; i++)
  19.                 {        test();                                }
  20.                
  21.        
  22.         clock_t finish = clock();
  23.         cout<<"time used is: "<<finish -start<<"ms"<<endl;
  24.         start = clock();
  25.                 for(int i=0; i<core; i++)
  26.                 {        test();        }
  27.         finish = clock();
  28.         cout<<"time used is: "<<finish -start<<"ms"<<endl;
  29. }
复制代码
这段代码中添加了运行时间测试语句,是为了比较并行处理的效果,并行指导语句只有一句#pragma omp parallel for,其作用就是将for循环的内部迭代使用多个线程处理。
第二个循环在非并行的参照组,在我的机子上这两段代码的时间分别是35ms,65ms左右。

(3)OpenMPId 的一般语句并行化        #pragma omp parallel
  1.         {       
  2.                 /*并行区域1*/
  3.                 #pragma omp sections
  4.                 {
  5.                         #pragma omp section
  6.                         {        cout<<"hello ->thread:"<<omp_get_thread_num()<<endl;        }
  7.                         #pragma omp section
  8.                         {        cout<<"hello ->thread:"<<omp_get_thread_num()<<endl;        }
  9.                 }
  10.                 /*并行区域2*/
  11.                 #pragma omp sections
  12.                 {
  13.                         #pragma omp section
  14.                         {        cout<<"world ->thread:"<<omp_get_thread_num()<<endl;        }
  15.                         #pragma omp section
  16.                         {        cout<<"world ->thread:"<<omp_get_thread_num()<<endl;        }
  17.                 }
  18.         };
复制代码
    使用#pragma omp parallel{ 语句 }的形式给出总的并行块,sections划分出并行分区,区域内部的section之间多线程并行处理,sections之间串行处理,如上述程序中,并行区域1先处理,并行区域2后处理;而并行区域1中的显示hello的两个section并行处理,并行区域2中显示world的两个section并行处理。





(4)OpenMPI 的并行调度算法
OpenMPI 的调度算法一共有三个:static , dynamic, guided. 另外有一个根据环境变量选择三者之一的runtime选项。使用方法也十分简单:        sum =0;
  1.         #pragma omp parallel for schedule(dynamic)
  2.         for(int i=0; i<100; ++i)
  3.         {        sum +=i;        }
  4.         cout<<sum<<endl;
复制代码
添加的 schedule(dynamic) ,同理有 schedule(static) 和 schedule(guided) ,另外你如果不给出schedule,那么默认选择的是static选项。三者的区别如下:





static       : 每个线程分配  迭代总数 / 线程数 的迭代次数,如9500次迭代,10个线程,那么每个线程被分配950次迭代。任务分配可能不均衡。因为每次迭代的时间可能不同。
dynamic : 每次线程完成了当前工作就重新申请新的工作,开销比static大,但能基本保证任务分配的均衡。
guided   :  程序员可以给出分配公式,指导任务的分配。

待续……






作者:theprinceofelf 发表于2012-1-16 21:32:30 原文链接


您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-29 21:44 , Processed in 0.012253 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表