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

标签 c++ openmp

我不了解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/

相关文章:

c++ - const_iterators 更快吗?

c++ - 批处理时如何优化 omp 并行化

c++ - 如果由另一个 OpenMP 程序调用,则外部调用的 OpenMP 程序仅使用一个线程运行

c - 如何在openmp中为每个线程分配特定的作业以进行矩阵加法

c - 使用 GCC 时 OpenMP 没有进行实际的并行处理

c - 如何在OMP编译指示中声明数组

c++ - 在 C++ 中扩展 Thrift 生成的对象

c++ - 什么时候将 () 与类一起使用?

c++ - 如何在 3d 场景中旋转单个对象?

c++ - 为什么我的 for 循环在检查 vector 是否排序时无法正常工作?