You create an OpenMP application by using Microsoft Visual C++. You write the following code segment to efficiently calculate the total number of odd and even numbers in the array data in parallel. (Line numbers are included for reference only.)
01 int data[100000];
02 //Initialize array values
03 int even=0;
04 int odd=0;
05 #pragma omp parallel for
06 for(int cnt=0;cnt<100000;cnt++)
07 {
09 if(data[cnt]%2==0)
10 {
12 even++;
13 }
14 else
15 {
17 odd++;
18 }
20 }
21 std::cout << "even: " << even << ", odd: " << odd << "n";
You discover that the code segment does not compute the correct result when it is executed with more than one thread. You need to ensure that the application computes the result correctly regardless of the number of threads.
What should you do?
A.
Replace line 05 with the following line of code:
#pragma omp parallel for private(even,odd)
B.
Replace line 05 with the following line of code:
#pragma omp parallel for reduction(+:even,odd)
C.
Insert the following line of code at lines 08, 11, and 16:
#pragma omp atomic
D.
Insert the following line of code at line 08:
#pragma omp critical
{
Insert the following line of code at line 19:
}