等待整个omp块完成,然后再调用第二个函数

时间:2018-10-23 20:00:02

标签: c++ openmp

我没有C ++中的openmp经验,我想学习如何正确解决我的问题。我有30个文件需要由同一功能独立处理。每次激活该功能时,都会生成一个新的输出文件(out01.txt至out30.txt),并保存结果。我的计算机上有12个处理器,并且想用10个来解决这个问题。

我需要更改代码以等待所有30个文件被处理以执行C ++中的其他例程。目前,我无法强制我的代码等待所有omp范围执行,然后再移动第二个函数。

请在下面找到我的代码草稿。

   int W = 10; 
   int i = 1;
   ostringstream fileName;
   int th_id, nthreads;

        omp_set_num_threads(W);
        #pragma omp parallel shared (nFiles) private(i,fileName,th_id)
        {
             #pragma omp for schedule(static)
            for ( i = 1; i <= nFiles; i++)
            {
                th_id = omp_get_thread_num();
                 cout << "Th_id: " << th_id << endl;

            // CALCULATION IS PERFORMED HERE FOR EACH FILE

            }
        }

            // THIS is the point where the program should wait for the whole block to be finished
            // Calling the second function ...

1 个答案:

答案 0 :(得分:1)

“ omp for”和“ omp parallel”编译指示在其作用域的末尾都有一个隐式屏障。因此,并行部分之后的代码要等到并行部分结束后才能执行。因此,您的代码应该可以完美运行。

如果仍然存在问题,那不是因为您的代码没有在并行区域的末尾等待。

请向我们提供有关在执行此代码期间发生的情况的更多详细信息。这样,我们也许可以找到导致问题的真正原因。