使用omp_set_num_threads()设置线程数为2,但是omp_get_num_threads()返回1
我有使用OpenMP的以下C / C ++代码:
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);
如您所见,我正在尝试根据命令行传递的参数设置要使用的处理器数量。
但是,我得到以下输出:
argv[4]: 2 //OK nProcessors: 2 //OK omp_get_num_threads(): 1 //WTF?!
为什么omp_get_num_threads()
返回2?!!!
正如已经指出的那样,我在串行区域中调用omp_get_num_threads()
,因此函数返回1
。
但是,我有以下并行代码:
#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); ...
哪个输出:
0 //tid 1 //nThreads - this should be 2! 0 1 0 1 0 1 ...
omp_get_num_threads()
调用在代码的serial部分中返回1。 见链接
因此,您需要使用并行代码来获取正确的值,这里代码应该如下所示:
#include #include int main (int argc, const char * argv[]) { int nProcessors = omp_get_max_threads(); std::cout<
此代码生成:
2
1 0 tid 2 nThreads 0 tid 2 nThreads 0 tid 2 nThreads 1 tid 2 nThreads 1 tid 2 nThreads
似乎你没有启用打开mp或你的循环不是openmp可以平行的forms
你使用的是错误的function。 使用omp_get_max_threads
检查允许的最大线程数。
已经指出omp_get_num_threads()
在代码的连续部分中返回1
。 因此,即使通过omp_set_num_threads()
设置大于1
的线程总数,对omp_get_num_threads()
任何调用都将返回1
,除非我们处于并行部分。 下面的例子试图弄清楚这一点
#include #include int main() { const int maxNumThreads = omp_get_max_threads(); printf("Maximum number of threads for this machine: %i\n", maxNumThreads); printf("Not yet started a parallel Section: the number of threads is %i\n", omp_get_num_threads()); printf("Setting the maximum number of threads...\n"); omp_set_num_threads(maxNumThreads); printf("Once again, not yet started a parallel Section: the number of threads is still %i\n", omp_get_num_threads()); printf("Starting a parallel Section...\n"); #pragma omp parallel for for (int i = 0; i < maxNumThreads; i++) { int tid = omp_get_thread_num(); printf("This is thread %i announcing that the number of launched threads is %i\n", tid, omp_get_num_threads()); } }