OpenMP 嵌套并行操作的特性

1 执行模型

       OpenMP 采用 fork-join(分叉-合并)并行执行模式。线程遇到并行构造时,就会创建由其自身及其他一些额外(可能为零个)线程组成的线程组。遇到并行构造的线程成为新组中的主线程。组中的其他线程称为组的从属线程。所有组成员都执行并行构造内的代码。如果某个线程完成了其在并行构造内的工作,它就会在并行构造末尾的隐式屏障处等待。当所有组成员都到达该屏障时,这些线程就可以离开该屏障了。主线程继续执行并行构造之后的用户代码,而从属线程则等待被召集加入到其他组。

       OpenMP 并行区域之间可以互相嵌套。如果禁用嵌套并行操作,则由遇到并行区域内并行构造的线程所创建的新组仅包含遇到并行构造的线程。如果启用嵌套并行操作,则新组可以包含多个线程。

       OpenMP 运行时库维护一个线程池,该线程池可用作并行区域中的从属线程。当线程遇到并行构造并需要创建包含多个线程的线程组时,该线程将检查该池,从池中获取空闲线程,将其作为组的从属线程。如果池中没有足够的空闲线程,则主线程获取的从属线程可能会比所需的要少。组完成执行并行区域时,从属线程就会返回到池中。

2 控制嵌套并行操作

通过在执行程序前设置各种环境变量,可以在运行时控制嵌套并行操作。

       2.1 OMP_NESTED

       可通过设置 OMP_NESTED 环境变量或调用 omp_set_nested() 来启用或禁用嵌套并行操作。

以下示例中的嵌套并行构造具有三个级别。


示例 1 嵌套并行操作示例
[cpp]  view plain copy
  1. #include <omp.h>  
  2. #include <stdio.h>  
  3. void report_num_threads(int level)  
  4. {  
  5.     #pragma omp single  
  6.     {  
  7.         printf("Level %d: number of threads in the team - %d\n",  
  8.                   level, omp_get_num_threads());  
  9.     }  
  10.  }  
  11. int main()  
  12. {  
  13.     omp_set_dynamic(0);  
  14.     #pragma omp parallel num_threads(2)  
  15.     {  
  16.         report_num_threads(1);  
  17.         #pragma omp parallel num_threads(2)  
  18.         {  
  19.             report_num_threads(2);  
  20.             #pragma omp parallel num_threads(2)  
  21.             {  
  22.                 report_num_threads(3);  
  23.             }  
  24.         }  
  25.     }  
  26.     return(0);  
  27. }  

        启用嵌套并行操作时,编译和运行此程序会产生以下(经过排序的)输出:


% setenv OMP_NESTED TRUE
% a.out
Level 1: number of threads in the team - 2
Level 2: number of threads in the team - 2
Level 2: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 3: number of threads in the team - 2

        比较禁用嵌套并行操作时运行相同程序的输出结果:


% setenv OMP_NESTED FALSE
% a.out
Level 1: number of threads in the team - 2
Level 2: number of threads in the team - 1
Level 3: number of threads in the team - 1
Level 2: number of threads in the team - 1
Level 3: number of threads in the team - 1

2.2 SUNW_MP_MAX_POOL_THREADS

       OpenMP 运行时库维护一个线程池,该线程池可用作并行区域中的从属线程。设置 SUNW_MP_MAX_POOL_THREADS 环境变量可控制池中线程的数量。缺省值为 1023。

       线程池只包含运行时库创建的非用户线程。它不包含初始线程或由用户程序显式创建的任何线程。如果将此环境变量设置为零,则线程池为空,并且将由一个线程执行所有并行区域。

       以下示例说明,如果池中没有足够的线程,并行区域可能获取较少的线程。代码与上面的代码相同。使所有并行区域同时处于活动状态所需的线程数为 8 个。池需要至少包含 7 个线程。如果将 SUNW_MP_MAX_POOL_THREADS 设置为 5,则四个最里面的并行区域中的两个区域可能无法获取所请求的所有从属线程。一种可能的结果如下所示。


% setenv OMP_NESTED TRUE
% setenv SUNW_MP_MAX_POOL_THREADS 5
% a.out
Level 1: number of threads in the team - 2
Level 2: number of threads in the team - 2
Level 2: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 3: number of threads in the team - 1
Level 3: number of threads in the team - 1

