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 / redscat3.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2010 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 /*
7  * Test of reduce scatter with large data (needed in MPICH to trigger the
8  * long-data algorithm)
9  *
10  * Each processor contributes its rank + the index to the reduction,
11  * then receives the ith sum
12  *
13  * Can be called with any number of processors.
14  */
15
16 #include "mpi.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include "mpitest.h"
20 #include "mpicolltest.h"
21
22 /* Limit the number of error reports */
23 #define MAX_ERRORS 10
24
25 int main(int argc, char **argv)
26 {
27     int err = 0;
28     int *sendbuf, *recvbuf, *recvcounts;
29     int size, rank, i, j, idx, mycount, sumval;
30     MPI_Comm comm;
31
32
33     MTest_Init(&argc, &argv);
34     comm = MPI_COMM_WORLD;
35
36     MPI_Comm_size(comm, &size);
37     MPI_Comm_rank(comm, &rank);
38     recvcounts = (int *) malloc(size * sizeof(int));
39     if (!recvcounts) {
40         fprintf(stderr, "Could not allocate %d ints for recvcounts\n", size);
41         MPI_Abort(MPI_COMM_WORLD, 1);
42     }
43     mycount = (1024 * 1024) / size;
44     for (i = 0; i < size; i++)
45         recvcounts[i] = mycount;
46     sendbuf = (int *) malloc(mycount * size * sizeof(int));
47     if (!sendbuf) {
48         fprintf(stderr, "Could not allocate %d ints for sendbuf\n", mycount * size);
49         MPI_Abort(MPI_COMM_WORLD, 1);
50     }
51     idx = 0;
52     for (i = 0; i < size; i++) {
53         for (j = 0; j < mycount; j++) {
54             sendbuf[idx++] = rank + i;
55         }
56     }
57     recvbuf = (int *) malloc(mycount * sizeof(int));
58     if (!recvbuf) {
59         fprintf(stderr, "Could not allocate %d ints for recvbuf\n", mycount);
60         MPI_Abort(MPI_COMM_WORLD, 1);
61     }
62     for (i = 0; i < mycount; i++) {
63         recvbuf[i] = -1;
64     }
65
66     MTest_Reduce_scatter(sendbuf, recvbuf, recvcounts, MPI_INT, MPI_SUM, comm);
67
68     sumval = size * rank + ((size - 1) * size) / 2;
69     /* recvbuf should be size * (rank + i) */
70     for (i = 0; i < mycount; i++) {
71         if (recvbuf[i] != sumval) {
72             err++;
73             if (err < MAX_ERRORS) {
74                 fprintf(stdout, "Did not get expected value for reduce scatter\n");
75                 fprintf(stdout, "[%d] Got recvbuf[%d] = %d expected %d\n",
76                         rank, i, recvbuf[i], sumval);
77             }
78         }
79     }
80
81 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
82     MTest_Reduce_scatter(MPI_IN_PLACE, sendbuf, recvcounts, MPI_INT, MPI_SUM, comm);
83
84     sumval = size * rank + ((size - 1) * size) / 2;
85     /* recv'ed values for my process should be size * (rank + i) */
86     for (i = 0; i < mycount; i++) {
87         if (sendbuf[i] != sumval) {
88             err++;
89             if (err < MAX_ERRORS) {
90                 fprintf(stdout, "Did not get expected value for reduce scatter (in place)\n");
91                 fprintf(stdout, "[%d] Got buf[%d] = %d expected %d\n", rank, i, sendbuf[i], sumval);
92             }
93         }
94     }
95 #endif
96
97     free(sendbuf);
98     free(recvbuf);
99     free(recvcounts);
100
101     MTest_Finalize(err);
102
103     MPI_Finalize();
104
105     return 0;
106 }