num_threads 与 omp_set_num_threads 与 OMP_NUM_THREADS 之间的区别 [英] Difference between num_threads vs. omp_set_num_threads vs OMP_NUM_THREADS

查看:77
本文介绍了num_threads 与 omp_set_num_threads 与 OMP_NUM_THREADS 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在代码的并行部分指定线程数的方法感到非常困惑.我知道我可以使用:

I am quite confused about the ways to specify the number of threads in parallel part of a code. I know I can use:

  1. 环境变量 OMP_NUM_THREADS
  2. 函数 omp_set_num_threads(int)
  3. num_threads(int) in #pragma omp parallel for num_threads(NB_OF_THREADS)

到目前为止我收集到的前两个是等价的.但是第三个呢?有人可以提供更详细的差异说明吗,我在互联网上找不到任何关于 1/2 和 3 之间差异的信息.

What I have gathered so far the first two are equivalent. But what about the third one? Can someone provide a more detailed exposition of the difference, I could not find any information in the internet regarding the difference between 1/2 and 3.

推荐答案

OMP_NUM_THREADSomp_set_num_threads() 不是等价的.环境变量仅用于设置控制组中最大线程数的nthreads-var ICV(内部控制变量)的初始值.omp_set_num_threads() 可用于随时更改 nthreads-var 的值(当然在任何并行区域之外)并影响所有后续并行区域.因此设置一个值,例如n,到OMP_NUM_THREADS 相当于在遇到第一个并行区域之前调用omp_set_num_threads(n).

OMP_NUM_THREADS and omp_set_num_threads() are not equivalent. The environment variable is only used to set the initial value of the nthreads-var ICV (internal control variable) which controls the maximum number of threads in a team. omp_set_num_threads() can be used to change the value of nthreads-var at any time (outside of any parallel regions, of course) and affects all subsequent parallel regions. Therefore setting a value, e.g. n, to OMP_NUM_THREADS is equivalent to calling omp_set_num_threads(n) before the very first parallel region is encountered.

确定并行区域内线程数的算法在OpenMP 规范 可在 OpenMP 网站上免费获得:

The algorithm to determine the number of threads in a parallel region is very clearly described in the OpenMP specification that is available freely on the OpenMP website:

ifnum_threads 子句存在

然后让 ThreadsRequested 成为num_threads 子句表达式的值;

then let ThreadsRequested be the value of the num_threads clause expression;

else let ThreadsRequested = nthreads-var 的第一个元素的值;

else let ThreadsRequested = value of the first element of nthreads-var;

在规范的ICV Override Relationships部分中列出了设置nthreads-var的不同方式的优先级:

That priority of the different ways to set nthreads-var is listed in the ICV Override Relationships part of the specification:

num_threads 子句和 omp_set_num_threads() 覆盖了 OMP_NUM_THREADS 环境变量的值和 OMP_NUM_THREADS 环境变量的初始值.em>nthreads-var ICV.

The num_threads clause and omp_set_num_threads() override the value of the OMP_NUM_THREADS environment variable and the initial value of the first element of the nthreads-var ICV.

翻译成人类语言,即:

  • OMP_NUM_THREADS(如果存在)最初指定线程数;
  • omp_set_num_threads() 的调用会覆盖OMP_NUM_THREADS 的值;
  • num_threads 子句的存在覆盖了其他两个值.
  • OMP_NUM_THREADS (if present) specifies initially the number of threads;
  • calls to omp_set_num_threads() override the value of OMP_NUM_THREADS;
  • the presence of the num_threads clause overrides both other values.

实际使用的线程数还受是否启用动态团队规模的影响(dyn-var ICV 可通过 OMP_DYNAMIC 和/或 omp_set_dynamic() 设置)、线程限制是否由 thread-limit-var 强加(可通过 OMP_THREAD_LIMIT 设置),以及是否嵌套并行(OMP_NESTED/omp_set_nested()) 是否启用.

The actual number of threads used is also affected by whether dynamic team sizes are enabled (dyn-var ICV settable via OMP_DYNAMIC and/or omp_set_dynamic()), by whether a thread limit is imposed by thread-limit-var (settable via OMP_THREAD_LIMIT), as well as by whether nested parallelism (OMP_NESTED / omp_set_nested()) is enabled or not.

这篇关于num_threads 与 omp_set_num_threads 与 OMP_NUM_THREADS 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