Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.1
Lecture 11
Programming Shared Memory III
OpenMP (Open Multi-Processing); Parallelization Application
Example - Pi Computation
IKC-MH.57 Introduction to High Performance and Parallel
Computing at December 29, 2023
Dr. Cem Özdo
˘
gan
Engineering Sciences Department
˙
Izmir Kâtip Çelebi University
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.2
Contents
1 Parallelization Application Example-OpenMP
Computing π
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.3
Computing π I
Computing PI using Sequential Code
An OpenMP version of a th readed program to compute PI number
using random numbers.
Computing PI using OpenMP directives -
Random N umbers.
Follow the steps b elow for execut ing OpenMP code ;
gcc -o code21 code21.c -fopenmp
./code21
1 /
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 An OpenMP version of a threaded program to compute PI .
3
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
/
4 #pragma omp p a r a l l e l d e f a u l t ( none ) p r i v a t e ( rand_no_x , rand_no_y ,
num_threads , sample_points_per_thread ) shared ( npoin ts )
red uc ti on ( + : sum ) num_threads (8 )
5 {
6 num_threads = omp_get_num_threads ( ) ;
7 sample_points_per_thread = npoi nts / num_threads ;
8
9 sum = 0;
10 f o r ( i n t i = 0; i < sample_points_per_thread ; i ++) {
11 rand_no_x = ( double ) rand ( ) / ( double )RAND_MAX;
12 rand_no_y = ( double ) rand ( ) / ( double )RAND_MAX;
13 i f ( ( ( rand_no_x 0.5)
*
( rand_no_x 0.5 ) + (
rand_no_y 0 . 5 )
*
( rand_no_y 0 . 5 ) ) < 0 .2 5) {
14 sum = sum + 1;
15 }
16 }
17 }
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.4
Computing π II
Note that this program is much easier to write in terms of
specifying creation and ter mination of threads compared
to the corresponding POSIX threaded program.
The omp_get_num_threads() function returns the number
of threads in the parallel r egion
The omp_get_thread_num() function returns the integer id
of each thread (recall that the master thread has an id 0) .
The parallel directive specifies that all variables except
npoints, the total number of random points in two
dimensions across all threads, are local.
Further more, the directive specifies that there are eight
threads, and the value of sum after all thr eads c omplete
execution is the sum of local values at each thread.
A for loop generates the required number of random points
(in two dimensions) and determines how many of them are
within the prescribed circle of unit diameter.
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.5
Computing π III
An OpenMP version of a threaded program t o compute PI numb er by
numerical integration withou t reduction clause.
Computing PI using OpenMP directives -
Numerical Integration. Follow the steps below;
export OMP_NUM_THREADS=4
gcc -o code22 code22.c -fopenmp
./code22
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.6
Computing π IV - Sequen tial Calculation
1 # i nc lude <s t d i o . h>
2 # include <math . h>
3 i n t main ( i n t argc , char
*
argv [ ] )
4 {
5 i n t done = 0 , n , i ;
6 double PI25DT = 3.141592653589793238462643;
7 double mypi , h , sum, x ;
8 whil e ( ! done )
9 {
10 p r i n t f ( " Enter the number o f i n t e r v a l s : (0 qu i t s ) " ) ;
11 scanf ( "%d " ,&n ) ;
12
13 i f ( n == 0) break ; /
*
Quit when " 0 " entered
*
/
14 /
*
I n t e g r a l l i m i t s are from 0 to 1
*
/
15 h = (1.0 0.0) / ( double )n ; /
*
Step leng th
*
/
16 sum = 0 . 0 ; /
*
I n i t i a l i z e sum vari a b l e
*
/
17 f o r ( i = 1; i <= n ; i += 1) /
*
loop over i n t e r v a l f o r
i n t e g r a t i o n
*
/
18 {
19 x = h
*
( ( double ) i 0. 5 ) ; /
*
Middle poi nt at step
*
/
20 sum += 4.0 / (1 . 0 + x
*
x ) ; /
*
Sum up at each step
*
/
21 / / p r i n t f ( " i=%d x=%f sum=%f \ n " , i , x , sum) ; /
*
p r i n t
inte r m e d i a t e steps
*
/
22 }
23 my pi = h
*
sum ; /
*
Obtain r e s u l t i n g pi number
*
/
24 p r i n t f ( " p i i s approxi mately % .16 f , E r r o r is %.16 f \ n" , mypi ,
fabs ( mypi PI 25 DT ) ) ;
25 }
26 }
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.7
Computing π IV - OpenMP Parallel Calculation
1 # i nc l ude < s t d i o . h>
2 # in c lu d e <math . h>
3 # in c lu d e <omp . h>
4 # de f ine NUM_THREADS 4
5
6 i n t main ( i n t argc , char
*
argv [ ] )
7 {
8 i n t done = 0 , n , i ;
9 double PI25DT = 3.141592653589793238462643;
10 double my pi , h , sum [NUM_THREADS] , x ;
11
12 w h il e ( ! done )
13 {
14 p r i n t f ( " Enter t he number of i n t e r v a l s : (0 qu i t s ) " ) ;
15 scanf ( "%d " ,&n ) ;
16
17 i f ( n == 0) break ; /
*
Qu it when " 0 " entered
*
/
18 /
*
I n t e g r a l l i m i t s are from 0 to 1
*
/
19 h = (1.0 0.0) / ( d ouble ) n ; /
*
Step l e ng t h
*
/
20 #pragma omp p a r a l l e l p r i v a t e ( i , x )
21 {
22 i n t id = omp_get_thread_num ( ) ;
23 sum[ id ] = 0 . 0 ; /
*
I n i t i a l i z e sum var i a b l e
*
/
24 f o r ( i = i d +1; i <= n ; i += NUM_THREADS) /
*
loop o ver i n t e r v a l f o r i n t e g r a t i o n
*
/
25 {
26 x = h
*
( ( dou ble ) i 0 . 5 ) ; /
*
Middle p o i n t at s tep
*
/
27 sum[ id ] += 4.0 / ( 1 . 0 + x
*
x ) ; /
*
Sum up at each s tep
*
/
28 }
29 }
30 f o r ( i =1; i <NUM_THREADS; i ++)
31 sum[ 0 ] += sum[ i ] ;
32 mypi = h
*
sum[ 0 ] ; /
*
Obtain r e s u l t i n g pi number
*
/
33 p r i n t f ( " p i i s app r ox i m at e ly %.16f , E rro r i s %.16 f \ n " , mypi , fabs ( mypi PI25DT ) ) ;
34 }
35 }
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.8
Computing π V - OpenMP Parallel Calculation
Access to Sequential Code & OpenMP Parallel Code
Table: Sequential and Ope nMP outpu ts for computing π number.
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.9
Computing π VI - OpenMP Parallel Calculation: Reduct ion
An OpenMP version of a threaded program to compute PI
number by numerical integration with reduction clause.
Computing PI using OpenMP directives - Reduction. Follow
the steps below for executing OpenMP code;
export OMP_NUM_THREADS=8
gcc -o code23 code23.c -fopenmp
./code23
Programming Shared
Memory III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application
Example-OpenMP
Computing π
11.10
Computing π VII - OpenMP Parallel Calculation: Reduction
1 # i nc lude <s t d i o . h>
2 # include <math . h>
3 # include <omp. h>
4 i n t main ( i n t argc , char
*
argv [ ] )
5 {
6 i n t done = 0 , n , i ;
7 double PI25DT = 3.141592653589793238462643;
8 double mypi , h , sum, x ;
9 whil e ( ! done )
10 {
11 p r i n t f ( " Enter the number o f i n t e r v a l s : (0 qu i t s ) " ) ;
12 scanf ( "%d " ,&n ) ;
13 i f ( n == 0) break ; /
*
Quit when " 0 " entered
*
/
14 /
*
I n t e g r a l l i m i t s are from 0 to 1
*
/
15 h = (1.0 0.0) / ( double )n ; /
*
Step leng th
*
/
16 sum = 0 . 0 ; /
*
I n i t i a l i z e sum vari a b l e
*
/
17 #pragma omp p a r a l l e l f o r p r i v a t e ( x ) reduc ti on ( + :sum )
18 f o r ( i = 1; i <= n ; i += 1) /
*
loop over i n t e r v a l f o r
i n t e g r a t i o n
*
/
19 {
20 x = h
*
( ( double ) i 0 . 5 ) ; /
*
Middle poi nt a t step
*
/
21 sum += 4.0 / (1. 0 + x
*
x ) ; /
*
Sum up at each step
*
/
22 / / p r i n t f ( " i=%d x=%f sum=%f \ n " , i , x , sum) ; /
*
inte r m e d i a t e steps
*
/
23 }
24 my pi = h
*
sum ; /
*
Obtain r e s u l t i n g pi number
*
/
25 p r i n t f ( " p i i s approxi mately % .16 f , E r r o r is %.16 f \ n" , mypi ,
fabs ( mypi PI 25 DT ) ) ;
26 }
27 }