How does the SECTIONS directive in OpenMP distribute work?

前端 未结 8 717
情话喂你
情话喂你 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:26

    Change the first line from

    #pragma omp sections

    into

    #pragma omp parallel sections

    "parallel" directive ensures that the two sections are assigned to two threads. Then, you will receive the following output id = 0, id = 1,

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

    Note that 'nowait' tells the compiler that threads do not need to wait to exit the section. In Fortran 'nowait' goes at the end of the loop or section, which makes this more obvious.

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

    If you want really start different threads in different sections, the nowait clause tells compiler that threads do not need to wait to enter a section.

    #pragma omp parallel sections nowait
    {
       ...
    }
    
    0 讨论(0)
  • 2020-11-29 08:41

    According to OpenMP standard 3.1, section 2.5.2 (emphasis mine):

    The sections construct is a noniterative worksharing construct that contains a set of structured blocks that are to be distributed among and executed by the threads in a team. Each structured block is executed once by one of the threads in the team in the context of its implicit task.

    ...

    Each structured block in the sections construct is preceded by a section directive except possibly the first block, for which a preceding section directive is optional. The method of scheduling the structured blocks among the threads in the team is implementation defined. There is an implicit barrier at the end of a sections construct unless a nowait clause is specified.

    So, applying these rules to your case, we can argue that:

    1. the different structured blocks identified in a sections directive are executed once, by one thread. In other words you have always four prints, whichever the number of threads
    2. the blocks in the first sections will be executed (in a non-deterministic order) before the blocks in the second sections (also executed in a non-deterministic order). This is because of the implicit barrier at the end of the work-sharing constructs
    3. the scheduling is implementation defined, so that you can't possibly control which thread has been assigned a given section

    Your output is thus due to the way your scheduler decided to assign the different blocks to the threads in the team.

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

    The idea of parallel sections is to give the compiler a hint that the various (inner) sections can be performed in parallel, for example:

    #pragma omp parallel sections
    {
       #pragma omp section
       {
          /* Executes in thread 1 */
       } 
       #pragma omp section
       {
          /* Executes in thread 2 */
       } 
       #pragma omp section
       {
          /* Executes in thread 3 */
       } 
       /* ... */
    }
    

    This is a hint to the compiler and not guaranteed to happen, though it should. Your output is kind of what is expected; it says that there are #sections being executed in thread id 1, and in thread 2. The output order is non-deterministic as you don't know what thread will run first.

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

    It may be helpful to add more information to the output line and to add more sections (if you have the thread-count)

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

    Then you may get more interesting output like this:

    section 1 id = 4,
    section 3 id = 3,
    section 2 id = 1,
    

    which shows how the sections may be executed in any order, by any available thread.

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