Logo AND Algorithmique Numérique Distribuée

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