B9 - single, master and masked directives
single directive
#pragma omp parallel
{
#pragma omp single
{
printf("Hello from thread %d\n",
omp_get_thread_num());
}
}
- the
single directive in OpenMP tells the runtime that only one thread from the team should execute a particular block of code, while the other threads skip that block
- particularly useful for initialization, printing results, or performing an operation that should happen exactly once
- there is an implicit barrier at the end of
single
nowait can be used
master and masked directives
- the
master directive makes only the master, thread 0, run the block
- there is no implicit barrier at the end
- it guarantees the correct serial order
#pragma omp parallel
{
#pragma omp single
{
printf("Any one thread executes this\n");
}
#pragma omp masked
{
printf("Thread 0 executes this\n");
}
}
- modern OpenMP standard has deprecated the
master construct in favour of the masked construct
masked defaults to the same behaviour (thread 0), but allows a filter clause to be specified
#pragma omp parallel num_threads(4)
{
#pragma omp masked filter(2)
{
printf("Thread %d\n", omp_get_thread_num());
}
}
>>> Thread 2