Rookie HPC

Work sharing

C | FORTRAN

single

Definition

single is a clause that must be used in a parallel region; it tells OpenMP that the associated block must be executed by one thread only, albeit not specifying which one. The other threads will wait at the end of the associated block, at an implicit barrier, unless the single clause is accompanied with a nowait clause. The single clause must not be confused with the master or critical clause.

Copy

Feedback

#pragma omp single [private(list)]
                   [firstprivate(list)]
                   [copyprivate(list)]

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to use a single clause.
 * @details A parallel region is created, in which a certain part is executed by
 * every thread, and another part is executed only by a single 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 single
        {
            printf("[Thread %d] Only a single thread executes this printf, I happen to be the one picked.\n", omp_get_thread_num());
        }
    }

    return EXIT_SUCCESS;
}