Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove unwanted files
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / reqfree.c
1 #include "test.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #include "mpi.h"
9
10 #define MAX_REQ 10000
11
12 #define DEFAULT_REQ 100
13 #define DEFAULT_LEN 20000
14 #define DEFAULT_LOOP 10
15
16 int main( int argc, char **argv )
17 {
18     int rank, size, loop, max_loop = DEFAULT_LOOP, max_req = DEFAULT_REQ;
19     int buf_len = DEFAULT_LEN;
20     int i, j, errs = 0, toterrs;
21     MPI_Request r;
22     MPI_Status  status;
23     int *(b[MAX_REQ]);
24     MPI_Datatype dtype;
25     int sendrank = 0, recvrank = 1;
26
27     MPI_Init( &argc, &argv );
28
29     dtype = MPI_INT;
30
31     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
32
33 /* 
34    The following test allows this test to run on small-memory systems
35    that support the sysconf call interface.  This test keeps the test from
36    becoming swap-bound.  For example, on an old Linux system or a
37    Sony Playstation 2 (really!) 
38  */
39 #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
40     if (rank == sendrank) 
41     { 
42         long n_pages, pagesize;
43         int  msglen_max = max_req * buf_len * sizeof(int);
44         n_pages  = sysconf( _SC_PHYS_PAGES );
45         pagesize = sysconf( _SC_PAGESIZE );
46         /* printf( "Total mem = %ld\n", n_pages * pagesize ); */
47         /* We want to avoid integer overflow in the size calculation.
48            The best way is to avoid computing any products (such
49            as total memory = n_pages * pagesize) and instead
50            compute a msglen_max that fits within 1/4 of the available 
51            pages */
52         if (n_pages > 0 && pagesize > 0) {
53             /* Recompute msglen_max */
54             int msgpages = 4 * ((msglen_max + pagesize - 1)/ pagesize);
55             while (n_pages < msgpages) { 
56                 msglen_max /= 2; msgpages /= 2; buf_len /= 2; 
57             }
58         }
59     }
60 #else
61     /* printf( "No sysconf\n" ); */
62 #endif
63
64     /* Check command line args (allow usage even with one processor */
65     argv++;
66     argc--;
67     while (argc--) {
68         if (strcmp( "-loop" , *argv ) == 0) {
69             argv++; argc--;
70             max_loop = atoi( *argv );
71         }
72         else if (strcmp( "-req", *argv ) == 0) {
73             argv++; argc--;
74             max_req = atoi( *argv );
75         }
76         else if (strcmp( "-len", *argv ) == 0) {
77             argv++; argc--;
78             buf_len = atoi( *argv );
79         }
80         else {
81             fprintf( stderr, 
82                      "Usage: reqfree [ -loop n ] [ -req n ] [ -len n ]\n" );
83             MPI_Abort( MPI_COMM_WORLD, 1 );
84         }
85         argv++;
86     }
87     
88     MPI_Comm_size( MPI_COMM_WORLD, &size );
89     if (size != 2) {
90         fprintf( stderr, "This program requires two processes\n" );
91         MPI_Abort( MPI_COMM_WORLD, 1 );
92     }
93
94     /* Assume only processor 0 has the command line */
95     MPI_Bcast( &max_loop, 1, MPI_INT, 0, MPI_COMM_WORLD );
96     MPI_Bcast( &max_req, 1, MPI_INT, 0, MPI_COMM_WORLD );
97     MPI_Bcast( &buf_len, 1, MPI_INT, 0, MPI_COMM_WORLD );
98
99     /* Allocate buffers */
100     for (i=0; i<max_req; i++) {
101         b[i] = (int *) malloc(buf_len * sizeof(int) );
102         if (!b[i]) {
103             fprintf( stderr, "Could not allocate %dth block of %d ints\n", 
104                      i, buf_len );
105             MPI_Abort( MPI_COMM_WORLD, 2 );
106         }
107         if (rank != sendrank) break;
108         for (j=0; j<buf_len; j++) {
109             b[i][j] = i * buf_len + j;
110         }
111     }
112
113     /* Loop several times to capture resource leaks */
114     for (loop=0; loop<max_loop; loop++) {
115         if (rank == sendrank) {
116             for (i=0; i<max_req; i++) {
117                 MPI_Isend( b[i], buf_len, dtype, recvrank, 0, 
118                            MPI_COMM_WORLD, &r );
119                 MPI_Request_free( &r ); 
120             }
121             MPI_Barrier( MPI_COMM_WORLD );
122             MPI_Barrier( MPI_COMM_WORLD );
123         }
124         else {
125             MPI_Barrier( MPI_COMM_WORLD );
126             for (i=0; i<max_req; i++) {
127                 MPI_Recv( b[0], buf_len, dtype, sendrank, 0, MPI_COMM_WORLD, 
128                           &status );
129                 for (j=0; j<buf_len; j++) {
130                     if (b[0][j] != i * buf_len + j) {
131                         errs++;
132                         fprintf( stdout, 
133                                  "at %d in %dth message, got %d expected %d\n",
134                                  j, i, b[0][j], i * buf_len + j );
135                         break;
136                     }
137                 }
138             }
139             MPI_Barrier( MPI_COMM_WORLD );
140         }
141     }
142
143     MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
144     if (rank == 0) {
145         if (toterrs == 0) printf( " No Errors\n" );
146         else              printf( "Found %d errors\n", toterrs );
147     }
148
149     MPI_Finalize( );
150     return 0;
151 }
152