gpt4 book ai didi

parallel-processing - OpenMP,使用所有内核并行

转载 作者:行者123 更新时间:2023-12-01 02:41:08 24 4
gpt4 key购买 nike

我有 4 个内核的计算机和带有 2 个重要任务的 OMP 应用程序。

int main()
{
#pragma omp parallel sections
{
#pragma omp section
WeightyTask1();

#pragma omp section
WeightyTask2();
}

return 0;
}

每个任务都有这么重要的部分:
#omp pragma parallel for
for (int i = 0; i < N; i++)
{
...
}

我用 -fopenmp 编译程序参数,制作 export OMP_NUM_THREADS=4 .
问题仅加载两个内核 .如何在我的任务中使用所有核心?

最佳答案

我最初的 react 是:你必须声明更多的并行性。

您已经定义了两个可以并行运行的任务。 OpenMP 尝试在两个以上的内核上运行它会减慢您的速度(因为缓存局部性和可能的​​错误共享)。

编辑 如果并行 for 循环的数量很大(例如,不低于 8 次迭代),并且您没有看到使用了 2 个以上的内核,请查看

  • omp_set_nested()
  • OMP_NESTED = TRUE | FALSE environment variable

    This environment variable enables or disables nested parallelism. The setting of this environment variable can be overridden by calling the omp_set_nested() runtime library function.

    If nested parallelism is disabled, nested parallel regions are serialized and run in the current thread.

    In the current implementation, nested parallel regions are always serialized. As a result, OMP_SET_NESTED does not have any effect, and omp_get_nested() always returns 0. If -qsmp=nested_par option is on (only in non-strict OMP mode), nested parallel regions may employ additional threads as available. However, no new team will be created to run nested parallel regions. The default value for OMP_NESTED is FALSE.

  • 关于parallel-processing - OpenMP,使用所有内核并行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8303126/

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