| [240d6f3] | 1 | /**
|
|---|
| 2 | * This program should deadlock but it doesn't.
|
|---|
| 3 | * Not all processes are executing the collective
|
|---|
| 4 | * operations in the same order.
|
|---|
| 5 | */
|
|---|
| 6 | #include<mpi.h>
|
|---|
| 7 | #include<stdlib.h>
|
|---|
| 8 | #include<assert.h>
|
|---|
| 9 | #include<stdio.h>
|
|---|
| 10 |
|
|---|
| [8f2c865] | 11 | int main(int argc, char * argv[])
|
|---|
| [240d6f3] | 12 | {
|
|---|
| 13 | int rank;
|
|---|
| 14 | int procs;
|
|---|
| 15 | int* sendBuf;
|
|---|
| 16 | int* rcvBuf;
|
|---|
| 17 | int* sum;
|
|---|
| 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 (rank == 0) {
|
|---|
| 24 | sendBuf = (int*)malloc(sizeof(int)*procs);
|
|---|
| 25 | sum = (int*)malloc(sizeof(int)*procs);
|
|---|
| 26 | for(int i=0; i < procs; i++){
|
|---|
| 27 | sendBuf[i] = i;
|
|---|
| 28 | sum[i] = 0;
|
|---|
| 29 | }
|
|---|
| 30 | }else{
|
|---|
| [8f2c865] | 31 | sum = (int*)malloc(sizeof(int)*procs);
|
|---|
| [240d6f3] | 32 | sendBuf = (int*)malloc(sizeof(int));
|
|---|
| 33 | }
|
|---|
| 34 | rcvBuf = (int*)malloc(sizeof(int)*procs);
|
|---|
| 35 |
|
|---|
| 36 | MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 37 |
|
|---|
| 38 | *sendBuf = *rcvBuf;
|
|---|
| 39 |
|
|---|
| 40 | if(rank % 2)
|
|---|
| 41 | MPI_Allgather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, MPI_COMM_WORLD);
|
|---|
| 42 | else
|
|---|
| [8f2c865] | 43 | MPI_Bcast(sum, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| [240d6f3] | 44 |
|
|---|
| 45 | printf("Vector process %d is: (", rank);
|
|---|
| 46 | for(int i=0; i<procs; i++){
|
|---|
| 47 | printf("%d", rcvBuf[i]);
|
|---|
| 48 | if(i != procs-1)
|
|---|
| 49 | printf(", ");
|
|---|
| 50 | }
|
|---|
| 51 | printf(")\n");
|
|---|
| 52 |
|
|---|
| 53 | if(rank%2)
|
|---|
| [8f2c865] | 54 | MPI_Bcast(sum, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| [240d6f3] | 55 | else
|
|---|
| 56 | MPI_Allgather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, MPI_COMM_WORLD);
|
|---|
| 57 |
|
|---|
| 58 | MPI_Reduce(rcvBuf, sum, procs, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
|
|---|
| 59 |
|
|---|
| 60 | if(rank == 0){
|
|---|
| 61 | printf("Vector sum is: (");
|
|---|
| 62 | for(int i=0; i<procs; i++){
|
|---|
| 63 | printf("%d", sum[i]);
|
|---|
| 64 | if(i != procs-1)
|
|---|
| 65 | printf(", ");
|
|---|
| 66 | }
|
|---|
| 67 | printf(")\n");
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | free(sendBuf);
|
|---|
| 71 | free(rcvBuf);
|
|---|
| 72 | free(sum);
|
|---|
| 73 | return 0;
|
|---|
| 74 | }
|
|---|