2 hvectest2 - 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
4 struct datatype for variable length vectors
13 /* Prototypes for picky compilers */
14 void ClearArray ( double *, int, double );
15 void SetArray ( double *, int );
18 static int verbose = 1;
20 static int verbose = 0;
23 void ClearArray( a, n, v )
28 for (i=0; i<n; i++) a[i] = v;
36 for (i=0; i<n; i++) a[i] = (double)i;
40 This test requires that the MPI implementation support predefined
41 MPI_Datatypes in static initializers (i.e., they must be compile time
42 constants). This was voted as a clarification on 4/26/95.
44 int main( int argc, char **argv )
46 int rank, size, to, from, tag, count, i;
48 int st_source, st_tag, st_count;
54 static int blens[2] = { 1, 1 };
55 MPI_Datatype types[2] = { MPI_DOUBLE, MPI_UB };
58 MPI_Init( &argc, &argv );
59 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
60 MPI_Comm_size( MPI_COMM_WORLD, &size );
62 /* dest writes out the received stats; for the output to be
63 consistant (with the final check), it should be procees 0 */
64 if (argc > 1 && argv[1] && strcmp( "-alt", argv[1] ) == 0) {
74 displs[1] = 10*sizeof(double);
78 types[0] = MPI_DOUBLE;
81 MPI_Type_struct( 2, blens, displs, types, &rowtype );
82 MPI_Type_commit( &rowtype );
83 /* First test: send a row */
89 SetArray( data, 100 );
91 MPI_Send( data, count, rowtype, to, tag, MPI_COMM_WORLD );
93 printf("%d sent", rank );
94 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
102 from = MPI_ANY_SOURCE;
104 ClearArray( data, 100, -1.0 );
105 MPI_Recv(data, count, MPI_DOUBLE, from, tag, MPI_COMM_WORLD,
108 st_source = status.MPI_SOURCE;
109 st_tag = status.MPI_TAG;
110 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
112 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
113 printf( "Status info: source = %d, tag = %d, count = %d\n",
114 st_source, st_tag, st_count );
117 printf( "%d received", rank);
118 for (i = 0; i < 10; i++) printf(" %f",data[i]); printf("\n");
120 for (i = 0; i < 10; i++) if (data[i] != 10*i) {
123 "[%d](rcv double) %d'th element = %f, should be %f\n",
124 rank, i, data[i], 10.0*i );
128 /* Second test: receive a column into row */
134 SetArray( data, 100 );
136 /* MPE_Print_datatype_pack_action( stdout, count,
137 MPI_DOUBLE, 0, 0 ); */
138 MPI_Send( data, count, MPI_DOUBLE, to, tag, MPI_COMM_WORLD );
140 printf("%d sent", rank );
141 for (i = 0; i < 10; i++) printf(" %f",data[i]);printf("\n");
148 from = MPI_ANY_SOURCE;
149 ClearArray( data, 100, -1.0 );
150 MPI_Recv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
152 /* MPE_Print_datatype_unpack_action( stdout, count, rowtype, 0, 0 ); */
154 st_source = status.MPI_SOURCE;
155 st_tag = status.MPI_TAG;
156 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
158 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
159 printf( "Status info: source = %d, tag = %d, count = %d\n",
160 st_source, st_tag, st_count );
163 printf( "%d received", rank);
164 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
166 for (i = 0; i < 10; i++) if (data[i*10] != i) {
169 "[%d](rcv row) %d'th element = %f, should be %f\n",
170 rank, i, data[i*10], 1.0*i );
174 /* Third test: send AND receive a row */
180 SetArray( data, 100 );
182 MPI_Send( data, count, rowtype, to, tag, MPI_COMM_WORLD );
184 printf("%d sent", rank );
185 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
192 from = MPI_ANY_SOURCE;
193 ClearArray( data, 100, -1.0 );
194 MPI_Recv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
197 st_source = status.MPI_SOURCE;
198 st_tag = status.MPI_TAG;
199 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
201 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
202 printf( "Status info: source = %d, tag = %d, count = %d\n",
203 st_source, st_tag, st_count );
206 printf( "%d received", rank);
207 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
209 for (i = 0; i < 10; i++) if (data[i*10] != i*10) {
212 "[%d](rcv row-row) %d'th element = %f, should be %f\n",
213 rank, i, data[i*10], 10.0*i );
217 /* Second Set of Tests: Use Isend and Irecv instead of Send and Recv */
218 /* First test: send a row */
224 SetArray( data, 100 );
226 MPI_Isend( data, count, rowtype, to, tag, MPI_COMM_WORLD, &handle );
227 MPI_Wait( &handle, &status );
229 printf("%d sent", rank );
230 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
238 from = MPI_ANY_SOURCE;
239 ClearArray( data, 100, -1.0 );
240 MPI_Irecv(data, count, MPI_DOUBLE, from, tag, MPI_COMM_WORLD,
242 MPI_Wait( &handle, &status );
244 st_source = status.MPI_SOURCE;
245 st_tag = status.MPI_TAG;
246 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
248 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
249 printf( "Status info: source = %d, tag = %d, count = %d\n",
250 st_source, st_tag, st_count );
253 printf( "%d received", rank);
254 for (i = 0; i < 10; i++) printf(" %f",data[i]); printf("\n");
256 for (i = 0; i < 10; i++) if (data[i] != 10*i) {
259 "[%d](ircv double) %d'th element = %f, should be %f\n",
260 rank, i, data[i], 10.0*i );
264 /* Second test: receive a column into row */
270 SetArray( data, 100 );
272 MPI_Isend( data, count, MPI_DOUBLE, to, tag, MPI_COMM_WORLD,
274 MPI_Wait( &handle, &status );
276 printf("%d sent", rank );
277 for (i = 0; i < 10; i++) printf(" %f",data[i]);printf("\n");
284 from = MPI_ANY_SOURCE;
285 ClearArray( data, 100, -1.0 );
286 MPI_Irecv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
288 MPI_Wait( &handle, &status );
290 st_source = status.MPI_SOURCE;
291 st_tag = status.MPI_TAG;
292 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
294 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
295 printf( "Status info: source = %d, tag = %d, count = %d\n",
296 st_source, st_tag, st_count );
299 printf( "%d received", rank);
300 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
302 for (i = 0; i < 10; i++) if (data[i*10] != i) {
305 "[%d](ircv row) %d'th element = %f, should be %f\n",
306 rank, i, data[i*10], 1.0*i );
310 /* Third test: send AND receive a row */
316 SetArray( data, 100 );
318 MPI_Isend( data, count, rowtype, to, tag, MPI_COMM_WORLD, &handle );
319 MPI_Wait( &handle, &status );
321 printf("%d sent", rank );
322 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
329 from = MPI_ANY_SOURCE;
330 ClearArray( data, 100, -1.0 );
331 MPI_Irecv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
333 MPI_Wait( &handle, &status );
335 st_source = status.MPI_SOURCE;
336 st_tag = status.MPI_TAG;
337 MPI_Get_count( &status, MPI_DOUBLE, &st_count );
339 if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
340 printf( "Status info: source = %d, tag = %d, count = %d\n",
341 st_source, st_tag, st_count );
344 printf( "%d received", rank);
345 for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
347 for (i = 0; i < 10; i++) if (data[i*10] != i*10) {
350 "[%d](ircv row-row) %d'th element = %f, should be %f\n",
351 rank, i, data[i*10], 10.0*i );
356 MPI_Allreduce( &i, &errcnt, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
358 printf( "Found %d errors in the run \n", errcnt );
360 MPI_Type_free( &rowtype );