Rookie HPC

About

Docs

Tools

Tests

master

Definition

master is a clause that must be used in a parallel region; it tells OpenMP that the associated block must be executed by the master thread only. The other threads do not wait at the end of the associated block as if there was an implicit barrier. The master clause must not be confused with the single clause.

Example

Copy

Feedback

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

/**
 * @brief Illustrates how to use a master clause.
 * @details A parallel region is created, in which a certain part is executed by
 * every thread, and another part is executed only by the master thread.
 **/
int main(int argc, char* argv[])
{
    // Use 4 threads when creating OpenMP parallel regions
    omp_set_num_threads(4);

    // Create the parallel region
    #pragma omp parallel
    {
        printf("[Thread %d] Every thread executes this printf.\n", omp_get_thread_num());

        #pragma omp barrier

        #pragma omp master
        {
            printf("[Thread %d] Only the master thread executes this printf, which is me.\n", omp_get_thread_num());
        }
    }

    return EXIT_SUCCESS;
}