Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change include order for smpi tests/examples to avoid conflicts
[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;
24 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
25     int i, sumval;
26     int *sendbuf;
27     int *recvbuf;
28 #endif
29     MPI_Comm comm;
30
31     MPI_Init(&argc, &argv);
32     comm = MPI_COMM_WORLD;
33
34     MPI_Comm_size(comm, &size);
35     MPI_Comm_rank(comm, &rank);
36
37 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
38     /* MPI_Reduce_scatter block was added in MPI-2.2 */
39     sendbuf = (int *) malloc(size * sizeof(int));
40     recvbuf = (int *) malloc(size * sizeof(int));
41     if (!sendbuf || !recvbuf) {
42         err++;
43         fprintf(stderr, "unable to allocate send/recv buffers, aborting");
44         MPI_Abort(MPI_COMM_WORLD, 1);
45         exit(1);
46     }
47     for (i=0; i<size; i++)
48         sendbuf[i] = rank + i;
49
50     MPI_Reduce_scatter_block(sendbuf, recvbuf, 1, MPI_INT, MPI_SUM, comm);
51
52     sumval = size * rank + ((size - 1) * size)/2;
53     if (recvbuf[0] != sumval) {
54         err++;
55         fprintf(stdout, "Did not get expected value for reduce scatter block\n");
56         fprintf(stdout, "[%d] Got %d expected %d\n", rank, recvbuf[0], sumval);
57     }
58
59     free(sendbuf);
60
61     /* let's try it again with MPI_IN_PLACE this time */
62     for (i=0; i<size; i++)
63         recvbuf[i] = rank + i;
64
65     MPI_Reduce_scatter_block(MPI_IN_PLACE, recvbuf, 1, MPI_INT, MPI_SUM, comm);
66
67     sumval = size * rank + ((size - 1) * size)/2;
68     if (recvbuf[0] != sumval) {
69         err++;
70         fprintf(stdout, "Did not get expected value for reduce scatter block\n");
71         fprintf(stdout, "[%d] Got %d expected %d\n", rank, recvbuf[0], sumval);
72     }
73
74     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
75     if (MPI_SUCCESS == MPI_Reduce_scatter_block(recvbuf, recvbuf, 1, MPI_INT, MPI_SUM, comm))
76         err++;
77
78     free(recvbuf);
79 #endif
80
81     MPI_Allreduce(&err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
82     if (rank == 0 && toterr == 0) {
83         printf(" No Errors\n");
84     }
85     MPI_Finalize();
86
87     return toterr;
88 }