Professional Documents
Culture Documents
Programming
and MPI
A course for IIT-M. September 2008
R Badrinath, STSD Bangalore
(ramamurthy.badrinath@hp.com)
Why
Further R&D in sciences, engineering
Provide computing services to the region
Create new opportunities in education and skills
Audience Check
Contents
1.
2.
3.
4.
MPI_InitInstead we
Understand Issues
MPI_Comm_rank
MPI_Comm_size
Understand Concepts
MPI_Send
5.
MPI_Recv Learn
6.
MPI_Bcast
7.
MPI_Create_comm
8.
MPI_Sendrecv
9.
MPI_Scatter
10.
MPI_Gather
Go by motivating examples
Try out some of the examples
IIT-Madras
Outline
Sequential
vs Parallel programming
Shared
vs Distributed Memory
Parallel
Communication
MPI
Examples
MPI
Concepts
The
role of IO
IIT-Madras
vs Computation
Sequential vs Parallel
We
Main
Reality
Methodology
IIT-Madras
Program
Memory
Communications channel
SMPs vs Clusters
IIT-Madras
Each
The
IIT-Madras
What
Who
Access
Whether
When
What
1
Work Break-down
Parallel
Prefer
algorithm
Usually
Breaking
Fine
IIT-Madras
Digression
work
#include <mpi.h>
#include <stdio.h>
int main()
{
int total_size, my_rank;
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &total_size);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
printf("\n Total number of programs = %d, out of
which rank of this process is %d\n", total_size,
my_rank);
MPI_Finalize();
return 0;
}
1
IIT-Madras
Getting it to work
Compile it:
mpicc o simple simple.c
Run it
This depends a bit on the system
mpirun -np2 simple
qsub l ncpus=2 o simple.out /opt/hpmpi/bin/mpirun <your
program location>/simple
[Fun: qsub l ncpus=2 I hostname ]
IIT-Madras
What goes on
Same
Each
This
We
Typically
1
IIT-Madras
2
5
1
2
7
IIT-Madras
0
7
2
0
1
..
..
..
6
..
1
..
..
5
..
..
0
2
..
2
0
..
3
2
0
IIT-Madras
Parallelizing Floyd
Actually
After
Ideal
What
Need
p<n.
We
1
IIT-Madras
Remember
the
observation
for (k=0;k<n;k++) {
The MPI
Model
-All nodes run
the same
code!! P
replica tasks!!
k_here = k - LOW_END_OF_MY_BLOCK(id);
for(j=0;j<n;j++)
Distributed Memory
rowk[j]=a[k_here][j];
Model
-Some
times
/* rowk
is broadcast by the owner and received by
they
need to
others..
do different
The MPI code will come here later */
things
for(i=0;i<GET_MY_BLOCK_SIZE(id);i++)
for(j=0;j<n;j++)
a[i,j]=min(a[i][j],
a[i][k]+rowk[j]);
1
IIT-Madras
Functions
IIT-Madras
Notion of send/receive/broadcast
MPI_Bcast() /* use to broadcast rowk[] */
IIT-Madras
a[MAX][MAX];
n=20; /* real size of the matrix,
be read in */
id,p;
MPI_Init(argc,argv);
MPI_Comm_rank(MPI_COMM_WORLD,&id);
MPI_Comm_size(MPI_COMM_WORLD,&p);
.
./* This is where all the real work happens */
.
MPI_Finalize();
/* Epilogue */
2
IIT-Madras
23
Tasks/CPUs
Visualizing the executionMultiple
maybe on the same
Job is Launched
Tasks On
CPUs
node
Scheduler ensures 1
task per cpu
IIT-Madras
Communication vs Computation
Often communication is needed between iterations
to complete the work.
Often the more the tasks the more the
communication can become.
IIT-Madras
Communication primitives
MPI_Send(sendbuffer,
senddatalength,
datatype, destination, tag,
communicator);
MPI_Send(Hello, strlen(Hello),
MPI_CHAR, 2 ,
100,
MPI_COMM_WORLD);
MPI_Recv(recvbuffer, revcdatalength,
MPI_CHAR, source, tag,
MPI_COMM_WORLD,
&status);
Send-Recv happen in pairs.
2
IIT-Madras
Collectives
Broadcast
is one-to-all communication
Both receivers and sender call the same
function
All MUST call it. All end up with SAME result.
MPI_Bcast (buffer, count, type, root, comm);
Examples
MPI_Bcast(&k, 1, MPI_INT, 0,
MPI_Comm_World);
Task 0 sends its integer k and all others receive it.
MPI_Bcast(rowk,n,MPI_INT,current_owner_task
,MPI_COMM_WORLD);
Current_owner_task sends rowk to all
others.
2
IIT-Madras
1
1
MPI_Bcast(&x,1,..,0,..);
x : 0
MPI_Bcast(&x,1,..,0,..);
2
2
MPI_Bcast(&x,1,..,0,..);
IIT-Madras
erator,
root,comm);
Task
Must
IIT-Madras
Scattering as opposed to
broadcasting
All
Rank0
Rank1
Rank0
3
IIT-Madras
Rank2
Rank3
Make
Make
If
IIT-Madras
Sometime
Master
E.g.,
E.g.,
IIT-Madras
IIT-Madras
Slaves
IIT-Madras
}
3
IIT-Madras
IIT-Madras
38
Mutliply:
Cij = (Aik * Bkj)
BMR Algorithm:
IIT-Madras
Mutliply:
Cij = (Aik * Bkj)
BMR Algorithm:
IIT-Madras
41
Communicators
can be created on
subgroups of processes.
Communicators
topology
IIT-Madras
IIT-Madras
a grid
MPI_Cart_create(MPI_COMM_WORLD,
2,
dim_sizes, istorus, canreorder, &grid_comm);
int dim_sizes[2], int istorus[2], int canreorder,
MPI_Comm grid_comm
Divide
MPI_Cart_sub(grid_comm,free,&rowcom)
IIT-Madras
45
/ OpenMP
sided Communication
and IO
IIT-Madras
Where does
the interesting
pragma omp for
fit in our MPI
Floyd?
How do I
assign exactly
one MPI task
per CPU?
IIT-Madras
One-Sided Communication
Have
RDMA
Get
Put
IIT-Madras
IO in Parallel Programs
Typically
Simpler to program
Natural because of some post processing
occasionally needed (sorting)
All nodes generating IO requests might overwhelm
fileserver, essentially sequentializing it.
Performance
Parallel
IIT-Madras
IIT-Madras
IIT-Madras
Algorithm Basics
Breakdown
Topologies
in Communication
Communication
Impact
Overhead vs Computation
of Parallel IO
IIT-Madras
MPI_Init
MPI_Finalize
MPI_Comm_size
MPI_Comm_Rank
MPI_Send
MPI_Recv
MPI_Sendrecv_replace
MPI_Bcast
MPI_Reduce
MPI_Cart_create
MPI_Cart_sub
MPI_Scatter
IIT-Madras
References
1.
2.
3.
4.
IIT-Madras