You are on page 1of 10

Reg No : 18BCE0661

Name : Dhruv Sakhuja


Slot : L49 + L50

Exercise 1 (OpenMP-I)

SCENARIO – I

Write a simple OpenMP program for vector addition (sum). It is helpful to understand how
threads are created in OpenMP.

To examine the above scenario, we are going to take two one-dimensional arrays, eargch of
size of 5. We will then create 5 threads. Eargch thread will be responsible for one addition
operation.

Note: In OpenMP, pre-defined preprecessor directive #pragma omp parallel is used to create
threads. We can mention the number of threads to be created as a parameter to
num_threads(). If you are not mentioning num_threads(), then the number of threads to be
created is equal to number of cores in processor. Thread id can be obtained by using
predefined function omp_get_thread_num().

ALGORITHM:

Step 1: Include library file


Step 2: Define the size of arrays whose elements will be added together
Step 3: Define the number of threads to use for vector addition Step 4: Allocate space for the array.
Step 5: Initialize arrays a and b with consecutive integer values.
Step 6: Determine how many elements each process will work on.
Step 7: Print output.
Step 8: Clean up memory.

SOURCE CODE:
#include <stdlib.h>
#include <stdio.h>
#include <omp.h

#define ARRAY_SIZE 8
#define NUM_THREADS 4

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


{

int * a;
int * b;
int * c;

int n = ARRAY_SIZE;
int n_per_thread;
int total_threads = NUM_THREADS;
int i; // loop index
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50

a = (int *) malloc(sizeof(int)*n);
b = (int *) malloc(sizeof(int)*n);
c = (int *) malloc(sizeof(int)*n);

for(i=0; i<n; i++) {


a[i] = i;
}
for(i=0; i<n; i++) {
b[i] = i;
}

omp_set_num_threads(total_threads);

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];

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]);
}

free(a); free(b); free(c);

return 0;
}
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50

OUTPUT SCREEN SHOT:

RESULTS:
Thus the vector addition of two vectors was performed using OpenMP and the result was verified.
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50
SCENARIO – II

Write a simple OpenMP program for performing dot product It is helpful to understand how
threads can be executed in parallel to sequentially check eargch element of the list for the
target value until a match is found or until all the elements have been searched.

Note: In OpenMP, to parallelize the for loop, the openMP directive is: #pragma omp parallel
for.

BRIEF ABOUT YOUR APPROARGCH:

Step 1: Define a length of dot product vectors and number of OpenMP threads.
Step 2: Assign storage for dot product vectors. Next, we will initialize dot product vectors.
Step 3: Initialize global sum.
Step 4: Performing the dot product in an OpenMP parallel region for loop with a sum reduction for
illustration purposes.
Step 5: Explicitly sets number of threads.
Step 6: Each thread keeps track of its partial sum.
Step 7: Print the output.

SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <omp.h>
#define SIZE 10

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

float u[SIZE], v[SIZE], dp,dpp;


int i, j, tid;

dp=0.0;
for(i=0;i<SIZE;i++){
u[i]=1.0*(i+1);
v[i]=1.0*(i+2);
}
printf("\n values of u and v:\n");

for (i=0;i<SIZE;i++){
printf(" u[%d]= %.1f\t v[%d]= %.1f\n",i,u[i],i,v[i]);
}
#pragma omp parallel shared(u,v,dp,dpp) private (tid,i)
{
tid=omp_get_thread_num();

#pragma omp for private (i)


for(i=0;i<SIZE;i++){
dpp+=u[i]*v[i];
printf("thread: %d\n", tid);
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50
SCENARIO – III
}
#pragma omp critical
{
dp=dpp;
printf("thread %d\n",tid);
}

printf("\n dot product is %f\n",dp);

OUTPUT :
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50
SCENARIO –
IV

RESULTS:
Thus the vector dot product of two vectors was performed using OpenMP and the output was
verified.
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50
SCENARIO – III

Write a simple openMP program to demonstrate the sharing of a loop iteration by number of
threads . You can have a chunk size of 10 .

ALGORITHM
Step 1: Add required libraries.
Step 2: Initialize
Step 3: A parallel do/for loop divides up the iterations of the loop between threads. Step 4: There is
a synchronization point at the end of the loop: all threads must finish their iterations before any
thread can proceed.
Step 5: If the loop gives the same answers, if it is run in reverse order, then it is almost certainly
parallel.
Step 6: Print final output.

SOURCE CODE:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define CHUNKSIZE 10
#define N 10

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


{
int nthreads, tid, i, chunk;
float a[N], b[N], c[N];

/* Some initializations */
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]);
}
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50
}
}

OUTPUT:

RESULTS: Thus loop iteration by a number of threads was demonstrated using OpenMP and the
result was verified.

SCENARIO – IV

Write a open MP program to demonstrate the sharing of works of a section using threads.
You can perform arithmetic operations on the one dimensional array and this section load can
be shared by the threads.

ALGORITHM
Step 1: Add required libraries.
Step 2: Initialize.
Step 3: Allows separate blocks of code to be executed in parallel (e.g. several independent
subroutines)
Step 4: There is a synchronization point at the end of the blocks: all threads must finish their blocks
before any thread can proceed.
Step 5: Print final output.

SOURCE CODE:

#include <omp.h>
#include <stdio.h>
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50
#include <stdlib.h>
#define N 10

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


{
int i, nthreads, tid;
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;
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\n", nthreads);
}
printf("Thread %d starting...\n",tid);

#pragma omp sections nowait


{
#pragma omp section
{
printf("Thread %d doing section 1\n",tid);
for (i=0; i<N; i++)
{
c[i] = a[i] + b[i];
printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
}
}

#pragma omp section


{
printf("Thread %d doing section 2\n",tid);
for (i=0; i<N; i++)
{
d[i] = a[i] * b[i];
printf("Thread %d: d[%d]= %f\n",tid,i,d[i]);
}
}

printf("Thread %d done.\n",tid);

}
}
OUTPUT:

RESULTS: Thus sharing of section by a number of threads was demonstrated using OpenMP and
the result was verified.

You might also like