source: CPL/oasis3-mct/branches/OASIS3-MCT_5.0_branch/lib/mct/mpi-serial/tests/ctest.c @ 6331

Last change on this file since 6331 was 6331, checked in by aclsce, 17 months ago

Moved oasis-mct_5.0 in oasis3-mct/branches directory.

File size: 24.3 KB
Line 
1#include <mpi.h>
2#include <stdio.h>
3#include <stdlib.h>
4
5#ifdef HAVE_CONFIG_H
6#include <config.h>
7#endif
8
9#ifdef TEST_INTERNAL
10#include <mpiP.h>
11#include <type.h>
12#else
13MPI_Request req;
14#endif
15
16
17int errcount = 0;
18//simplest example:  contiguous
19// type of 5 MPI_INT
20
21void test_simple_contig()
22{
23  int i;
24  int a [5] = {1, 2, 3, 4, 5};
25  int b [5];
26  MPI_Datatype contig_type;
27
28  //Contiguous type of simple types
29  printf("\nContiguous type of 5 x MPI_INT\n");
30  MPI_Type_contiguous(5, MPI_INT, &contig_type);
31  MPI_Type_commit(&contig_type);
32
33#ifdef TEST_INTERNAL
34  print_typemap(contig_type);
35  copy_data(&a, &b, contig_type);
36#else
37  MPI_Isend(&a, 1, contig_type, 0, 0, MPI_COMM_WORLD, &req);
38  MPI_Irecv(&b, 1, contig_type, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD,&req);
39#endif
40
41  printf("a = [");
42  for (i = 0; i < 5; i++)
43    printf("%d ", a[i]);
44  printf("]\n");
45
46  printf("b = [");
47  for (i = 0; i < 5; i++)
48    printf("%d ", b[i]);
49  printf("]\n");
50
51  for (i = 0; i < 5; i++)
52    if (a[i]!=b[i])
53    {
54      printf(">>>FAILED: test_simple_contig\n");
55      errcount++;
56      return;
57    }
58}
59
60// vector type of MPI_INTs
61
62void test_simple_vector()
63{
64  int i;
65  int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
66  int b[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
67  int index_test []= {0, 1, 3, 4, 6, 7};
68  MPI_Datatype vector_type;
69
70  //Vector type of simple types
71  printf("\nVector type of 3 groups of 2 MPI_INT, stride of 3.\n");
72  MPI_Type_vector(3, 2, 3, MPI_INT, &vector_type);
73  MPI_Type_commit(&vector_type);
74
75#ifdef TEST_INTERNAL
76  print_typemap(vector_type);
77  copy_data(&a, &b, vector_type);
78#else
79  MPI_Isend(&a, 1, vector_type, 0, 0, MPI_COMM_WORLD, &req);
80  MPI_Irecv(&b, 1, vector_type, 0, 0, MPI_COMM_WORLD, &req);
81#endif
82
83  printf("a = [");
84  for (i = 0; i < 10; i++)
85    printf("%d ", a[i]);
86  printf("]\n");
87
88  printf("b = [");
89  for (i = 0; i < 10; i++)
90    printf("%d ", b[i]);
91  printf("]\n");
92
93  for (i = 0; i < 6; i++)
94    if (a[index_test[i]]!=b[index_test[i]])
95    {
96      printf(">>>FAILED: test_simple_vector\n");
97      errcount++;
98      return;
99    }
100}
101//vector type (byte addressed, using
102// sizeof(int) to compute stride
103
104void test_simple_hvector()
105{
106  MPI_Datatype vector_type;
107  int i;
108  int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
109  int b[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
110  int index_test [6] = {0, 1, 4, 5, 8, 9};
111  //Vector (byte-addressed) of simple types
112  printf("\nVector type of 3 groups of 2 MPI_INT, stride of 16 bytes.\n");
113  MPI_Type_hvector(3, 2, 4*sizeof(int), MPI_INT, &vector_type);
114  MPI_Type_commit(&vector_type);
115
116#ifdef TEST_INTERNAL
117  print_typemap(vector_type);
118  copy_data(&a, &b, vector_type);
119#else
120  MPI_Isend(&a, 1, vector_type, 0, 0, MPI_COMM_WORLD, &req);
121  MPI_Irecv(&b, 1, vector_type, 0, 0, MPI_COMM_WORLD, &req);
122#endif
123
124  printf("a = [");
125  for (i = 0; i < 10; i++)
126    printf("%d ", a[i]);
127  printf("]\n");
128
129  printf("b = [");
130  for (i = 0; i < 10; i++)
131    printf("%d ", b[i]);
132  printf("]\n");
133
134  for (i = 0; i < 6; i++)
135    if (a[index_test[i]]!=b[index_test[i]])
136    {
137      printf(">>>FAILED: test_simple_hvector\n");
138      errcount++;
139      return;
140    }
141}
142
143//indexed type.
144
145void test_simple_indexed()
146{
147  int i;
148  int a[15] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
149  int b[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
150  int index_test [6] = {0, 5, 6, 10, 11, 12};
151  int blens[3] = {2, 1, 3};
152  int disps[3] = {5, 0, 10};
153  MPI_Datatype indexed_type;
154  //Indexed of simple types
155
156  printf("\nIndexed type of MPI_INT.\n");
157
158  MPI_Type_indexed(3, blens, disps, MPI_INT, &indexed_type);
159  MPI_Type_commit(&indexed_type);
160
161#ifdef TEST_INTERNAL
162  print_typemap(indexed_type);
163  copy_data(&a, &b, indexed_type);
164#else
165  MPI_Isend(&a, 1, indexed_type, 0, 0, MPI_COMM_WORLD, &req);
166  MPI_Irecv(&b, 1, indexed_type, 0, 0, MPI_COMM_WORLD, &req);
167#endif
168
169  printf("a = [");
170  for (i = 0; i < 15; i++)
171    printf("%d ", a[i]);
172  printf("]\n");
173
174  printf("b = [");
175  for (i = 0; i < 15; i++)
176    printf("%d ", b[i]);
177  printf("]\n");
178
179  for (i = 0; i < 6; i++)
180    if (a[index_test[i]]!=b[index_test[i]])
181    {
182      printf(">>>FAILED: test_simple_indexed\n");
183      errcount++;
184      return;
185    }
186}
187
188//block indexed.  Same as indexed except
189//static block length
190
191void test_simple_bindexed()
192{
193  int i;
194  int disps[3] = {0, 4, 7};
195  int a [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
196  int b [10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
197  int index_test[6] = {0, 1, 4, 5, 7, 8};
198  MPI_Datatype indexed_type;
199
200  //block indexed of simple types
201  printf("\nBlock indexed type of MPI_INT.\n");
202  MPI_Type_create_indexed_block(3, 2, disps, MPI_INT, &indexed_type);
203  MPI_Type_commit(&indexed_type);
204#ifdef TEST_INTERNAL
205  copy_data(&a, &b, indexed_type);
206  print_typemap(indexed_type);
207#else
208  MPI_Isend(&a, 1, indexed_type, 0, 0, MPI_COMM_WORLD, &req);
209  MPI_Irecv(&b, 1, indexed_type, 0, 0, MPI_COMM_WORLD, &req);
210#endif
211
212  printf("a = [");
213  for (i = 0; i < 10; i++)
214    printf("%d ", a[i]);
215  printf("]\n");
216
217  printf("b = [");
218  for (i = 0; i < 10; i++)
219    printf("%d ", b[i]);
220  printf("]\n");
221
222  for (i = 0; i < 6; i++)
223    if (a[index_test[i]]!=b[index_test[i]])
224    {
225      printf(">>>FAILED: test_simple_bindexed\n");
226      errcount++;
227      return;
228    }
229}
230
231//hindexed:  same as indexed, but
232//using byte displacements based off of sizeof(int)
233//(no reason why this shouldn't work)
234
235void test_simple_hindexed()
236{
237  int i;
238  int a [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
239  int b [10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
240  int index_test [6] = {0, 2, 3, 5, 6, 7};
241  int blens[3] = {2, 1, 3};
242  MPI_Aint disps[3] = {2*sizeof(int), 0, 5*sizeof(int)};
243  MPI_Datatype indexed_type;
244
245//Indexed (byte-addressed) of simple types
246  printf("\nBlock indexed (byte addressed) type of MPI_INT.\n");
247  MPI_Type_hindexed(3, blens, disps, MPI_INT, &indexed_type);
248  MPI_Type_commit(&indexed_type);
249#ifdef TEST_INTERNAL
250  print_typemap(indexed_type);
251  copy_data(&a, &b, indexed_type);
252#else
253  MPI_Isend(&a, 1, indexed_type, 0, 0, MPI_COMM_WORLD, &req);
254  MPI_Irecv(&b, 1, indexed_type, 0, 0, MPI_COMM_WORLD, &req);
255#endif
256
257  printf("a = [");
258  for (i = 0; i < 10; i++)
259    printf("%d ", a[i]);
260  printf("]\n");
261
262  printf("b = [");
263  for (i = 0; i < 10; i++)
264    printf("%d ", b[i]);
265  printf("]\n");
266
267  for (i = 0; i < 6; i++)
268    if (a[index_test[i]]!=b[index_test[i]])
269    {
270      printf(">>>FAILED: test_simple_hindexed\n");
271      errcount++;
272      return;
273    }
274}
275
276
277/*
278 * void struct_test()
279{
280  int blocklengths[6];
281  int offsets[6];
282  MPI_Aint boffsets[6];
283  MPI_Datatype types[6];
284  MPI_Datatype struct_type, newtype, newtype2, sstruct,
285               indexed_type, vector_type;
286  MPI_Aint extent2, extent3;
287  //struct type of simple types
288  printf("\nStruct of simple types\n");
289  blocklengths[0] = 3;
290  blocklengths[1] = 5;
291  blocklengths[2] = 2;
292  blocklengths[3] = 1;
293  boffsets[0] = 0;
294  boffsets[1] = 24;
295  boffsets[2] = 32;
296  boffsets[3] = 40;
297  types[0] = MPI_DOUBLE;
298  types[1] = MPI_CHAR;
299  types[2] = MPI_INT;
300  types[3] = MPI_LONG_DOUBLE;
301
302  MPI_Type_struct(4, blocklengths, boffsets, types, &struct_type);
303  print_typemap(struct_type);
304
305  //struct type of simple types, with artificial LB and UB
306  printf("\nStruct type of simple types, with LB and UB.\n");
307  blocklengths[0] = 2;
308  blocklengths[1] = 4;
309  blocklengths[2] = 1;
310  blocklengths[3] = 24;
311  blocklengths[4] = 1;
312  boffsets[0] = 0;
313  boffsets[1] = 40;
314  boffsets[2] = 80;
315  boffsets[3] = 48;
316  boffsets[4] = -8;
317  types[0] = MPI_LONG;
318  types[1] = MPI_INT;
319  types[2] = MPI_UB;
320  types[3] = MPI_CHAR;
321  types[4] = MPI_LB;
322
323  MPI_Type_struct(5, blocklengths, boffsets, types, &newtype2);
324  print_typemap(newtype2);
325
326  //struct type:  2 int, 1 float
327  printf("\nSimple struct for use: 2 int, 1 float\n");
328  blocklengths[0] = 2;
329  blocklengths[1] = 1;
330  boffsets[0] = 0;
331  boffsets[1] = 8;
332  types[0] = MPI_INT;
333  types[1] = MPI_FLOAT;
334
335  MPI_Type_struct(2, blocklengths, boffsets, types, &sstruct);
336  print_typemap(sstruct);
337
338  //contiguous type of complex (struct) type
339  printf("\nContiguous type of complex (struct) type\n");
340  MPI_Type_contiguous(3, newtype2, &newtype);
341  print_typemap(newtype);
342
343  //vector type of complex type
344  printf("\nVector type of struct\n");
345  MPI_Type_vector(3, 2, 2, struct_type, &vector_type);
346  print_typemap(vector_type);
347
348  //indexed of complex type
349  printf("\nIndexed type of struct\n");
350  blocklengths[0] = 1;
351  blocklengths[1] = 2;
352  offsets[0]      = 0;
353  offsets[1]      = 7;
354  MPI_Type_indexed(2, blocklengths, offsets, sstruct, &indexed_type);
355  print_typemap(indexed_type);
356
357  //struct of simple/complex
358  printf("\nStruct of smaller structs and simple types\n");
359  MPI_Type_extent(sstruct, &extent2);
360  MPI_Type_extent(indexed_type, &extent3);
361  blocklengths[0] = 2;
362  blocklengths[1] = 1;
363  blocklengths[2] = 4;
364  blocklengths[3] = 5;
365  boffsets[0]     = 0;
366  boffsets[1]     = 2 * extent2;
367  boffsets[2]     = boffsets[1] + extent3;
368  boffsets[3]     = boffsets[2] + 4;
369  types[0]        = sstruct;
370  types[1]        = indexed_type;
371  types[2]        = MPI_CHAR;
372  types[3]        = newtype2;
373
374  MPI_Type_struct(4, blocklengths, boffsets, types, &struct_type);
375  print_typemap(struct_type);
376}
377*/
378
379//simple struct, comprised of an int, 2 chars
380// and a long int value.
381
382void test_simple_struct()
383{
384  struct {int a; char b; char c; long d; } s1;
385  struct {int a; char b; char c; long d; } s2;
386
387  int blens[4] = {1, 2, 1};
388  MPI_Aint disps[4] = {0, 4, 8};
389  MPI_Datatype types[4] = {MPI_INT, MPI_CHAR, MPI_LONG};
390  MPI_Datatype struct_type;
391
392  printf("\nSimple struct type: 1 int, 2 char, 1 long\n");
393  MPI_Type_struct(3, blens, disps, types, &struct_type);
394  MPI_Type_commit(&struct_type);
395  s1.a = 10;
396  s1.b = 'x';
397  s1.c = 'a';
398  s1.d = 3000;
399
400#ifdef TEST_INTERNAL
401  print_typemap(struct_type);
402  copy_data(&s1, &s2, struct_type);
403#else
404  MPI_Isend(&s1, 1, struct_type, 0, 0, MPI_COMM_WORLD, &req);
405  MPI_Irecv(&s2, 1, struct_type, 0, 0, MPI_COMM_WORLD, &req);
406#endif
407
408  if (!(s1.a==s2.a && s1.b==s2.b && s1.c==s2.c && s1.d==s2.d))
409  {
410    printf(">>>FAILED: test_simple_struct\n");
411    errcount++;
412    return;
413  }
414}
415
416// combine one struct into another struct for a complex
417// type. This should test any funny padding issues
418
419void test_complex_struct()
420{
421  MPI_Datatype sstruct;
422  typedef struct {long a; long b; char c; int d; int e;} st;
423  typedef struct {st a; int b; char c;} st2;
424  st s1 = {.a = 100, .b = 200, .c = 'x', .d = 45, .e = 50};
425  st s2;
426  st2 s3 = {.a = { .a = 40, .b = 100, .c = 'x', .d = 50, .e = 20}, .b = 100, .c = 'g'} ;
427  st2 s4;
428  int blens[3] = {2, 2, 1};
429  MPI_Aint disps[3] = {0, 2*sizeof(long) + sizeof(int), 2*sizeof(long)};
430  MPI_Datatype types[3] = {MPI_LONG, MPI_INT, MPI_CHAR};
431  MPI_Datatype newtype;
432
433
434  printf("\nSimple struct to create complex struct\n");
435  MPI_Type_struct(3, blens, disps, types, &newtype);
436  MPI_Type_commit(&newtype);
437#ifdef TEST_INTERNAL
438  print_typemap(newtype);
439  copy_data(&s1, &s2, newtype);
440#else
441  MPI_Isend(&s1, 1, newtype, 0, 0, MPI_COMM_WORLD, &req);
442  MPI_Irecv(&s2, 1, newtype, 0, 0, MPI_COMM_WORLD, &req);
443#endif
444
445 if (!(s1.a==s2.a && s1.b==s2.b && s1.c==s2.c && s1.d==s2.d && s1.e==s2.e))
446  {
447    printf(">>>FAILED: test_complex_struct\n");
448    errcount++;
449    return;
450  }
451  MPI_Datatype newtype2;
452
453  blens[0] = 1;
454  blens[1] = 1;
455  blens[2] = 1;
456  disps[0] = 0;
457  disps[1] = sizeof(st);
458  disps[2] = sizeof(st) + sizeof(int);
459  types[0] = newtype;
460  types[1] = MPI_INT;
461  types[2] = MPI_CHAR;
462
463  printf("\nComplex struct type composed of other struct.\n");
464  MPI_Type_struct(3, blens, disps, types, &newtype2);
465  MPI_Type_commit(&newtype2);
466#ifdef TEST_INTERNAL
467  print_typemap(newtype2);
468  copy_data(&s3, &s4, newtype2);
469#else
470  MPI_Isend(&s3, 1, newtype2, 0, 0, MPI_COMM_WORLD, &req);
471  MPI_Irecv(&s4, 1, newtype2, 0, 0, MPI_COMM_WORLD, &req);
472#endif
473
474  if (!(s3.a.a==s4.a.a && s3.a.b==s4.a.b && s3.a.c==s4.a.c && s3.b==s4.b && s3.c==s4.c))
475  {
476    printf(">>>FAILED: test_complex_struct\n");
477    errcount++;
478    return;
479  }
480}
481
482// Indexed struct.  This one is a bit complicated
483// as to datatype layout, so it will also test the
484// padding issue
485
486void test_indexed_struct()
487{
488  int i;
489
490  //simple struct vars
491  int s_blens[4] = {1,1,1,2};
492  MPI_Aint s_disps[4];
493  MPI_Datatype s_types[4] = {MPI_CHAR, MPI_LONG,
494                             MPI_CHAR, MPI_INT};
495  MPI_Datatype s_struct;
496  int i_blens[3] = {3, 1, 2};
497  int i_disps[3] = {0, 5, 7};
498  MPI_Datatype i_struct_indexed;
499  int index_test [6] = {0,1,2,5,7,8};
500  char* sadd;
501  typedef struct
502    {char a; long b; char c; int d; int e;}
503    struct_t;
504
505  struct_t send[10];
506  struct_t recv[10];
507
508  //initialize the structs
509  for (i = 0; i < 10; i++)
510  {
511    send[i].a = i;
512    send[i].b = 2*i;
513    send[i].c = 'A' + i;
514    send[i].d = i;
515    send[i].e =-i;
516    recv[i].a=0;
517    recv[i].b=0;
518    recv[i].c=' ';
519    recv[i].d=0;
520    recv[i].e=0;
521  }
522
523  //set the displacements by using address differences
524  sadd = (char *)&send[0];
525  s_disps[0] = (char*)&(send[0].a) - sadd;
526  s_disps[1] = (char*)&(send[0].b) - sadd;
527  s_disps[2] = (char*)&(send[0].c) - sadd;
528  s_disps[3] = (char*)&(send[0].d) - sadd;
529  //e is "contiguous" of d
530
531
532  MPI_Type_struct(4, s_blens, s_disps, s_types, &s_struct);
533  MPI_Type_commit(&s_struct);
534#ifdef TEST_INTERNAL
535  print_typemap(s_struct);
536#endif
537
538  //now, create an indexed type of this struct
539  MPI_Type_indexed(3, i_blens, i_disps,
540                   s_struct, &i_struct_indexed);
541  MPI_Type_commit(&i_struct_indexed);
542
543#ifdef TEST_INTERNAL
544  print_typemap(i_struct_indexed);
545  copy_data2(send, 1, i_struct_indexed, recv, 1, i_struct_indexed);
546#else
547  MPI_Isend(&send, 1, i_struct_indexed, 0, 0, MPI_COMM_WORLD, &req);
548  MPI_Irecv(&recv, 1, i_struct_indexed, 0, 0, MPI_COMM_WORLD, &req);
549#endif
550
551  for (i = 0; i < 6; i++)
552  {
553    if (!(send[index_test[i]].a==recv[index_test[i]].a
554      && send[index_test[i]].b==recv[index_test[i]].b
555      && send[index_test[i]].c==recv[index_test[i]].c
556      && send[index_test[i]].d==recv[index_test[i]].d
557      && send[index_test[i]].e==recv[index_test[i]].e))
558    {
559      printf(">>>FAILED: test_indexed_struct\n");
560      errcount++;
561      return;
562    }
563  }
564
565  //to make things really interesting, let's send as the
566  //indexed type, and receive instead as _count_
567  //consecutive struct types
568#ifdef TEST_INTERNAL
569  copy_data2(send, 1, i_struct_indexed, recv, 6, s_struct);
570#else
571  MPI_Gather(&send, 1, i_struct_indexed, &recv,
572             6, s_struct, 0, MPI_COMM_WORLD);
573
574//  MPI_Isend(&send, 1, i_struct_indexed, 0, 0, MPI_COMM_WORLD, &req);
575//  MPI_Irecv(&recv, 6, s_struct, 0, 0, MPI_COMM_WORLD, &req);
576
577#endif
578
579  for (i = 0; i < 6; i++)
580  {
581    if (!(send[index_test[i]].a==recv[i].a
582      && send[index_test[i]].b==recv[i].b
583      && send[index_test[i]].c==recv[i].c
584      && send[index_test[i]].d==recv[i].d
585      && send[index_test[i]].e==recv[i].e))
586    {
587      printf(">>>FAILED: test_indexed_struct (multiple recv)\n");
588      errcount++;
589      return;
590    }
591  }
592
593}
594
595
596//test a differing issue with send/receive
597//A contiguous type of 5 MPI_INTs is sent, and is
598//received using a receive x5 of MPI_INT
599
600void test_multiple()
601{
602  int i;
603  int a[5] = {1, 2, 3, 4, 5};
604  int b[5] = {0, 0, 0, 0, 0};
605
606
607
608  MPI_Datatype contig5int;
609
610  printf("\nSend contiguous of 5 MPI_INT, receive 5 x MPI_INT\n");
611  MPI_Type_contiguous(5, MPI_INT, &contig5int);
612  MPI_Type_commit(&contig5int);
613
614#ifdef TEST_INTERNAL
615  copy_data2(&a, 5, MPI_INT, &b, 1, contig5int);
616#else
617  MPI_Isend(&a, 5, MPI_INT, 0, 0, MPI_COMM_WORLD, &req);
618  MPI_Irecv(&b, 1, contig5int, 0, 0, MPI_COMM_WORLD, &req);
619#endif
620
621
622  printf("a = [");
623  for (i = 0; i < 5; i++)
624    printf("%d ", a[i]);
625  printf("]\n");
626
627  printf("b = [");
628  for (i = 0; i < 5; i++)
629    printf("%d ", b[i]);
630  printf("]\n");
631
632  for (i = 0; i < 5; i++)
633    if (a[i]!=b[i])
634    {
635      printf(">>>FAILED: test_multiple\n");
636      errcount++;
637      return;
638    }
639}
640
641void test_multiple_struct()
642{
643  int i;
644  typedef struct {int a; double b; char c;} struct_t;
645  struct_t s1[5],s2[5];
646  MPI_Aint disps[3];
647  int blens[3] = {1,1,1};
648  MPI_Datatype types[3] = {MPI_INT, MPI_DOUBLE, MPI_CHAR};
649  MPI_Datatype struct_type, contig_struct;
650
651  disps[0] = 0;
652  disps[1] = (char*) &(s1[0].b) - (char*) &s1[0];
653  disps[2] = (char*) &(s1[0].c) - (char*) &s1[0];
654
655  for (i=0; i<5; i++)
656  {
657    s1[i].a=i; s1[i].b=i+15.0; s1[i].c='a'+i;
658    s2[i].a=0; s2[i].b=0.0   ; s2[i].c=0    ;
659  }
660
661  MPI_Type_struct(3, blens, disps, types, &struct_type);
662  MPI_Type_commit(&struct_type);
663  MPI_Type_contiguous(5, struct_type, &contig_struct);
664  MPI_Type_commit(&contig_struct);
665  printf("\nSend contiguous of 5 struct, receive 5x struct\n");
666
667#ifdef TEST_INTERNAL
668  copy_data2(&s1, 1, contig_struct, &s2, 5, struct_type);
669#else
670  MPI_Isend(&s1, 1, contig_struct, 0, 0, MPI_COMM_WORLD, &req);
671  MPI_Irecv(&s2, 5, struct_type, 0, 0, MPI_COMM_WORLD, &req);
672#endif
673
674  for (i = 0; i < 5; i++)
675    if (!(s1[i].a == s2[i].a && s1[i].b == s2[i].b && s1[i].c == s2[i].c))
676    {
677      printf(">>>FAILED: test_multiple_struct\n");
678      errcount++;
679      return;
680    }
681}
682
683// packed type.  Pack some arbitrary simple
684// values into a buffer and copy.
685
686void test_packed()
687{
688  int SIZE = 77;
689  int i = 8;
690  char c[] = "abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyzabcdefg\0";
691  int j;
692  double k = 0.234234, l;
693  char d[SIZE];
694  char buffer[110];
695  char recv[110];
696  int position = 0;
697
698  printf("\nSimple packed type (int, char, double)\n");
699  c[SIZE-1] = '\0';
700  MPI_Pack(&i, 1, MPI_INT, buffer, 110, &position, MPI_COMM_WORLD);
701  MPI_Pack(c, SIZE, MPI_CHAR, buffer, 110, &position, MPI_COMM_WORLD);
702  MPI_Pack(&k, 1, MPI_DOUBLE, buffer, 110, &position, MPI_COMM_WORLD);
703#ifdef TEST_INTERNAL
704  copy_data2(&buffer, position, MPI_PACKED, &recv, position, MPI_PACKED);
705#else
706  MPI_Isend(&buffer, position, MPI_PACKED, 0, 0, MPI_COMM_WORLD, &req);
707  MPI_Irecv(&recv, position, MPI_PACKED, 0, 0, MPI_COMM_WORLD,&req);
708#endif
709
710  position = 0;
711
712  MPI_Unpack(&recv, 110, &position, &j, 1, MPI_INT, MPI_COMM_WORLD);
713  MPI_Unpack(&recv, 110, &position, d, SIZE, MPI_CHAR, MPI_COMM_WORLD);
714  MPI_Unpack(&recv, 110, &position, &l, 1, MPI_DOUBLE, MPI_COMM_WORLD);
715
716  if (!(i==j && k==l))
717  {
718    printf(">>>FAILED: test_packed\n");
719    errcount++;
720    return;
721  }
722}
723
724// Complex pack.  Includes struct types that are packed
725
726void test_packed_complex()
727{
728  struct {int a; char b; char c; long d; } s1;
729  struct {int a; char b; char c; long d; } s2;
730
731  MPI_Aint size;
732  int pos = 0;
733  int x = 10, y;
734  float f = 0.345, g;
735  char buf[100];
736  char rbuf[100];
737  int blens[3] = {1, 1, 1};
738  MPI_Aint disps[3];
739  MPI_Datatype types[3] = {MPI_INT, MPI_CHAR, MPI_LONG};
740  MPI_Datatype struct_type;
741
742  disps[0] = 0;
743  disps[1] = (char*) &s1.b - (char*)&s1.a;
744  disps[2] = (char*) &s1.d - (char*)&s1.a;
745
746  printf("\nComplex packed type\n");
747
748  MPI_Type_struct(3, blens, disps, types, &struct_type);
749  s1.a = 10;
750  s1.b = 'x';
751  s1.c = 'a';
752  s1.d = 3000;
753
754  MPI_Pack_size(1, struct_type,MPI_COMM_WORLD, &size);
755  MPI_Pack(&x, 1, MPI_INT, buf, 100, &pos, MPI_COMM_WORLD);
756  MPI_Pack(&s1, 1, struct_type, buf, 100, &pos, MPI_COMM_WORLD);
757  MPI_Pack(&f, 1, MPI_FLOAT, buf, 100, &pos, MPI_COMM_WORLD);
758
759#ifdef TEST_INTERNAL
760  copy_data2(&buf, pos, MPI_PACKED, &rbuf, pos, MPI_PACKED);
761#else
762  MPI_Isend(&buf, pos, MPI_PACKED, 0, 0, MPI_COMM_WORLD, &req);
763  MPI_Irecv(&rbuf, pos, MPI_PACKED, 0, 0, MPI_COMM_WORLD,&req);
764#endif
765
766  pos = 0;
767  MPI_Unpack(&rbuf, 100, &pos, &y, 1, MPI_INT, 0);
768  MPI_Unpack(&rbuf, 100, &pos, &s2, 1, struct_type, 0);
769  MPI_Unpack(&rbuf, 100, &pos, &g, 1, MPI_FLOAT, 0);
770
771  if (!(s1.a==s2.a && s1.b==s2.b /*&& s1.c==s2.c*/ && s1.d==s2.d && x == y && f == g))
772  {
773    printf(">>>FAILED: test_packed_complex\n");
774    errcount++;
775    return;
776  }
777
778}
779
780//Macro used in test_collectives
781#define test_eq(s1, s2, op) { \
782  printf("testing %s\n",op); \
783  if (!(s1.a == s2.a && s1.b == s2.b &&  \
784        s1.c == s2.c && s1.d == s2.d)) {\
785    errcount++; \
786    printf(">>>FAILED: test_collectives: %s\n", op); \
787  } \
788}
789
790void test_collectives()
791{
792  typedef struct {int a; int b; double c; long d;} struct_t;
793  MPI_Datatype struct_type;
794  struct_t s1 = {.a=1, .b=2, .c=4.00, .d=100},
795           s2 = {.a=0, .b=0, .c=0.00, .d=0  };
796  MPI_Aint disps[3];
797
798  int disp = 0;
799  int sendcount = 1, recvcount = 1;
800
801
802  int blens[3] = {2,1,1};
803  MPI_Datatype types[3] = {MPI_INT, MPI_DOUBLE, MPI_LONG};
804
805  disps[0] = 0;
806  disps[1] = (char*)&s1.c - (char*) &s1.a;
807  disps[2] = (char*)&s1.d - (char*) &s1.a;
808
809  MPI_Type_struct(3, blens, disps, types, &struct_type);
810  MPI_Type_commit(&struct_type);
811
812  MPI_Bcast(&s1, sendcount, struct_type, 0, MPI_COMM_WORLD);
813  MPI_Gather(&s1, sendcount, struct_type, &s2, recvcount,
814                  struct_type, 0, MPI_COMM_WORLD);
815  test_eq(s1,s2,"MPI_Gather");
816
817  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
818  MPI_Gatherv(&s1, sendcount, struct_type, &s2, &recvcount, &disp,
819                  struct_type, 0, MPI_COMM_WORLD);
820  test_eq(s1,s2,"MPI_Gatherv");
821  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
822  MPI_Allgather(&s1, sendcount, struct_type, &s2, recvcount,
823                  struct_type, MPI_COMM_WORLD);
824  test_eq(s1,s2,"MPI_Allgather");
825  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
826  MPI_Allgatherv(&s1, sendcount, struct_type, &s2, &recvcount, &disp,
827             struct_type, MPI_COMM_WORLD);
828  test_eq(s1,s2,"MPI_Allgatherv");
829
830  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
831  MPI_Scatter(&s1, sendcount, struct_type,
832              &s2, recvcount, struct_type,
833               0, MPI_COMM_WORLD);
834  test_eq(s1,s2,"MPI_Scatter");
835
836  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
837  MPI_Scatterv(&s1, &sendcount, &disp, struct_type, &s2, recvcount,
838             struct_type, 0, MPI_COMM_WORLD);
839  test_eq(s1,s2,"MPI_Scatterv");
840
841  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
842  MPI_Reduce(&s1, &s2, sendcount, struct_type, MPI_MAX, 0, MPI_COMM_WORLD);
843  test_eq(s1, s2, "MPI_Reduce");
844
845  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
846  MPI_Allreduce(&s1, &s2, sendcount, struct_type, MPI_MAX, MPI_COMM_WORLD);
847  test_eq(s1, s2, "MPI_Allreduce");
848
849  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
850  MPI_Alltoall(&s1, sendcount, struct_type,
851               &s2, recvcount, struct_type, MPI_COMM_WORLD);
852  test_eq(s1, s2, "MPI_Alltoall");
853
854  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
855  MPI_Alltoallv(&s1, &sendcount, &disp, struct_type,
856                &s2, &recvcount, &disp, struct_type, MPI_COMM_WORLD);
857  test_eq(s1, s2, "MPI_Alltoallv");
858
859  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
860  MPI_Reduce_scatter(&s1, &s2, &recvcount,struct_type, MPI_MAX, MPI_COMM_WORLD);
861  test_eq(s1, s2, "MPI_Reduce_scatter");
862
863  s2.a=0; s2.b=0; s2.c=0.00; s2.d=0;
864  MPI_Scan(&s1, &s2, sendcount,struct_type, MPI_MAX, MPI_COMM_WORLD);
865  test_eq(s1, s2, "MPI_Scan");
866}
867/*
868void vector_test()
869{
870  int c[3][2] = { {1, 2}, {3, 4}, {5, 6} };
871  int d[3][2] = { {0, 0}, {0, 0}, {0, 0} };
872  int i;
873  MPI_Datatype vector_type;
874  //test vector.  First and third rows of array
875  printf("\nVector type of first and third rows in INT array\n");
876  MPI_Type_vector(2, 2, 4, MPI_INT, &vector_type);
877
878  print_typemap(vector_type);
879
880  copy_data(&c, &d, vector_type);
881
882  for (i = 0; i < 3; i++)
883    printf("%d %d\n", d[i][0], d[i][1]);
884}
885
886void indexed_test()
887{
888  //we want the 2nd, 3rd, 5th, and 8th elements (starting at 0)
889  int i;
890  int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
891  int b[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
892  int blens[3] = {2, 1, 1};
893  int disps[3] = {2, 5, 8};
894  MPI_Datatype indexed_type;
895
896  printf("\nIndexed:  2nd, 3rd, 5th, and 8th elements (0 base)\n");
897  MPI_Type_indexed(3, blens, disps, MPI_INT, &indexed_type);
898
899  print_typemap(indexed_type);
900
901  copy_data(&a, &b, indexed_type);
902
903  for (i = 0; i < 10; i++)
904    printf("%d ", b[i]);
905  printf("\n");
906}
907
908void structtests()
909{
910  int a[5] = {1, 2, 3, 4, 5};
911  int b[5];
912
913  MPI_Datatype type, vector_type;
914
915  //test contiguous
916  printf("\nContiguous type of 5 MPI_INT\n");
917  MPI_Type_contiguous(5, MPI_INT, &type);
918  printf("Done.\n");
919  fflush(stdout);
920  print_typemap(type);
921  copy_data(&a, &b, type);
922  printf("b = %d\n", a[4]);
923}
924*/
925
926int main(int argc, char ** argv)
927{
928  char version[MPI_MAX_LIBRARY_VERSION_STRING];
929  int vlen;
930
931  MPI_Init(&argc, &argv);
932
933  MPI_Get_library_version(version,&vlen);
934  printf("MPI version=\"%s\" (len=%d)\n",version,vlen);
935
936//  structtests();
937//  indexed_test();
938//  struct_test();
939
940//  printf("\n\n---End of samples:  Testing now---\n\n");
941#ifdef TEST_INTERNAL
942  printf("Using internal tests\n");
943#endif
944  test_simple_contig();
945  test_simple_vector();
946  test_simple_hvector();
947  test_simple_indexed();
948  test_simple_bindexed();
949  test_simple_hindexed();
950  test_simple_struct();
951  test_complex_struct();
952  test_indexed_struct();
953  test_multiple();
954  test_multiple_struct();
955  test_packed();
956  test_packed_complex();
957  test_collectives();
958
959  MPI_Finalize();
960  if (errcount)
961    printf("Found %d errors\n", errcount);
962  else
963    printf(">>>PASSED ALL TESTS. No errors. <<<\n");
964
965  return(errcount);
966}
967
Note: See TracBrowser for help on using the repository browser.