1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
11 * User-defined operation on a long value (tests proper handling of
12 * possible pipelining in the implementation of reductions with user-defined
15 static void add(double *invec, double *inoutvec, int *len, MPI_Datatype *dtype)
18 for (i = 0; i < n; i++) {
19 inoutvec[i] = invec[i] + inoutvec[i];
23 int main(int argc, char **argv)
26 int i, rank, size, bufsize, errcnt = 0, toterr;
27 double *inbuf, *outbuf, value;
29 MPI_Init(&argc, &argv);
30 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
31 MPI_Comm_size(MPI_COMM_WORLD, &size);
32 MPI_Op_create((MPI_User_function *) add, 1, &op);
35 while (bufsize < 100000) {
36 inbuf = (double *) malloc(bufsize * sizeof(double));
37 outbuf = (double *) malloc(bufsize * sizeof(double));
38 if (!inbuf || !outbuf) {
39 fprintf(stderr, "Could not allocate buffers for size %d\n", bufsize);
44 value = (rank & 0x1) ? 1.0 : -1.0;
45 for (i = 0; i < bufsize; i++) {
49 MPI_Allreduce(inbuf, outbuf, bufsize, MPI_DOUBLE, op, MPI_COMM_WORLD);
51 value = (size & 0x1) ? -1.0 : 0.0;
52 for (i = 0; i < bufsize; i++) {
53 if (outbuf[i] != value) {
55 printf("outbuf[%d] = %f, should = %f\n", i, outbuf[i], value);
64 MPI_Allreduce(&errcnt, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
65 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
68 printf(" No Errors\n");
70 printf("*! %d errors!\n", toterr);