Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Upgrade coll mpich testlist to new mpich
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / redscatbkinter.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 block 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
21 int main(int argc, char **argv)
22 {
23     int err = 0;
24     int size, rsize, rank, i;
25     int recvcount,              /* Each process receives this much data */
26      sendcount,                 /* Each process contributes this much data */
27      basecount;                 /* Unit of elements - basecount *rsize is recvcount,
28                                  * etc. */
29     int isLeftGroup;
30     long long *sendbuf, *recvbuf;
31     long long sumval;
32     MPI_Comm comm;
33
34
35     MTest_Init(&argc, &argv);
36     comm = MPI_COMM_WORLD;
37
38     basecount = 1024;
39
40     while (MTestGetIntercomm(&comm, &isLeftGroup, 2)) {
41         if (comm == MPI_COMM_NULL)
42             continue;
43
44         MPI_Comm_remote_size(comm, &rsize);
45         MPI_Comm_size(comm, &size);
46         MPI_Comm_rank(comm, &rank);
47
48         if (0) {
49             printf("[%d] %s (%d,%d) remote %d\n", rank, isLeftGroup ? "L" : "R", rank, size, rsize);
50         }
51
52         recvcount = basecount * rsize;
53         sendcount = basecount * rsize * size;
54
55         sendbuf = (long long *) malloc(sendcount * sizeof(long long));
56         if (!sendbuf) {
57             fprintf(stderr, "Could not allocate %d ints for sendbuf\n", sendcount);
58             MPI_Abort(MPI_COMM_WORLD, 1);
59         }
60
61         for (i = 0; i < sendcount; i++) {
62             sendbuf[i] = (long long) (rank * sendcount + i);
63         }
64         recvbuf = (long long *) malloc(recvcount * sizeof(long long));
65         if (!recvbuf) {
66             fprintf(stderr, "Could not allocate %d ints for recvbuf\n", recvcount);
67             MPI_Abort(MPI_COMM_WORLD, 1);
68         }
69         for (i = 0; i < recvcount; i++) {
70             recvbuf[i] = (long long) (-i);
71         }
72
73         MPI_Reduce_scatter_block(sendbuf, recvbuf, recvcount, MPI_LONG_LONG, MPI_SUM, comm);
74
75         /* Check received data */
76         for (i = 0; i < recvcount; i++) {
77             sumval = (long long) (sendcount) * (long long) ((rsize * (rsize - 1)) / 2) +
78                 (long long) (i + rank * rsize * basecount) * (long long) rsize;
79             if (recvbuf[i] != sumval) {
80                 err++;
81                 if (err < 4) {
82                     fprintf(stdout, "Did not get expected value for reduce scatter\n");
83                     fprintf(stdout, "[%d] %s recvbuf[%d] = %lld, expected %lld\n",
84                             rank, isLeftGroup ? "L" : "R", i, recvbuf[i], sumval);
85                 }
86             }
87         }
88
89         free(sendbuf);
90         free(recvbuf);
91
92         MTestFreeComm(&comm);
93     }
94
95     MTest_Finalize(err);
96
97     MPI_Finalize();
98
99     return 0;
100 }