Lecture15 PDF
Lecture15 PDF
Autumn, 2016
Lecture 15
Distributed memory
• Each (4-core) chip has its own memory
Imperial College
London
Parallel computing paradigms
Related approaches:
• Hybrid programming: mix of shared-memory (OpenMP) and
distributed-memory (MPI) programming
Imperial College
London
MPI intro
• MPI: Message Passing Interface
Imperial College
London
OpenMP schematic
Program starts with
single master thread
Imperial College
London
MPI schematic
Program starts with
all processes
running
Start program
MPI controls
communication
between processes
Parallel region (4 processes)
Imperial College
London
MPI intro
• Basic idea: calls to MPI subroutines control data exchange
between processors
• Example:
This will send the integer n which has size 1 from processor 0 to all
of the other processors.
Imperial College
London
MPI broadcast
P0 data P0 data
P1 P1 data
P2 P2 data
P3 P3 data
Imperial College
London
MPI intro
• Basic idea: calls to MPI subroutines control data exchange
between processors
• Example:
This will send the integer n which has size 1 from processor 0 to all
of the other processors.
Imperial College
London
Fortran code structure
! Basic Fortran 90 code structure!
!
!1. Header!
program template!
!
!2. Variable declarations (e.g. integers, real numbers,...)!
!
!3. basic code: input, loops, if-statements, subroutine calls!
print *, 'template code'!
!
!
!4. End program!
end program template!
!
! To compile this code:!
! $ gfortran -o f90template.exe f90template.f90!
! To run the resulting executable: $ ./f90template.exe
Imperial College
London
MPI intro
! Basic MPI + Fortran 90 code structure! See mpif90template.f90
!
!1. Header!
program template!
use mpi!
!
!2a. Variable declarations (e.g. integers, real numbers,...)!
integer :: myid, numprocs, ierr!
!
!2b. Initialize MPI!
call MPI_INIT(ierr)!
call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)!
call MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr)!
!
!3. basic code: input, loops, if-statements, subroutine calls!
print *, 'this is proc # ',myid, 'of ', numprocs!
!
!
!4. End program!
call MPI_FINALIZE(ierr)!
end program template!
!
! To compile this code:!
! $ mpif90 -o mpitemplate.exe mpif90template.f90!
! To run the resulting executable with 4 processes:$ mpiexec -n 4 mpitemplate.exe
Imperial College
London
MPI intro
! Basic MPI + Fortran 90 code structure! See mpif90template.f90
!
!1. Header!
program template!
use mpi!
!
!2a. Variable declarations (e.g. integers, real numbers,...)!
integer :: myid, numprocs, ierr!
!
!2b. Initialize MPI!
call MPI_INIT(ierr)!
call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)!
call MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr)!
!
!3. basic code: input, loops, if-statements, subroutine calls!
print *, 'this is proc # ',myid, 'of ', numprocs!
!
!
!4. End program!
call MPI_FINALIZE(ierr)!
end program template!
!
! To compile this code:!
! $ mpif90 -o mpitemplate.exe mpif90template.f90!
! To run the resulting executable with 4 processes:$ mpiexec -n 4 mpitemplate.exe
Imperial College
London
MPI intro
! Basic MPI + Fortran 90 code structure! See mpif90template.f90
!
!1. Header!
program template!
use mpi!
!
!2a. Variable declarations (e.g. integers, real numbers,...)!
integer :: myid, numprocs, ierr!
!
!2b. Initialize MPI!
call MPI_INIT(ierr)!
call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)!
call MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr)!
!
!3. basic code: input, loops, if-statements, subroutine calls!
print *, 'this is proc # ',myid, 'of ', numprocs!
!
!
!4. End program!
call MPI_FINALIZE(ierr)!
end program template!
!
! To compile this code:!
! $ mpif90 -o mpitemplate.exe mpif90template.f90!
! To run the resulting executable with 4 processes:$ mpiexec -n 4 mpitemplate.exe
Imperial College
London
MPI intro
• Compile + run:
Imperial College
London
MPI+Fortran example: computing an integral
• Estimate integral
with midpoint rule,
Imperial College
London
MPI+Fortran quadrature
Two most important tasks:
2. Each processor will compute its own partial sum, sum_proc,
how do we compute sum(sum_proc)?
Imperial College
London
MPI+Fortran quadrature
Two most important tasks:
2. Each processor will compute its own partial sum, sum_proc,
how do we compute sum(sum_proc)?
• N = number of intervals
Imperial College
London
MPI+Fortran quadrature
• N = number of intervals
Imperial College
London
MPI+Fortran quadrature
Two most important tasks:
2. Each processor will compute its own partial sum, sum_proc,
how do we compute sum(sum_proc)?
Use MPI_REDUCE
Imperial College
London
MPI reduce
P1 data2 P1
P2 data3 P2
P3 data4 P3
Imperial College
London
MPI+Fortran quadrature
Two most important tasks:
2. Each processor will compute its own partial sum, sum_proc,
how do we compute sum(sum_proc)?
• Use MPI_REDUCE
Imperial College
London
MPI+Fortran quadrature
Two most important tasks:
2. Each processor will compute its own partial sum, sum_proc,
how do we compute sum(sum_proc)?
• Use MPI_REDUCE
Imperial College
London
MPI+Fortran quadrature
This will:
1. Collect the double precision variable data which has size 1 from
each processor.
Imperial College
London
MPI+Fortran quadrature
midpoint_p.f90: distribute data
Imperial College
London
MPI+Fortran quadrature
midpoint_p.f90: 1. distribute data, 2. compute sum_proc
Imperial College
London
MPI+Fortran quadrature
midpoint_p.f90: 1. distribute data, 2. compute sum_proc, 3. reduction
Imperial College
London
Other collective operations
• Scatter and gather
Imperial College
London
MPI scatter
P0 [f1,f2,f3,f4] P0 f1
P1 P1 f2
P2 P2 f3
P3 P3 f4
Imperial College
London
MPI gather
P0 [f1,f2,f3,f4] P0 f1
P1 P1 f2
P2 P2 f3
P3 P3 f4
Imperial College
London
k 1
Ti+1 2Tik + Tik 11
Other Other collective
collective
2
= operations
Si
operations
x
2
x 1 k 1
• • Scatter
Scatter and k and gather
Ti gather
= Si + Ti+1 + Tik 11
2 2
dT • Gather
• iGather allTparticles
all particles 2Ton
i+ Ti 1
processor
i+1 on processor
=• S
• Compute i (t)interaction
Compute + interaction
forcesforces
2 for,particles
i =on1,that
for particles 2,
on..., Nprocessor
that
processor
dt x
2 XN
d xi
2
= f (|xi xj |), i = 1, 2, ..., N
dt j=1
• Avoid
• Avoid forproblems
for big big problems (why?)
(why?)
Imperial College
Imperial College
London London
MPI collective data movement
Imperial College
London
From Using MPI