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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8969748/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:24:30  来源:igfitidea点击:

Set number of threads using omp_set_num_threads() to 2, but omp_get_num_threads() returns 1

c++cparallel-processingopenmp

提问by Eamorr

I have the following C/C++ code using 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);

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?!!!

为什么不omp_get_num_threads()返回 2?!!!



As has been pointed out, I'm calling omp_get_num_threads()in a serial region, hence the function returns 1.

正如已经指出的那样,我omp_get_num_threads()在串行区域中调用,因此函数返回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
...

回答by tune2fs

The omp_get_num_threads()call returns 1 in the serial section of the code. See Link

omp_get_num_threads()调用在代码的串行部分返回 1。见链接

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

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

似乎您没有启用 open mp 或者您的循环不是 openmp 可以并行化的形式

回答by Bort

you are using the wrong function. use omp_get_max_threadsto check for the maximum number of allowed threads.

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

回答by HymanOLantern

It has been already pointed out that omp_get_num_threads()returns 1in sequential sections of the code. Accordingly, even if setting, by omp_set_num_threads(), an overall number of threads larger than 1, any call to omp_get_num_threads()will return 1, unless we are in a parallel section. The example below tries to clarify this point

已经指出在代码的连续部分中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());
    }

}