Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.1
Lecture 7
Programming Using the
Message- Passing Paradigm III
MPI: the Message Passing Interfac e; Parallelization Application
Example - Pi Computation
IKC-MH.57 Introduction to High Performance and Parallel
Computing at November 24, 2023
Dr. Cem Özdo
˘
gan
Engineering Sciences Department
˙
Izmir Kâtip Çelebi Univer sity
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.2
Contents
1 Parallelization Application Example
Pi Computation
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.3
Pi Computation I
π by numerically evaluating the integral
Z
1
0
1
1 + x
2
dx =
π
4
Midpoint Rule for
R
b
a
f (x)dx (b a)f(x
m
)
Figure: Mi dpoint Rul e.
Midpoint Rule becomes
Z
1
0
1
1 + x
2
dx
n
X
i=1
1
1 +
i0.5
n
2
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.4
Pi Computation II
Sequential Code:
1 # inc lude < s t d i o . h>
2 # i nc lud e <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 w hi le ( ! done )
9 {
10 p r i n t f ( " Enter the number of i n t e r v a l s : (0 qu i t s ) " ) ;
11 scanf ( "%d " ,&n ) ;
12 i f ( n == 0) break ; /
*
Qui t when " 0" entered
*
/
13 /
*
I n t e g r a l l i m i t s are from 0 to 1
*
/
14 h = (1.0 0.0) / ( double ) n ; /
*
Step le ngt h
*
/
15 sum = 0 . 0 ; /
*
I n i t i a l i z e sum v a ria b l e
*
/
16 /
*
loop over i n t e r v a l f o r i n t e g r a t i o n
*
/
17 fo r ( i = 1; i <= n ; i += 1)
18 {
19 x = h
*
( ( double ) i 0.5) ; /
*
Middle po i nt at step
*
/
20 sum += 4.0 / (1 . 0 + x
*
x ) ; /
*
Sum up at each step
*
/
21 / / ( " i=%d x=%f sum=%f \ n " , i , x , sum) ; /
*
p r i n t int e rm e di a t e steps
*
/
22 }
23 mypi = h
*
sum ; /
*
Obtain r e s u l t i n g p i number
*
/
24 p r i n t f ( " p i i s ap proximately %.16 f , Err o r i s %.16 f \ n" , mypi ,
\ \
25 fabs ( mypi PI25DT ) ) ;
26 }
27 }
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.5
Pi Computation III
Figure: S equential Code Output.
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.6
Pi Computation IV
Parallel Code:
The master process reads number of intervals from
standard input, this number is then sent t o the processes.
Having received the number of int ervals, e ach process
evalua tes the total area of n/size rectangles under the
curve.
The contr ibutions to the total area under the curve are
collected from participating processes by the master
process, which then adds them up, and prints the result on
standard output.
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.7
Pi Computation V
1 # include < s t d i o . h>
2 # i nc lud e <math . h>
3 # i nc lud e " mpi . h "
4
5 i n t main ( i n t argc , char
*
argv [ ] )
6 {
7 i n t done = 0 , n , i ;
8 double PI 25DT = 3.141592653589793238462643;
9 double mypi , h , sum, x ;
10 i n t size , rank , me;
11 i n t tag =11;
12 MPI_Status st at u s ;
13 double mysum ;
14 double p i ;
15
16 MPI_I nit (& argc , &argv ) ; /
*
I n i t i a l i z e MPI
*
/
17 MPI_Comm_size(MPI_COMM_WORLD, &si ze ) ; /
*
Get number of processes
*
/
18 MPI_Comm_rank (MPI_COMM_WORLD, &rank ) ; /
*
Get own i d e n t i f i e r
*
/
19
20 while ( ! done )
21 {
22 i f ( rank == 0) { /
*
Process 0 does t h i s
*
/
23 p r i n t f ( " Enter the number of i n t e r v a l s : (0 qu i t s ) " ) ;
24 scanf ( "%d" ,&n ) ;
25 /
*
Send a message c on t ain i n g number of i n t e r v a l s to a l l ot her
processes
*
/
26 f o r ( i =1; i < size ; i ++) {
27 MPI_Send(&n , 1 , MPI _INT , i , tag , MPI_COMM_WORLD) ; /
*
Bl ockin g
se nd
*
/
28 }
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.8
Pi Computation VI
1 i f ( n == 0) break ; /
*
Qui t when " 0 " entered
*
/
2 /
*
Computing l o c a l p i number f o r rank 0 process
*
/
3 /
*
I n t e g r a l l i m i t s are from 0 t o 1
*
/
4 h = (1.0 0.0) / ( double ) n ; /
*
Step l eng th
*
/
5 mysum = 0 . 0; /
*
I n i t i a l i z e sum va r iab l e
*
/
6 f o r ( i = rank +1; i <= n ; i += si ze ) /
*
Loop over i n t e r v a l f o r
i n t e g r a t i o n
*
/
7 {
8 x = h
*
( ( double ) i 0 .5 ) ; /
*
Middle po i nt at step
*
/
9 mysum += 4.0 / ( 1. 0 + x
*
x ) ; /
*
Sum up a t each step
*
/
10 / / p r i n t f ( " i=%d x=%f sum=%f \ n " , i , x , sum) ; /
*
Int erm edi at e
steps
*
/
11 }
12 mypi = h
*
mysum; /
*
Obtain l o c a l r e s u l t i n g p i number
*
/
13 /
*
Receive a message c o nta i nin g lo c a l r e s u l t i n g pi number from
a l l oth er processes
*
/
14 f o r ( i =1; i < size ; i ++) {
15 MPI_Recv (& pi , 1 , MPI_DOUBLE, i , tag , MPI_COMM_WORLD, &s tat u s )
; /
*
Bl ockin g r eci ev e
*
/
16 p r i n t f ( " Process 0 : Received l o c a l r e s u l t i n g p i number : %.16 f
from process %d \ n " , pi , i ) ;
17 mypi=mypi+ p i ; /
*
Reduce a l l l o c a l values t o mypi v a ria b l e
*
/
18 }
19 p r i n t f ( " p i i s a ppr oxi matel y %.16 f , E rro r i s %.16 f \ n " , mypi , fabs (
mypi PI25DT) ) ;
20 }
21 else /
*
Other processes do t h i s
*
/
22 {
23 MPI_Recv (&n , 1 , MPI_I NT , 0 , tag , MPI_COMM_WORLD, &s t at u s ) ; /
*
Bl ockin g rec ie ve
*
/
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.9
Pi Computation VII
1 p r i n t f ( " Process %d : Received number of i n t e r v a l s as %d from
process 0 \ n " , rank , n ) ;
2 i f ( n == 0) break ; /
*
Qui t when " 0" entered
*
/
3 /
*
Computing l o c a l p i number f o r othe r processes
*
/
4 /
*
I n t e g r a l l i m i t s are from 0 t o 1
*
/
5 h = (1.0 0.0) / ( double ) n ; /
*
Step l eng th
*
/
6 mysum = 0 . 0 ; /
*
I n i t i a l i z e sum va r iab l e
*
/
7 fo r ( i = rank +1; i <= n ; i += size ) /
*
Loop over i n t e r v a l f o r
i n t e g r a t i o n
*
/
8 {
9 x = h
*
( ( double ) i 0 . 5) ; /
*
Middle po i nt at step
*
/
10 mysum += 4.0 / ( 1. 0 + x
*
x ) ; /
*
Sum up at each step
*
/
11 / / p r i n t f ( " i=%d x=%f sum=%f \ n " , i , x , sum) ; /
*
Int erm edi a te
steps
*
/
12 }
13 mypi = h
*
mysum; /
*
Obtain l o c a l r e s u l t i n g p i number
*
/
14 /
*
Send a message c o nta i n in g lo c a l r e s u l t i n g pi number to
master processes
*
/
15 MPI_Send(&mypi , 1 , MPI_DOUBLE, 0 , tag , MPI_COMM_WORLD) ; /
*
Bl ockin g send
*
/
16 }
17 }
18 MPI_Finalize ( ) ;
19 }
Programming Using the
Message-Passing
Paradigm III
Dr. Cem Özdo
˘
gan
LOGIK
Parallelization
Application Example
Pi Computation
7.10
Pi Computation VIII
Figure: Parallel Code Output.