Question: C. OpenMP (12.5 points each) Program 1 Assume that array is an array of size int values, but each value is a 1. What does

C. OpenMP (12.5 points each)

Program 1

Assume that array is an array of size int values, but each value is a 1. What does this print out when run on 4 threads? What does it run on 8 threads? Modify this code to get it to calculate and print out the actual sum of the values in the best possible way.

int sum =0;

# pragma omp parallel default ( none ) shared ( array , sum )

{

for ( int i =0; i < size ; i ++)

{

# pragma omp atomic

sum += array [ i ];

}

}

printf ( " total : %d " , sum );

Program 2

Use OpenMP for loop pragmas to parallelize this code as efficiently as possible. The only modifications you are allowed to do are pragmas. values is a std::vector.

for ( int i =1; i < values . size (); i ++)

{

values [ i] = values [i ]+ values [i -1];

}

for ( int i =0; i < values . size (); i ++)

{

for ( int j =0; j < array [i ]; j ++)

{

array [i] += sqrt ( j )*( i -j );

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!