How would you rewrite the following sequential code so that

How would you rewrite the following sequential code so that it can be run as two parallel threads

on a dualcore processor? Try to balance the loads as much as possible between the two threads:

int A[80], B[80], C[80], D[80];

for (i = 0 to 40)

{

A[i] = B[i] * D[2*i];

C[i] = C[i] + B[2*i];

D[i] = 2*B[2*i];

A[i+40] = C[2*i] + B[i];

}

Solution

The above given sequnetial code could be rewritten in two parrallel thread to balance the load as below :

Thread 1:
int A[80], B[80], C[80], D[80];
for (i = 0 to 40)
{
A[i] = B[i] * D[2*i];
C[i] = C[i] + B[2*i];
A[i+40] = C[2*i] + B[i];
}

Thread 2

int A[80], B[80], C[80], D[80];
for (i = 0 to 40)
{
D[i] = 2*B[2*i];
}

Here in thread 1 we are just calculating values for array A and in thread 2 we are calculating for array D, these could be divided because in the given sequential code there is no connection or dependency while calculating A and D those are not accessing each other\'s values.

How would you rewrite the following sequential code so that it can be run as two parallel threads on a dualcore processor? Try to balance the loads as much as p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site