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.
10 int add(double *, double *, int *, MPI_Datatype *);
12 * User-defined operation on a long value (tests proper handling of
13 * possible pipelining in the implementation of reductions with user-defined
16 int add(double *invec, double *inoutvec, int *len, MPI_Datatype * dtype)
19 for (i = 0; i < n; i++) {
20 inoutvec[i] = invec[i] + inoutvec[i];
25 int main(int argc, char **argv)
28 int i, rank, size, bufsize, errcnt = 0, toterr;
29 double *inbuf, *outbuf, value;
31 MPI_Init(&argc, &argv);
32 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
33 MPI_Comm_size(MPI_COMM_WORLD, &size);
34 MPI_Op_create((MPI_User_function *) add, 1, &op);
37 while (bufsize < 100000) {
38 inbuf = (double *) malloc(bufsize * sizeof(double));
39 outbuf = (double *) malloc(bufsize * sizeof(double));
40 if (!inbuf || !outbuf) {
41 fprintf(stderr, "Could not allocate buffers for size %d\n", bufsize);
46 value = (rank & 0x1) ? 1.0 : -1.0;
47 for (i = 0; i < bufsize; i++) {
51 MPI_Allreduce(inbuf, outbuf, bufsize, MPI_DOUBLE, op, MPI_COMM_WORLD);
53 value = (size & 0x1) ? -1.0 : 0.0;
54 for (i = 0; i < bufsize; i++) {
55 if (outbuf[i] != value) {
57 printf("outbuf[%d] = %f, should = %f\n", i, outbuf[i], value);
66 MPI_Allreduce(&errcnt, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
67 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
70 printf(" No Errors\n");
72 printf("*! %d errors!\n", toterr);