| 1 | /* This example shows misusage of MPI collective routines. With a
|
|---|
| 2 | * macro TYPE or TYPE2, this example shows inconsistency of MPI
|
|---|
| 3 | * datatypes of collective calls. With a macro OPERATOR, this example
|
|---|
| 4 | * shows inconsistency of MPI operators of collective calls.
|
|---|
| 5 | **/
|
|---|
| 6 | #include <mpi.h>
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 | #ifdef _CIVL
|
|---|
| 9 | #include<civlc.cvh>
|
|---|
| 10 | #endif
|
|---|
| 11 |
|
|---|
| 12 | #define WCOMM MPI_COMM_WORLD
|
|---|
| 13 |
|
|---|
| 14 | int main(int argc, char **argv){
|
|---|
| 15 | int npes, mype, ierr;
|
|---|
| 16 | double sum, val; int calc, knt=1;
|
|---|
| 17 |
|
|---|
| 18 | ierr = MPI_Init(&argc, &argv);
|
|---|
| 19 | ierr = MPI_Comm_size(WCOMM, &npes);
|
|---|
| 20 | ierr = MPI_Comm_rank(WCOMM, &mype);
|
|---|
| 21 | val = (double)mype;
|
|---|
| 22 |
|
|---|
| 23 | #ifdef TYPE
|
|---|
| 24 | if(mype%2)
|
|---|
| 25 | ierr = MPI_Allreduce(&val, &sum, knt, MPI_DOUBLE, MPI_SUM, WCOMM);
|
|---|
| 26 | else
|
|---|
| 27 | ierr = MPI_Allreduce(&val, &sum, knt, MPI_INT, MPI_SUM, WCOMM);
|
|---|
| 28 | #elif defined TYPE2
|
|---|
| 29 | if(mype%2)
|
|---|
| 30 | ierr = MPI_Allreduce(&val, &sum, knt, MPI_DOUBLE, MPI_SUM, WCOMM);
|
|---|
| 31 | else {
|
|---|
| 32 | int sum, val;
|
|---|
| 33 |
|
|---|
| 34 | ierr = MPI_Allreduce(&val, &sum, knt, MPI_INT, MPI_SUM, WCOMM);
|
|---|
| 35 | }
|
|---|
| 36 | #elif defined OPERATOR
|
|---|
| 37 | if(mype%2)
|
|---|
| 38 | ierr = MPI_Allreduce(&val, &sum, knt, MPI_DOUBLE, MPI_SUM, WCOMM);
|
|---|
| 39 | else
|
|---|
| 40 | ierr = MPI_Allreduce(&val, &sum, knt, MPI_DOUBLE, MPI_MAX, WCOMM);
|
|---|
| 41 | #else
|
|---|
| 42 | ierr = MPI_Allreduce(&val, &sum, knt, MPI_DOUBLE, MPI_SUM, WCOMM);
|
|---|
| 43 | #endif
|
|---|
| 44 |
|
|---|
| 45 | calc = ((npes - 1) * npes) / 2;
|
|---|
| 46 | printf(" PE: %d sum=%5.0f calc=%d\n", mype, sum, calc);
|
|---|
| 47 | #ifdef _CIVL
|
|---|
| 48 | #ifndef OPERATOR
|
|---|
| 49 | $assert(sum == calc);
|
|---|
| 50 | #endif
|
|---|
| 51 | #endif
|
|---|
| 52 | ierr = MPI_Finalize();
|
|---|
| 53 | }
|
|---|