Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich-test / env / gtime.c
1 #include <stdio.h>
2 #include "mpi.h"
3 #include "test.h"
4 #include <math.h>
5
6 /* # define MPI_Wtime PMPI_Wtime */
7
8 /*
9  * This program tests that if MPI_WTIME_IS_GLOBAL is set, the timer
10  * IS in fact global.  We have some suspicions about certain vendor systems
11  */
12
13 int CheckTime( void );
14
15 /*
16  * Check time tests that the timers are synchronized 
17  */
18 int CheckTime( void )
19 {
20     int        rank, size, i;
21     double     wtick, t1, t2, t3, delta_t;
22     int        ntest=20;
23     MPI_Status status;
24     int        err = 0;
25     double     max_diff = 0.0;
26
27     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
28     MPI_Comm_size( MPI_COMM_WORLD, &size );
29
30     if (rank == 0) {
31         wtick = MPI_Wtick();
32 #ifdef DEBUG
33         printf( "Wtick is %lf\n", wtick );
34 #endif
35         while (ntest--) {
36             for (i=1; i<size; i++) {
37                 MPI_Send( MPI_BOTTOM, 0, MPI_INT, i, 0, MPI_COMM_WORLD );
38                 MPI_Recv( MPI_BOTTOM, 0, MPI_INT, i, 1, MPI_COMM_WORLD, 
39                           &status );
40                 t1 = MPI_Wtime();
41                 MPI_Send( &t1, 1, MPI_DOUBLE, i, 2, MPI_COMM_WORLD );
42                 MPI_Recv( &t2, 1, MPI_DOUBLE, i, 3, MPI_COMM_WORLD, &status );
43                 t3 = MPI_Wtime();
44 #ifdef DEBUG
45                 printf( "Process %d(%f) to 0(%f): diff= %f\n", 
46                         i, 0.5 * (t1 + t3), t2, 0.5*(t1+t3)-t2 );
47 #endif
48                 delta_t = fabs( 0.5 * (t1 + t3) - t2 );
49                 if( delta_t > (t3 - t1 + wtick)) {
50                     err++;
51                     printf( "Process %d has %f; Process 0 has %f\n",
52                             i, t2, 0.5 * (t1 + t3) );
53                 }
54                 if (delta_t > max_diff) max_diff = delta_t;
55             }
56 #ifdef DEBUG        
57             printf( "delta_t = %lf\n", delta_t );
58 #endif
59             /* Release all process for the next pass */
60             for (i=1; i<size; i++) {
61                 MPI_Send( MPI_BOTTOM, 0, MPI_INT, i, 3, MPI_COMM_WORLD );
62             }
63         }
64     }
65     else {
66         while (ntest--) {
67             MPI_Recv( MPI_BOTTOM, 0, MPI_INT, 0, 0, MPI_COMM_WORLD, &status );
68             MPI_Send( MPI_BOTTOM, 0, MPI_INT, 0, 1, MPI_COMM_WORLD );
69             /* Insure a symmetric transfer */
70             MPI_Recv( &t1, 1, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD, &status );
71             t2 = MPI_Wtime();
72             MPI_Send( &t2, 1, MPI_DOUBLE, 0, 3, MPI_COMM_WORLD );
73             MPI_Recv( MPI_BOTTOM, 0, MPI_INT, 0, 3, MPI_COMM_WORLD, &status );
74         }
75     }
76     return err;
77 }
78
79 int main( int argc, char **argv )
80 {
81     int    err = 0;
82     void *v;
83     int  flag=0;
84     int  vval;
85     int  rank;
86     double t1;
87
88     MPI_Init( &argc, &argv );
89     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
90
91     MPI_Attr_get( MPI_COMM_WORLD, MPI_WTIME_IS_GLOBAL, &v, &flag );
92 #ifdef DEBUG
93     if (v) vval = *(int*)v; else vval = 0;
94     printf( "WTIME flag = %d; val = %d\n", flag, vval );
95 #endif
96     if (flag) {
97         /* Wtime need not be set */
98         vval = *(int*)v;
99         if (vval < 0 || vval > 1) {
100             err++;
101             fprintf( stderr, "Invalid value for WTIME_IS_GLOBAL (got %d)\n", 
102                      vval );
103         }
104     }
105     if (flag && vval) {
106         /* Wtime is global is true.  Check it */
107 #ifdef DEBUG
108         printf( "WTIME_IS_GLOBAL\n" );
109 #endif  
110         err += CheckTime();
111         
112         /* Wait for 10 seconds */
113         t1 = MPI_Wtime();
114         while (MPI_Wtime() - t1 < 10.0) ;
115         
116         err += CheckTime();
117     }
118     if (rank == 0) {
119         if (err > 0) {
120             printf( "Errors in MPI_WTIME_IS_GLOBAL\n" );
121         }
122         else {
123             printf( " No Errors\n" );
124         }
125     }
126     /* The SGI implementation of MPI sometimes fails to flush stdout 
127        properly.  This fflush will work around that bug.  */
128     /* fflush( stdout ); */
129     MPI_Finalize( );
130     
131     return err;
132 }