Message Passing Interface (MPI)
MPI is the dominant way of communicating by message passing. Messages are sent at the process level. The MPI model enables parallel execution with several processes cooperating on the same task. Each process works with its own data. Processes communicate by sending and receiving messages to and from each other. Most MPI programs are based on the SPMD (Single-Program-Multiple-Data) model, which means that all processes run the same program with their own data. To enable cooperation, each process has its own ID. Usually one process runs per processor core.
Messages
A message transfers a number of data items of a certain type from memory to the memory of another process. A message typically contains
sender and receiver ID
data type
amount of data
data
message type
Messages can be sent synchronously or asynchronously. Receives are usually synchronous, which means that the receiving process must wait until the whole message has been received. There are also different communication models
Point-to-Point - between two processes
Collective - group communication between several processes:
Broadcast - sending from one process to all others
Scatter - sending information to several processes
Gather - receiving information from several processes
Reduction - global sum, product, min, max, …
Problems when writing MPI can be deadlocks, i.e. waiting for messages that never arrive. Most scientific programs have a simple structure, which results in a simple communication pattern. The main purpose of MPI programs is code portability and efficient implementation of a parallel program.
Programming
To use the MPI library in a program, the appropriate headers for type checking must first be included in the source file:
C and C++: #include
Fortran 77 (no longer recommended): include ‘mpif.h’
Fortran 90: use mpi
Fortran 95 and MPI 3.0: use MPI_f08
Library calls have the following form:
C:
error = MPI_Xxxxx(parameter, ...);
MPI_Xxxxx(parameter, ...);
Fortran:
CALL MPI_XXXXX(parameter, ..., IERROR)
Handles
Handles are used to address internal MPI structures. In C, handles are types defined with typedef. In Fortran they are INTEGERs
Initialisation
Before starting work, the MPI library must be initialised with the command
C:
int MPI_Init(int *argc, char ***argv)
Fortran:
MPI_INIT(IERROR)
INTEGER IERROR
To get the ID or rank, i.e. the process number, we call
MPI_Comm_rank(MPI_Comm comm, int *rank)
MPI_COMM_RANK(COMM, RANK, IERROR)
INTEGER COMM, RANK, IERROR
The total number of processes in the initialisation is obtained with
MPI_Comm_size(MPI_Comm comm, int *size)
MPI_COMM_SIZE(COMM, SIZE, IERROR)
INTEGER COMM, SIZE, IERROR
At the end, the program finishes with
int MPI_Finalize()
MPI_FINALIZE(IERROR)
INTEGER IERROR
Messages
A message contains a set of elements of a certain type
MPI types
Basic types
Derived types
Derived types can be composed of basic types
Types in C differ from types in Fortran
MPI data type
C type
Basic MPI data types
MPI_CHAR
signed char
MPI_SHORT
signed short int
MPI_INT
signed int
MPI_LONG
signed long int
MPI_UNSIGNED_CHAR
unsigned char
MPI_UNSIGNED_SHORT
unsigned short int
MPI_UNSIGNED
unsigned int
MPI_UNSIGNED_LONG
unsigned long int
MPI_FLOAT
float
MPI_DOUBLE
double
MPI_LONG_DOUBLE
long double
MPI_BYTE
MPI_PACKED
MPI data type
Fortran type
Basic data types in Fortran
MPI_INTEGER
INTEGER
MPI_REAL
REAL
MPI_DOUBLE_PRECISION
DOUBLE PRECISION
MPI_COMPLEX
COMPLEX
MPI_LOGICAL
LOGICAL
MPI_CHARACTER
CHARACTER(1)
MPI_BYTE
MPI_PACKED
Messages can be sent in different ways
standard
synchronous
buffered
ready
receive
An example of sending a synchronous message is
int MPI_Ssend(void *buf, int count,
MPI_Datatype datatype,
int dest, int tag,
MPI_Comm comm)
or in Fortran:
MPI_SSEND(BUF, COUNT, DATATYPE, DEST,TAG, COMM, IERROR)
BUF(*)
INTEGER COUNT, DATATYPE, DEST, TAG
INTEGER COMM, IERROR
It is received with:
int MPI_Recv(void *buf, int count,
MPI_Datatype datatype,
int source, int tag,
MPI_Comm comm, MPI_Status *status)
or in Fortran
MPI_RECV(BUF, COUNT, DATATYPE, SOURCE, TAG, COMM, STATUS, IERROR)
BUF(*)
INTEGER COUNT, DATATYPE, SOURCE, TAG, COMM,
STATUS(MPI_STATUS_SIZE), IERROR
For successful communication, the correct address of the final receiver (rank) must always be given. The receiver must also specify the source it receives from. The TAGs and message type must also match, and of course the memory reserved for receiving must be large enough.
Input/Output
A commonly forgotten limitation of HPC is program input and output; even the standard HP Linpack test does not take I/O into account. However, data must ultimately end up on external storage. The problem of HPC systems is simultaneous access to the disk subsystem, which leads to a complex implementation with several distributed RAID groups, metadata servers (MDS), buffers, … Several processes cannot write to the same file at the same time.
POSIX I/O is the standard way of accessing files on conventional computers, but it does not support the parallel access that HPC needs. The POSIX approach can be used in the main MPI program, which handles all file operations. All this can increase communication.
MPI I/O - For more advanced work, the MPI I/O libraries are used, which are part of the MPI-2 standard and are based on arrays managed by the library.
Working with files in MPI IO starts with opening:
MPI_File_open(MPI_Comm comm, char *filename, int amode, MPI_Info info, MPI_File *fh)
MPI_FILE_OPEN(COMM, FILENAME, AMODE, INFO, FH, IERR)
CHARACTER*(*) FILENAME
INTEGER COMM, AMODE, INFO, FH, IERR
Example:
MPI_File fh;
int amode = MPI_MODE_RDONLY;
MPI_File_open(MPI_COMM_WORLD, “data.in”, amode, MPI_INFO_NULL, &fh);
integer fh
integer amode = MPI_MODE_RDONLY
call MPI_FILE_OPEN(MPI_COMM_WORLD, ‘data.in’, amode, MPI_INFO_NULL, fh, ierr)
It is closed with
MPI_File_close(MPI_File *fh)
MPI_FILE_CLOSE(FH, IERR)
INTEGER FH, IERR
Reading data of a chosen type on each process is possible with
MPI_File_read_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status)
MPI_FILE_READ_ALL(FH, BUF, COUNT, DATATYPE, STATUS, IERR)
INTEGER FH, COUNT, DATATYPE, STATUS(MPI_STATUS_SIZE), IERR
We can also set views on the file and thereby define different starting offsets for data in the file. This allows us to assemble the data of all processes.
Derived data types
MPI has a set of basic types. Parts of arrays can also be sent. For example, if we want to send only part of an int[10] array, we use the command
MPI_Send(x, 4, MPI_INT, ...);
We can also choose the start by passing a pointer
MPI_Send(&x[2], 4, MPI_INT, ...);
MPI_SEND(x(3), 4, MPI_INTEGER, ...)
If we want to send only parts of matrices or vectors, we can do this with a derived type.
MPI_Type_vector(int count, int blocklength, int stride, MPI_Datatype oldtype, MPI_Datatype *newtype);
MPI_TYPE_VECTOR(COUNT, BLOCKLENGTH, STRIDE,OLDTYPE, NEWTYPE, IERR)
INTEGER COUNT, BLOCKLENGTH, STRIDE, OLDTYPE
INTEGER NEWTYPE, IERR
MPI_Datatype vector3x2;
MPI_Type_vector(3, 2, 4, MPI_FLOAT, &vector3x2)
MPI_Type_commit(&vector3x2)
integer vector3x2
call MPI_TYPE_VECTOR(2, 3, 5, MPI_REAL, vector3x2, ierr)
call MPI_TYPE_COMMIT(vector3x2, ierr)
which can be used for sending
MPI_Send(&x[0][0], 1, subarray3x2, ...);
MPI_SEND(x , 1, subarray3x2, ...)
MPI_SEND(x(1,1) , 1, subarray3x2, ...)
One-sided communication
In the one-sided communication model, the receiver does not participate directly. Remote memory can also be addressed directly – Remote Memory Access (RMA). With RMA the entire memory can be accessed, even in distributed architectures. In the MPI-2 standard we have
origin - the process that makes the call
target - the process whose memory is accessed
source - the array from which data is copied
destination - the array into which data is copied
One-sided communication is a major part of the MPI-2 standard. Access to RMA memory must be synchronised to prevent simultaneous writing and reading at the same locations. This can be achieved with
collective synchronisation across all processors (with barriers - MPI_Barrier),
pairwise synchronisation
locks
The most basic access to remote memory is with
MPI_PUT(origin_addr, origin_count, origin_datatype, target_rank, target_disp,
target_count, target_datatype, window)
In RMA calls, windows replace communicators. There is no choice of transfer mode, and all operations are non-blocking. It can therefore happen that the program continues working before the data has been transferred. This also requires that the sender’s data must not be modified in the meantime, until synchronisation with e.g. MPI_WIN_FENCE, locks or MPI_WIN_START/MPI_WIN_POST.
Optimisation and split communicators in MPI
Program execution can be slowed down by the following categories:
lack of parallelism - Amdahl’s law Tp = Ts( (1-a)/p + a)
poor load balancing
synchronisation
communication
Instead of the MPI_COMM_WORLD communicator, smaller communicators can also be used to group processes according to the architectural structure of communications. When MPI_COMM_WORLD is split, processes then have ranks numbered sequentially within each communicator separately.
MPI program examples
Hello, world
Let us create an MPI program that prints the standard greeting on the cluster using the SLURM system. The program will only print a greeting from each process. To determine the total number of processes we use the MPI_COMM_WORLD variable.
For compiling, we must first select the environment, which is done with the command
module load intel openmpi
The program is compiled with:
mpicc -g -o hello hello.c -limf -lm
#include
int main(int argc, char *argv[])
{
int rank, velikost;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &velikost);
printf("Pozdrav iz procesa št. %d\n", rank);
if (rank == 0)
printf("Skupno število procesov je %d\n", velikost);
MPI_Finalize();
}
Create a “batch” file mpibatch.sh with the following content:
#!/bin/bash
#SBATCH --partition=rome
#SBATCH --ntasks=8
#SBATCH --ntasks-per-core=1
#SBATCH --output=hello-%j.out
#SBATCH --error=hello-%j.err
mpirun hello
and run it with the command sbatch mpibatch.sh, which writes the result and statistics to the output file. The program output is as follows:
Pozdrav iz procesa št. 0
Skupno število procesov je 8
Pozdrav iz procesa št. 1
Pozdrav iz procesa št. 3
Pozdrav iz procesa št. 5
Pozdrav iz procesa št. 2
Pozdrav iz procesa št. 4
Pozdrav iz procesa št. 6
Pozdrav iz procesa št. 7
Computing Pi
We want to compute an approximation of pi with the equation

