Question: Task 2.2 - Work Sharing This task explores the use of the for work-sharing construct. The program provided that adds two vectors together using a

Task 2.2 - Work Sharing

This task explores the use of the for work-sharing construct. The program provided that adds two vectors

together using a work-sharing approach to assign work to threads is given below:

#include

#include

#include

#define CHUNKSIZE 10

#define N 100

int main (int argc, char *argv[]) {

int nthreads, tid, i, chunk;

float a[N], b[N], c[N];

for (i=0; i < N; i++)

a[i] = b[i] = i * 1.0; // initialize arrays

chunk = CHUNKSIZE;

#pragma omp parallel shared(a,b,c,nthreads,chunk) private(i,tid) {

tid = omp_get_thread_num();

if (tid == 0) {

nthreads = omp_get_num_threads();

printf("Number of threads = %d ", nthreads);

}

printf("Thread %d starting... ",tid);

#pragma omp for schedule(dynamic,chunk)

for (i=0; i

c[i] = a[i] + b[i];

printf("Thread %d: c[%d]= %f ",tid,i,c[i]);

}

} /* end of parallel section */

}

This program has an overall parallel region within which there is a work-sharing for construct. Compile and

execute the program. Depending upon the scheduling of work different threads might add elements of the

vector. It may be that one thread does all the work. Execute the program several times to see any different

thread scheduling. In the case that multiple threads are being used, observe how they may interleave.

Alter the code from dynamic scheduling to static scheduling and repeat. What are your conclusions?

Time of execution

Measure the execution time by instrumenting the code with the OpenMP routine omp_get_wtime() at the

beginning and end of the program, finding the difference in time.

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!