在openmp中突破结构化块 [英] breaking out of structured block in openmp

查看:107
本文介绍了在openmp中突破结构化块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用openmp编写程序,其中结构块是while循环.

I am trying to write a program using openmp in which the structure block is a while loop.

#pragma omp parallel num_threads(x)
while(condition){

}

我必须决定对任何线程停止的条件进行编码的方式.我需要知道在while循环中使用break语句是否合适.

I have to decide upon the way to code the condition on which any thread would stop. I need to know if it is proper to have a break statement in the while loop.

推荐答案

您的问题有点不完整.您说过"任何线程都会停止的条件",但是之后的数学呢:

Your Question is a bit incomplete. You said "Condition on Which any thread would stop", but what about the after math:

  1. 其余线程也应退出.
  2. 其余线程应继续进行,直到它们符合条件为止.

情况1:

bool abort = 0;
#pragma omp parallel num_threads(x) private(abort)
{
   while(!abort)
   {
      // The work you need to do.
      #pragma omp critical
      {
          if(condition==false)
          {
             abort = 1; 
          }
      }
   }
}

情况2:

#pragma omp parallel num_threads(x)
{
    while(condition)
   {
        // The work you need to do.
   }
}

这篇关于在openmp中突破结构化块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