What is the benefit of '#pragma omp master' as opposed to '#pragma omp single'?

16,447

Solution 1

Though a single nowait construct is most of the time equivalent to a master construct:

  1. The master construct can be used inside a work-sharing construct, should any need arise. This is not the case for a single nowait construct, as two work-sharing constructs can't be nested within the same parallel region

  2. Some libraries want the main thread to perform certain operations. For instance the MPI library, when initialized with a level of thread support equal to MPI_THREAD_FUNNELED, allows only the main threads to make MPI calls

Solution 2

In addition to nesting limitations single construct can be implemented slower than master construct because it is more complicated and flexible. You may want to check your particular implementation, but in general master can be implemented faster, so multiple invocations of it may benefit comparing to single nowait construct.

Share:
16,447

Related videos on Youtube

Josh Milthorpe
Author by

Josh Milthorpe

I am a Lecturer and Research Fellow at the Australian National University Research School of Computer Science.  My mission is to develop the tools and techniques that computational scientists use to understand the world. I am particularly interested in combining machine learning and high-performance computing for physical science. I also contribute to related efforts in parallel programming models, resilience, and numerical computing.

Updated on June 04, 2022

Comments

  • Josh Milthorpe
    Josh Milthorpe about 2 years

    In OpenMP any code inside a #pragma omp master directive is executed by a single thread (the master), without an implied barrier at end of the region. (See section on MASTER directive in the LLNL OpenMP tutorial).

    This seems equivalent to #pragma omp single nowait (with the exception that rather than the 'master', any thread may execute the single region).

    Under what circumstances, if any, is it beneficial to use #pragma omp master?

  • Jeff Hammond
    Jeff Hammond almost 11 years
    I'm not aware of any implementation for which MPI_THREAD_FUNNELED performs better than MPI_THREAD_SERIALIZED, although I understand how the difference can emerge.
  • user1494080
    user1494080 over 2 years
    The master construct cannot be used inside a work-sharing construct. As the standard says: "A master region may not be closely nested inside a worksharing, atomic, or explicit task region." I'm also not sure what the semantics should be of a master nested in a parallel for loop, for example.