As a basis, use the hello.c program so that each process independently computes the value of pi. Check whether all processes print the same number.
#include
#define N 840
#define SQR(x) (x)*(x)
int main(int argc, char *argv[])
{
int rank, velikost;
int i;
double pi = 0.0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &velikost);
for (i=1; i < N; ++i)
pi += 1.0/(1.0+SQR((i-0.5)/N));
pi *= 4.0/N;
printf("Rezultat #%d: Pi= %lf\n", rank, pi);
if (rank == 0)
printf("Skupno število procesov je %d\n", velikost);
MPI_Finalize();
}
Now split the processes so that they compute over different ranges. With two processes, rank 0 would compute i=1..N/2 and rank 1 i=N/2..N. Set N to 840, since it is divisible by 2 to 8. Print the partial sums and add them up by hand.
#include
#define N 840
#define SQR(x) (x)*(x)
int main(int argc, char *argv[])
{
int rank, np;
int i, start, end;
float pi = 0.0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &np);
start = N*rank/np+1; end = start + N/np-1;
printf("Obseg #%d : [%d:%d]\n", rank, start, end);
for (i=start; i <= end; ++i)
pi += 1.0/(1.0+SQR((i-0.5)/N));
pi *= 4.0/N;
printf("Delna vsota #%d: Pi= %f\n", rank, pi);
MPI_Finalize();
}
Now combine the partial sums by sending them to the main process (rank 0) for summation
All processes (except the main one) send their partial sums to the main process
the main process receives the values of all other processes and adds them up
For communication we use MPI_Ssend() and MPI_Recv()
Extend the program with
Use the MPI_Wtime function to measure the time needed for the computation
double starttime, endtime;
starttime = MPI_Wtime();
... računamo
endtime = MPI_Wtime();
printf("Pi = %f izračunan v %lf\n", pi, endtime-starttime);
Check and ensure that the program works correctly even if N is not a multiple of the number of processes P
Ping pong
Let us write a program that repeatedly sends messages back and forth between two processes (rank 0 and 1). Use MPI_Send for sending. The program must be written so that it also works correctly with a larger number of processes. To simplify, we will use an array of real numbers without initialising it.
Add time measurement for all communications
See how the time varies with message size
Plot a graph to determine the latency (the time needed for a message of size zero).
program pingpong
use MPI_f08
implicit none
integer i, rank
integer buf_size
parameter (buf_size=8*64)
real buf(buf_size)
type(MPI_Status) :: stat
double precision :: start, finish
call MPI_Init()
call MPI_Comm_Rank(MPI_COMM_WORLD, rank)
do i=1,51
if (rank .eq. 0) then
if (i .eq. 2) start = MPI_Wtime()
call MPI_Send(buf, count=buf_size, datatype=MPI_REAL, dest=1, tag=17, comm=MPI_COMM_WORLD)
call MPI_Recv(buf, count=buf_size, datatype=MPI_REAL, source=1, tag=23, comm=MPI_COMM_WORLD, status=stat)
else if (rank .eq. 1) then
call MPI_Recv(buf, count=buf_size, datatype=MPI_REAL, source=0, tag=17, comm=MPI_COMM_WORLD, status=stat)
call MPI_Send(buf, count=buf_size, datatype=MPI_REAL, dest=0, tag=23, comm=MPI_COMM_WORLD)
end if
end do
finish = MPI_Wtime()
if (rank .eq. 0) then
print *, 'delta_time =', (finish-start)/(2*50)*1e6
end if
call MPI_Barrier(MPI_COMM_WORLD)
call MPI_Finalize()
end program
One-sided communication
We convert the ring sum program to RMA mode
#include <stdio.h>
#include <mpi.h>
#define to_right 201
int main (int argc, char *argv[])
{
int my_rank, size;
int snd_buf, rcv_buf;
int right, left;
int sum, i;
MPI_Win win;
MPI_Status status;
MPI_Request request;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
right = (my_rank+1) % size;
left = (my_rank-1+size) % size;
/* ... this SPMD-style neighbor computation with modulo has the same meaning as: */
/* right = my_rank + 1; */
/* if (right == size) right = 0; */
/* left = my_rank - 1; */
/* if (left == -1) left = size-1;*/
sum = 0;
snd_buf = my_rank;
MPI_Win_create(&rcv_buf, sizeof(int), sizeof(int), MPI_INFO_NULL,
MPI_COMM_WORLD, &win);
for( i = 0; i < size; i++)
{
#if 0
MPI_Issend(&snd_buf, 1, MPI_INT, right, to_right,
MPI_COMM_WORLD, &request);
MPI_Recv(&rcv_buf, 1, MPI_INT, left, to_right,
MPI_COMM_WORLD, &status);
MPI_Wait(&request, &status);
#else
MPI_Win_fence(MPI_MODE_NOSTORE | MPI_MODE_NOPRECEDE, win);
MPI_Put(&snd_buf, 1, MPI_INT, right, (MPI_Aint)0, 1, MPI_INT, win);
MPI_Win_fence(MPI_MODE_NOSTORE | MPI_MODE_NOPUT | MPI_MODE_NOSUCCEED, win);
#endif
snd_buf = rcv_buf;
sum += rcv_buf;
}
printf ("PE%i:\tSum = %i\n", my_rank, sum);
MPI_Finalize();
}
</mpi.h></stdio.h>
Message Passing Interface (MPI)
MPI je dominanten način komuniciranja s pošiljanjem sporočil. Sporočila se pošiljajo na nivoju procesov. Model MPI omogoča vzporedno izvajanje s sodelovanjem večih procesov na isti nalogi. Vsak proces ima pri delu svoje podatke. Procesi komunicirajo tako, da si med sabo pošiljajo in sprejemajo sporočila.Večina MPI programov temelji na modelu SPMD (Single-Program-Multiple-Data), kar pomeni, da vsi procesi izvajajo isti program s svojimi podatki. Da se omogoča medsebojno delo imajo procesi vsak svoj ID. Običajno se poganja en proces na eno procesorsko jedro.
Sporočila
Sporočilo prenaša številne podatke določenega tipa iz spomina v spomin drugega procesa. Sporočilo tipično vsebuje
ID pošiljatelja in prejemnika
tip podatkov
količino podatkov
podatke
tip sporočila
Pošiljanje sporočil je možno sinhrono ali asinhrono. Sprejemi so običajno sinhroni, kar pomeni da mora sprejemni proces počakati, da se celotno sporočilo sprejme. Obstajajo tudi različni modeli komunikacije
Point-to-Point - med dvema procesoma
Collective - skupinska komunikacija med več procesi:
Broadcast - Pošiljanje iz enega procesa na vse ostale
Scatter - pošiljanje informacije več procesov
Gather - prejemanje informacije večih procesov
Reduction - gobalen seštevek, produkt, min, max, …
Težave pri pisanju MPI so lahko zamrznitve (deadlock) oz čakanje na sporočila, ki nikoli ne pridejo. Večina znanstvenih programov ima enostavno zgradbo kar rezultira v enostavnem vzorcu komunikacij. Glavni namen programov MPI je prenosljivost kode in učinkovita implementacija vzporednega programa.
Programiranje
Za uporabo knjižnice MPI v programu je potrebno najprej vključiti v izvorno datoteko ustrezne glave za kontrolo tipov:
C in C++: #include
Fortran 77 (se ne priporoca vec): include ‘mpif.h’
Fortran 90: use mpi
Fortran 95 in MPI 3.0: use MPI_f08
Ukazi za klicanje knjižnice so naslednje oblike:
C:
error = MPI_Xxxxx(parameter, ...);
MPI_Xxxxx(parameter, ...);
Fortran:
CALL MPI_XXXXX(parameter, ..., IERROR)
Označbe (handles)
Z označevalni je mogoče naslavljati interne strukture MP. V C-ju so ozančevalci definirani tipi z typedef. V fortranu do to INTEGER-ji
Inicializacija
Pred začetkom dela, je potrebno inicializirati knjižnico MPI z ukazom
C:
int MPI_Init(int *argc, char ***argv)
Fortran:
MPI_INIT(IERROR)
INTEGER IERROR
Da dobimo ID ali rank oziroma številko procesa kličemo ukaz
MPI_Comm_rank(MPI_Comm comm, int *rank)
MPI_COMM_RANK(COMM, RANK, IERROR)
INTEGER COMM, RANK, IERROR
Celotno število procesov v inicializaciji dobimo z
MPI_Comm_size(MPI_Comm comm, int *size)
MPI_COMM_SIZE(COMM, SIZE, IERROR)
INTEGER COMM, SIZE, IERROR
Na koncu se program konča z
int MPI_Finalize()
MPI_FINALIZE(IERROR)
INTEGER IERROR
Sporočila
Sporočilo vsebuje množico elementov določenega tipa
Tipi MPI
Osnovni tipi
Izpeljani tipi
Izpeljani tipi so lahko sestavljeni iz osnovnih
Tipi v C-ju so različni od tipov v Fotranu
MPI tip podatka
Tip v C-ju
Osnovni tipi podatkov v MPI
MPI_CHAR
signed char
MPI_SHORT
signed short int
MPI_INT
signed int
MPI_LONG
signed long int
MPI_UNSIGNED_CHAR
unsigned char
MPI_UNSIGNED_SHORT
unsigned short int
MPI_UNSIGNED
unsigned int
MPI_UNSIGNED_LONG
unsigned long int
MPI_FLOAT
float
MPI_DOUBLE
double
MPI_LONG_DOUBLE
long double
MPI_BYTE
MPI_PACKED
MPI tip podatka
Tip v Fortranu
Osnovni tipi podatkov v Fotranu
MPI_INTEGER
INTEGER
MPI_REAL
REAL
MPI_DOUBLE_PRECISION
DOUBLE PRECISION
MPI_COMPLEX
COMPLEX
MPI_LOGICAL
LOGICAL
MPI_CHARACTER
CHARACTER(1)
MPI_BYTE
MPI_PACKED
Sporočila lahko pošiljamo na različne načine
standardno
sinhrono
z vmesnim pomnilnikom (buffered)
Sporočilo pripravljenosti
sprejem
Primer pošiljanjja sinhronega sporočila je
int MPI_Ssend(void *buf, int count,
MPI_Datatype datatype,
int dest, int tag,
MPI_Comm comm)
ali v fortranu:
MPI_SSEND(BUF, COUNT, DATATYPE, DEST,TAG, COMM, IERROR)
BUF(*)
INTEGER COUNT, DATATYPE, DEST, TAG
INTEGER COMM, IERROR
Sprejemamo pa ga z:
int MPI_Recv(void *buf, int count,
MPI_Datatype datatype,
int source, int tag,
MPI_Comm comm, MPI_Status *status)
ali v Fortranu
MPI_RECV(BUF, COUNT, DATATYPE, SOURCE, TAG, COMM, STATUS, IERROR)
BUF(*)
INTEGER COUNT, DATATYPE, SOURCE, TAG, COMM,
STATUS(MPI_STATUS_SIZE), IERROR
Za uspešno komuniciranje je vedno potrebno podati pravilen naslov končnega sprejemnika (rank). Tudi prejemnik mora podati vir iz katerega sprejem. Skladni morajo biti tui TAG-i, tip sporočila in seveda zadostna velikost rezerviranega spomina za sprejem.
Input/Output ali Vhod/Izhod
Običajno je pozabljena omejitev HPC prav vhod in izhod iz programov, saj celo standardni HP Linpack test ne upošteva I/O. Vendar pa morajo podatki na koncu obležati na zunanji enoti. Problem HPC sistemov je v simultanem dostopu do diskovnega podsistema kar vodi do zahtevne implementacije z več porazdeljenih RAID skupin, posrednikov do podatkov (MDS), vmesnih pomnilnikov, … Več procesov ne more hkrati pisati v isto datoteko.
POSIX I/O je standarden način dosttopanja do datoetk na klasičnih računalnikih vendar nima podprtega vzporednega dostopa, ki ga HPC potrebuje. Posix način je možno uporabiti v glavnem MPI programu, ki skrbi za vse delo z datotekami. Vse to lahko poveča komunikacijo.
MPI I/O - Za naprednejše delo se uporabi MPI I/O knjižnice, ki so del MPI-2 standarda in temelji na poljih za katere skrbi knjižnica.
Delo y datotekami v MPI IO se začne z odpiranjem:
MPI_File_open(MPI_Comm comm, char *filename, int amode, MPI_Info info, MPI_File *fh)
MPI_FILE_OPEN(COMM, FILENAME, AMODE, INFO, FH, IERR)
CHARACTER*(*) FILENAME
INTEGER COMM, AMODE, INFO, FH, IERR
Primer:
MPI_File fh;
int amode = MPI_MODE_RDONLY;
MPI_File_open(MPI_COMM_WORLD, “data.in”, amode, MPI_INFO_NULL, &fh);
integer fh
integer amode = MPI_MODE_RDONLY
call MPI_FILE_OPEN(MPI_COMM_WORLD, ‘data.in’, amode, MPI_INFO_NULL, fh, ierr)
Zapremo jo z
MPI_File_close(MPI_File *fh)
MPI_FILE_CLOSE(FH, IERR)
INTEGER FH, IERR
Branje podatkov izbranega tipa na vsakem procesu je možno z
MPI_File_read_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status)
MPI_FILE_READ_ALL(FH, BUF, COUNT, DATATYPE, STATUS, IERR)
INTEGER FH, COUNT, DATATYPE, STATUS(MPI_STATUS_SIZE), IERR
Nastavimo lahko tudi poglede na datoteko in s tem določimo različne začetke za podatke v datoteki. S tem lako naredimo sestavljanko podatkov vseh procesov.
Izpeljani podatkovni tipi
MPI ima množico osnovnik tipov. Možno je pošiljati tudi dele polj. če npr. želimo poslati le del polja int[10] uporabimo ukaz
MPI_Send(x, 4, MPI_INT, ...);
Lahko izberemo tudi začetek s podajanjem kazalca
MPI_Send(&x[2], 4, MPI_INT, ...);
MPI_SEND(x(3), 4, MPI_INTEGER, ...)
Če želimo pošiljati le dele matrik oz vektorjev, lahko to naredimo z izpeljanim tipom.
MPI_Type_vector(int count, int blocklength, int stride, MPI_Datatype oldtype, MPI_Datatype *newtype);
MPI_TYPE_VECTOR(COUNT, BLOCKLENGTH, STRIDE,OLDTYPE, NEWTYPE, IERR)
INTEGER COUNT, BLOCKLENGTH, STRIDE, OLDTYPE
INTEGER NEWTYPE, IERR
MPI_Datatype vector3x2;
MPI_Type_vector(3, 2, 4, MPI_FLOAT, &vector3x2)
MPI_Type_commit(&vector3x2)
integer vector3x2
call MPI_TYPE_VECTOR(2, 3, 5, MPI_REAL, vector3x2, ierr)
call MPI_TYPE_COMMIT(vector3x2, ierr)
kar lahko uporabimo za pošiljanje
MPI_Send(&x[0][0], 1, subarray3x2, ...);
MPI_SEND(x , 1, subarray3x2, ...)
MPI_SEND(x(1,1) , 1, subarray3x2, ...)
Enostranska komunikacija
Pri modelu z enostansko komunikacijo sprejemnik ne sodeluje neposredno. Oddaljeni spomin se lahko naslovi tudi neposredno – Remote Memory Access (RMA). Z RMA lahko dosegamo celoten spomin. Celo v ditribuiranih arhitekturah. V standardu MPI-2 imamo
origin - proces ki izvede kloc
target - proces v katerega spomin se posega
source - polje iz katerega so podatki kopirani
destination - polje v katerega so podatki skopirani
Enostransk komunikacija je glavnina pri vzpostavitvi standarda MPI-2. Dostop do RMA spomina je potrebno sinhronizirati, da ne prite do hkratnega pisanja in branja na istih lokacijah. To lahko dosežemo z
kolektivno sinhronizacijo preko vseh procesorjev (z mejniki - MPI_Barrier),
medsebojno sinhronizacijo
zaklepi (locks)
Najbolj osnoven dostop do oddaljenega spomina je z
MPI_PUT(origin_addr, origin_count, origin_datatype, target_rank, target_disp,
target_count, target_datatype, window)
S klici RMA okna (windows) zamenjujejo komunikatorje. Ni pa možnosti načina prenosov in so vse operacije brez blokad. Tako se lahko zgodi, da program nadaljuje z delom še preden so bili podatki prenešeni. To pa zahteva tudi, da se medtem podatki pošiljatelja ne smejo popravljati. Do sinhronizacije z npr MPI_WIN_FENCE, zaklepi ali MPI_WIN_START/MPI_WIN_POST.
Optimizacija in deljeni komunikatorji v MPI
Izvajanje programov lahko upočasni naslednje kategorije:
pomanjkanje vzporednosti - Amdahlov zakon Tp = Ts( (1-a)/p + a)
slaba porazdelitev bremena
sinhronizacija
komunikacija
Namesto komunikatorja MPI_COMM_WORLD lahko uporabimo tudi manjše komunikatorje zato, da združimo procese glede na arhitekturno zgradbo komunikacij. Pri razdelitvi komunikatorja MPI_COMM_WORLD imajo potem procesi ranke štete po vrsti v vsakem komunikatorju posebej.
Primeri MPI programov
Hello, world
Izdelajmo MPI program, ki izpiše standardni pozdrav na gruči z uporabo sistema SLURM. Program bo le izpisal pozdrav iz vsakega procesa. Za ugotovitev celotnega števila procesov uporabimo spremenljivko MPI_COMM_WORLD.
Za prevajanje moramo naprej izbrati okolje, kar naredimo z ukazom
module load intel openmpi
Program prevedemo z:
mpicc -g -o hello hello.c -limf -lm
#include
int main(int argc, char *argv[])
{
int rank, velikost;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &velikost);
printf("Pozdrav iz procesa št. %d\n", rank);
if (rank == 0)
printf("Skupno število procesov je %d\n", velikost);
MPI_Finalize();
}
Ustvarimo “batch” datoteko mpibatch.sh z naslednjo vsebino:
#!/bin/bash
#SBATCH --partition=rome
#SBATCH --ntasks=8
#SBATCH --ntasks-per-core=1
#SBATCH --output=hello-%j.out
#SBATCH --error=hello-%j.err
mpirun hello
in jo poženomo z ukazom sbatch mpibatch.sh, ki v izhodno datoteko napiše rezultat in statistiko. Izpis programa je naslednji:
Pozdrav iz procesa št. 0
Skupno število procesov je 8
Pozdrav iz procesa št. 1
Pozdrav iz procesa št. 3
Pozdrav iz procesa št. 5
Pozdrav iz procesa št. 2
Pozdrav iz procesa št. 4
Pozdrav iz procesa št. 6
Pozdrav iz procesa št. 7
Izračun števila Pi
Želimo izračunati aproksimacijo števila pi z enačbo

