| [4ccacf8] | 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 | */
|
|---|
| [a7f50d5] | 6 | #include<mpi.h>
|
|---|
| 7 | #include<stdlib.h>
|
|---|
| 8 |
|
|---|
| [b12731c] | 9 | int main(int argc, char * argv[])
|
|---|
| 10 | {
|
|---|
| [a7f50d5] | 11 | int rank;
|
|---|
| 12 | int procs;
|
|---|
| 13 | int* values;
|
|---|
| 14 |
|
|---|
| [b12731c] | 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 | for(int i = 0; i < procs; i++){
|
|---|
| 22 | values[i] = i;
|
|---|
| 23 | }
|
|---|
| [a7f50d5] | 24 | }else{
|
|---|
| [b12731c] | 25 | values = (int*)malloc(sizeof(int));
|
|---|
| [a7f50d5] | 26 | }
|
|---|
| [b12731c] | 27 |
|
|---|
| 28 | #if defined TYPE
|
|---|
| [a7f50d5] | 29 | if (rank != 2)
|
|---|
| [b12731c] | 30 | MPI_Scatter(values, 1, MPI_INT, values, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 31 | else
|
|---|
| 32 | MPI_Scatter(values, 1, MPI_FLOAT, values, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
|
|---|
| 33 | #elif defined ROOT
|
|---|
| 34 | if (rank != 2)
|
|---|
| 35 | MPI_Scatter(values, 1, MPI_INT, values, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 36 | else
|
|---|
| 37 | MPI_Scatter(values, 1, MPI_INT, values, 1, MPI_INT, 2, MPI_COMM_WORLD);
|
|---|
| 38 | #else
|
|---|
| 39 | if (rank != 2)
|
|---|
| 40 | MPI_Scatter(values, 1, MPI_INT, values, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| [a7f50d5] | 43 | free(values);
|
|---|
| [b12731c] | 44 | MPI_Finalize();
|
|---|
| [a7f50d5] | 45 | return 0;
|
|---|
| 46 | }
|
|---|