2 hvectest - test program that sends an array of floats from the first
3 process of a group to the last, using send and recv and the
13 static int verbose = 1;
15 static int verbose = 0;
17 int main( int argc, char **argv )
19 int rank, size, to, from, tag, count, i;
21 int st_source, st_tag, st_count;
28 MPI_Init( &argc, &argv );
29 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
30 MPI_Comm_size( MPI_COMM_WORLD, &size );
38 MPI_Type_vector( 10, 1, 10, MPI_DOUBLE, &rowtype );
39 MPI_Type_commit( &rowtype );
40 /* First test: send a row */
45 for (i = 0; i < 100; i++)
48 MPI_Send( data, count, rowtype, to, tag, MPI_COMM_WORLD );
50 printf("%d sent", rank );
51 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
58 from = MPI_ANY_SOURCE;
59 MPI_Recv(data, count, MPI_DOUBLE, from, tag, MPI_COMM_WORLD,
62 st_source = status.MPI_SOURCE;
63 st_tag = status.MPI_TAG;
64 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
66 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
67 printf( "Status info: source = %d, tag = %d, count = %d\n",
68 st_source, st_tag, st_count );
71 printf( "%d received", rank);
72 for (i = 0; i < 10; i++) printf(" %f",data[i]); printf("\n");
74 for (i = 0; i < 10; i++) if (data[i] != 10*i) {
77 "[%d](rcv double) %d'th element = %f, should be %f\n",
78 rank, i, data[i], 10.0*i );
82 /* Second test: receive a column into row */
88 for (i = 0; i < 100; i++)
91 MPI_Send( data, count, MPI_DOUBLE, to, tag, MPI_COMM_WORLD );
93 printf("%d sent", rank );
94 for (i = 0; i < 10; i++) printf(" %f",data[i]);printf("\n");
101 from = MPI_ANY_SOURCE;
102 MPI_Recv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
105 st_source = status.MPI_SOURCE;
106 st_tag = status.MPI_TAG;
107 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
109 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
110 printf( "Status info: source = %d, tag = %d, count = %d\n",
111 st_source, st_tag, st_count );
114 printf( "%d received", rank);
115 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
117 for (i = 0; i < 10; i++) if (data[i*10] != i) {
120 "[%d](rcv row) %d'th element = %f, should be %f\n",
121 rank, i, data[i*10], 1.0*i );
125 /* Third test: send AND receive a row */
131 for (i = 0; i < 100; i++)
134 MPI_Send( data, count, rowtype, to, tag, MPI_COMM_WORLD );
136 printf("%d sent", rank );
137 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
144 from = MPI_ANY_SOURCE;
145 MPI_Recv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
148 st_source = status.MPI_SOURCE;
149 st_tag = status.MPI_TAG;
150 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
152 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
153 printf( "Status info: source = %d, tag = %d, count = %d\n",
154 st_source, st_tag, st_count );
157 printf( "%d received", rank);
158 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
160 for (i = 0; i < 10; i++) if (data[i*10] != i*10) {
163 "[%d](rcv row-row) %d'th element = %f, should be %f\n",
164 rank, i, data[i*10], 10.0*i );
168 /* Second Set of Tests: Use Isend and Irecv instead of Send and Recv */
169 /* First test: send a row */
175 for (i = 0; i < 100; i++)
178 MPI_Isend( data, count, rowtype, to, tag, MPI_COMM_WORLD, &handle );
179 MPI_Wait( &handle, &status );
181 printf("%d sent", rank );
182 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
190 from = MPI_ANY_SOURCE;
191 MPI_Irecv(data, count, MPI_DOUBLE, from, tag, MPI_COMM_WORLD,
193 MPI_Wait( &handle, &status );
195 st_source = status.MPI_SOURCE;
196 st_tag = status.MPI_TAG;
197 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
199 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
200 printf( "Status info: source = %d, tag = %d, count = %d\n",
201 st_source, st_tag, st_count );
204 printf( "%d received", rank);
205 for (i = 0; i < 10; i++) printf(" %f",data[i]); printf("\n");
207 for (i = 0; i < 10; i++) if (data[i] != 10*i) {
210 "[%d](ircv double) %d'th element = %f, should be %f\n",
211 rank, i, data[i], 10.0*i );
215 /* Second test: receive a column into row */
221 for (i = 0; i < 100; i++)
224 MPI_Isend( data, count, MPI_DOUBLE, to, tag, MPI_COMM_WORLD,
226 MPI_Wait( &handle, &status );
228 printf("%d sent", rank );
229 for (i = 0; i < 10; i++) printf(" %f",data[i]);printf("\n");
236 from = MPI_ANY_SOURCE;
237 MPI_Irecv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
239 MPI_Wait( &handle, &status );
241 st_source = status.MPI_SOURCE;
242 st_tag = status.MPI_TAG;
243 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
245 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
246 printf( "Status info: source = %d, tag = %d, count = %d\n",
247 st_source, st_tag, st_count );
250 printf( "%d received", rank);
251 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
253 for (i = 0; i < 10; i++) if (data[i*10] != i) {
256 "[%d](ircv row) %d'th element = %f, should be %f\n",
257 rank, i, data[i*10], 1.0*i );
261 /* Third test: send AND receive a row */
267 for (i = 0; i < 100; i++)
270 MPI_Isend( data, count, rowtype, to, tag, MPI_COMM_WORLD, &handle );
271 MPI_Wait( &handle, &status );
273 printf("%d sent", rank );
274 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
281 from = MPI_ANY_SOURCE;
282 MPI_Irecv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
284 MPI_Wait( &handle, &status );
286 st_source = status.MPI_SOURCE;
287 st_tag = status.MPI_TAG;
288 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
290 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
291 printf( "Status info: source = %d, tag = %d, count = %d\n",
292 st_source, st_tag, st_count );
295 printf( "%d received", rank);
296 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
298 for (i = 0; i < 10; i++) if (data[i*10] != i*10) {
301 "[%d](ircv row-row) %d'th element = %f, should be %f\n",
302 rank, i, data[i*10], 10.0*i );
307 MPI_Allreduce( &i, &errcnt, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
309 printf( "Found %d errors in the run \n", errcnt );
311 MPI_Type_free( &rowtype );