Sunteți pe pagina 1din 5

Arhitectura sistemelor paralele

MPI Message Passing Interface

#include<stdio.h>
#include<mpi.h>

MPI Hello world

int main(int argc, char** argv) {


/// Initializeaza mediu MPI
MPI_Init(NULL, NULL);
/// Obtine numarul de procesoare
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
/// Afla rangul procesorului
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
/// Afla numele procesorului
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
/// Afisarea mesajului "hello world"
printf("Hello world de la procesorul %s, cu rangul %i din %i procesoare\n",
processor_name, world_rank, world_size);
/// Finalizarea mediului MPI
MPI_Finalize();
return 0;
}

- int MPI_Init( int *argc, char ***argv ) indic argumentele pentru initializare; rutina trebuie
chemat de procesul principal, cel care execut i MPI_Finalize
- int MPI_Comm_size( MPI_Comm comm, int *size ) determinarea numarului de
procesoare
- int MPI_Comm_rank( MPI_Comm comm, int *rank ) determinarea rangului (numarului
procesorului)
- int MPI_Get_processor_name( char *name, int *resultlen ) determinarea numelui
procesorului
- int MPI_Finalize( void ) finalizarea mediul de execuie MPI; trebuie chemat de toate
procesele
- int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest, int tag,
MPI_Comm comm)
Input Parameters
Buf: initial address of send buffer (choice)
Count: number of elements in send buffer (nonnegative integer)
Datatype: datatype of each send buffer element (handle)
Dest: rank of destination (integer)
Tag: message tag (integer)
Comm: communicator (handle)

- int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag,
MPI_Comm comm, MPI_Status *status)
Output Parameters
Buf: initial address of receive buffer (choice)
Status: status object (Status)
Input Parameters
Count: maximum number of elements in receive buffer (integer)
Datatype: datatype of each receive buffer element (handle)
Source: rank of source (integer)
Tag: message tag (integer)
Comm: communicator (handle)
Exemple:
MPI_Send( &A[i], 5, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Recv( &Vec, 5, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

Calcularea lui Pi prin


metoda Monte Carlo

int main(int argc, char* argv)


{
int niter=0; ///numar iteratii
double x,y;
int i,count;
double z;
double pi;
int SEED=1234; ///valoare de initilizare a generatorului de numerea aleatoare
srand(SEED);
printf("Numarul de iteratii pentru estimarea lui pi: ");
scanf("%i",&niter);
count=0;
for ( i=0; i<niter; i++) {
x = (double)rand()/(double)RAND_MAX;
y = (double)rand()/(double)RAND_MAX;
z = x*x+y*y;
if (z<=1) count++;
}
pi=(double)count/(double)niter*4.0;
printf("Folosind %d iteratii , pi = %.6f fata de valoare reala pi = 3.141592; eroarea este
%lf\n",niter,pi, fabs(pi- 3.141592));
}