Logo AND Algorithmique Numérique Distribuée

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