Professional Documents
Culture Documents
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:
SOURCE CODE:
#include <stdlib.h>
#include <stdio.h>
#include <omp.h
#define ARRAY_SIZE 8
#define NUM_THREADS 4
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);
omp_set_num_threads(total_threads);
n_per_thread = n/total_threads;
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]);
}
return 0;
}
Reg No : 18BCE0661
Name : Dhruv Sakhuja
Slot : L49 + L50
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.
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
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();
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
/* Some initializations */
for (i=0; i < N; i++)
a[i] = b[i] = i * 1.0;
chunk = CHUNKSIZE;
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
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.