Programming Assignment: On Openmp
Programming Assignment: On Openmp
“Programming assignment
on OpenMP”
Submitted by
Navachethan M ISE -7C 1NT18IS099
COURSE C-ORDINATOR
Mrs.Deepika.K.M
Assistant Professor
Department of Information Science and Engineering
NMIT
1|Page
INDEX:
Sl. No. Title Pg. No.
1 Introduction 3
13 Conclusion 19
2|Page
INTRODUCTION:
As the world is growing people are getting closely related to the technology we have started with
mathematics as a basic step and today have arrived at the time where a lot of new technologies have
evolved and are being updated regularly. From traditional ledger to SQL and later NoSQL databases are
introduced. AI/ML , bigdata to predict the future in various aspects from cancer to a patient to stock
market. Also block chain to provide security. Also a lot of programming languages and tools are
introduced every now and then. In mean time people are building more faster and accurate calculating
st
machines, today time & data are the most precious and key concept of 21 century software industry. One
of the method to improve the performance of the computation is by introducing the concept of parallel
programming or scalable computing. It is generally defined as “Parallel programming is a kind of
computation technique where complex problems are split into smaller one and all are computed parallely
or simultaneously”.
Parallel computing is one of the major concept for research in the industry. Companies invest more
to improve the computation speed and accuracy of their machines every time. Concept of multiple core
processors is introduced that helps in performing the parallel computations. Nowadays every computers,
laptops , etc… are having the feature of parallel computing and currently the introduction of GPU’s have
increased the performance of computing. A lot of tools and technologies are introduced to perform parallel
programming. Some of them are MPI(Message Passing Interface), OpenMP, CUDA, spark to name a few.
The main aim of this report is use to learn some of the methods of parallel computing using
OpenMP, CUDA and spark by learning and executing the programs that can be run parallely and are basic
examples of scalable computing.
3|Page
OpenMP:
The section of code that is meant to run in parallel is marked accordingly, with a compiler directive
that will cause the threads to form before the section is executed. Each thread has an id attached to it which
can be obtained using a function (called omp_get_thread_num() ). The thread id is an integer, and the
primary thread has an id of 0. After the execution of the parallelized code, the threads join back into the
primary thread, which continues onward to the end of the program.
The OpenMP Architecture Review Board (ARB) published its first API specifications, OpenMP for
Fortran 1.0, in October 1997. In October the following year they released the C/C++ standard. 2000 saw
version 2.0 of the Fortran specifications with version 2.0 of the C/C++ specifications being released in
2002. Version 2.5 is a combined C/C++/Fortran specification that was released in 2005. Up to version 2.0,
OpenMP primarily specified ways to parallelize highly regular loops, as they occur in matrix-oriented
numerical programming, where the number of iterations of the loop is known at entry time. This was
recognized as a limitation, and various task parallel extensions were added to implementations. In 2005, an
effort to standardize task parallelism was formed, which published a proposal in 2007, taking inspiration
from task parallelism features in Cilk, X10 and Chapel. Version 3.0 was released in May 2008. Included in
the new features in 3.0 is the concept of tasks and the task construct, significantly broadening the scope of
OpenMP beyond the parallel loop constructs that made up most of OpenMP 2.0. Version 4.0
of the specification was released in July 2013. It adds or improves the following features support for
accelerators atomics error handling thread affinity tasking extensions user
defined reduction; SIMD support; Fortran 2003 support. The current version is 5.1, released in November
2020.
#include <stdio.h>
#include <omp.h>
4|Page
int main(void)
{
#pragma omp parallel
printf("Hello, world.\n");
return 0;
}
Hello, world.
Hello, world.
OpenMP Programs:
Program:
int main () {
int n = 45;
# pragma omp parallel
{
int t = omp_get_thread_num ();
printf ("%d: %ld\n", t, fib (n + t));
}
5|Page
return 0;
}
Output:
6|Page
2. Program to print numbers from 1 to max (parallelizing loops with independent iterations).
The following program prints number from 1 to max which takes user input. The program prints the
number and the corresponding thread number that print the value. The threads created run parallel to each
other and each thread inside a loop run independent to each other wherein the max variable is the only
variable that is being shared, the variable I used for iteration will be a private variable for each thread.
Program:
#include <stdio .h>
# include <omp.h>
int main ( int argc , char * argv []) {
int max;
sscanf ( argv [1] , "%d", &max );
# pragma omp parallel for for ( int i = 1; i <= max; i++)
printf ("%d: %d\n", omp_get_thread_num () ,
i); return 0;
}
Output:
7|Page
3. Program for OpenMP data sharing.
This program is about the data sharing in the parallel programming. This program lets us know how
the variable before pragma are shared and the variable after parallelization are kept private for each
individual thread. Here the data x is shared where as the variable i is a private variable for each
thread created and all the threads created run parallely and that can be noticed in the output.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main(void){ int
i;
int x;
x=44;
#pragma omp parallel for private(x)
for(i=0;i<=10;i++){
x=i;
printf("Thread number: %d x: %d\n",omp_get_thread_num(),x);
}
printf("x is %d\n", x);
}
Output:
8|Page
4. OpenMP program for multiple loops.
This program is used to know the parallelizing of multiple loops. The program consists of two for
loops that are parallelized. In this the outer loop is parallelized and run in multiple threads but the
inner for loop runs in sequential order as shown in the output. Hence multiple for loops cannot be
directly parallelized. Method of combining the loop or parallelizing separately is a better solution.
Program:
# include <stdio .h>
# include <omp.h>
int main ( int argc , char * argv []) {
int max;
sscanf ( argv [1] , "%d", &max );
# pragma omp parallel for for ( int i = 1; i <= max; i++) for ( int j = 1; j <= max; j ++)
printf ("%d: (%d ,%d)\n", omp_get_thread_num () , i, j);
return 0;
}
Output:
9|Page
5. OpenMP program to parallelize multiple loops.
This is the updation made to the program 4 where the parallelization of multiple nested for loop
was restricted. One of the method to overcome this drawback is to collapse the nested for loops
where the for loops are combined and a single for loop is created which is later parallelized.
Program:
# include <stdio .h>
# include <omp.h>
int main ( int argc , char * argv []) {
int max; sscanf ( argv [1] , "%d", &max );
# pragma omp parallel for collapse (2) for ( int i = 1; i <= max; i++)
for ( int j = 1; j <= max; j ++)
printf ("%d: (%d ,%d)\n", omp_get_thread_num () , i, j);
return 0;
}
Output:
10 | P a g e
6. OpenMP program to parallelize nested for loop separately.
This is the updation made to the program 4 where the parallelization of multiple nested for loop
was restricted. One of the method to overcome this drawback is to parallelize nested for loop
separately as shown in the program which is better when compared to collapse method which is
later parallelized.
Program:
# include <stdio .h>
# include <omp.h>
int main ( int argc , char * argv []) {
int max; sscanf ( argv [1] , "%d", &max );
# pragma omp parallel for
for ( int i = 1; i <= max; i++) {
# pragma omp parallel for
for ( int j = 1; j <= max; j ++) {
printf ("%d: (%d ,%d)\n", omp_get_thread_num () , i, j);
}
}
return 0;
}
Output:
11 | P a g e
7. Program to understand combining the parallel solutions in OpenMP.
Parallel programming main objective is to breakdown the complex problem into simpler one and
run them parallely to reduce the time of execution. Sometimes the program can give parallel
outputs and no need of combining the results, but sometime the parallel output obtained is partial
answer and needs to be combined to obtain a final desired output. Hence the following condition s
analyzed from the given program.
Program:
# include <stdio .h>
int main ( int argc , char * argv []) {
int max; sscanf ( argv [1] , "%d", &max );
int sum = 0;
# pragma omp parallel for for ( int i = 1; i <= max; i++) sum = sum + i;
printf ("%d\n",
sum ); return 0;
}
Output:
12 | P a g e
Note: You can see that the output for sum of n numbers is varying. For example in the given output
sum of 10 natural numbers should be 55 i.e. 1+2+3+4+5+6+7+8+9+10 = 55, but you can see
that the output is varying like 40, sometimes it shows 44, 42 and so on. Point to be noted is in
parallel programming the sum operation is carried out by the multiple threads and as a result
since the operation is carried out by one thread other thread which needs the same operation
misses the opportunity and results in the race around condition that leads to wrong output. So to
overcome this problem one of the solutions is putting the sum operation statement in critical
section which is going to be seen in the consecutive program.
13 | P a g e
8. OpenMP program to show usage of critical section in parallel programming.
As discussed above sometime parallel programming can give wrong output due to the race
condition occurred when multiple threads want to perform the same computation. One of the
method to overcome this problem is putting the computation statement inside the critical section
that reduces the risk of race condition occurrence. Time taken may increase a bit but it is better than
a race condition that provides wrong output.
Program:
# include <stdio .h>
int main ( int argc , char * argv []) {
int max; sscanf ( argv [1] , "%d", &max );
int sum = 0;
# pragma omp parallel for for ( int i = 1; i <= max; i++)
# pragma omp critical
sum = sum + i;
printf ("%d\n", sum );
return 0;
}
Output:
Note: Checkout the output after using the critical section. The output is not changing and is
accurate.
14 | P a g e
9. OpenMP program to check the distribution of iterations among the threads.
The program mentioned is used to know the distribution of iterations among the threads that are
created. This lets us know how the iterations are distributed and how the computation is being
carried out and finally how the sum up is done and the final result is printed. Users can define the
number of threads that can be utilized and in the output you can see the number of threads in first
output is 4 and second output is 8 and also you can see the pattern in which the iterations are being
distributed among the team of threads.
Program:
# include <stdio .h>
# include <unistd .h>
# include <omp.h>
int main ( int argc , char * argv []) {
int max; sscanf ( argv [1] , "%d", &max );
long int sum = 0;
# pragma omp parallel for reduction (+: sum) schedule ( runtime ) for ( int i = 1; i <= max; i++) {
printf ("%2d @ %d\n", i, omp_get_thread_num ());
sleep (i < 4 ? i + 1 : 1);
sum = sum + i;
}
printf ("%ld\n", sum );
return 0;
}
Output:
15 | P a g e
10. A middleman company is providing the loan support services to the people. It asks users to
enter their interest in the loans and the information is stored in matrix format. Also it has another
matrix where in for each bank the interest rate for the loan type is stored. Can you give a solution
regarding what will be the total interest rate for each person in each bank?
Provide a solution for this using “FOR DIRECTIVE” of OpenMp library.
Program: #include
<stdio.h>
#include <time.h>
#include<omp.h>
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k,tid;
clock_ttic,toc;
16 | P a g e
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
inttid=omp_get_thread_num();
#pragma omp for
for(i=0; i<r1; ++i)
{
printf("\nHI I AM THREAD %d AND MY VALUE=%d\n",tid,i);
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
17 | P a g e
if(j == c2-1)
printf("\n\n");
}
return 0;
}
Output:
18 | P a g e
Conclusion:
Parallel programming or scalable computing is one of the major aspect of software industry. So far
as per our aim we have gone through the introduction of parallel programming some of the commonly and
widely used tools and libraries such as OpenMp, MPI, CUDA, spark to name a few. We have successfully
known some of the simple yet effective concepts of OpenMP such a how to run the program, nested
parallelism, critical section usage, collapse, separate parallelization, distribution of iteration among the
threads and finally an application of OpenMP (middleman problem) is also analyzed and thus we have a
good basic knowledge about what is OpenMP its significance and how to run OpenMP programs and have
sound knowledge about some of the concepts mentioned above.
19 | P a g e