Question: I have this C++ code i need to complete the last 4 functions related to OpenMP and subsequently fill the executime table. 1.sumWithLoop function to

I have this C++ code i need to complete the last 4 functions related to OpenMP and subsequently fill the executime table.

1.sumWithLoop function to compute the sum of elements in the array using a for loop sequentially.

2. sumWithLoop_OMP function to compute the sum of elements in the array using a for loop in parallel (using OMP).

3. complete sumRec function to compute the sum of elements in the array recursively by splitting the parameter array into halves every time.

4. compete SumRec_OMP function to compute the sum of elements in the array recursively by splitting the parameter array into halves every time and executing two recursive calls in parallel (using OMP).

/* * Usage info: * g++ -fopenmp -o prob6 prob6.cpp * ./prob6 * To save the output in a file, redirect stdout to a file, i.e., * ./prob6 > file.txt * where file.txt is the desired name of the output file. */

#include #include #include #include #include

using namespace std;

int sumWithLoop(int * A, int n); int sumWithLoop_OMP(int * A, int n); int sumRec(int * A, int n, int start, int end); int sumRec_OMP(int * A, int n, int start, int end);

int main() { double wtime; int n; printf ( \" How many random numbers do you want to generate? \" ); scanf ( \"%d\", &n ); int * A; A = new int[n];

//generate n of random numbers srand( (unsigned) time(NULL) ); for(int i = 0; i { A[i] = rand() % 100; }

int num_procs = omp_get_num_procs ( ); int max_threads = omp_get_max_threads ( ); printf ( \" Number of processors available = %d \", num_procs ); printf ( \" Number of threads = %d \", max_threads ); //executing sumWithLoop and also mesuring its execution time wtime = omp_get_wtime ( ); int sum1 = sumWithLoop(A, n); wtime = omp_get_wtime ( ) - wtime; printf( \" sum loop is %d \", sum1 ); printf ( \" time %14f \", wtime );

//executing sumWithLoop_OMP and also mesuring its execution time wtime = omp_get_wtime ( ); int sum2 = sumWithLoop_OMP(A, n); wtime = omp_get_wtime ( ) - wtime; printf( \" sum loop OMP is %d \", sum2 ); printf ( \" time %14f \", wtime ); //executing sumRec and also mesuring its execution time wtime = omp_get_wtime ( ); int sum3 = sumRec(A, n, 0, n-1); wtime = omp_get_wtime ( ) - wtime; printf( \" sum recursion is %d \", sum3 ); printf ( \" time %14f \", wtime ); //executing sumRec_OMP and also mesuring its execution time wtime = omp_get_wtime ( ); int sum4 = sumRec_OMP(A, n, 0, n-1); wtime = omp_get_wtime ( ) - wtime; printf( \" sum recursion OMP is %d \", sum4 ); printf ( \" time %14f \", wtime ); }

int sumWithLoop(int * A, int n) { //TO BE COMPLETED }

int sumWithLoop_OMP(int * A, int n) { //TO BE COMPLETED }

int sumRec(int * A, int n, int start, int end) { //TO BE COMPLETED }

int sumRec_OMP(int * A, int n, int start, int end) { //TO BE COMPLETED }

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 Programming Questions!