|
| 1 | +// Transmit data of different types using their associated datatype constants. |
| 2 | + |
| 3 | +#include "mpi.h" |
| 4 | +#include <assert.h> |
| 5 | + |
| 6 | +void test_INT(MPI_Comm comm, int rank, int exp) { |
| 7 | + int send = rank; |
| 8 | + int recv = -1; |
| 9 | + MPI_Allreduce(&send, &recv, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 10 | + assert(recv == exp); |
| 11 | +} |
| 12 | + |
| 13 | +void test_LONG(MPI_Comm comm, int rank, int exp) { |
| 14 | + long int send = rank; |
| 15 | + long int recv = -1; |
| 16 | + MPI_Allreduce(&send, &recv, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD); |
| 17 | + assert(recv == (long int)exp); |
| 18 | +} |
| 19 | + |
| 20 | +void test_FLOAT(MPI_Comm comm, int rank, int exp) { |
| 21 | + float send = (float)rank; |
| 22 | + float recv = -1.0; |
| 23 | + MPI_Allreduce(&send, &recv, 1, MPI_FLOAT, MPI_SUM, MPI_COMM_WORLD); |
| 24 | + assert(recv == (float)exp); |
| 25 | +} |
| 26 | + |
| 27 | +void test_DOUBLE(MPI_Comm comm, int rank, int exp) { |
| 28 | + double send = (double)rank; |
| 29 | + double recv = -1.0; |
| 30 | + MPI_Allreduce(&send, &recv, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); |
| 31 | + assert(recv == (double)exp); |
| 32 | +} |
| 33 | + |
| 34 | +void test_COUNT(MPI_Comm comm, int rank, int exp) { |
| 35 | + MPI_Count send = (MPI_Count)rank; |
| 36 | + MPI_Count recv = -1.0; |
| 37 | + MPI_Allreduce(&send, &recv, 1, MPI_COUNT, MPI_SUM, MPI_COMM_WORLD); |
| 38 | + assert(recv == (MPI_Count)exp); |
| 39 | +} |
| 40 | + |
| 41 | +int main(int argc, char *argv[]) { |
| 42 | + int rank = -1; |
| 43 | + int size = -1; |
| 44 | + MPI_Init(&argc,&argv); |
| 45 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 46 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 47 | + |
| 48 | + int exp = 0; |
| 49 | + for (int i=0; i<size; ++i) { |
| 50 | + exp = exp + i; |
| 51 | + } |
| 52 | + |
| 53 | + test_INT(MPI_COMM_WORLD, rank, exp); |
| 54 | + test_LONG(MPI_COMM_WORLD, rank, exp); |
| 55 | + test_FLOAT(MPI_COMM_WORLD, rank, exp); |
| 56 | + test_DOUBLE(MPI_COMM_WORLD, rank, exp); |
| 57 | + test_COUNT(MPI_COMM_WORLD, rank, exp); |
| 58 | + |
| 59 | + MPI_Finalize(); |
| 60 | + return 0; |
| 61 | +} |
0 commit comments