2.3 SUNW_MP_MAX_NESTED_LEVELS

       环境变量 SUNW_MP_MAX_NESTED_LEVELS 可控制需要多个线程的嵌套活动并行区域的最大深度。

       活动嵌套深度大于此环境变量值的任何活动并行区域将仅由一个线程来执行。如果并行区域没有 IF 子句,或者其 IF 子句计算为 true,则将此并行区域视为活动区域。活动嵌套级别的缺省最大数量是 4。

       以下代码将创建 4 级嵌套并行区域。如果将 SUNW_MP_MAX_NESTED_LEVELS 设置为 2,则嵌套深度为 3 和 4 的嵌套并行区域将由单个线程来执行。

[cpp]  view plain copy
  1. #include <omp.h>  
  2. #include <stdio.h>  
  3. #define DEPTH 5  
  4. void report_num_threads(int level)  
  5. {  
  6.     #pragma omp single  
  7.     {  
  8.         printf("Level %d: number of threads in the team - %d\n",  
  9.                level, omp_get_num_threads());  
  10.     }  
  11. }  
  12. void nested(int depth)  
  13. {  
  14.     if (depth == DEPTH)  
  15.         return;  
  16.   
  17.     #pragma omp parallel num_threads(2)  
  18.     {  
  19.         report_num_threads(depth);  
  20.         nested(depth+1);  
  21.     }  
  22. }  
  23. int main()  
  24. {  
  25.     omp_set_dynamic(0);  
  26.     omp_set_nested(1);  
  27.     nested(1);  
  28.     return(0);  
  29. }  


       使用最大嵌套级别 4 来编译和运行此程序会产生以下可能的输出。(实际结果取决于操作系统调度线程的方式。)


% setenv SUNW_MP_MAX_NESTED_LEVELS 4
% a.out |sort +2n
Level 1: number of threads in the team - 2
Level 2: number of threads in the team - 2
Level 2: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 3: number of threads in the team - 2
Level 4: number of threads in the team - 2
Level 4: number of threads in the team - 2
Level 4: number of threads in the team - 2
Level 4: number of threads in the team - 2
Level 4: number of threads in the team - 2
Level 4: number of threads in the team - 2
Level 4: number of threads in the team - 2
Level 4: number of threads in the team - 2

      使用设置为 2 的嵌套级别来运行会产生以下可能的结果:


% setenv SUNW_MP_MAX_NESTED_LEVELS 2
% a.out |sort 
Level 1: number of threads in the team - 2
Level 2: number of threads in the team - 2
Level 2: number of threads in the team - 2
Level 3: number of threads in the team - 1
Level 3: number of threads in the team - 1
Level 3: number of threads in the team - 1
Level 3: number of threads in the team - 1
Level 4: number of threads in the team - 1
Level 4: number of threads in the team - 1
Level 4: number of threads in the team - 1
Level 4: number of threads in the team - 1

      此外,这些示例只显示了一些可能的结果。实际结果取决于操作系统调度线程的方式。

3 在嵌套并行区域中使用 OpenMP 库例程

      在嵌套并行区域中调用以下 OpenMP 例程需要仔细斟酌。

- omp_set_num_threads()
- omp_get_max_threads()
- omp_set_dynamic()
- omp_get_dynamic()
- omp_set_nested()
- omp_get_nested()

      "set" 调用只影响调用线程所遇到的处于同一嵌套级别或内部嵌套级别的后续并行区域。它们不影响其他线程遇到的并行区域。

      "get" 调用将返回由调用线程设置的值。当某个线程成为执行并行区域的组的主线程后,所有其他的组成员会继承该主线程的值。当主线程退出嵌套并行区域,并继续执行封闭并行区域时,该线程的值会恢复为刚执行嵌套并行区域之前封闭并行区域中的值。


