Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / allred6.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 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[] = "Test MPI_Allreduce with apparent non-commutative operators";
14 */
15 /* While the operator is in fact commutative, this forces the MPI code to
16    run the code that is used for non-commutative operators, and for
17    various message lengths.  Other tests check truly non-commutative
18    operators */
19
20 void mysum(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype);
21
22 void mysum(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype)
23 {
24     const int *cin = (const int *) cinPtr;
25     int *cout = (int *) coutPtr;
26     int i, n = *count;
27     for (i = 0; i < n; i++)
28         cout[i] += cin[i];
29 }
30
31 int main(int argc, char *argv[])
32 {
33     int errs = 0;
34     int rank, size;
35     int minsize = 2, count;
36     MPI_Comm comm;
37     MPI_Op op;
38     int *buf, i;
39
40     MTest_Init(&argc, &argv);
41
42     MPI_Op_create(mysum, 0, &op);
43
44     while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
45         if (comm == MPI_COMM_NULL)
46             continue;
47         MPI_Comm_size(comm, &size);
48         MPI_Comm_rank(comm, &rank);
49
50         for (count = 1; count < 65000; count = count * 2) {
51             /* Contiguous data */
52             buf = (int *) malloc(count * sizeof(int));
53             for (i = 0; i < count; i++)
54                 buf[i] = rank + i;
55             MPI_Allreduce(MPI_IN_PLACE, buf, count, MPI_INT, op, comm);
56             /* Check the results */
57             for (i = 0; i < count; i++) {
58                 int result = i * size + (size * (size - 1)) / 2;
59                 if (buf[i] != result) {
60                     errs++;
61                     if (errs < 10) {
62                         fprintf(stderr, "buf[%d] = %d expected %d\n", i, buf[i], result);
63                     }
64                 }
65             }
66             free(buf);
67         }
68         MTestFreeComm(&comm);
69     }
70     MPI_Op_free(&op);
71
72     MTest_Finalize(errs);
73     MPI_Finalize();
74     return 0;
75 }