Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / red_scat_block.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2009 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 /*
7  * Test of reduce scatter block.
8  *
9  * Each process contributes its rank + the index to the reduction,
10  * then receives the ith sum
11  *
12  * Can be called with any number of processes.
13  */
14
15 #include "mpi.h"
16 #include "mpitest.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 int main(int argc, char **argv)
21 {
22     int err = 0;
23     int toterr, size, rank, i, sumval;
24     int *sendbuf;
25     int *recvbuf;
26     MPI_Comm comm;
27
28     MPI_Init(&argc, &argv);
29     comm = MPI_COMM_WORLD;
30
31     MPI_Comm_size(comm, &size);
32     MPI_Comm_rank(comm, &rank);
33
34 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
35     /* MPI_Reduce_scatter block was added in MPI-2.2 */
36     sendbuf = (int *) malloc(size * sizeof(int));
37     recvbuf = (int *) malloc(size * sizeof(int));
38     if (!sendbuf || !recvbuf) {
39         err++;
40         fprintf(stderr, "unable to allocate send/recv buffers, aborting");
41         MPI_Abort(MPI_COMM_WORLD, 1);
42     }
43     for (i=0; i<size; i++)
44         sendbuf[i] = rank + i;
45
46     MPI_Reduce_scatter_block(sendbuf, recvbuf, 1, MPI_INT, MPI_SUM, comm);
47
48     sumval = size * rank + ((size - 1) * size)/2;
49     if (recvbuf[0] != sumval) {
50         err++;
51         fprintf(stdout, "Did not get expected value for reduce scatter block\n");
52         fprintf(stdout, "[%d] Got %d expected %d\n", rank, recvbuf[0], sumval);
53     }
54
55     free(sendbuf);
56
57     /* let's try it again with MPI_IN_PLACE this time */
58     for (i=0; i<size; i++)
59         recvbuf[i] = rank + i;
60
61     MPI_Reduce_scatter_block(MPI_IN_PLACE, recvbuf, 1, MPI_INT, MPI_SUM, comm);
62
63     sumval = size * rank + ((size - 1) * size)/2;
64     if (recvbuf[0] != sumval) {
65         err++;
66         fprintf(stdout, "Did not get expected value for reduce scatter block\n");
67         fprintf(stdout, "[%d] Got %d expected %d\n", rank, recvbuf[0], sumval);
68     }
69     free(recvbuf);
70 #endif
71
72     MPI_Allreduce(&err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
73     if (rank == 0 && toterr == 0) {
74         printf(" No Errors\n");
75     }
76     MPI_Finalize();
77
78     return toterr;
79 }