Which line of code should you insert at line 06?

You create an OpenMP application by using Microsoft Visual C++. The application includes an OpenMP parallel region. The region contains the following code segment.
(Line numbers are included for reference only.)

01 int global=0;
03 void PerformComputation(int value)
04 {
05 //…
07 global+=value;
08 }
10 int _tmain(int argc, _TCHAR* argv[])
11 {
12 #pragma omp parallel for
13 for(int count=0;count<100;count++)
14 {
15 //…
16 PerformComputation(count);
17 //…
18 }
19 }

For each iteration that updates the global variable, the for loop must call the PerformComputation method.
You need to ensure that shared memory access to the global variable is protected.
Which line of code should you insert at line 06?

You create an OpenMP application by using Microsoft Visual C++. The application includes an OpenMP parallel region. The region contains the following code segment.
(Line numbers are included for reference only.)

01 int global=0;
03 void PerformComputation(int value)
04 {
05 //…
07 global+=value;
08 }
10 int _tmain(int argc, _TCHAR* argv[])
11 {
12 #pragma omp parallel for
13 for(int count=0;count<100;count++)
14 {
15 //…
16 PerformComputation(count);
17 //…
18 }
19 }

For each iteration that updates the global variable, the for loop must call the PerformComputation method.
You need to ensure that shared memory access to the global variable is protected.
Which line of code should you insert at line 06?

A.
#pragma omp single

B.
#pragma omp atomic

C.
#pragma omp master

D.
#pragma omp barrier

Explanation:
25. You create an OpenMP application by using Microsoft Visual C++. You write the following code segment. (Line numbers are included for reference only.) 01 void doStepA()
02 {
03 //…
04 }
06 void doStepB()
07 {
08 //…
09 }
11 int _tmain(int argc, _TCHAR* argv[])
12 {
13 #pragma omp parallel
14 {
15 doStepA();
16 doStepB();
17 }
18 }
All the threads in the parallel region must complete the doStepA method before they call the doStepB
method.
You need to replace the code segment from lines 14 through 17 to meet the requirements.
Which code segment should you use?

A. {
doStepA();
#pragma omp barrier
doStepB();
}
B. {
#pragma omp critical
{
doStepA();
}
doStepB();
}
C. {
omp_lock_t myLock;
omp_init_lock(&myLock);
#pragma omp sections
{
#pragma omp section
{
omp_set_lock(&myLock);
doStepA();
omp_unset_lock(&myLock);
}
#pragma omp section
{
doStepB();
}
}
}
D. {
omp_lock_t myLock;
omp_init_lock(&myLock);
#pragma omp sections
{
#pragma omp section
{
omp_set_lock(&myLock);
doStepA();
}
#pragma omp section
{
omp_unset_lock(&myLock);
doStepB();
}
}
}



Leave a Reply 0

Your email address will not be published. Required fields are marked *