omp_set_num_threads不工作 [英] omp_set_num_threads not working
问题描述
我有以下C / C ++代码:
int nProcessors = omp_get_max_threads
if(argv [4]!= NULL){
printf(argv [4]:%s\\\
,argv [4]);
nProcessors = atoi(argv [4]);
printf(nProcessors:%d\\\
,nProcessors);
}
omp_set_num_threads(nProcessors);
printf(omp_get_num_threads():%d\\\
,omp_get_num_threads());
exit(0);
正如你所看到的,我试图根据参数设置要使用的处理器数
但是,我得到以下输出:
code> argv [4]:2 // OKnProcessors:2 // OK
omp_get_num_threads():1 // WTF?
为什么 omp_get_num_threads()
2?!!!
请帮助!
:正如已经指出的,我在串行区域调用omp_get_num_threads(),因此函数返回1。
但是,我有以下并行代码: p>
#pragma omp parallel for private(i,j,tid,_hash)firstprivate(firstTime)reduction(+:nChunksDetected)
for(i = 0; i tid = omp_get_thread_num();
printf(%d\\\
,tid);
int nThreads = omp_get_num_threads();
printf(%d\\\
,nThreads);
...
其输出:
$ b b
0 // tid
1 // nThreads - 这应该是2!
0
1
0
1
0
1
...
omp_get_num_threads()
调用在序列节的代码。请参见链接
所以你需要有并行代码来获取正确的值,这里代码如下:
#include< iostream>
#include< omp.h>
int main(int argc,const char * argv [])
{
int nProcessors = omp_get_max_threads();
std :: cout<< nProcessors<< std :: endl;
omp_set_num_threads(nProcessors);
std :: cout<< omp_get_num_threads()<< std :: endl;
#pragma omp parallel for
for(int i = 0; i <5; i ++){
int tid = omp_get_thread_num
std :: cout<< tid<<\t tid<< std :: endl;
int nThreads = omp_get_num_threads();
std :: cout<< nThreads<<\t nThreads<< std :: endl;
}
exit(0);
}
此代码产生:
2
1
0 tid
2 nThreads
0 tid
2 nThreads
0 tid
2 nThreads
1 tid
2 nThreads
1 tid
2 nThreads
看来你已经打开mp没有启用,或者你的循环不是可以被openmp
I have the following C/C++ code:
int nProcessors=omp_get_max_threads();
if(argv[4]!=NULL){
printf("argv[4]: %s\n",argv[4]);
nProcessors=atoi(argv[4]);
printf("nProcessors: %d\n",nProcessors);
}
omp_set_num_threads(nProcessors);
printf("omp_get_num_threads(): %d\n",omp_get_num_threads());
exit(0);
As you can see, I'm trying to set the number of processors to use based on an argument passed on the command line.
However, I'm getting the following output:
argv[4]: 2 //OK
nProcessors: 2 //OK
omp_get_num_threads(): 1 //WTF?!
Why isn't omp_get_num_threads()
returning 2?!!!
Please help!
Edit: as has been pointed out, I'm calling omp_get_num_threads() in a serial region hence the function returning 1.
However, I have the following parallel code:
#pragma omp parallel for private(i,j,tid,_hash) firstprivate(firstTime) reduction(+:nChunksDetected)
for(i=0;i<fileLen-CHUNKSIZE;i++){
tid=omp_get_thread_num();
printf("%d\n",tid);
int nThreads=omp_get_num_threads();
printf("%d\n",nThreads);
...
which outputs:
0 //tid
1 //nThreads - this should be 2!
0
1
0
1
0
1
...
The omp_get_num_threads()
call returns 1 in the serial section of the code. See Link
So you need to have parallel code to get the correct value, here how your code should look like:
#include <iostream>
#include <omp.h>
int main (int argc, const char * argv[])
{
int nProcessors=omp_get_max_threads();
std::cout<<nProcessors<<std::endl;
omp_set_num_threads(nProcessors);
std::cout<<omp_get_num_threads()<<std::endl;
#pragma omp parallel for
for(int i=0;i<5;i++){
int tid=omp_get_thread_num();
std::cout<<tid<<"\t tid"<<std::endl;
int nThreads=omp_get_num_threads();
std::cout<<nThreads<<"\t nThreads"<<std::endl;
}
exit(0);
}
This code produces:
2
1
0 tid
2 nThreads
0 tid
2 nThreads
0 tid
2 nThreads
1 tid
2 nThreads
1 tid
2 nThreads
It seems that you have either open mp not enabled or your loop is not in the form that can be parallized by openmp
这篇关于omp_set_num_threads不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!