0

我是 OpenMP 的新手,我无法在每个线程循环迭代中创建线程。我的问题可能听起来很幼稚,请多多包涵。

#pragma omp parallel private(a,b) shared(f)
{
     #pragma omp for 
      for(...)
     {
      //some operations
      // I want to parallelize the code in italics along within in the multi threaded for loop
      *int x=func1(a,b);*
      *int val1=validate(x);*
      *int y=func2(a,b);*
      *int val2=validate(y);*
      }
}
4

1 回答 1

0

在 for 循环中,所有线程都忙于循环迭代,因此没有资源可以在迭代中并行执行。如果工作平衡良好,您将不会获得任何更好的性能。

如果很难/不可能很好地平衡工作与并行工作。您可以尝试在循环中生成任务,然后执行后续工作。但请注意任务生成的开销。

#pragma omp parallel private(a,b) shared(f)
{
     #pragma omp for nowait
      for(...)
     {
      //some operations

      #pragma omp task
      {
      int x=func1(a,b);
      int val1=validate(x);
      }

      #pragma omp task
      {
      int y=func2(a,b);
      int val2=validate(y);
      }

     }

     // wait for all tasks to be finished (implicit at the end of the parallel region (here))
     #pragma omp taskwait      
}
于 2013-01-20T17:08:02.927 回答