Aufstieg:
Environment und Errorhandling . . Blockierende Kommunikation . . Gruppen, Kontexte und Kommunikatoren
Funktionsübersicht

Hilfesystem für den Message Passing Interface Standard MPI


Beispiel für die Verwendung der sechs grundlegenden Funktionen


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

int main(int argc, char *argv[]) { 
    int        numprocs, myid, buf, i;
    MPI_Status stat;

    if (MPI_Init(&argc,&argv)!=MPI_SUCCESS) {
	fprintf(stderr, "MPI_Init failed.");
	return -1;
    }
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    fprintf(stderr,"I'm number %d of %d.\n", myid, numprocs);
    if (myid==0) {
	buf=1;
	for (i=1; i<numprocs; i++) {
	    MPI_Send(&buf,1,MPI_INT,i,0,MPI_COMM_WORLD);
	}
	fprintf(stderr,"Proc %d: Message sent to %d processes.\n", myid,i-1);
    }
    else {
	MPI_Recv(&buf,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,
		 &stat);
	fprintf(stderr,"Proc %d: Received data %d from %d with tag %d.\n",
		myid, buf, stat.MPI_SOURCE, stat.MPI_TAG);
    }
    MPI_Finalize();
}


Makefile C-Quelltext