How does the SECTIONS directive in OpenMP distribute work?

前端 未结 8 718
情话喂你
情话喂你 2020-11-29 08:14

In OpenMP when using omp sections, will the threads be distributed to the blocks inside the sections, or will each thread be assigned to each sections?

相关标签:
8条回答
  • 2020-11-29 08:44

    The code posted by the OP will never execute in parallel, because the parallel keyword does not appear. The fact that the OP got ids different from 0 shows that probably his code was embedded in a parallel directive. However, this is not clear from his post, and might confuse beginners.

    The minimum sensible example is (for the first example posted by the OP):

    #pragma omp parallel sections
    {
        #pragma omp section
        { 
            printf ("id = %d, \n", omp_get_thread_num());
        }
    
        #pragma omp section
        { 
            printf ("id = %d, \n", omp_get_thread_num());
        }
    }
    

    On my machine, this prints

    id = 0,
    id = 1,
    

    showing that the two sections are being executed by different threads.

    It's worth noting that however this code can not extract more parallelism than two threads: if it is executed with more threads, the other threads don't have any work to do and will just sit down idle.

    0 讨论(0)
  • 2020-11-29 08:46

    You are missing parallel keyword. The parallel keyword triggers the openmp run in parallel.

    0 讨论(0)
提交回复
热议问题