Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / longuser.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 int add(double *, double *, int *, MPI_Datatype *);
11 /*
12  * User-defined operation on a long value (tests proper handling of
13  * possible pipelining in the implementation of reductions with user-defined
14  * operations).
15  */
16 int add(double *invec, double *inoutvec, int *len, MPI_Datatype * dtype)
17 {
18     int i, n = *len;
19     for (i = 0; i < n; i++) {
20         inoutvec[i] = invec[i] + inoutvec[i];
21     }
22     return 0;
23 }
24
25 int main(int argc, char **argv)
26 {
27     MPI_Op op;
28     int i, rank, size, bufsize, errcnt = 0, toterr;
29     double *inbuf, *outbuf, value;
30
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);
35
36     bufsize = 1;
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);
42             errcnt++;
43             break;
44         }
45
46         value = (rank & 0x1) ? 1.0 : -1.0;
47         for (i = 0; i < bufsize; i++) {
48             inbuf[i] = value;
49             outbuf[i] = 100.0;
50         }
51         MPI_Allreduce(inbuf, outbuf, bufsize, MPI_DOUBLE, op, MPI_COMM_WORLD);
52         /* Check values */
53         value = (size & 0x1) ? -1.0 : 0.0;
54         for (i = 0; i < bufsize; i++) {
55             if (outbuf[i] != value) {
56                 if (errcnt < 10)
57                     printf("outbuf[%d] = %f, should = %f\n", i, outbuf[i], value);
58                 errcnt++;
59             }
60         }
61         free(inbuf);
62         free(outbuf);
63         bufsize *= 2;
64     }
65
66     MPI_Allreduce(&errcnt, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
67     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
68     if (rank == 0) {
69         if (toterr == 0)
70             printf(" No Errors\n");
71         else
72             printf("*! %d errors!\n", toterr);
73     }
74
75     MPI_Op_free(&op);
76     MPI_Finalize();
77     return 0;
78 }