0% found this document useful (0 votes)
92 views

Cse 4001-Parallel and Distributed Computing Lab Digital Assessment-1 Name: Avulapati Anusha REG - NO: 17BCE0435

This document contains code for three OpenMP programs that perform parallel computing tasks: 1) A vector addition program that divides the work of adding corresponding elements of input arrays a and b across 4 threads, storing the results in array c. 2) A loop work sharing program that dynamically divides the work of performing calculations on arrays a and b across multiple threads, storing results in c. 3) A section work sharing program that divides the work of performing two separate calculations on arrays a and b across threads, storing results in arrays c and d.

Uploaded by

Asrith Kakumanu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Cse 4001-Parallel and Distributed Computing Lab Digital Assessment-1 Name: Avulapati Anusha REG - NO: 17BCE0435

This document contains code for three OpenMP programs that perform parallel computing tasks: 1) A vector addition program that divides the work of adding corresponding elements of input arrays a and b across 4 threads, storing the results in array c. 2) A loop work sharing program that dynamically divides the work of performing calculations on arrays a and b across multiple threads, storing results in c. 3) A section work sharing program that divides the work of performing two separate calculations on arrays a and b across threads, storing results in arrays c and d.

Uploaded by

Asrith Kakumanu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CSE 4001- PARALLEL AND DISTRIBUTED COMPUTING

LAB DIGITAL ASSESSMENT-1


NAME: AVULAPATI ANUSHA
REG.NO: 17BCE0435

a) Using OpenMP, Design, develop and run a multi-threaded program to


perform and print vector addition.
CODE:-
#include <stdlib.h> //malloc and free
#include <stdio.h> //printf
#include <omp.h>
//OpenMP
#define ARRAY_SIZE 8 //Size of arrays whose elements will be added
together.
#define NUM_THREADS 4 //Number of threads to use for vector addition.
int main (int argc, char *argv[])
{
int * a;
int * b;
int * c;
int n = ARRAY_SIZE;
// number of array elements
int n_per_thread;
// elements per thread
int total_threads = NUM_THREADS; // number of threads to use
int i;
// loop index
// allocate spce for the arrays
a = (int *) malloc(sizeof(int)*n);
b = (int *) malloc(sizeof(int)*n);
c = (int *) malloc(sizeof(int)*n);
// initialize arrays a and b with consecutive integer values
// as a simple example
for(i=0; i<n; i++) {
a[i] = i;
}
for(i=0; i<n; i++) {
b[i] = i;
}
omp_set_num_threads(total_threads);
// determine how many elements each process will work on
n_per_thread = n/total_threads;
#pragma omp parallel for shared(a, b, c) private(i) schedule(static,
n_per_thread)
for(i=0; i<n; i++) {c[i] = a[i]+b[i];
// Which thread am I? Show who works on what for this samll example
printf("Thread %d works on element%d\n", omp_get_thread_num(), i);
}
printf("i\ta[i]\t+\tb[i]\t=\tc[i]\n");
for(i=0; i<n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", i, a[i], b[i], c[i]);
}
// clean up memory
free(a); free(b); free(c);
return 0;
}

OUTPUT:-
b) Using OpenMP, Design, develop and run a multi-threaded program to
perform Loop work Sharing.
CODE:-
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#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;
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\n", nthreads);
}
printf("Thread %d starting...\n",tid);
#pragma omp for schedule(dynamic, chunk)
for (i=0; i<N; i++)
{
c[i] = a[i] + b[i];
printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
}
} /* end of parallel section */
}

c) Using OpenMP, Design, develop and run a multi-threaded program to


perform Section work sharing
CODE:-
#include <omp.h>
#define N 1000
main(int argc, char *argv[]) {
int i;
float a[N], b[N], c[N], d[N];
for (i=0; i < N; i++) {
a[i] = i * 1.5;
b[i] = i + 22.35;
}
#pragma omp parallel shared(a,b,c,d) private(i)
{
#pragma omp sections nowait
{
#pragma omp section
for (i=0; i < N; i++)
c[i] = a[i] + b[i];
#pragma omp section
for (i=0; i < N; i++)
d[i] = a[i] * b[i];
}}}

OUTPUT:

You might also like