| 1 | /**
|
|---|
| 2 | * This example demonstrates the usage of MPI collective operations,
|
|---|
| 3 | * which should be called in the same orders for all MPI processes.
|
|---|
| 4 | * This example has an error when there are more than two MPI processes.
|
|---|
| 5 | */
|
|---|
| 6 | #include<mpi.h>
|
|---|
| 7 | #include<stdlib.h>
|
|---|
| 8 |
|
|---|
| 9 | int main(int argc, char * argv[])
|
|---|
| 10 | {
|
|---|
| 11 | int rank;
|
|---|
| 12 | int procs;
|
|---|
| 13 | int* values;
|
|---|
| 14 |
|
|---|
| 15 | MPI_Init(&argc,&argv);
|
|---|
| 16 | MPI_Comm_size(MPI_COMM_WORLD, &procs);
|
|---|
| 17 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| 18 |
|
|---|
| 19 | if (rank == 0 || rank == 2) {
|
|---|
| 20 | values = (int*)malloc(sizeof(int)*procs);
|
|---|
| 21 | }else{
|
|---|
| 22 | values = (int*)malloc(sizeof(int));
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | *values = procs + rank;
|
|---|
| 26 |
|
|---|
| 27 | #ifdef TYPE
|
|---|
| 28 | if (rank != 2)
|
|---|
| 29 | MPI_Gather(values, 1, MPI_INT, values, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 30 | else
|
|---|
| 31 | MPI_Gather(values, 1, MPI_FLOAT, values, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
|
|---|
| 32 | #elif defined ROOT
|
|---|
| 33 | if (rank != 2)
|
|---|
| 34 | MPI_Gather(values, 1, MPI_INT, values, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 35 | else
|
|---|
| 36 | MPI_Gather(values, 1, MPI_INT, values, 1, MPI_INT, 2, MPI_COMM_WORLD);
|
|---|
| 37 | #else
|
|---|
| 38 | if (rank != 2)
|
|---|
| 39 | MPI_Gather(values, 1, MPI_INT, values, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 40 | #endif
|
|---|
| 41 |
|
|---|
| 42 | free(values);
|
|---|
| 43 | MPI_Finalize();
|
|---|
| 44 | return 0;
|
|---|
| 45 | }
|
|---|