gpt4 book ai didi

c++ - OpenMP omp_get_num_threads()V.S. omp_get_max_threads()

转载 作者:行者123 更新时间:2023-12-02 10:07:32 26 4
gpt4 key购买 nike

我不了解omp_get_num_threads()和omp_get_max_threads()之间的区别。我按以下方式复制演示代码。

    omp_set_nested(1);
omp_set_max_active_levels(10);
omp_set_dynamic(0);
omp_set_num_threads(2);
#pragma omp parallel
{
omp_set_num_threads(3);

#pragma omp parallel
{
omp_set_num_threads(4);
#pragma omp single
{
std::cout << omp_get_max_active_levels() << " " << omp_get_num_threads() << " "
<< omp_get_max_threads() << std::endl;
}
}

#pragma omp barrier
#pragma omp single
{
std::cout << omp_get_max_active_levels() << " " << omp_get_num_threads() << " "
<< omp_get_max_threads() << std::endl;
}
}

然后我得到以下输出。
10 3 4
10 3 4
10 3 4
10 3 3

我已经检查了官方文档,但对此仍然感到困惑。

最佳答案

从文档:
omp_get_num_threads

The omp_get_num_threads routine returns the number of threads in the team executing the parallel region to which the routine region binds. If called from the sequential part of a program, this routine returns 1.


omp_get_max_threads

The value returned by omp_get_max_threads is the value of the first element of the nthreads-var ICV of the current task. This value is also an upper bound on the number of threads that could be used to form a new team if a parallel region without a num_threadsclause were encountered after execution returns from this routine.



下图说明了线程流。您的输出可能不正确,我无法使用clang + libomp或gcc + libGOMP复制它。

enter image description here

如果未同时指定线程数,则 omp_get_max_threads始终返回新的 parallel构造可以创建的线程数。当在内部并行区域的 omp_set_num_threads上设置4时,可以创建的新的不同线程的最大数量为4,但是在该区域中正在使用3。对于外部平行区域,最大值为3,并且正在使用2。

在串行代码中,在任何编译指示中,线程数均为1,但如果未通过 omp_set_num_threadsOMP_NUM_THREADS环境变量进行更改,则最大值是系统的默认值(通常是内核数)

关于c++ - OpenMP omp_get_num_threads()V.S. omp_get_max_threads(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59434959/

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