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 / scantst.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 "mpitest.h"
9
10 void addem ( int *, int *, int *, MPI_Datatype * );
11 void assoc ( int *, int *, int *, MPI_Datatype * );
12
13 void addem( int *invec, int *inoutvec, int *len, MPI_Datatype *dtype)
14 {
15   int i;
16   for ( i=0; i<*len; i++ ) 
17     inoutvec[i] += invec[i];
18 }
19
20 #define BAD_ANSWER 100000
21
22 /*
23     The operation is inoutvec[i] = invec[i] op inoutvec[i] 
24     (see 4.9.4).  The order is important.
25
26     Note that the computation is in process rank (in the communicator)
27     order, independant of the root.
28  */
29 void assoc( int *invec, int *inoutvec, int *len, MPI_Datatype *dtype)
30 {
31   int i;
32   for ( i=0; i<*len; i++ )  {
33     if (inoutvec[i] <= invec[i] ) {
34       int rank;
35       MPI_Comm_rank( MPI_COMM_WORLD, &rank );
36       fprintf( stderr, "[%d] inout[0] = %d, in[0] = %d\n", 
37               rank, inoutvec[0], invec[0] );
38       inoutvec[i] = BAD_ANSWER;
39       }
40     else 
41       inoutvec[i] = invec[i];
42   }
43 }
44
45 int main( int argc, char **argv )
46 {
47     int              rank, size, i;
48     int              data;
49     int              errors=0;
50     int              result = -100;
51     int              correct_result;
52     MPI_Op           op_assoc, op_addem;
53     MPI_Comm comm=MPI_COMM_WORLD;
54     
55     MPI_Init( &argc, &argv );
56     MPI_Op_create( (MPI_User_function *)assoc, 0, &op_assoc );
57     MPI_Op_create( (MPI_User_function *)addem, 1, &op_addem );
58
59     /* Run this for a variety of communicator sizes */
60
61     MPI_Comm_rank( comm, &rank );
62     MPI_Comm_size( comm, &size );
63
64     data = rank;
65         
66     correct_result = 0;
67     for (i=0;i<=rank;i++)
68         correct_result += i;
69     
70     MPI_Scan ( &data, &result, 1, MPI_INT, MPI_SUM, comm );
71     if (result != correct_result) {
72         fprintf( stderr, "[%d] Error suming ints with scan\n", rank );
73         errors++;
74     }
75
76     MPI_Scan ( &data, &result, 1, MPI_INT, MPI_SUM, comm );
77     if (result != correct_result) {
78         fprintf( stderr, "[%d] Error summing ints with scan (2)\n", rank );
79         errors++;
80     }
81     
82     data = rank;
83     result = -100;
84     MPI_Scan ( &data, &result, 1, MPI_INT, op_addem, comm );
85     if (result != correct_result) {
86         fprintf( stderr, "[%d] Error summing ints with scan (userop)\n", 
87                  rank );
88         errors++;
89     }
90     
91     MPI_Scan ( &data, &result, 1, MPI_INT, op_addem, comm );
92     if (result != correct_result) {
93         fprintf( stderr, "[%d] Error summing ints with scan (userop2)\n", 
94                  rank );
95         errors++;
96     }
97     result = -100;
98     data = rank;
99     MPI_Scan ( &data, &result, 1, MPI_INT, op_assoc, comm );
100     if (result == BAD_ANSWER) {
101         fprintf( stderr, "[%d] Error scanning with non-commutative op\n",
102                  rank );
103         errors++;
104     }
105
106 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
107     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
108     if (MPI_SUCCESS == MPI_Scan( &data, &data, 1, MPI_INT, op_assoc, comm))
109         errors++;
110 #endif
111
112     MPI_Op_free( &op_assoc );
113     MPI_Op_free( &op_addem );
114     
115     MPI_Finalize();
116     if (errors)
117         printf( "[%d] done with ERRORS(%d)!\n", rank, errors );
118     else {
119         if (rank == 0) 
120             printf(" No Errors\n");
121     }
122
123     return errors;
124 }