I am working on some OpenMP program and I have the following code:
#pragma omp parallel sections shared(a, b, r1, r2)
{
#pragma omp section
{
a = 1;
r1 = b;
}
#pragma omp section
{
b = 1;
r2 = a;
}
}
The main thing I want to observe here is that r1 = 1
or r2 = 1
(this is allowed by the specification) but I have run this code snippet for more than 1000000 times and I cannot observe it. The only result I got is r1 = 0, r2 = 1
(I was expecting at least some of them will be r1 = 1, r2 = 0
. I am wondering if the implementation is serialising my program. I used omp_set_num_threads(2);
to force two threads, but how I can be sure that the two threads are actually run on two physical cores (I am using a dual-core Macbook pro, and I am using gcc-4.9 installed by homebrew).
Your example is perfectly fine. The problem is the minimal workload for the two threads. Section 1 is scheduled to thread #0 and Section 2 to thread #1. However, thread #0 finished his work before the thread #1 has started. It just look like sequential execution of your sections.
I modified your example:
#include <iostream>
#include <unistd.h>
int main()
{
int a, b, r1=0, r2=0;
#pragma omp parallel sections shared(a, b, r1, r2)
{
#pragma omp section
{
a = 1;
sleep(10);
std::cout << "section #1" << std::endl;
r1 = b;
}
#pragma omp section
{
b = 1;
sleep(5);
std::cout << "section #2 "<< std::endl;
r2 = a;
}
}
std::cout << r1 << r2 << std::endl;
}
The output shows
section #2
section #1
11
Just as you might have expected.