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;
24 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
31 MPI_Init(&argc, &argv);
32 comm = MPI_COMM_WORLD;
34 MPI_Comm_size(comm, &size);
35 MPI_Comm_rank(comm, &rank);
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) {
43 fprintf(stderr, "unable to allocate send/recv buffers, aborting");
44 MPI_Abort(MPI_COMM_WORLD, 1);
47 for (i=0; i<size; i++)
48 sendbuf[i] = rank + i;
50 MPI_Reduce_scatter_block(sendbuf, recvbuf, 1, MPI_INT, MPI_SUM, comm);
52 sumval = size * rank + ((size - 1) * size)/2;
53 if (recvbuf[0] != sumval) {
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);
61 /* let's try it again with MPI_IN_PLACE this time */
62 for (i=0; i<size; i++)
63 recvbuf[i] = rank + i;
65 MPI_Reduce_scatter_block(MPI_IN_PLACE, recvbuf, 1, MPI_INT, MPI_SUM, comm);
67 sumval = size * rank + ((size - 1) * size)/2;
68 if (recvbuf[0] != sumval) {
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);
76 MPI_Allreduce(&err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
77 if (rank == 0 && toterr == 0) {
78 printf(" No Errors\n");