Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
support MPI_Op_commutative call, as it was already implemented internally
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / reduce_local.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2009 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "A simple test of MPI_Reduce_local";
14 */
15
16 #define MAX_BUF_ELEMENTS (65000)
17
18 static int uop_errs = 0;
19
20 /* prototype to keep the compiler happy */
21 static void user_op(void *invec, void *inoutvec, int *len, MPI_Datatype * datatype);
22
23 static void user_op(void *invec, void *inoutvec, int *len, MPI_Datatype * datatype)
24 {
25     int i;
26     int *invec_int = (int *) invec;
27     int *inoutvec_int = (int *) inoutvec;
28
29     if (*datatype != MPI_INT) {
30         ++uop_errs;
31         printf("invalid datatype passed to user_op");
32         return;
33     }
34
35     for (i = 0; i < *len; ++i) {
36         inoutvec_int[i] = invec_int[i] * 2 + inoutvec_int[i];
37     }
38 }
39
40 int main(int argc, char *argv[])
41 {
42     int errs = 0;
43     int i;
44     int *inbuf = NULL;
45     int *inoutbuf = NULL;
46     int count = -1;
47     MPI_Op uop = MPI_OP_NULL;
48
49     MTest_Init(&argc, &argv);
50 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
51     /* this function was added in MPI-2.2 */
52
53     inbuf = malloc(sizeof(int) * MAX_BUF_ELEMENTS);
54     inoutbuf = malloc(sizeof(int) * MAX_BUF_ELEMENTS);
55
56     for (count = 0; count < MAX_BUF_ELEMENTS; count > 0 ? count *= 2 : count++) {
57         for (i = 0; i < count; ++i) {
58             inbuf[i] = i;
59             inoutbuf[i] = i;
60         }
61         MPI_Reduce_local(inbuf, inoutbuf, count, MPI_INT, MPI_SUM);
62         for (i = 0; i < count; ++i)
63             if (inbuf[i] != i) {
64                 ++errs;
65                 if (inoutbuf[i] != (2 * i))
66                     ++errs;
67             }
68     }
69
70     /* make sure that user-define ops work too */
71     MPI_Op_create(&user_op, 0 /*!commute */ , &uop);
72     for (count = 0; count < MAX_BUF_ELEMENTS; count > 0 ? count *= 2 : count++) {
73         for (i = 0; i < count; ++i) {
74             inbuf[i] = i;
75             inoutbuf[i] = i;
76         }
77         MPI_Reduce_local(inbuf, inoutbuf, count, MPI_INT, uop);
78         errs += uop_errs;
79         for (i = 0; i < count; ++i)
80             if (inbuf[i] != i) {
81                 ++errs;
82                 if (inoutbuf[i] != (3 * i))
83                     ++errs;
84             }
85     }
86     MPI_Op_free(&uop);
87
88     free(inbuf);
89     free(inoutbuf);
90 #endif
91
92     MTest_Finalize(errs);
93     MPI_Finalize();
94     return 0;
95 }