如何在程序中设置OpenMP线程数? [英] How can I set the number of OpenMP threads from within the program?

查看:1463
本文介绍了如何在程序中设置OpenMP线程数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以以下方式运行程序

$ OMP_NUM_TRHEADS=4 ./a.out

将活动OpenMP线程数限制为4,如htop所示.但是,如果不是在Bash中绑定OMP_NUM_THREADS环境变量,而是调用

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

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

在调用任何启用OpenMP的功能之前,先main开始,这似乎没有效果.

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?

推荐答案

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

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

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

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

#pragma omp parallel num_threads(number_of_threads)

选项#2

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

Option #2

Use omp_set_num_threads API function before a parallel region begins:

#include <omp.h>

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

注意:这两个选项的优先级都高于OMP_NUM_THREADS环境变量,但num_threads子句的优先级高于omp_set_num_threads.

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

为什么setenv无效?

OpenMP规范(重点是我)中对此进行了介绍:

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

第4章

环境变量

[...]在程序启动后对环境变量的修改,即使被程序本身修改了,也被OpenMP实现忽略.但是,某些ICV的设置可以在OpenMP程序执行期间通过使用适当的指令子句或OpenMP API例程进行修改. [...]

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. [...]


1)第三个运行时选项允许通过将并行区域重置为1(仅主线程)来更改执行并行区域的线程数.到num_threads子句或omp_set_num_threads调用中的数字,这是该子句所属指令中的if子句.


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.

这篇关于如何在程序中设置OpenMP线程数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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