Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / waitall.c
1 /*
2  * This code tests waitall; in particular, the that ordering requirement
3  * on nonblocking communication is observed.
4  */
5
6 #include <stdio.h>
7 #include "mpi.h"
8
9 #if defined(NEEDS_STDLIB_PROTOTYPES)
10 #include "protofix.h"
11 #endif
12
13 #ifdef HAVE_UNISTD_H
14 /* For sleep */
15 #include <unistd.h>
16 #endif
17
18 #define MAX_REQ 32
19
20 #ifndef HAVE_SLEEP
21 void sleep( int secs )
22 {
23 #ifdef VX_WORKS
24     /* Also needs include <time.h>? */
25     struct timespec rqtp = { 10, 0 };
26     nanosleep(&rqtp, NULL);
27 #else
28     double t;
29     t = MPI_Wtime();
30     while (MPI_Wtime() - t < (double)secs) ;
31 #endif
32 }
33 #endif
34
35 int main( int argc, char **argv )
36 {
37     int rank, size;
38     int i, j, count, err = 0, toterr;
39     MPI_Request r[MAX_REQ];
40     MPI_Status  s[MAX_REQ];
41     int         buf[MAX_REQ][MAX_REQ];
42
43     MPI_Init( &argc, &argv );
44     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
45     MPI_Comm_size( MPI_COMM_WORLD, &size );
46
47     if (size < 2) {
48         fprintf( stderr, "This test requires at least 2 processes\n" );
49         MPI_Abort( MPI_COMM_WORLD, 1 );
50     }
51     /* First, cause the wait all to happen AFTER the Sends */
52     if (rank == 0) {
53         for (i=0; i<MAX_REQ; i++) {
54             MPI_Irecv( buf[i], i+1, MPI_INT, 1, 99, MPI_COMM_WORLD, 
55                        &r[MAX_REQ-1-i] ); 
56         }
57         MPI_Waitall( MAX_REQ, r, s );
58         /* Check that we've received the correct data */
59         for (i=0; i<MAX_REQ; i++) {
60             MPI_Get_count( &s[MAX_REQ-1-i], MPI_INT, &count );
61             if (count != i) {
62                 err++;
63                 fprintf( stderr, "Wrong count (%d) for request %d\n", 
64                          count, MAX_REQ-1-i );
65             }
66         }
67     }
68     else if (rank == 1) {
69         for (i=0; i<MAX_REQ; i++) {
70             for (j=0; j<=i; j++)
71                 buf[i][j] = i * MAX_REQ + j;
72             MPI_Send( buf[i], i, MPI_INT, 0, 99, MPI_COMM_WORLD );
73         }
74     }
75
76     /* Second, cause the waitall to start BEFORE the Sends */
77     if (rank == 0) {
78         for (i=0; i<MAX_REQ; i++) {
79             MPI_Irecv( buf[i], i+1, MPI_INT, 1, 99, MPI_COMM_WORLD, 
80                        &r[MAX_REQ-1-i] ); 
81         }
82         MPI_Send( MPI_BOTTOM, 0, MPI_INT, 1, 0, MPI_COMM_WORLD );
83         MPI_Waitall( MAX_REQ, r, s );
84         /* Check that we've received the correct data */
85         for (i=0; i<MAX_REQ; i++) {
86             MPI_Get_count( &s[MAX_REQ-1-i], MPI_INT, &count );
87             if (count != i) {
88                 err++;
89                 fprintf( stderr, 
90                          "Wrong count (%d) for request %d (waitall posted)\n", 
91                          count, MAX_REQ-1-i );
92             }
93         }
94     }
95     else if (rank == 1) {
96         MPI_Recv( MPI_BOTTOM, 0, MPI_INT, 0, 0, MPI_COMM_WORLD, &s[0] );
97         sleep( 2 );
98         for (i=0; i<MAX_REQ; i++) {
99             for (j=0; j<=i; j++)
100                 buf[i][j] = i * MAX_REQ + j;
101             MPI_Send( buf[i], i, MPI_INT, 0, 99, MPI_COMM_WORLD );
102         }
103     }
104
105
106     MPI_Barrier( MPI_COMM_WORLD );
107     if (rank == 0) {
108         toterr = err;
109         if (toterr == 0) 
110             printf( "Test complete\n" );
111         else
112             printf( "Found %d errors in test!\n", toterr );
113     }
114     
115     MPI_Finalize();
116     return 0;
117 }
118
119
120