gpt4 book ai didi

c++ - 非for循环的OpenMP并行化

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:26 25 4
gpt4 key购买 nike

到目前为止,我只使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com