OpenMP: sections分段并行

 除了循环结构可以进行并行之外,还可以进行分段并行(parallel section)。迄今为止,每谈到如何去并行一个程序时,我们主要关心的是在同一时间将一个任务划分成多个然后用多线程去完成。然而,如果这个程序的一系列操作中后面的计算并不依赖于前面的计算,意思就是说它们之间相互并不依赖,这将有利于分配不同的任务给不同的线程去执行。当然,这对于那些执行过程非常短(总体计算量非常小)或程序本身具有很强的依赖性(前面与后面之间、循环之间),要实现并行是非常困难或不可能的。为了对付这种情况,OpenMP提供了分段协同工作结构(sections work-sharing construct),它可以使整个程序任务并行分割,并指定每个任务给不同的线程。在Fortran中程序中以sections指令开头,end sections指令结束,其中每段以section指令开始。
      C中sections指令结构如下:
#pragma omp sections [clause[[,] clause] .. ]
{
[#pragma omp section]
structured-block
[#pragma omp section]
structured-block
……………..
}
      其中条件clause可以为private(list)、firstprivate(list)、lastprivate(list)、reduction(operator: list)和 等。
      采用section定义的每段程序都将只执行一次,sections中的每段section将并行执行。一个程序中可以定义多个sections,每个sections中又可以定义多个section。同一个sections中section之间处于并行状态。sections与其他sections之间处于串行状态。

      如下在程序中定义两段sections,其中每段都有两个section,代码如下:

[cpp]  view plain copy
  1. //File: SectionsTest.cpp  
  2. #include"stdafx.h"  
  3. #include<omp.h>  
  4. #include<iostream>  
  5. using namespace std;  
  6. void SectionsTest()  
  7. {  
  8.     int i;  
  9.     #pragma omp parallel num_threads(4)  
  10.     {  
  11.         #pragma omp sections //第1个sections  
  12.         {  
  13.             #pragma omp section  
  14.             {  
  15.                 cout<<"section 1 线程ID:"<<omp_get_thread_num()<<"\n";  
  16.             }  
  17.             #pragma omp section  
  18.             {  
  19.                 cout<<"section 2 线程ID:"<<omp_get_thread_num()<<"\n";  
  20.             }  
  21.         }  
  22.         #pragma omp sections //第2个sections  
  23.         {  
  24.             #pragma omp section  
  25.             {  
  26.                 cout<<"section 3 线程ID:"<<omp_get_thread_num()<<"\n";  
  27.             }  
  28.             #pragma omp section  
  29.             {  
  30.                 cout<<"section 4 线程ID:"<<omp_get_thread_num()<<"\n";  
  31.             }  
  32.         }  
  33.     }  
  34. }  

      运行程序,其结果有:

section 1 线程ID:0
section 2 线程ID:1
section 3 线程ID:3
section 4 线程ID:1

      或者其他的结果,但是都会发现section 1与section 2的线程ID永远都可能相同(除非CPU只支持一个线程,或上面代码中num_threads设置为1),同样,section 3与section 4的线程ID也不可能相同,但是,section 1的线程ID有可能与section 3或section 4的线程ID相同,section 2的线程ID有可能与section 3或section 4的线程ID相同。之所以出现这种现象的原因就是,第一个sections与第二个sections在程序中处于串行,而第一个sections中的section1和section2它们处于并行,第二个sections中的section3和section4也处于并行。上面程序的运行示意图如下图所示:

      如果将上段程序中num_threads设置为2,则计算结果中线程ID只会是0和1,这样可以更加明确sections之间的运行顺序。

      上例中并行是基于每个sections的,如果只有一个sections,其并行定义如下例子的代码:

[cpp]  view plain copy
  1. //File: SectionsTest02.cpp  
  2. #include"stdafx.h"  
  3. #include<omp.h>  
  4. #include<iostream>  
  5. using namespace std;  
  6. void SectionsTest02()  
  7. {  
  8.     #pragma omp parallel sections num_threads(4)  
  9.     {  
  10.         #pragma omp section  
  11.         {  
  12.             cout<<"section 1 线程ID:"<<omp_get_thread_num()<<"\n";  
  13.         }  
  14.         #pragma omp section  
  15.         {  
  16.             cout<<"section 2 线程ID:"<<omp_get_thread_num()<<"\n";  
  17.         }  
  18.         #pragma omp section  
  19.         {  
  20.             cout<<"section 3 线程ID:"<<omp_get_thread_num()<<"\n";  
  21.         }  
  22.         #pragma omp section  
  23.         {  
  24.             cout<<"section 4 线程ID:"<<omp_get_thread_num()<<"\n";  
  25.         }  
  26.     }  
  27. }  

运行程序,其结果可能为如下:

section 1 线程ID:0

section 3 线程ID:2

section 2 线程ID:1

section 4 线程ID:3

      很明显section1与2、3、4不一定按顺序输出,因为在这里它们已经并行,输出顺序具体要看那个线程先执行完,不像前面的代码中第一个sections肯定是在第二个sections的前面输出。

      如果要使sections之间并行,只需要在sections后加上nowait指令即可,如下例子:

[cpp]  view plain copy
  1. //File: SectionsTest03.cpp  
  2. #include"stdafx.h"  
  3. #include<omp.h>  
  4. #include<iostream>  
  5. using namespace std;  
  6. void SectionsTest03()  
  7. {  
  8.     #pragma omp parallel  
  9.     {  
  10.         #pragma omp sections nowait //第个sections  
  11.         {  
  12.             #pragma omp section  
  13.             {  
  14.                 cout<<"section 1 线程ID:"<<omp_get_thread_num()<<"\n";  
  15.             }  
  16.             #pragma omp section  
  17.             {  
  18.                 cout<<"section 2 线程ID:"<<omp_get_thread_num()<<"\n";  
  19.             }  
  20.             #pragma omp section  
  21.             {  
  22.                 cout<<"section 3 线程ID:"<<omp_get_thread_num()<<"\n";  
  23.             }  
  24.         }  
  25.         #pragma omp sections //第个sections  
  26.         {  
  27.             #pragma omp section  
  28.             {  
  29.                 cout<<"section 4 线程ID:"<<omp_get_thread_num()<<"\n";  
  30.             }  
  31.             #pragma omp section  
  32.             {  
  33.                 cout<<"section 5 线程ID:"<<omp_get_thread_num()<<"\n";  
  34.             }  
  35.             #pragma omp section  
  36.             {  
  37.                 cout<<"section 6 线程ID:"<<omp_get_thread_num()<<"\n";  
  38.             }  
  39.         }  
  40.     }  
  41. }  

      运行程序,其结果如下:

section 1 线程ID:6

section 5 线程ID:3

section 2 线程ID:1

section 4 线程ID:4

section 3 线程ID:5

section 6 线程ID:2

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值