Za osnovo uporabimo program hello.c tako, da vsak proces neodvisno izračuna vrednost pi. Preverimo ali vsi procesi izpišejo isto številko.
#include
#define N 840
#define SQR(x) (x)*(x)
int main(int argc, char *argv[])
{
int rank, velikost;
int i;
double pi = 0.0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &velikost);
for (i=1; i < N; ++i)
pi += 1.0/(1.0+SQR((i-0.5)/N));
pi *= 4.0/N;
printf("Rezultat #%d: Pi= %lf\n", rank, pi);
if (rank == 0)
printf("Skupno število procesov je %d\n", velikost);
MPI_Finalize();
}
Sedaj razdelimo procese tako, da računajo v različnih obsegih. Če bi imeli dva procesa bo rank 0 bo računal i=1..N/2, rank 1 pa i=N/2..N. N nastavimo na 840, saj je deljivo z 2 do 8. Izpišimo delne vsote in jih izračunamo na pamet.
#include
#define N 840
#define SQR(x) (x)*(x)
int main(int argc, char *argv[])
{
int rank, np;
int i, start, end;
float pi = 0.0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &np);
start = N*rank/np+1; end = start + N/np-1;
printf("Obseg #%d : [%d:%d]\n", rank, start, end);
for (i=start; i <= end; ++i)
pi += 1.0/(1.0+SQR((i-0.5)/N));
pi *= 4.0/N;
printf("Delna vsota #%d: Pi= %f\n", rank, pi);
MPI_Finalize();
}
Sedaj sestavimo delne vsote s pošiljanjem glavnemu procesu (rank 0) za seštevanje
Vsi procesi (razen glavnega) pošiljajo delne vsote glavnemu
glavni sprejema vrednosti vseh ostalih procesov in jih sešteje
Za komunikacijo uporabimo MPI_Ssend() in MPI_Recv()
Program dopolnnimo z
Uporabimo funkcijo MPI_Wtime za izračun potrebnega časa za izračun
double starttime, endtime;
starttime = MPI_Wtime();
... računamo
endtime = MPI_Wtime();
printf("Pi = %f izračunan v %lf\n", pi, endtime-starttime);
Preverimo in zagotovimo, da program deluje pravilno tudi če N ni mnogokratnik števila procesov P
Ping pong
Napišimo program. ki v dveh procesih (rank 0 in 1) ponavljajoče pošilja sporočila sem in tja. Uporabimo MPI_Send za posiljanje. Izdelati je potrebno program tako, da pravilno deluje tudi pri večjem številu procesov. Da poenostavimo bomo uporabili polje realnih števil ne da bi ga inicializirali.
Dodajmo meritev časa za vse komunikacije
Poglejmo kako čas varira z velikostjo sporočila
Narišimo graf, da ugotovimo latenco (čas potreben za sporočilo velikosti nič).
program pingpong
use MPI_f08
implicit none
integer i, rank
integer buf_size
parameter (buf_size=8*64)
real buf(buf_size)
type(MPI_Status) :: stat
double precision :: start, finish
call MPI_Init()
call MPI_Comm_Rank(MPI_COMM_WORLD, rank)
do i=1,51
if (rank .eq. 0) then
if (i .eq. 2) start = MPI_Wtime()
call MPI_Send(buf, count=buf_size, datatype=MPI_REAL, dest=1, tag=17, comm=MPI_COMM_WORLD)
call MPI_Recv(buf, count=buf_size, datatype=MPI_REAL, source=1, tag=23, comm=MPI_COMM_WORLD, status=stat)
else if (rank .eq. 1) then
call MPI_Recv(buf, count=buf_size, datatype=MPI_REAL, source=0, tag=17, comm=MPI_COMM_WORLD, status=stat)
call MPI_Send(buf, count=buf_size, datatype=MPI_REAL, dest=0, tag=23, comm=MPI_COMM_WORLD)
end if
end do
finish = MPI_Wtime()
if (rank .eq. 0) then
print *, 'delta_time =', (finish-start)/(2*50)*1e6
end if
call MPI_Barrier(MPI_COMM_WORLD)
call MPI_Finalize()
end program
Enostranska komunikacija
Program za krozno vsoto predelamo v RMA nacin
#include <stdio.h>
#include <mpi.h>
#define to_right 201
int main (int argc, char *argv[])
{
int my_rank, size;
int snd_buf, rcv_buf;
int right, left;
int sum, i;
MPI_Win win;
MPI_Status status;
MPI_Request request;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
right = (my_rank+1) % size;
left = (my_rank-1+size) % size;
/* ... this SPMD-style neighbor computation with modulo has the same meaning as: */
/* right = my_rank + 1; */
/* if (right == size) right = 0; */
/* left = my_rank - 1; */
/* if (left == -1) left = size-1;*/
sum = 0;
snd_buf = my_rank;
MPI_Win_create(&rcv_buf, sizeof(int), sizeof(int), MPI_INFO_NULL,
MPI_COMM_WORLD, &win);
for( i = 0; i < size; i++)
{
#if 0
MPI_Issend(&snd_buf, 1, MPI_INT, right, to_right,
MPI_COMM_WORLD, &request);
MPI_Recv(&rcv_buf, 1, MPI_INT, left, to_right,
MPI_COMM_WORLD, &status);
MPI_Wait(&request, &status);
#else
MPI_Win_fence(MPI_MODE_NOSTORE | MPI_MODE_NOPRECEDE, win);
MPI_Put(&snd_buf, 1, MPI_INT, right, (MPI_Aint)0, 1, MPI_INT, win);
MPI_Win_fence(MPI_MODE_NOSTORE | MPI_MODE_NOPUT | MPI_MODE_NOSUCCEED, win);
#endif
snd_buf = rcv_buf;
sum += rcv_buf;
}
printf ("PE%i:\tSum = %i\n", my_rank, sum);
MPI_Finalize();
}
</mpi.h></stdio.h>