示例 2 在并行区域中调用 OpenMP 例程
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <omp.h>  
  3. int main()  
  4. {  
  5.     omp_set_nested(1);  
  6.     omp_set_dynamic(0);  
  7.     #pragma omp parallel num_threads(2)  
  8.     {  
  9.         if (omp_get_thread_num() == 0)  
  10.             omp_set_num_threads(4);       /* line A */  
  11.         else  
  12.             omp_set_num_threads(6);       /* line B */  
  13.   
  14.         /* The following statement will print out 
  15.          * 
  16.          * 0: 2 4 
  17.          * 1: 2 6 
  18.          * 
  19.          * omp_get_num_threads() returns the number 
  20.          * of the threads in the team, so it is 
  21.          * the same for the two threads in the team. 
  22.          */  
  23.         printf("%d: %d %d\n", omp_get_thread_num(),  
  24.                omp_get_num_threads(),  
  25.                omp_get_max_threads());  
  26.   
  27.         /* Two inner parallel regions will be created 
  28.          * one with a team of 4 threads, and the other 
  29.          * with a team of 6 threads. 
  30.          */  
  31.         #pragma omp parallel  
  32.         {  
  33.             #pragma omp master  
  34.             {  
  35.                 /* The following statement will print out 
  36.                  * 
  37.                  * Inner: 4 
  38.                  * Inner: 6 
  39.                  */  
  40.                 printf("Inner: %d\n", omp_get_num_threads());  
  41.             }  
  42.             omp_set_num_threads(7);      /* line C */  
  43.         }  
  44.         /* Again two inner parallel regions will be created, 
  45.          * one with a team of 4 threads, and the other 
  46.          * with a team of 6 threads. 
  47.          * 
  48.          * The omp_set_num_threads(7) call at line C 
  49.          * has no effect here, since it affects only 
  50.          * parallel regions at the same or inner nesting 
  51.          * level as line C. 
  52.          */  
  53.   
  54.         #pragma omp parallel  
  55.         {  
  56.             printf("count me.\n");  
  57.         }  
  58.     }  
  59.     return(0);  
  60. }  


编译和运行此程序会产生一种以下可能的结果:


% a.out
0: 2 4
Inner: 4
1: 2 6
Inner: 6
count me.
count me.
count me.
count me.
count me.
count me.
count me.
count me.
count me.
count me.

4 有关使用嵌套并行操作的一些提示

  • 嵌套并行区域提供一种直接的方法来允许多个线程参与到计算中。

    例如,假定您的程序包含两级并行操作,并且每个级别的并行操作等级为 2。此外,还假定您的系统有四个 CPU,您要使用全部四个 CPU 来加快此程序的执行速度。如果只并行化其中任意一个级别,则只需使用两个 CPU。您想要并行化两个级别。

  • 嵌套并行区域容易创建过多的线程,从而占用过多的系统资源。请适当地设置 SUNW_MP_MAX_POOL_THREADS 和SUNW_MP_MAX_NESTED_LEVELS 以限制使用的线程数,防止系统资源枯竭。

  • 创建嵌套并行区域会增加开销。如果在外部级别有足够的并行操作并且负载平衡,通常在计算的外部级别使用所有线程要比在内部级别创建嵌套并行区域更有效。

    例如,假定您的程序包含两级并行操作。外部级别的并行操作等级为 4,并且负载平衡。您的系统具有四个 CPU,您要使用所有四个 CPU 来加快此程序的执行速度。那么,通常将所有 4 个线程用于外部级别比将 2 个线程用于外部并行区域而将其他 2 个线程用作内部并行区域的从属线程的性能要好。

    实例:
    [cpp]  view plain copy
    1. //#include <stdio>    
    2. void report_num_threads(int level)    
    3. {    
    4. //#pragma omp parallel    
    5. {    
    6. printf("level %d:number of threads in the team - %d\n",level,omp_get_num_threads());    
    7. }    
    8. }    
    9.     
    10. int _tmain(int argc, _TCHAR* argv[])    
    11. {    
    12. omp_set_nested(1);    
    13. //omp_set_dynamic(0);    
    14. #pragma omp parallel num_threads(2)    
    15. {    
    16. //printf("here1\n");    
    17. report_num_threads(1);    
    18. #pragma omp parallel num_threads(2)    
    19. {    
    20. //printf("here1\n");    
    21. report_num_threads(2);    
    22. #pragma omp parallel num_threads(2)    
    23. {    
    24. //printf("here1\n");    
    25. report_num_threads(3);    
    26. }    
    27. }    
    28. }    
    29. return 0;    
    30. }    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值