2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
13 static char MTEST_Descrip[] = "Test MPI_Allreduce with commutative user-defined operations";
16 #define MAX_BLOCKLEN 10000
17 #define IS_COMMUTATIVE 1
19 /* We make the error count global so that we can easily control the output
20 of error information (in particular, limiting it after the first 10
24 /* parameter for a vector type of MPI_INT */
28 void uop(void *, void *, int *, MPI_Datatype *);
29 void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype)
31 const int *cin = (const int *) cinPtr;
32 int *cout = (int *) coutPtr;
35 for (int i = 0; i < *count; i++) {
36 for (int j = 0; j < g_blocklen; j++) {
43 static void init_buf(void *buf, int count, int rank)
48 for (int i = 0; i < count; i++) {
49 for (int j = 0; j < g_blocklen; j++) {
56 static int check_result(void *buf, int count, int size)
62 for (int i = 0; i < count; i++) {
63 for (int j = 0; j < g_blocklen; j++) {
64 int exp = size * (size - 1) / 2 + (i + j) * size;
67 if (errs + lerrs < 10) {
68 printf("[%d - %d] expected %d, got %d, %s\n",
69 i, j, exp, p[k], MTestGetIntracommName());
78 int main(int argc, char *argv[])
83 MPI_Datatype datatype;
85 MTest_Init(&argc, &argv);
87 MPI_Op_create(uop, IS_COMMUTATIVE, &op);
89 while (MTestGetIntracommGeneral(&comm, 2, 1)) {
90 if (comm == MPI_COMM_NULL) {
95 MPI_Comm_rank(comm, &rank);
96 MPI_Comm_size(comm, &size);
99 for (int n = 1; n < MAX_BLOCKLEN; n *= 2) {
101 int extent = g_blocklen * g_stride * sizeof(int);
102 MPI_Type_vector(g_blocklen, 1, g_stride, MPI_INT, &datatype);
103 MPI_Type_commit(&datatype);
105 buf = (int *) malloc(extent * count);
107 MPI_Abort(MPI_COMM_WORLD, 1);
109 bufout = (int *) malloc(extent * count);
111 MPI_Abort(MPI_COMM_WORLD, 1);
114 init_buf(buf, count, rank);
115 MPI_Allreduce(buf, bufout, count, datatype, op, comm);
116 errs += check_result(bufout, count, size);
118 /* do it again using MPI_IN_PLACE */
119 init_buf(bufout, count, rank);
120 MPI_Allreduce(MPI_IN_PLACE, bufout, count, datatype, op, comm);
121 errs += check_result(bufout, count, size);
125 MPI_Type_free(&datatype);
128 MTestFreeComm(&comm);
133 MTest_Finalize(errs);
134 return MTestReturnValue(errs);