Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
first commit to add the mpich-test suite to smpi tesh suite. Obviously all tests...
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / waitall4.c
1 /*
2   Test of waitall.  This makes sure that the requests in a wait can occur
3   in any order.
4
5   Run with 4 processes.  This checks for code that listens to a specified
6   process.  This is similar to the test in waitall3, except that the 
7   wait is on sends instead of receives.  Messages are sent by process 2 to
8   processes 0 and 1.  Process 3 is uninvolved.
9   */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include "mpi.h"
14
15 #if defined(NEEDS_STDLIB_PROTOTYPES)
16 #include "protofix.h"
17 #endif
18
19 void Pause( double );
20
21 void Pause( double sec )
22 {
23   /*double t1 =*/ MPI_Wtime();
24   smpi_sleep(sec);
25 //  while (MPI_Wtime() - t1 < sec) ;
26 }
27
28 int main( int argc, char **argv )
29 {
30     int size, rank, flag, i;
31     int *buf1, *buf2, cnt;
32     double t0;
33     MPI_Status statuses[2];
34     MPI_Request req[2];
35     
36     MPI_Init( &argc, &argv );
37     MPI_Comm_size( MPI_COMM_WORLD, &size );
38     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
39
40     if (size < 3) {
41         printf( "This test requires at least 3 processors\n" );
42         MPI_Abort( MPI_COMM_WORLD, 1 );
43         return 1;
44     }
45   
46     /* Large enough that almost certainly a rendezvous algorithm will be used
47        by Issend.  buflimit.c will give you a more reliable value */
48     cnt = 35000;
49
50     /* Test:
51      process 0                    process 1               process 2
52                                                           Issend1
53                                                           Issend0
54      Barrier                      Barrier                 Barrier
55      pause(2 sec)                 pause(2 sec)            pause(1 sec)
56      irecv2                                               waitall
57      test(2) for 5 secs
58      sendrecv (process 1)         sendrecv(process0)
59                                   recv2
60      wait(2) if necessary
61
62      If the test for Irecv2 never succeeds, then the waitall appears to be
63      waiting for req1 first.  By using Issend, we can keep the program from
64      hanging.
65     */
66     buf1 = (int *)malloc( cnt * sizeof(int) );
67     buf2 = (int *)malloc( cnt * sizeof(int) );
68     if (!buf1 || !buf2) {
69         printf( "Could not allocate buffers of size %d\n", cnt );
70         MPI_Abort( MPI_COMM_WORLD, 1 );
71         return 1;
72     }
73     
74     for (i=0; i<cnt; i++) {
75         buf1[i] = i;
76         buf2[i] = i;
77     }
78
79     MPI_Barrier( MPI_COMM_WORLD );
80     if (rank == 0) {
81         MPI_Barrier( MPI_COMM_WORLD );
82         Pause( 2.0 );
83         MPI_Irecv( buf2, cnt, MPI_INT, 2, 2, MPI_COMM_WORLD, &req[0] );
84         t0 = MPI_Wtime();
85         flag = 0;
86         while (t0 + 5.0 > MPI_Wtime() && !flag) 
87             MPI_Test( &req[0], &flag, &statuses[0] );
88         /* printf( "Test succeeded at %f with flag %d\n", MPI_Wtime()-t0, flag ); */
89         /* Tell process 2 to go ahead */
90         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_BYTE, 1, 3, 
91                       MPI_BOTTOM, 0, MPI_BYTE, 1, 3, MPI_COMM_WORLD, &statuses[0] );
92         if (!flag) {
93             printf( 
94     "*ERROR: MPI_Waitall appears to be waiting for requests in the order\n\
95 they appear in the request list\n" );
96             /* We can wait now since process 2 should have allowed the wait
97                to proceed */
98             MPI_Wait( &req[0], &statuses[0] );
99         }
100         else {
101             printf( " No Errors\n" ) ;
102         }
103     }
104     else if (rank == 2) {
105         MPI_Isend( buf1, cnt, MPI_INT, 1, 1, MPI_COMM_WORLD, &req[0] );
106         MPI_Isend( buf2, cnt, MPI_INT, 0, 2, MPI_COMM_WORLD, &req[1] );
107         MPI_Barrier( MPI_COMM_WORLD );
108         Pause( 1.0 );
109         MPI_Waitall( 2, req, statuses );
110     }
111     else if (rank == 1) {
112         MPI_Status status;
113         MPI_Barrier( MPI_COMM_WORLD );
114         /* Wait for process 0 to tell us to go ahead */
115         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_BYTE, 0, 3, 
116                       MPI_BOTTOM, 0, MPI_BYTE, 0, 3, MPI_COMM_WORLD, &statuses[0] );
117         MPI_Recv( buf1, cnt, MPI_INT, 2, 1, MPI_COMM_WORLD, &status );
118     }
119     else {
120         MPI_Barrier( MPI_COMM_WORLD );
121     }
122     
123     free( buf1 );
124     free( buf2 );
125     MPI_Finalize();
126     return 0;
127 }