Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / longmsgs.c
1 #include "test.h"
2 #include "mpi.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8
9 #define MIN_MESSAGE_LENGTH 256
10 #define MAX_MESSAGE_LENGTH (16*1024*1024)
11 #define TAG1 1
12 #define TAG2 2
13 #define TAG3 3
14 #define TAG4 4
15 #define TAGSR 101
16
17 int verbose = 0;
18
19 void Resetbuf( char *, int );
20 void Checkbuf( char *, int, MPI_Status * );
21
22 void Resetbuf( char *buf, int len )
23 {
24     int i;
25     for (i=0; i<len; i++) 
26         buf[i] = 0;
27 }
28
29 void Checkbuf( char *buf, int len, MPI_Status *status )
30 {
31     int count, i;
32     int err = 0;
33     char ival;
34     
35     MPI_Get_count( status, MPI_CHAR, &count );
36     if (count != len) {
37         fprintf( stderr, "Got len of %d but expected %d\n", count, len );
38         err++;
39     }
40     ival = 0;
41     for (i=0; i<len; i++) {
42         if (buf[i] != ival) {
43             err++;
44             fprintf( stderr, 
45                      "Found wrong value in buffer[%d] = %d, expected %d\n",
46                      i, buf[i], ival );
47             if (err > 10) break;
48         }
49         ival++;
50     }
51     if (err) MPI_Abort( MPI_COMM_WORLD, 1 );
52 }
53
54 int main( int argc, char *argv[] )
55 {
56     int msglen, i;
57     int msglen_min = MIN_MESSAGE_LENGTH;
58     int msglen_max = MAX_MESSAGE_LENGTH;
59     int rank,poolsize,Master;
60     char *sendbuf,*recvbuf;
61     char ival;
62     MPI_Request request;
63     MPI_Status status;
64         
65     MPI_Init(&argc,&argv);
66     MPI_Comm_size(MPI_COMM_WORLD,&poolsize);
67     MPI_Comm_rank(MPI_COMM_WORLD,&rank);
68
69     if(poolsize != 2) {
70         printf("Expected exactly 2 MPI processes\n");
71         MPI_Abort( MPI_COMM_WORLD, 1 );
72     }
73
74 /* 
75    The following test allows this test to run on small-memory systems
76    that support the sysconf call interface.  This test keeps the test from
77    becoming swap-bound.  For example, on an old Linux system or a
78    Sony Playstation 2 (really!) 
79  */
80 #if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
81     { 
82         long n_pages, pagesize;
83         int  actmsglen_max;
84         n_pages  = sysconf( _SC_PHYS_PAGES );
85         pagesize = sysconf( _SC_PAGESIZE );
86         /* We want to avoid integer overflow in the size calculation.
87            The best way is to avoid computing any products (such
88            as total memory = n_pages * pagesize) and instead
89            compute a msglen_max that fits within 1/4 of the available 
90            pages */
91         if (n_pages > 0 && pagesize > 0) {
92             /* Recompute msglen_max */
93             int msgpages = 4 * ((msglen_max + pagesize - 1)/ pagesize);
94             while (n_pages < msgpages) { msglen_max /= 2; msgpages /= 2; }
95         }
96         /* printf ( "before = %d\n", msglen_max ); */
97         MPI_Allreduce( &msglen_max, &actmsglen_max, 1, MPI_INT, 
98                        MPI_MIN, MPI_COMM_WORLD );
99         msglen_max = actmsglen_max;
100         /* printf ( "after = %d\n", msglen_max ); */
101     }
102 #endif
103
104     Master = (rank == 0);       
105
106     if(Master && verbose)
107         printf("Size (bytes)\n------------\n");
108     for(msglen = msglen_min; msglen <= msglen_max; msglen *= 2) {
109
110         sendbuf = malloc(msglen);
111         recvbuf = malloc(msglen);
112         if(sendbuf == NULL || recvbuf == NULL) {
113             printf("Can't allocate %d bytes\n",msglen);
114             MPI_Abort( MPI_COMM_WORLD, 1 );
115         }
116
117         ival = 0;
118         for (i=0; i<msglen; i++) {
119             sendbuf[i] = ival++;
120             recvbuf[i] = 0;
121         }
122
123
124         if(Master && verbose) 
125             printf("%d\n",msglen);
126         fflush(stdout);
127
128         MPI_Barrier(MPI_COMM_WORLD);
129                 
130         /* Send/Recv */
131         if(Master) 
132             MPI_Send(sendbuf,msglen,MPI_CHAR,1,TAG1,MPI_COMM_WORLD);
133         else {
134             Resetbuf( recvbuf, msglen );
135             MPI_Recv(recvbuf,msglen,MPI_CHAR,0,TAG1,MPI_COMM_WORLD,&status);
136             Checkbuf( recvbuf, msglen, &status );
137         }
138
139         MPI_Barrier(MPI_COMM_WORLD);
140
141         /* Ssend/Recv */
142         if(Master) 
143             MPI_Send(sendbuf,msglen,MPI_CHAR,1,TAG2,MPI_COMM_WORLD);
144         else {
145             Resetbuf( recvbuf, msglen );
146             MPI_Recv(recvbuf,msglen,MPI_CHAR,0,TAG2,MPI_COMM_WORLD,&status);
147             Checkbuf( recvbuf, msglen, &status );
148         }
149
150         MPI_Barrier(MPI_COMM_WORLD);
151                 
152         /* Rsend/Recv */
153 /*      if (Master) {*/
154 /*          MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, 1, TAGSR,*/
155 /*                        MPI_BOTTOM, 0, MPI_INT, 1, TAGSR,*/
156 /*                        MPI_COMM_WORLD, &status );*/
157 /*          MPI_Rsend( sendbuf,msglen,MPI_CHAR,1,TAG3,MPI_COMM_WORLD );*/
158 /*      }*/
159 /*      else {*/
160 /*          Resetbuf( recvbuf, msglen );*/
161 /*          MPI_Irecv( recvbuf,msglen,MPI_CHAR,0,TAG3,MPI_COMM_WORLD,&request);*/
162 /*          MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, 0, TAGSR,*/
163 /*                        MPI_BOTTOM, 0, MPI_INT, 0, TAGSR,*/
164 /*                        MPI_COMM_WORLD, &status );*/
165 /*          MPI_Wait( &request, &status );*/
166 /*          Checkbuf( recvbuf, msglen, &status );*/
167 /*      }*/
168 /*          */
169 /*      MPI_Barrier(MPI_COMM_WORLD);*/
170
171         /* Isend/Recv - receive not ready */
172         if(Master) {
173             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, 1, TAGSR,
174                           MPI_BOTTOM, 0, MPI_INT, 1, TAGSR,
175                           MPI_COMM_WORLD, &status );
176             MPI_Isend(sendbuf,msglen,MPI_CHAR,1,TAG4,MPI_COMM_WORLD, &request);
177             MPI_Wait( &request, &status );
178         }
179         else {
180             Resetbuf( recvbuf, msglen );
181             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, 0, TAGSR,
182                           MPI_BOTTOM, 0, MPI_INT, 0, TAGSR,
183                           MPI_COMM_WORLD, &status );
184             MPI_Recv(recvbuf,msglen,MPI_CHAR,0,TAG4,MPI_COMM_WORLD,&status);
185             Checkbuf( recvbuf, msglen, &status );
186         }
187
188         MPI_Barrier(MPI_COMM_WORLD);
189
190         free(sendbuf);
191         free(recvbuf);
192     }
193
194     if (rank == 0) {
195         /* If we do not abort, we saw no errors */
196         printf( " No Errors\n" );
197     }
198
199     MPI_Finalize();
200     return 0;
201 }