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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started