gpt4 book ai didi

c - 两个不同的 pragma for-s 之间是否有障碍

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

有 8 个城市。我们对每个城市的不同统计数据进行计算。

我需要知道在 for 循环的末尾是否有障碍,以便在所有其他先前城市的统计数据完成后开始下一组计算。

肯定是这样的,因为每次计算都依赖于前一次。

#pragma omp for
for (int i = 0; i < count; ++i)
{
// calculate stats about population
}

// is there a barrier in here?
// Or do I need an explicit barrier
// #pragma omp barrier

#pragma omp for
for (int i = 0; i < count; ++i)
{
// calculate stats about cars
}

// is there a barrier in here?

#pragma omp for
for (int i = 0; i < count; ++i)
{
// calculate stats about weather
}

// ...same idea

最佳答案

是的,如果您使用#pragma omp parallel for,您将在循环结束时有一个隐式屏障,等待所有线程完成后再继续执行.

不需要放置显式pragma omp barrier

根据 OpenMP 4.0 Complete Specifications (1.3 第 10 行):

The task region of the task being executed by the encountering thread is suspended, and each member of the new team executes its implicit task. There is an implicit barrier at the end of the parallel construct.

关于c - 两个不同的 pragma for-s 之间是否有障碍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47191907/

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