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

前端 未结 3 2014
孤独总比滥情好
孤独总比滥情好 2021-02-05 05:36

I have the following C/C++ code using OpenMP:

    int nProcessors=omp_get_max_threads();
    if(argv[4]!=NULL){
        printf(\"argv[4]: %s\\n\",argv[4]);
              


        
3条回答
  •  无人及你
    2021-02-05 06:01

    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 
    #include 
    
    int main (int argc, const char * argv[])
    {
        int nProcessors = omp_get_max_threads();
    
        std::cout<

    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

提交回复
热议问题