| [6e48678] | 1 | /**
|
|---|
| 2 | * This program is written according to the example described in
|
|---|
| 3 | * http://www.mcs.anl.gov/research/projects/mpi/mpi-standard/mpi-report-1.1/node86.htm#Node86
|
|---|
| 4 | */
|
|---|
| 5 | #include<stdio.h>
|
|---|
| 6 | #include<mpi.h>
|
|---|
| 7 | #include<assert.h>
|
|---|
| 8 |
|
|---|
| 9 | int main(int argc, char* argv[]){
|
|---|
| 10 | int rank;
|
|---|
| 11 | int procs;
|
|---|
| 12 | int* sendBuf;
|
|---|
| 13 | int* rcvBuf;
|
|---|
| 14 | int* sum;
|
|---|
| 15 | int buf1, buf2;
|
|---|
| 16 |
|
|---|
| 17 | MPI_Init(&argc,&argv);
|
|---|
| 18 | MPI_Comm_size(MPI_COMM_WORLD, &procs);
|
|---|
| 19 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| 20 |
|
|---|
| 21 | if(procs != 3){
|
|---|
| 22 | printf("This program requires exactly three processes.\n");
|
|---|
| 23 | return 1;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | buf1=rank;
|
|---|
| 27 | buf2=rank;
|
|---|
| 28 |
|
|---|
| 29 | switch(rank) {
|
|---|
| 30 | case 0:
|
|---|
| 31 | MPI_Bcast(&buf1, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 32 | MPI_Send(&buf2, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
|
|---|
| 33 | break;
|
|---|
| 34 | case 1:
|
|---|
| 35 | MPI_Recv(&buf2, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 36 | MPI_Bcast(&buf1, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 37 | MPI_Recv(&buf2, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 38 | break;
|
|---|
| 39 | case 2:
|
|---|
| 40 | MPI_Send(&buf2, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
|
|---|
| 41 | MPI_Bcast(&buf1, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 42 | break;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | printf("process %d: buf1=%d, buf2=%d\n", rank, buf1, buf2);
|
|---|
| 46 | if(rank != 0)
|
|---|
| 47 | #ifdef FASSERT
|
|---|
| 48 | assert(buf1==0 && buf2 == 2);
|
|---|
| 49 | #else
|
|---|
| 50 | assert(buf1==0 && (buf2==0||buf2==2));
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | MPI_Finalize();
|
|---|
| 54 | return 0;
|
|---|
| 55 | }
|
|---|