Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the program below: #include #include #include #include using namespace std; int main() { const long MAX = 500000000; const int RANGE = 6; vector

Consider the program below: #include  #include  #include  #include  using namespace std; int main() { const long MAX = 500000000; const int RANGE = 6; vector num(MAX); unsigned long long int sum = 0; int h1=0; int h2=0; int h3=0; int h4=0; int h5=0; for (int i = 0; i < MAX; i++) { num[i] = rand() % RANGE; //cout << num[i] << endl; //generates random numbers 1 to 5 } auto start = chrono::high_resolution_clock::now(); for (int i = 0; i < MAX; i++) { sum = sum + num[i]; } for(int i=0; i < MAX; i++){ switch(num[i]){ case 1: h1++; break; case 2: h2++; break; case 3: h3++; break; case 4: h4++; break; case 5: h5++; break; } } auto end = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast(end - start); cout << "Histogram Result is : " << endl; cout << "1 ----> " << h1 << " SUM : " << h1 << endl; cout << "2 ----> " << h1 << " SUM : " << h2*2 << endl; cout << "3 ----> " << h1 << " SUM : " << h3*3 << endl; cout << "4 ----> " << h1 << " SUM : " << h4*4 << endl; cout << "5 ----> " << h1 << " SUM : " << h5*5 << endl; unsigned long long int histogramsum = h1 + (h2*2) + (h3*3) + (h4*4) + (h5*5); cout << "The sum of the vector is : " << sum << " and the sum of the histogram is " << histogramsum << endl; cout << "TOTAL RUNNING TIME IS : " << duration.count() << " milliseconds...."; return 0; }

The program initializes a vector with an assigned capacity and a random number for each element. The program counts the histogram of all of the contents of the vector ranging from 1 to 5 and displays it on the screen. The program is also getting the sum of all of the vectors and displays it afterwards. I just displayed the equivalent sum of the histogram and as well as the sum that i was able to get from the vectors for double checking if they match so as to know the program is correct.

Your task is to improve the performance of the program, as we can see that there are things that we can do in concurrent here namely (counting of histogram and getting the sum of the vector). Convert the program to support multithreading in counting the histogram and getting the sum of the vector.

-Run the original program and get the screenshot

-Run your threaded program and get the screenshot

-Determine the percentage of enhancement you were able to achieve using the threaded version and display it on your threaded program. Save the value of the total run on the original program and have it encoded on your threaded program to get the percentage.

-You can make variables global

-Use code sharing technique for your thread

-Be aware of race condition when histogram counting is being implemented as thread thus there is a need to use for locks

-Submit your CPP file and as well as the 2 screenshot of the output (original and threaded)

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

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions