1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2009 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
7 * Test of reduce scatter block.
9 * Each process contributes its rank + the index to the reduction,
10 * then receives the ith sum
12 * Can be called with any number of processes.
20 int main(int argc, char **argv)
23 int toterr, size, rank, i, sumval;
28 MPI_Init(&argc, &argv);
29 comm = MPI_COMM_WORLD;
31 MPI_Comm_size(comm, &size);
32 MPI_Comm_rank(comm, &rank);
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) {
40 fprintf(stderr, "unable to allocate send/recv buffers, aborting");
41 MPI_Abort(MPI_COMM_WORLD, 1);
43 for (i = 0; i < size; i++)
44 sendbuf[i] = rank + i;
46 MPI_Reduce_scatter_block(sendbuf, recvbuf, 1, MPI_INT, MPI_SUM, comm);
48 sumval = size * rank + ((size - 1) * size) / 2;
49 if (recvbuf[0] != sumval) {
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);
57 /* let's try it again with MPI_IN_PLACE this time */
58 for (i = 0; i < size; i++)
59 recvbuf[i] = rank + i;
61 MPI_Reduce_scatter_block(MPI_IN_PLACE, recvbuf, 1, MPI_INT, MPI_SUM, comm);
63 sumval = size * rank + ((size - 1) * size) / 2;
64 if (recvbuf[0] != sumval) {
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);
72 MPI_Allreduce(&err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
73 if (rank == 0 && toterr == 0) {
74 printf(" No Errors\n");