12 #define DEFAULT_REQ 100
13 #define DEFAULT_LEN 20000
14 #define DEFAULT_LOOP 10
16 int main( int argc, char **argv )
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;
25 int sendrank = 0, recvrank = 1;
27 MPI_Init( &argc, &argv );
31 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
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!)
39 #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
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
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;
61 /* printf( "No sysconf\n" ); */
64 /* Check command line args (allow usage even with one processor */
68 if (strcmp( "-loop" , *argv ) == 0) {
70 max_loop = atoi( *argv );
72 else if (strcmp( "-req", *argv ) == 0) {
74 max_req = atoi( *argv );
76 else if (strcmp( "-len", *argv ) == 0) {
78 buf_len = atoi( *argv );
82 "Usage: reqfree [ -loop n ] [ -req n ] [ -len n ]\n" );
83 MPI_Abort( MPI_COMM_WORLD, 1 );
88 MPI_Comm_size( MPI_COMM_WORLD, &size );
90 fprintf( stderr, "This program requires two processes\n" );
91 MPI_Abort( MPI_COMM_WORLD, 1 );
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 );
99 /* Allocate buffers */
100 for (i=0; i<max_req; i++) {
101 b[i] = (int *) malloc(buf_len * sizeof(int) );
103 fprintf( stderr, "Could not allocate %dth block of %d ints\n",
105 MPI_Abort( MPI_COMM_WORLD, 2 );
107 if (rank != sendrank) break;
108 for (j=0; j<buf_len; j++) {
109 b[i][j] = i * buf_len + j;
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 );
121 MPI_Barrier( MPI_COMM_WORLD );
122 MPI_Barrier( MPI_COMM_WORLD );
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,
129 for (j=0; j<buf_len; j++) {
130 if (b[0][j] != i * buf_len + j) {
133 "at %d in %dth message, got %d expected %d\n",
134 j, i, b[0][j], i * buf_len + j );
139 MPI_Barrier( MPI_COMM_WORLD );
143 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
145 if (toterrs == 0) printf( " No Errors\n" );
146 else printf( "Found %d errors\n", toterrs );