Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / uoplong.c
1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
2 /*
3  *  (C) 2012 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 #include "mpi.h"
8 #include "mpitest.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 /*
13  * Test user-defined operations with a large number of elements.
14  * Added because a talk at EuroMPI'12 claimed that these failed with
15  * more than 64k elements
16  */
17
18 #define MAX_ERRS 10
19 #define MAX_COUNT 120000
20
21 void myop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype);
22
23 /*
24  * myop takes a datatype that is a triple of doubles, and computes
25  * the sum, max, min of the respective elements of the triple.
26  */
27 void myop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype)
28 {
29     int i, n = *count;
30     double const *cin = (double *) cinPtr;
31     double *cout = (double *) coutPtr;
32
33     for (i = 0; i < n; i++) {
34         cout[0] += cin[0];
35         cout[1] = (cout[1] > cin[1]) ? cout[1] : cin[1];
36         cout[2] = (cout[2] < cin[2]) ? cout[2] : cin[2];
37         cin += 3;
38         cout += 3;
39     }
40 }
41
42 int main(int argc, char *argv[])
43 {
44     int errs = 0;
45     int wsize, wrank, i, count;
46     MPI_Datatype tripleType;
47     double *inVal, *outVal;
48     double maxval, sumval, minval;
49     MPI_Op op;
50
51     MTest_Init(&argc, &argv);
52     MPI_Op_create(myop, 0, &op);
53     MPI_Type_contiguous(3, MPI_DOUBLE, &tripleType);
54     MPI_Type_commit(&tripleType);
55
56     MPI_Comm_size(MPI_COMM_WORLD, &wsize);
57     MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
58
59     for (count = 1; count < MAX_COUNT; count += count) {
60         if (wrank == 0)
61             MTestPrintfMsg(1, "Count = %d\n", count);
62         inVal = (double *) malloc(3 * count * sizeof(double));
63         outVal = (double *) malloc(3 * count * sizeof(double));
64         if (!inVal || !outVal) {
65             fprintf(stderr, "Unable to allocate %d words for data\n", 3 * count);
66             MPI_Abort(MPI_COMM_WORLD, 1);
67         }
68         for (i = 0; i < count * 3; i++) {
69             outVal[i] = -1;
70             inVal[i] = 1 + (i & 0x3);
71         }
72         MPI_Reduce(inVal, outVal, count, tripleType, op, 0, MPI_COMM_WORLD);
73         /* Check Result values */
74         if (wrank == 0) {
75             for (i = 0; i < 3 * count; i += 3) {
76                 sumval = wsize * (1 + (i & 0x3));
77                 maxval = 1 + ((i + 1) & 0x3);
78                 minval = 1 + ((i + 2) & 0x3);
79                 if (outVal[i] != sumval) {
80                     if (errs < MAX_ERRS)
81                         fprintf(stderr, "%d: outval[%d] = %f, expected %f (sum)\n",
82                                 count, i, outVal[i], sumval);
83                     errs++;
84                 }
85                 if (outVal[i + 1] != maxval) {
86                     if (errs < MAX_ERRS)
87                         fprintf(stderr, "%d: outval[%d] = %f, expected %f (max)\n",
88                                 count, i + 1, outVal[i + 1], maxval);
89                     errs++;
90                 }
91                 if (outVal[i + 2] != minval) {
92                     if (errs < MAX_ERRS)
93                         fprintf(stderr, "%d: outval[%d] = %f, expected %f (min)\n",
94                                 count, i + 2, outVal[i + 2], minval);
95                     errs++;
96                 }
97             }
98         }
99
100         free(inVal);
101         free(outVal);
102     }
103
104     MPI_Op_free(&op);
105     MPI_Type_free(&tripleType);
106     MTest_Finalize(errs);
107     MPI_Finalize();
108     return 0;
109 }