How does the SECTIONS directive in OpenMP distribute work?

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

提交回复
热议问题