Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change include order for smpi tests/examples to avoid conflicts
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / allred6.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test MPI_Allreduce with apparent non-commutative operators";
14 */
15 /* While the operator is in fact commutative, this forces the MPI code to
16    run the code that is used for non-commutative operators, and for 
17    various message lengths.  Other tests check truly non-commutative 
18    operators */
19
20 void mysum( void *cinPtr, void *coutPtr, int *count, MPI_Datatype *dtype );
21
22 void mysum( void *cinPtr, void *coutPtr, int *count, MPI_Datatype *dtype )
23 {
24     const int *cin = (const int *)cinPtr;
25     int       *cout = (int *)coutPtr;
26     int        i, n = *count;
27     for (i=0; i<n; i++) 
28         cout[i] += cin[i];
29 }
30 int main( int argc, char *argv[] )
31 {
32     int      errs = 0;
33     int      rank, size;
34     int      minsize = 2, count; 
35     MPI_Comm comm;
36     MPI_Op   op;
37     int      *buf, i;
38
39     MTest_Init( &argc, &argv );
40
41     MPI_Op_create( mysum, 0, &op );
42
43     while (MTestGetIntracommGeneral( &comm, minsize, 1 )) {
44         if (comm == MPI_COMM_NULL) continue;
45         MPI_Comm_size( comm, &size );
46         MPI_Comm_rank( comm, &rank );
47         
48         for (count = 1; count < 65000; count = count * 2) {
49             /* Contiguous data */
50             buf = (int *)malloc( count * sizeof(int) );
51             for (i=0; i<count; i++) buf[i] = rank + i;
52             MPI_Allreduce( MPI_IN_PLACE, buf, count, MPI_INT, op, comm );
53             /* Check the results */
54             for (i=0; i<count; i++) {
55                 int result = i * size + (size*(size-1))/2;
56                 if (buf[i] != result) {
57                     errs ++;
58                     if (errs < 10) {
59                         fprintf( stderr, "buf[%d] = %d expected %d\n",
60                                  i, buf[i], result );
61                     }
62                 }
63             }
64             free( buf );
65         }
66         MTestFreeComm( &comm );
67     }
68     MPI_Op_free( &op );
69
70 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
71     /* Check to make sure that aliasing is disallowed correctly */
72     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
73     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
74     if (MPI_SUCCESS == MPI_Allreduce(&rank, &rank, 1, MPI_INT, MPI_OP_NULL, MPI_COMM_WORLD))
75         errs++;
76 #endif
77
78     MTest_Finalize( errs );
79     MPI_Finalize();
80     return 0;
81 }