Question: C ++ code: Given the serial code that adds two vectors, convert the code into a parallel program and use it to find the sum

C ++ code: Given the serial code that adds two vectors, convert the code into a parallel program and use it to find the sum of the elements of the array given in the code.

// given the arrays int a[] = { 4, 3, 1, 6, 8, 9, 100, 23, 13, 44, 14, 67, 89, 34, 23, 69, 100, 65, 32, 33, 4 ,5 ,6 ,18 }; int b[] = { 1, 7, 8, 4, 9, 12, 10, 3, 1, 14, 7, 7, 9, 3, 3, 6, 11, 165, 2, 1, 14, 15, 8, 33 }; int c[24]; // there are 24 elements in a and b // adds every item of the array to the sum for (int i = 0; i < 24 ; i++) { c[i] = a[i] + b[i]; } // Showing the elements of Array c for (int i = 0; i < 24 ; i++) { cout << c[i] << " "; }

#include #include using namespace std;

// This program finds the sum of numbers from 0-100 with // variable p number of threads int global_sum = 0; int N = 101; const int p = 5;

void myThreadMethod(int id) { int local_sum = 0; int mystart = id * N / p; int myend = (id + 1) * N / p; for (int i = mystart; i < myend; i++) { local_sum += i; }

global_sum += local_sum; }

int main() { thread* t[p]; for (int i = 0; i < p; i++) { t[i] = new thread(myThreadMethod, i); }

for (int i = 0; i < p; i++) { t[i]->join(); }

cout << "Result: " << global_sum << endl;

}

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!