Erzeugt drei Intrakommunikatoren und bildet einen Interkommunikator zwischen jeweils zweien. Dann werden ein paar Daten über Interkommunikator hin und her geschickt. Jeder Prozeß sendet seine ID aus MPI_COMM_WORLD an die Prozesse der anderen Intrakommunikatoren mit der gleichen lokalen ID.
#include "mpi.h"
#include <stdio.h>
#define GROUPS 3
int main(int argc, char *argv[]) {
int me, newid, numprocs, groupsize, color, msg, rbuf;
int local_leader, remote_leader;
MPI_Status stat;
MPI_Comm local_comm;
MPI_Comm myFirstComm, mySecondComm;
if (MPI_Init(&argc, &argv)!=MPI_SUCCESS) {
fprintf(stderr,"MPI_Init failed.\n");
return -1;
}
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
if ((numprocs%GROUPS)!=0) {
if (me==0) {
fprintf(stderr,"Number of processes should be a multiple of %d\n", GROUPS);
}
MPI_Finalize();
return -1;
}
color=me%GROUPS;
MPI_Comm_split(MPI_COMM_WORLD, color, me, &local_comm);
MPI_Comm_rank(local_comm, &newid);
MPI_Comm_size(local_comm, &groupsize);
fprintf(stderr,"Proc %d: I'm now number %d in group with color %d.\n",
me, newid, color);
if (color==0) {
MPI_Intercomm_create(local_comm, 0, MPI_COMM_WORLD, 1,
111, &myFirstComm);
MPI_Intercomm_create(local_comm, 0, MPI_COMM_WORLD, 2,
222, &mySecondComm);
}
if (color==1) {
MPI_Intercomm_create(local_comm, 0, MPI_COMM_WORLD, 0,
111, &myFirstComm);
MPI_Intercomm_create(local_comm, 0, MPI_COMM_WORLD, 2,
999, &mySecondComm);
}
if (color==2) {
MPI_Intercomm_create(local_comm, 0, MPI_COMM_WORLD, 0,
222, &myFirstComm);
MPI_Intercomm_create(local_comm, 0, MPI_COMM_WORLD, 1,
999, &mySecondComm);
}
MPI_Barrier( MPI_COMM_WORLD);
msg=me;
if (color==0) {
MPI_Send(&msg, 1, MPI_INT, newid, 0, myFirstComm);
MPI_Send(&msg, 1, MPI_INT, newid, 0, mySecondComm);
fprintf(stderr,"Proc %d: Sending %d to partners.\n", me, msg);
MPI_Recv(&rbuf,1,MPI_INT,newid,MPI_ANY_TAG,myFirstComm,&stat);
fprintf(stderr,"Proc %d: Received %d from remote %d (tag %d)\n",
me, rbuf, stat.MPI_SOURCE, stat.MPI_TAG);
MPI_Recv(&rbuf,1,MPI_INT,newid,MPI_ANY_TAG,mySecondComm,&stat);
fprintf(stderr,"Proc %d: Received %d from remote %d (tag %d)\n",
me, rbuf, stat.MPI_SOURCE, stat.MPI_TAG);
}
if (color==1) {
MPI_Recv(&rbuf,1,MPI_INT,newid,MPI_ANY_TAG,myFirstComm,&stat);
fprintf(stderr,"Proc %d: Received %d from remote %d (tag %d)\n",
me, rbuf, stat.MPI_SOURCE, stat.MPI_TAG);
MPI_Send(&msg, 1, MPI_INT, newid, 0, myFirstComm);
MPI_Send(&msg, 1, MPI_INT, newid, 0, mySecondComm);
fprintf(stderr,"Proc %d: Sending %d to partners.\n", me, msg);
MPI_Recv(&rbuf,1,MPI_INT,newid,MPI_ANY_TAG,mySecondComm,&stat);
fprintf(stderr,"Proc %d: Received %d from remote %d (tag %d)\n",
me, rbuf, stat.MPI_SOURCE, stat.MPI_TAG);
}
if (color==2) {
MPI_Recv(&rbuf,1,MPI_INT,newid,MPI_ANY_TAG,myFirstComm,&stat);
fprintf(stderr,"Proc %d: Received %d from remote %d (tag %d)\n",
me, rbuf, stat.MPI_SOURCE, stat.MPI_TAG);
MPI_Recv(&rbuf,1,MPI_INT,newid,MPI_ANY_TAG,mySecondComm,&stat);
fprintf(stderr,"Proc %d: Received %d from remote %d (tag %d)\n",
me, rbuf, stat.MPI_SOURCE, stat.MPI_TAG);
MPI_Send(&msg, 1, MPI_INT, newid, 0, myFirstComm);
MPI_Send(&msg, 1, MPI_INT, newid, 0, mySecondComm);
fprintf(stderr,"Proc %d: Sending %d to partners.\n", me, msg);
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Comm_free(&myFirstComm);
MPI_Comm_free(&mySecondComm);
MPI_Comm_free(&local_comm);
MPI_Finalize();
}
| Makefile | C-Quelltext |