Rookie HPC

About

Docs

Tools

Tests

default

Definition

default is the OpenMP clause that changes the data-sharing policy to apply to variables not explicitly assigned to a data-sharing policy. The data-sharing policy passed is either shared or none. If this clause is not specified, it defaults to shared.

Copy

Feedback

default(policy)

Parameters

policy
The policy to apply to all variables not declared in a data-sharing policy clause (none or shared). With the constant “none”, the programmer is required to explicitly declare the data-sharing policy of every variable.

Example

Copy

Feedback

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
 
/**
 * @brief Illustrates the OpenMP default clause.
 * @details An int is passed to a parallel region. Then:
 *     - 1st step: thread 0 writes "123" in the int
 *     - 2nd step: thread 1 prints the value of the int
 * The default policy, set to shared, becomes visible when the value read by thread 1 is the one written by thread 0.
 **/
int main(int argc, char* argv[])
{
    // Use 2 OpenMP threads
    omp_set_num_threads(2);
 
    // The int that will be shared among threads
    int val = 0;
 
     // Variables not part of a data-sharing clause will be "shared" by default.
    #pragma omp parallel default(shared)
    {
        // Step 1: thread 0 writes the value
        if(omp_get_thread_num() == 0)
        {
            printf("Thread 0 sets the value of \"val\" to 123.\n");
            val = 123;
        }
 
        // Threads wait each other before progressing to step 2
        #pragma omp barrier
        
        // Step 2: thread 1 reads the value
        if(omp_get_thread_num() == 1)
        {
            printf("Thread 1 reads the value of \"val\": %d.\n", val);
        }
    }
 
    return EXIT_SUCCESS;
}