Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_Abort can theorically fail. Add a call to exit() to ensure that the program...
[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 1200000
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;
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 allocated %d words for data\n", 
66                      3 * count );
67             MPI_Abort( MPI_COMM_WORLD, 1 );
68             exit(1);
69         }
70         for (i=0; i<count*3; i++) {
71             outVal[i] = -1;
72             inVal[i]  = 1 + (i & 0x3);
73         }
74         MPI_Reduce( inVal, outVal, count, tripleType, op, 0, MPI_COMM_WORLD );
75         /* Check Result values */
76         if (wrank == 0) {
77             for (i=0; i<3*count; i+=3) {
78                 sumval = wsize * (1 + (i & 0x3));
79                 maxval = 1 + ((i+1) & 0x3);
80                 if (outVal[i] != sumval) {
81                     if (errs < MAX_ERRS) 
82                         fprintf( stderr, "%d: outval[%d] = %f, expected %f (sum)\n", 
83                                  count, i, outVal[i], sumval );
84                     errs++;
85                 }
86                 if (outVal[i+1] != maxval) {
87                     if (errs < MAX_ERRS) 
88                         fprintf( stderr, "%d: outval[%d] = %f, expected %f (max)\n", 
89                                  count, i+1, outVal[i+1], maxval );
90                     errs++;
91                 }
92                 if (outVal[i+2] != 1 + ((i+2)&0x3)) {
93                     if (errs < MAX_ERRS) 
94                         fprintf( stderr, "%d: outval[%d] = %f, expected %f (min)\n", 
95                                  count, i+2, outVal[i+2], (double)(1 + ((i+2)^0x3)) );
96                     errs++;
97                 }
98             }
99         }
100
101         free( inVal );
102         free( outVal );
103     }
104     
105     MPI_Op_free( &op );
106     MPI_Type_free( &tripleType );
107     MTest_Finalize( errs );
108     MPI_Finalize( );
109     return 0;
110 }