Intel® C++ Compiler 16.0 User and Reference Guide

omp single

Specifies a structured block that will be executed only once by a single thread in the team.

Syntax

#pragma omp single [clause,clause , ...]

structured-block

Arguments

clause

Can be one or more of the following clauses:

copyprivate(list)

Provides a mechanism to use a private variable in list to broadcast a value from the data environment of one implicit task to the data environments of the other implicit tasks belonging to the parallel region.

firstprivate(list)

Provides a superset of the functionality provided by the private clause. Each private data object is initialized with the value of the original object.

nowait(integer expression)

Indicates that an implementation may omit the barrier at the end of the worksharing region.

private(list)

Declares variables to be private to each thread in a team.

Description

Only one thread will be allowed to execute the structured block. The rest of the threads in the team wait at the implicit barrier at the end of the single construct, unless nowait is specified. If nowait is specified, then the rest of the threads in the team immediately execute the code after the structured block.

The following example demonstrates how to use this pragma to make sure that the printf function is executed only once. All the threads in the team that do not execute the function proceed immediately to the following calculations.

Example

#include <omp.h>
#pragma omp parallel {
 	#pragma omp single nowait { printf(“Starting calculation\n”); }
	
	// Do some calculation
}