如何在程序中设置OpenMP线程的数量?

时间:2022-07-25 20:59:27

Running the program as

运行程序,

$ OMP_NUM_TRHEADS=4 ./a.out

limits the number of active OpenMP threads to 4, as evidenced by htop. However, if instead of binding the OMP_NUM_THREADS environment variable in Bash, I call

将活动的OpenMP线程的数量限制为4个,htop已经证明了这一点。但是,如果不是在Bash中绑定OMP_NUM_THREADS环境变量,而是调用

setenv("OMP_NUM_THREADS", "4", 1);

from main before calling any OpenMP-enabled functions, this seems to have no effect.

在调用任何启用openmp的函数之前,这似乎没有任何效果。

Why is this happening? How can I set the number of OpenMP threads from within the program, if it's possible at all?

为什么会这样?如果可能的话,如何在程序中设置OpenMP线程的数量?

1 个解决方案

#1


3  

There are two ways1 one can use to set the number of threads from within the program:

有两种方法可以用来设置程序内的线程数:

Option #1

Use num_threads clause in a directive that opens a parallel region:

在打开一个并行区域的指令中使用num_threads子句:

#pragma omp parallel num_threads(number_of_threads)

Option #2

Use omp_set_num_threads API function before a parallel region begins:

在并行区域开始之前使用omp_set_num_threads API函数:

#include <omp.h>

// ...
omp_set_num_threads(number_of_threads);
#pragma omp parallel

Note: Both options take priority over OMP_NUM_THREADS environment variable, but num_threads clause has precedence over omp_set_num_threads.

注意:这两个选项都优先于OMP_NUM_THREADS环境变量,但是num_threads子句优先于omp_set_num_threads。

Why setenv fails to have any effect?

为什么setenv没有任何效果?

This is covered in the OpenMP specification (emphasis mine):

这是OpenMP规范(重点是我的):

Chapter 4

Environment Variables

[...] Modifications to the environment variables after the program has started, even if modified by the program itself, are ignored by the OpenMP implementation. However, the settings of some of the ICVs can be modified during the execution of the OpenMP program by the use of the appropriate directive clauses or OpenMP API routines. [...]

[…]程序启动后对环境变量的修改,即使程序本身进行了修改,也会被OpenMP实现忽略。但是,在执行OpenMP程序时,可以使用适当的指令子句或OpenMP API例程修改一些ICVs的设置。[…]


1) There is a third run-time option that allows to alter the number of threads executing a parallel region that follows by resetting it to 1 (master thread only) or to the number from num_threads clause or omp_set_num_threads call, which is an if clause in a directive the clause belongs to.

1)还有第三个运行时选项,允许改变数量的线程执行一个平行的地区,将其重新设置为1(主线程只)或数量从num_threads条款或omp_set_num_threads调用,这是一个如果在指令条款属于。

#1


3  

There are two ways1 one can use to set the number of threads from within the program:

有两种方法可以用来设置程序内的线程数:

Option #1

Use num_threads clause in a directive that opens a parallel region:

在打开一个并行区域的指令中使用num_threads子句:

#pragma omp parallel num_threads(number_of_threads)

Option #2

Use omp_set_num_threads API function before a parallel region begins:

在并行区域开始之前使用omp_set_num_threads API函数:

#include <omp.h>

// ...
omp_set_num_threads(number_of_threads);
#pragma omp parallel

Note: Both options take priority over OMP_NUM_THREADS environment variable, but num_threads clause has precedence over omp_set_num_threads.

注意:这两个选项都优先于OMP_NUM_THREADS环境变量,但是num_threads子句优先于omp_set_num_threads。

Why setenv fails to have any effect?

为什么setenv没有任何效果?

This is covered in the OpenMP specification (emphasis mine):

这是OpenMP规范(重点是我的):

Chapter 4

Environment Variables

[...] Modifications to the environment variables after the program has started, even if modified by the program itself, are ignored by the OpenMP implementation. However, the settings of some of the ICVs can be modified during the execution of the OpenMP program by the use of the appropriate directive clauses or OpenMP API routines. [...]

[…]程序启动后对环境变量的修改,即使程序本身进行了修改,也会被OpenMP实现忽略。但是,在执行OpenMP程序时,可以使用适当的指令子句或OpenMP API例程修改一些ICVs的设置。[…]


1) There is a third run-time option that allows to alter the number of threads executing a parallel region that follows by resetting it to 1 (master thread only) or to the number from num_threads clause or omp_set_num_threads call, which is an if clause in a directive the clause belongs to.

1)还有第三个运行时选项,允许改变数量的线程执行一个平行的地区,将其重新设置为1(主线程只)或数量从num_threads条款或omp_set_num_threads调用,这是一个如果在指令条款属于。