Question: Task 2.3 - Work Sharing with sections construct This task explores the use of the sections construction. The program that adds elements of two vectors
Task 2.3 - Work Sharing with sections construct
This task explores the use of the sections construction. The program that adds elements of two vectors to form
a third and also multiplies the elements of the arrays to produce a fourth vector is given below:
#include
#include
#include
#define N 50
int main (int argc, char *argv[]) {
int i, nthreads, tid;
float a[N], b[N], c[N], d[N];
/* Some initializations */
for (i=0; i a[i] = i * 1.5; b[i] = i + 22.35; c[i] = d[i] = 0.0; } #pragma omp parallel shared(a,b,c,d,nthreads) 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 sections nowait { #pragma omp section { printf("Thread %d doing section 1 ",tid); for (i=0; i c[i] = a[i] + b[i]; printf("Thread %d: c[%d]= %f ",tid,i,c[i]); } } #pragma omp section { printf("Thread %d doing section 2 ",tid); for (i=0; i d[i] = a[i] * b[i]; printf("Thread %d: d[%d]= %f ",tid,i,d[i]); } } } /* end of sections */ printf("Thread %d done. ",tid); } /* end of parallel section */ } This program has a parallel region but now with variables declared as shared among the threads as well as private variables. Also there is a sections work sharing construct. Within the sections construct, there are individual section blocks that are to be executed once by one member of the team of threads. Compile and execute the program and make conclusions on its execution.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
