Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 }

image text in transcribed

5. [7 pts] Continue reading the notes on OpenMP (file OpenMP.pd), see the attached prob5.cpp file, and complete the program by writing code to compute a sum of randomly generated numbers by: a) [l pt] Complete sumWithLoop function to compute the sum of elements in the array using a for loop sequentially. Include your code for this function in the file that you will be submitting. b) [1 pt] Complete sum WithLop(MP function to compute the sum of elements in the array using a for loop in parallel (using OMP). Include your code for this function in the file that you will be submitting. c) [1 pt] Complete sum Rec function to compute the sum of elements in the array recursively by splitting the parameter array into halves every time. Include your code for this function in the file that you will be submitting. d) [1 pt] 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). Include your code for this function in the file that you will be submitting. e) [3 pts] Using 4 threads, execute your program using 100, 10000, 1000000, and 103 as the values of n (the number of randomly generated numbers and also the array size). Then record the execution time for each of the four functions above in a table format n-1000000-10 -100000000 Execution time sumWithLoop sum WithLoop OMP sum Rec SumRec OMP n-100 n-10000 Note: after completing your functions, you can compile& run it in general as $ gt+ -fopenmp -o prob5 prob5.cpp To set the number of threads to 4, type the following before you execute the prob5 $ export OMP NUM THREADS=4 To execute, type ./prob!5 5. [7 pts] Continue reading the notes on OpenMP (file OpenMP.pd), see the attached prob5.cpp file, and complete the program by writing code to compute a sum of randomly generated numbers by: a) [l pt] Complete sumWithLoop function to compute the sum of elements in the array using a for loop sequentially. Include your code for this function in the file that you will be submitting. b) [1 pt] Complete sum WithLop(MP function to compute the sum of elements in the array using a for loop in parallel (using OMP). Include your code for this function in the file that you will be submitting. c) [1 pt] Complete sum Rec function to compute the sum of elements in the array recursively by splitting the parameter array into halves every time. Include your code for this function in the file that you will be submitting. d) [1 pt] 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). Include your code for this function in the file that you will be submitting. e) [3 pts] Using 4 threads, execute your program using 100, 10000, 1000000, and 103 as the values of n (the number of randomly generated numbers and also the array size). Then record the execution time for each of the four functions above in a table format n-1000000-10 -100000000 Execution time sumWithLoop sum WithLoop OMP sum Rec SumRec OMP n-100 n-10000 Note: after completing your functions, you can compile& run it in general as $ gt+ -fopenmp -o prob5 prob5.cpp To set the number of threads to 4, type the following before you execute the prob5 $ export OMP NUM THREADS=4 To execute, type ./prob!5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions

Question

Select suitable tools to analyze service problems.

Answered: 1 week ago