Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove unwanted files
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / sendorder.c
1 /* 
2    Test ordering of messages that differ only in data
3
4    sendorder [ -n number-of-sends ] [ -m length-of-long-sends ]
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "mpi.h"
11
12 /* Prototypes */
13 void delay( int );
14 void CheckStatus( MPI_Status *, int, int, int, int * );
15
16 /* 
17    This is a delay to make sure that several messages are in the queue when 
18    the MPI_Recv is called
19
20    10ms delay for now.
21 */
22 void delay( int ms )
23 {
24   double  deltat = ms * 0.001;
25   MPI_Wtime();
26   //while (MPI_Wtime() - t < deltat) ;
27   smpi_sleep(deltat);
28 }
29
30 void CheckStatus( MPI_Status *status, int tag, int src, int cnt, int *err )
31 {
32   int n;
33   
34   if (status->MPI_TAG != tag && status->MPI_SOURCE != src) { 
35     if (*err < 10) {
36       fprintf( stdout, 
37        "Error in message status! tag = %d and source = %d\n", status->MPI_TAG, 
38                    status->MPI_SOURCE );
39         }
40     (void)*err++;
41   }
42   MPI_Get_count( status, MPI_INT, &n );
43   if (n != cnt) {
44     if (*err < 10) {
45       fprintf( stdout, 
46        "Error in message status!  length is %d and should be %d\n", n, cnt );
47     }
48     (void)*err++;
49   }
50 }
51
52 int main( int argc, char *argv[] )
53 {
54   int i, n, m, val, *buf;
55   MPI_Status status;
56   int src, dest, tag, err = 0, toterr;
57   int rank, size;
58   MPI_Comm comm;
59
60   MPI_Init( &argc, &argv );
61
62   n    = 1000;    /* Number of tests */
63   comm = MPI_COMM_WORLD;
64   tag  = 3;
65   m    = 1000;    /* Size in ints of longer buffer */
66
67   /* Check for options
68    */
69   argc--; argv++;
70   while (argc > 0) {
71     if (argv[0] && strcmp( argv[0], "-n" ) == 0) {
72       argc++;
73       n = atoi( argv[0] );
74     }
75     else if (argv[0] && strcmp( argv[0], "-m" ) == 0) {
76       argc++;
77       m = atoi( argv[0] );
78     }
79     argc--; argv++;
80   }
81   /* Ensure that everyone has the values */
82   MPI_Bcast( &n, 1, MPI_INT, 0, MPI_COMM_WORLD );
83   MPI_Bcast( &m, 1, MPI_INT, 0, MPI_COMM_WORLD );
84
85   MPI_Comm_rank( comm, &rank );
86   MPI_Comm_size( comm, &size );
87   if (size < 2) {
88     fprintf( stderr, "This program requires at least 2 processes\n" );
89     MPI_Abort( MPI_COMM_WORLD, 1 );
90   }
91   src  = 0;
92   dest = size - 1;
93
94   /* Single Int */
95   MPI_Barrier( comm );
96   if (rank == src) {
97     for (i=0; i<n; i++) {
98       MPI_Send( &i, 1, MPI_INT, dest, tag, comm );
99     }
100   }
101   else if (rank == dest) {
102     for (i=0; i<n; i++) {
103       delay( 10 );
104       MPI_Recv( &val, 1, MPI_INT, src, tag, comm, &status );
105       /* The messages are sent in order that matches the value of i; 
106          if they are not received in order, this will show up in the
107          value here */
108       if (val != i) { 
109         if (err < 10) {
110           fprintf( stdout, 
111    "Error in message order (single int): message %d received when %d expected\n", val, i );
112         }
113         err++;
114       }
115       CheckStatus( &status, tag, src, 1, &err );
116     }
117   }
118
119   /* Alternating message sizes */
120   buf = (int *)malloc( m * sizeof(int) );
121   if (!buf) {
122     fprintf( stdout, "Could not allocate %d ints\n", m );
123     MPI_Abort( MPI_COMM_WORLD, 1 );
124   }
125   for (i=0; i<m; i++) buf[i] = - i;
126
127   MPI_Barrier( comm );
128   if (rank == src) {
129     for (i=0; i<n; i++) {
130       buf[0] = i;
131       MPI_Send( &i, 1, MPI_INT, dest, tag, comm );
132       MPI_Send( buf, m, MPI_INT, dest, tag, comm );
133     }
134   }
135   else if (rank == dest) {
136     for (i=0; i<n; i++) {
137       delay( 10 );
138       MPI_Recv( &val, 1, MPI_INT, src, tag, comm, &status );
139       if (val != i) { 
140         if (err < 10) {
141           fprintf( stdout, 
142    "Error in message order: message %d received when %d expected\n", val, i );
143         }
144         err++;
145       }
146       CheckStatus( &status, tag, src, 1, &err );
147
148       MPI_Recv( buf, m, MPI_INT, src, tag, comm, &status );
149       if (buf[0] != i) { 
150         if (err < 10) {
151           fprintf( stdout, 
152    "Error in message order: message buf[] %d received when %d expected\n", 
153                    buf[0], i );
154         }
155         err++;
156       }
157       CheckStatus( &status, tag, src, m, &err );
158     }
159   }
160   
161   /* Finally error reporting: make sure that rank 0 reports the message */
162   MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
163   MPI_Comm_rank( MPI_COMM_WORLD, &rank );
164   if (rank == 0) {
165     if (toterr) printf( "Found %d errors\n", toterr );
166     else        printf( " No Errors\n" );
167   }
168
169   MPI_Barrier( MPI_COMM_WORLD );
170   MPI_Finalize();
171   return 0;
172 }
173