C++ OpenMP set_num_threads() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11095309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenMP set_num_threads() is not working
提问by Nurlan
I am writing a parallel program using OpenMP in C++.
我正在使用 C++ 中的 OpenMP 编写一个并行程序。
I want to control the number of threads in the program using omp_set_num_threads()
, but it does not work.
我想使用 控制程序中的线程数omp_set_num_threads()
,但它不起作用。
#include <iostream>
#include <omp.h>
#include "mpi.h"
using namespace std;
int myrank;
int groupsize;
double sum;
double t1,t2;
int n = 10000000;
int main(int argc, char *argv[])
{
MPI_Init( &argc, &argv);
MPI_Comm_rank( MPI_COMM_WORLD, &myrank );
MPI_Comm_size(MPI_COMM_WORLD,&groupsize);
omp_set_num_threads(4);
sum = 0;
#pragma omp for reduction(+:sum)
for (int i = 0; i < n; i++)
sum+= i/(n/10);
cout<<"sum="<<sum<<endl;
cout<<"threads="<<omp_get_num_threads()<<endl;
MPI_Finalize();
return 0;
}
The program outputs:
程序输出:
sum = 4.5e+007
threads=1
How to control the number of threads?
如何控制线程数?
回答by Hristo Iliev
Besides calling omp_get_num_threads()
outside of the parallel region in your case, calling omp_set_num_threads()
still doesn't guarantee that the OpenMP runtime will use exactly the specified number of threads. omp_set_num_threads()
is used to override the value of the environment variable OMP_NUM_THREADS
and they both control the upper limitof the size of the thread team that OpenMP would spawn for all parallel regions (in the case of OMP_NUM_THREADS
) or for any consequent parallel region (after a call to omp_set_num_threads()
). There is something called dynamic teams that could still pick smaller number of threads if the run-time system deems it more appropriate. You can disable dynamic teams by calling omp_set_dynamic(0)
or by setting the environment variable OMP_DYNAMIC
to false
.
除了omp_get_num_threads()
在您的情况下在并行区域之外调用之外,调用omp_set_num_threads()
仍然不能保证 OpenMP 运行时将使用指定数量的线程。omp_set_num_threads()
用于覆盖环境变量的值OMP_NUM_THREADS
,他们都控制在上限线程队的OpenMP将产卵所有并行区域(在的情况下的尺寸OMP_NUM_THREADS
),或用于任何随后的并行区域(后到的呼叫omp_set_num_threads()
) . 如果运行时系统认为它更合适,有一种叫做动态团队的东西仍然可以选择较少数量的线程。你可以通过调用禁用动态团队omp_set_dynamic(0)
或通过设置环境变量OMP_DYNAMIC
来false
。
To enforce a given number of threads you should disable dynamic teams and specify the desired number of threads with either omp_set_num_threads()
:
要强制执行给定数量的线程,您应该禁用动态团队并使用以下任一方式指定所需的线程数量omp_set_num_threads()
:
omp_set_dynamic(0); // Explicitly disable dynamic teams
omp_set_num_threads(4); // Use 4 threads for all consecutive parallel regions
#pragma omp parallel ...
{
... 4 threads used here ...
}
or with the num_threads
OpenMP clause:
或使用num_threads
OpenMP 子句:
omp_set_dynamic(0); // Explicitly disable dynamic teams
// Spawn 4 threads for this parallel region only
#pragma omp parallel ... num_threads(4)
{
... 4 threads used here ...
}
回答by Smi
The omp_get_num_threads()
function returns the number of threads that are currently in the team executing the parallel region from which it is called. You are calling it outside of the parallel region, which is why it returns 1
.
该omp_get_num_threads()
函数返回当前在执行调用它的并行区域的组中的线程数。您在并行区域之外调用它,这就是它返回1
.
回答by jww
According to the GCC manual for omp_get_num_threads:
In a sequential section of the program omp_get_num_threads returns 1
在程序的顺序部分 omp_get_num_threads 返回 1
So this:
所以这:
cout<<"sum="<<sum<<endl;
cout<<"threads="<<omp_get_num_threads()<<endl;
Should be changed to something like:
应该改为:
#pragma omp parallel
{
cout<<"sum="<<sum<<endl;
cout<<"threads="<<omp_get_num_threads()<<endl;
}
The code I use follows Hristo's advice of disabling dynamic teams, too.
我使用的代码也遵循 Hristo 关于禁用动态团队的建议。
回答by Ali Jafar
I was facing the same problem . Solution is given below
我面临着同样的问题。下面给出解决方案
Right click on Source Program > Properties > Configuration Properties > C/C++ > Language > Now change Open MP support flag to Yes....
右键单击源程序 > 属性 > 配置属性 > C/C++ > 语言 > 现在将打开 MP 支持标志更改为是...。
You will get the desired result.
你会得到想要的结果。
回答by Yokesh
Try setting your num_threads inside your omp parallel code, it worked for me. This will give output as 4
尝试在 omp 并行代码中设置 num_threads,它对我有用。这将使输出为 4
#pragma omp parallel
{
omp_set_num_threads(4);
int id = omp_get_num_threads();
#pragma omp for
for (i = 0:n){foo(A);}
}
printf("Number of threads: %d", id);