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

Wait for the whole omp block to be finished before calling the second function

本文关键字:然后 调用 第二个 函数 omp 等待      更新时间:2023-10-16

我没有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 ...

"omp for"和"omp parallel"编译指示在其范围的末尾都有一个隐式障碍。因此,在并行部分结束之前,无法执行并行部分之后的代码。因此,您的代码应该完美运行。

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

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

相关文章: