Question: / / C + + code #include #include #include #include #include #include using namespace std; #define n 1 0 0 int A [ n ]

// C++ code
#include
#include
#include
#include
#include
#include
using namespace std;
#define n 100
int A[n];
auto printall =[](int p){
cout "---------------" endl;
for (int i =0; i p; i++){
for (int j =0; j n / p; j++){
cout std::setw(6) A[i * n / p + j];
}
cout endl;
}
cout endl;
};
int main()
{
srand(time(NULL));
for (int i =0; i n; i++)
A[i]= i;// rand()%10;
int p =4; // Assuming that p indicates # of processors
double k = n /(double)p;
printall(p);
// Stage 1: each Processor i computes a local prefix sum
// of a subarray of size n/p = log(n)= k
for (int i =0; i p; i++){
for (int j =1; j k; j++){
int sourcePos = i * k + j -1;
int destPos = i * k + j;
A[destPos]+= A[sourcePos];
}
}
printall(p);
// Stage 2: Prefix summation using only the rightmost value
// of each subarray (O(log(n/k)))
int i =0;
for (int j = pow(2, i); j = p; j++){
int destPos = j * k -1;
int sourcePos =(j - pow(2, i))* k -1;
if (destPos >0 && sourcePos >0)
A[destPos]+= A[sourcePos];
}
printall(p);
// Stage 3: each Proc i adds the value computed in Step 2 by Proc i-1 to
// each subarray element except for the last one
for (int i =1; i p; i++){
int sourcePos = i * k -1;
for (int j =0; j k -1; j++){
int destPos = i * k + j;
A[destPos]+= A[sourcePos];
}
}
printall(p);
return 0;
}
PLEASE PROVIDE 2 CODE. ONE NON-THREAD CODE. AND ONE THREADED CODE. FOR THREADED CODE, PLEASE USE 4 THREAD. THANKS
/ / C + + code #include #include #include

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