Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
one more test for attr
[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 /*
11  * User-defined operation on a long value (tests proper handling of
12  * possible pipelining in the implementation of reductions with user-defined
13  * operations).
14  */
15 static void add(double *invec, double *inoutvec, int *len, MPI_Datatype *dtype)
16 {
17     int i, n = *len;
18     for (i = 0; i < n; i++) {
19         inoutvec[i] = invec[i] + inoutvec[i];
20     }
21 }
22
23 int main(int argc, char **argv)
24 {
25     MPI_Op op;
26     int i, rank, size, bufsize, errcnt = 0, toterr;
27     double *inbuf, *outbuf, value;
28
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);
33
34     bufsize = 1;
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);
40             errcnt++;
41             break;
42         }
43
44         value = (rank & 0x1) ? 1.0 : -1.0;
45         for (i = 0; i < bufsize; i++) {
46             inbuf[i] = value;
47             outbuf[i] = 100.0;
48         }
49         MPI_Allreduce(inbuf, outbuf, bufsize, MPI_DOUBLE, op, MPI_COMM_WORLD);
50         /* Check values */
51         value = (size & 0x1) ? -1.0 : 0.0;
52         for (i = 0; i < bufsize; i++) {
53             if (outbuf[i] != value) {
54                 if (errcnt < 10)
55                     printf("outbuf[%d] = %f, should = %f\n", i, outbuf[i], value);
56                 errcnt++;
57             }
58         }
59         free(inbuf);
60         free(outbuf);
61         bufsize *= 2;
62     }
63
64     MPI_Allreduce(&errcnt, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
65     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
66     if (rank == 0) {
67         if (toterr == 0)
68             printf(" No Errors\n");
69         else
70             printf("*! %d errors!\n", toterr);
71     }
72
73     MPI_Op_free(&op);
74     MPI_Finalize();
75     return 0;
76 }