我正在尝试并行运行代码,但我对与 openmp 相关的私有/共享等内容感到困惑。我正在使用 c++(msvc12 或 gcc)和 openmp。
代码迭代循环,该循环由一个应该并行运行的块组成,然后是一个应该在所有并行工作完成后运行的块。处理并行内容的顺序无关紧要。代码如下所示:
// some X, M, N, Y, Z are some constant values
const int processes = 4;
std::vector<double> vct(X);
std::vector<std::vector<double> > stackVct(processes, std::vector<double>(Y));
std::vector<std::vector<std::string> > files(processes, M)
for(int i=0; i < N; ++i)
{
// parallel stuff
for(int process = 0; process < processes; ++process)
{
std::vector<double> &otherVct = stackVct[process];
const std::vector<std::string> &my_files = files[process];
for(int file = 0; file < my_files.size(); ++file)
{
// vct is read-only here, the value is not modified
doSomeOtherStuff(otherVct, vct);
// my_files[file] is read-only
std::vector<double> thirdVct(Y);
doSomeOtherStuff(my_files[file], thirdVct(Y));
// thirdVct and vct are read-only
doSomeOtherStuff2(thirdVct, otherVct, vct);
}
}
// when all the parallel stuff is done, do this job
// single thread stuff
// stackVct is read-only, vct is modified
doSingleTheadStuff(vct, stackVct)
}
如果性能更好,可以将“doSingleThreadSuff(...)”移到并行循环中,但需要单线程处理。最内层循环中的函数顺序不能更改。
我应该如何声明#pragma omp 的东西以使其正常工作?谢谢!