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 * Checks that non-commutative operations are not commuted and that
10 * all of the operations are performed.
12 * Can be called with any number of processors.
23 void left(void *a, void *b, int *count, MPI_Datatype * type);
24 void left(void *a, void *b, int *count, MPI_Datatype * type)
30 for (i = 0; i < *count; ++i) {
37 /* right(x,y) ==> y */
38 void right(void *a, void *b, int *count, MPI_Datatype * type);
39 void right(void *a, void *b, int *count, MPI_Datatype * type)
45 for (i = 0; i < *count; ++i) {
52 /* Just performs a simple sum but can be marked as non-commutative to
53 potentially trigger different logic in the implementation. */
54 void nc_sum(void *a, void *b, int *count, MPI_Datatype * type);
55 void nc_sum(void *a, void *b, int *count, MPI_Datatype * type)
61 for (i = 0; i < *count; ++i) {
62 inout[i] = in[i] + inout[i];
66 #define MAX_BLOCK_SIZE 256
68 int main(int argc, char **argv)
75 MPI_Op left_op, right_op, nc_sum_op;
77 MTest_Init(&argc, &argv);
78 comm = MPI_COMM_WORLD;
80 MPI_Comm_size(comm, &size);
81 MPI_Comm_rank(comm, &rank);
83 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
84 /* MPI_Reduce_scatter block was added in MPI-2.2 */
86 MPI_Op_create(&left, 0 /*non-commutative */ , &left_op);
87 MPI_Op_create(&right, 0 /*non-commutative */ , &right_op);
88 MPI_Op_create(&nc_sum, 0 /*non-commutative */ , &nc_sum_op);
90 for (block_size = 1; block_size < MAX_BLOCK_SIZE; block_size *= 2) {
91 sendbuf = (int *) malloc(block_size * size * sizeof(int));
92 recvbuf = malloc(block_size * sizeof(int));
94 for (i = 0; i < (size * block_size); i++)
95 sendbuf[i] = rank + i;
96 for (i = 0; i < block_size; i++)
97 recvbuf[i] = 0xdeadbeef;
99 MPI_Reduce_scatter_block(sendbuf, recvbuf, block_size, MPI_INT, left_op, comm);
100 for (i = 0; i < block_size; ++i)
101 if (recvbuf[i] != (rank * block_size + i))
104 MPI_Reduce_scatter_block(sendbuf, recvbuf, block_size, MPI_INT, right_op, comm);
105 for (i = 0; i < block_size; ++i)
106 if (recvbuf[i] != ((size - 1) + (rank * block_size) + i))
109 MPI_Reduce_scatter_block(sendbuf, recvbuf, block_size, MPI_INT, nc_sum_op, comm);
110 for (i = 0; i < block_size; ++i) {
111 int x = rank * block_size + i;
112 if (recvbuf[i] != (size * x + (size - 1) * size / 2))
120 MPI_Op_free(&left_op);
121 MPI_Op_free(&right_op);
122 MPI_Op_free(&nc_sum_op);
125 MTest_Finalize(errs);