Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / redscatblk3.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
21 int main(int argc, char **argv)
22 {
23     int err = 0;
24     int *sendbuf, *recvbuf;
25     int size, rank, i, j, idx, mycount, sumval;
26     MPI_Comm comm;
27
28
29     MTest_Init(&argc, &argv);
30     comm = MPI_COMM_WORLD;
31
32     MPI_Comm_size(comm, &size);
33     MPI_Comm_rank(comm, &rank);
34     mycount = (1024 * 1024) / size;
35
36     sendbuf = (int *) malloc(mycount * size * sizeof(int));
37     if (!sendbuf) {
38         fprintf(stderr, "Could not allocate %d ints for sendbuf\n", mycount * size);
39         MPI_Abort(MPI_COMM_WORLD, 1);
40     }
41     idx = 0;
42     for (i = 0; i < size; i++) {
43         for (j = 0; j < mycount; j++) {
44             sendbuf[idx++] = rank + i;
45         }
46     }
47     recvbuf = (int *) malloc(mycount * sizeof(int));
48     if (!recvbuf) {
49         fprintf(stderr, "Could not allocate %d ints for recvbuf\n", mycount);
50         MPI_Abort(MPI_COMM_WORLD, 1);
51     }
52
53     MPI_Reduce_scatter_block(sendbuf, recvbuf, mycount, MPI_INT, MPI_SUM, comm);
54
55     sumval = size * rank + ((size - 1) * size) / 2;
56     /* recvbuf should be size * (rank + i) */
57     for (i = 0; i < mycount; i++) {
58         if (recvbuf[i] != sumval) {
59             err++;
60             fprintf(stdout, "Did not get expected value for reduce scatter\n");
61             fprintf(stdout, "[%d] Got %d expected %d\n", rank, recvbuf[i], sumval);
62         }
63     }
64
65     MPI_Reduce_scatter_block(MPI_IN_PLACE, sendbuf, mycount, MPI_INT, MPI_SUM, comm);
66
67     sumval = size * rank + ((size - 1) * size) / 2;
68     /* recv'ed values for my process should be size * (rank + i) */
69     for (i = 0; i < mycount; i++) {
70         if (sendbuf[i] != sumval) {
71             err++;
72             fprintf(stdout, "Did not get expected value for reduce scatter (in place)\n");
73             fprintf(stdout, "[%d] Got %d expected %d\n", rank, sendbuf[i], sumval);
74         }
75     }
76
77     free(sendbuf);
78     free(recvbuf);
79
80     MTest_Finalize(err);
81
82     MPI_Finalize();
83
84     return 0;
85 }