| 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 | Process zero executes a broadcast, followed by a blocking send operation. Process one first executes a blocking receive that matches the send, followed by broadcast call that matches the broadcast of process zero. This program may deadlock. The broadcast call on process zero may block until process one executes the matching broadcast call, so that the send is not executed. Process one will definitely block on the receive and so, in this case, never executes the broadcast.
|
|---|
| 6 | */
|
|---|
| 7 | #include<stdio.h>
|
|---|
| 8 | #include<mpi.h>
|
|---|
| 9 | #include<assert.h>
|
|---|
| 10 |
|
|---|
| 11 | int main(int argc, char* argv[]){
|
|---|
| 12 | int rank;
|
|---|
| 13 | int procs;
|
|---|
| 14 | int* sendBuf;
|
|---|
| 15 | int* rcvBuf;
|
|---|
| 16 | int* sum;
|
|---|
| 17 | int buf1, buf2;
|
|---|
| 18 |
|
|---|
| 19 | MPI_Init(&argc,&argv);
|
|---|
| 20 | MPI_Comm_size(MPI_COMM_WORLD, &procs);
|
|---|
| 21 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| 22 |
|
|---|
| 23 | if(procs != 2){
|
|---|
| 24 | printf("This program requires exactly two processes.\n");
|
|---|
| 25 | return 1;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | if(rank == 0){
|
|---|
| 29 | buf1=1;
|
|---|
| 30 | buf2=2;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | switch(rank) {
|
|---|
| 34 | case 0:
|
|---|
| 35 | MPI_Bcast(&buf1, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 36 | MPI_Send(&buf2, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
|
|---|
| 37 | break;
|
|---|
| 38 | case 1:
|
|---|
| 39 | MPI_Recv(&buf2, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 40 | MPI_Bcast(&buf1, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 41 | break;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | printf("process %d: buf1=%d, buf2=%d\n", rank, buf1, buf2);
|
|---|
| 45 | assert(buf1==1 && buf2 == 2);
|
|---|
| 46 |
|
|---|
| 47 | MPI_Finalize();
|
|---|
| 48 | return 0;
|
|---|
| 49 | }
|
|---|