Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.

OMP how will this code execute

zzzelmu
Beginner
438 Views
Hi I'm struggling with a peace of code because I'm not sure how it is going to be executed
[cpp]#pragma omp parallel
		{
			#pragma omp sections
			{
				#pragma omp section
					do something...
				#pragma omp section
					do something...
			}
			#pragma omp sections
			{
				#pragma omp section
					do something...
			}
			#pragma omp sections
			{
				#pragma omp section
					do something...
			}
		}[/cpp]
Are the going to be executed all in parallel or each sections block in sequence.Thanks
0 Kudos
3 Replies
Michael_K_Intel2
Employee
438 Views
Hi,

The sections constructs in your code will be executed in serial, that is, one block after the other. The section blocks in the individual sections constructs may be executed concurrently or in serial, depending on the number of threads.

So, in this code and two threads:

#pragma omp sections // A
{
#pragma omp section // A1
do something
#pragma omp section // A2
do something
}
#pragma omp sections // B
{
#pragma omp section //B1
do something
#pragma omp section //B2
}

You will see that A and B are executed after each other. A1 and A2 will be executed concurrentely as well as B1 and B2.

Individual section constructs may be executed in serial too, if you have less threads than you have section constructs. So, in the above example, if you would only create a single OpenMP thread, A1 and A2 would be executed in serial.

Does that help?

Cheers,
-michael
0 Kudos
zzzelmu
Beginner
438 Views
10x it helps I was reviewing a code and was wondering why there were two sections with only one section in each one.
0 Kudos
Michael_K_Intel2
Employee
438 Views
Hi,

That's a good point. Since I do not know the code you're referring to, it might very well be a mistake or on purpose. Except for crazy way of getting something like single, I cannot imagine a good use of a sections construct with just one secion.

Cheers,
-michael
0 Kudos
Reply