到目前为止,我只使用 OpenMP 在 C++ 中并行化 for 循环。但是我想知道我是否可以执行其他不是并行循环的代码行。
void minimization(int *a, int *x) {
// this part of the code is dependent of other library.
}
void checkForNaN(int *a){
// check nan points
}
int main() {
// read data
minimization (a,b);
checkForNaN(x);
}
考虑上面的示例片段,其中 minimization(,)
和 checkForNaN()
是独立的,即任何一个的结果都不会影响另一个一次。是否可以并行化?
我是这样想的:
int main() {
// read data
#pragma omp parallel
{
minimization (a,b);
checkForNaN(x);
}
}
看起来正确吗?
最佳答案
这就是 OMP 部分的用途:)
int main() {
#pragma omp parallel sections
{
#pragma omp section
{
minimization(a,b);
}
#pragma omp section
{
checkForNaN(x);
}
}
}
关于c++ - 非for循环的OpenMP并行化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33130001/