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

江枫思渺然 提交于 2019-12-03 04:43:38
tune2fs

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

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

It has been already pointed out that omp_get_num_threads() returns 1 in 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

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!