Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change include order for smpi tests/examples to avoid conflicts
[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 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
44     int i;
45     int *inbuf = NULL;
46     int *inoutbuf = NULL;
47     int count = -1;
48     MPI_Op uop = MPI_OP_NULL;
49 #endif
50
51     MTest_Init(&argc, &argv);
52 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
53     /* this function was added in MPI-2.2 */
54
55     inbuf = malloc(sizeof(int) * MAX_BUF_ELEMENTS);
56     inoutbuf = malloc(sizeof(int) * MAX_BUF_ELEMENTS);
57
58     for (count = 0; count < MAX_BUF_ELEMENTS; count > 0 ? count*=2 : count++) {
59         for (i = 0; i < count; ++i) {
60             inbuf[i] = i;
61             inoutbuf[i] = i;
62         }
63         MPI_Reduce_local(inbuf, inoutbuf, count, MPI_INT, MPI_SUM);
64         for (i = 0; i < count; ++i)
65             if (inbuf[i] != i) {
66                 ++errs;
67             if (inoutbuf[i] != (2*i))
68                 ++errs;
69         }
70     }
71
72     /* make sure that user-define ops work too */
73     MPI_Op_create(&user_op, 0/*!commute*/, &uop);
74     for (count = 0; count < MAX_BUF_ELEMENTS; count > 0 ? count*=2 : count++) {
75         for (i = 0; i < count; ++i) {
76             inbuf[i] = i;
77             inoutbuf[i] = i;
78         }
79         MPI_Reduce_local(inbuf, inoutbuf, count, MPI_INT, uop);
80         errs += uop_errs;
81         for (i = 0; i < count; ++i)
82             if (inbuf[i] != i) {
83                 ++errs;
84             if (inoutbuf[i] != (3*i))
85                 ++errs;
86         }
87     }
88     MPI_Op_free(&uop);
89
90     free(inbuf);
91     free(inoutbuf);
92 #endif
93
94     MTest_Finalize(errs);
95     MPI_Finalize();
96     return 0;
97 }
98