使用omp_set_num_threads()设置线程数为2,但是omp_get_num_threads()返回1

时间:2012-01-23 09:52:20

标签: c++ c parallel-processing openmp

我有使用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
...

3 个答案:

答案 0 :(得分:31)

omp_get_num_threads()调用在代码的序列部分返回1。见Link

因此,您需要使用并行代码来获取正确的值,此处代码应如下所示:

#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并行化的形式

答案 1 :(得分:8)

你使用了错误的功能。使用omp_get_max_threads检查允许的最大线程数。

答案 2 :(得分:0)

已经指出omp_get_num_threads()在代码的连续部分中返回1。因此,即使通过omp_set_num_threads()设置大于1的线程总数,对omp_get_num_threads()的任何调用都将返回1,除非我们处于并行部分。下面的例子试图澄清这一点

#include <stdio.h>

#include <omp.h>

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());
    }

}