每个线程在OpenMP中执行一次代码

每个线程在OpenMP中执行一次代码

本文介绍了在没有默认构造函数的情况下,每个线程在OpenMP中执行一次代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用openMP V.2.0编写并行for循环.在并行区域的中间,我构造了一个对象,每个线程我希望构造一次.

I try to write a parallel for loop using openMP V.2.0. In the middle of the parallel region I construct an Object which I would like to be constructed once per thread.

 #pragma omp parallel for
  for (long i = 0; i < static_cast<long>(general_triangles.size()); ++i)
  {
      TrianglePointer tri = general_triangles[i];
      if (tri.GetClassification() == TO_CLASSIFY)
      {
          bool tri_has_correct_normal = true;
          // --- Construct tree once per thread ---
          Tree tree(*(gp_boolean_operator->mp_group_manager));
          if (tree.IsTriangleExternal(tri, tri_has_correct_normal))
          {
              tri.SetClassification(IS_EXTERNAL);
          }
      }
  }

每个线程有没有用于构造树的关键字?

Is there any keyword for constructing tree once per thread?

您是否建议改用bood_thread_ptr?

Do you suggest to use bood_thread_ptr instead?

推荐答案

考虑以下未经测试的代码:

Consider untested code like this :

#pragma omp parallel
{
// --- Construct one tree in each per thread ---
  Tree tree(*(gp_boolean_operator->mp_group_manager));

#pragma omp for
  for (long i = 0; i < static_cast<long>(general_triangles.size()); ++i)
  {
      TrianglePointer tri = general_triangles[i];
      if (tri.GetClassification() == TO_CLASSIFY)
      {
          bool tri_has_correct_normal = true;

          if (tree.IsTriangleExternal(tri, tri_has_correct_normal))
          {
              tri.SetClassification(IS_EXTERNAL);
          }
      }
  }
}

这表明您可以在可移植,独立于操作系统,OpenMP内完成所有这些操作,并且不会引入不必要的静态变量.

It shows that you can do all this inside portable, OS independent, OpenMP, and that you don't introduce an unnecessary static variable.

这篇关于在没有默认构造函数的情况下,每个线程在OpenMP中执行一次代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 00:18