【openMP】single (主从模式)

6 篇文章 0 订阅
3 篇文章 0 订阅

omp single

指定在当前的并行域里当前代码块只被执行一次(任意线程均可)

用法:

#pragma omp single [clause,clause , ...]

参数:

clause

Can be one or more of the following clauses:

copyprivate(list)

Provides a mechanism to use a private variable in list to broadcast a value from the data environment of one implicit task to the data environments of the other implicit tasks belonging to the parallel region.

firstprivate(list)

Provides a superset of the functionality provided by the private clause. Each private data object is initialized with the value of the original object.

nowait(integer expression)

Indicates that an implementation may omit the barrier at the end of the worksharing region.

private(list)

Declares variables to be private to each thread in a team.

 

当在主从模式使用single制导语句时,由于有条件分支,因此需要加上nowait语句(必须),否则程序会一直等所有线程同步而导致程序无法往下运行。

如下程序:

int main() {
   int data, flag = 0;
   #pragma omp parallel num_threads(2)
   {
      if (omp_get_thread_num()==0) {
         data = 42;
        #pragma omp single nowait
            {
                int i=0;
            }
      }
      else if (omp_get_thread_num()==1) {
         #pragma omp flush(flag, data)
         while (flag < 1) {
            #pragma omp flush(flag, data)
         }
         printf("flag=%d data=%d\n", flag, data);
         #pragma omp flush(flag, data)
         printf("flag=%d data=%d\n", flag, data);
      }
   }
   return 0;
}

如果omp single后面没有nowait,则程序不会跳出并行区,会一直在omp single结构化块结束的位置等待所有线程同步。

总结:omp single 在条件分支内部使用的时候,一定要加上nowait子句。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值