Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Upgrade coll mpich testlist to new mpich
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / redscatinter.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2011 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 /*
7  * Test of reduce scatter with large data on an intercommunicator
8  * (needed in MPICH to trigger the 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 int main(int argc, char **argv)
23 {
24     int err = 0;
25     int *recvcounts;
26     int size, rsize, rank, i;
27     int recvcount,              /* Each process receives this much data */
28      sendcount,                 /* Each process contributes this much data */
29      basecount;                 /* Unit of elements - basecount *rsize is recvcount,
30                                  * etc. */
31     int isLeftGroup;
32     long long *sendbuf, *recvbuf;
33     long long sumval;
34     MPI_Comm comm;
35
36
37     MTest_Init(&argc, &argv);
38     comm = MPI_COMM_WORLD;
39
40     basecount = 1024;
41
42     while (MTestGetIntercomm(&comm, &isLeftGroup, 2)) {
43         if (comm == MPI_COMM_NULL)
44             continue;
45
46         MPI_Comm_remote_size(comm, &rsize);
47         MPI_Comm_size(comm, &size);
48         MPI_Comm_rank(comm, &rank);
49
50         if (0) {
51             printf("[%d] %s (%d,%d) remote %d\n", rank, isLeftGroup ? "L" : "R", rank, size, rsize);
52         }
53
54         recvcount = basecount * rsize;
55         sendcount = basecount * rsize * size;
56
57         recvcounts = (int *) malloc(size * sizeof(int));
58         if (!recvcounts) {
59             fprintf(stderr, "Could not allocate %d int for recvcounts\n", size);
60             MPI_Abort(MPI_COMM_WORLD, 1);
61         }
62         for (i = 0; i < size; i++)
63             recvcounts[i] = recvcount;
64
65         sendbuf = (long long *) malloc(sendcount * sizeof(long long));
66         if (!sendbuf) {
67             fprintf(stderr, "Could not allocate %d ints for sendbuf\n", sendcount);
68             MPI_Abort(MPI_COMM_WORLD, 1);
69         }
70
71         for (i = 0; i < sendcount; i++) {
72             sendbuf[i] = (long long) (rank * sendcount + i);
73         }
74         recvbuf = (long long *) malloc(recvcount * sizeof(long long));
75         if (!recvbuf) {
76             fprintf(stderr, "Could not allocate %d ints for recvbuf\n", recvcount);
77             MPI_Abort(MPI_COMM_WORLD, 1);
78         }
79         for (i = 0; i < recvcount; i++) {
80             recvbuf[i] = (long long) (-i);
81         }
82
83         MTest_Reduce_scatter(sendbuf, recvbuf, recvcounts, MPI_LONG_LONG, MPI_SUM, comm);
84
85         /* Check received data */
86         for (i = 0; i < recvcount; i++) {
87             sumval = (long long) (sendcount) * (long long) ((rsize * (rsize - 1)) / 2) +
88                 (long long) (i + rank * rsize * basecount) * (long long) rsize;
89             if (recvbuf[i] != sumval) {
90                 err++;
91                 if (err < 4) {
92                     fprintf(stdout, "Did not get expected value for reduce scatter\n");
93                     fprintf(stdout, "[%d] %s recvbuf[%d] = %lld, expected %lld\n",
94                             rank, isLeftGroup ? "L" : "R", i, recvbuf[i], sumval);
95                 }
96             }
97         }
98
99         free(sendbuf);
100         free(recvbuf);
101         free(recvcounts);
102
103         MTestFreeComm(&comm);
104     }
105
106     MTest_Finalize(err);
107
108     MPI_Finalize();
109
110     return 0;
111 }