Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TPLTraits.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // Unit test for Zoltan2_TPLTraits.hpp
11 // Passes various zgno_t types to ASSIGN.
12 // Some combinations should work without error;
13 // for these, this test FAILS if ASSIGN throws an error.
14 // Some combinations should throw an error;
15 // for these, this test says it is GOOD if ASSIGN throws an error.
16 
17 #include <Teuchos_RCP.hpp>
18 #include <Zoltan2_Environment.hpp>
19 #include <Zoltan2_TPLTraits.hpp>
20 
21 #define MAX(a,b) ((a) > (b) ? (a) : (b))
22 
23 #ifdef HAVE_ZOLTAN2_SCOTCH
24 // stdint.h for int64_t in scotch header
25 #include <stdint.h>
26 extern "C"{
27 #ifndef HAVE_ZOLTAN2_MPI
28 #include "scotch.h"
29 #else
30 #include "ptscotch.h"
31 #endif
32 }
33 #endif // HAVE_ZOLTAN2_SCOTCH
34 
35 #ifdef HAVE_ZOLTAN2_PARMETIS
36 
37 extern "C"{
38 #include "parmetis.h"
39 }
40 
41 #define PARMETIS_IS_OK 1
42 
43 #endif // HAVE_ZOLTAN2_MPI
44 
45 
46 #define PRINTMSG(s) \
47  std::cout << (s) << " " << __FILE__ << ":" << __LINE__ << std::endl
48 
49 int main(int narg, char *arg[])
50 {
51  Tpetra::ScopeGuard tscope(&narg, &arg);
52 
53  int ierr = 0;
54 
56  // Test conversions into integers
57 
58  // Assignments that should always work
59  // (since the zgno value fits in an integer)
60  int intIdx;
61  try {
62  int zgno = 123;
64  }
65  catch (std::exception &e) {
66  PRINTMSG("FAIL: int to int");
67  ierr++;
68  }
69 
70  try {
71  unsigned int zgno = 123;
73  }
74  catch (std::exception &e) {
75  PRINTMSG("FAIL: unsigned int to int");
76  ierr++;
77  }
78 
79  try {
80  long zgno = 123;
82  }
83  catch (std::exception &e) {
84  PRINTMSG("FAIL: long to int");
85  ierr++;
86  }
87 
88  try {
89  size_t zgno = 123;
91  }
92  catch (std::exception &e) {
93  PRINTMSG("FAIL: size_t to int");
94  ierr++;
95  }
96 
97  // Assignments that should not work
98  try {
99  long long zgno = (long long)1 << 40;
101  }
102  catch (std::exception &e) {
103  PRINTMSG("GOOD: big long long to int throws exception");
104  }
105 
106  try {
107  size_t zgno = (size_t)1 << 40;
109  }
110  catch (std::exception &e) {
111  PRINTMSG("GOOD: big size_t to int throws exception");
112  }
113 
114  try {
115  unsigned zgno = (1 << 31) + 1;
117  }
118  catch (std::exception &e) {
119  PRINTMSG("GOOD: huge unsigned to int throws exception");
120  }
121 
123  // Test conversions into size_t
124 
125  // Assignments that should always work
126 
127  size_t sizetIdx;
128  try {
129  long long zgno = (long long)1 << 40;
131  }
132  catch (std::exception &e) {
133  PRINTMSG("FAIL: big long long to size_t");
134  ierr++;
135  }
136 
137  try {
138  size_t zgno = (size_t)1 << 40;
140  }
141  catch (std::exception &e) {
142  PRINTMSG("FAIL: big size_t to size_t");
143  ierr++;
144  }
145 
147  // Test conversions into int64_t
148 
149  // Assignments that should always work
150 
151  int64_t int64Idx;
152  try {
153  long long zgno = (long long)1 << 40;
155  }
156  catch (std::exception &e) {
157  PRINTMSG("FAIL: big long long to int64_t");
158  ierr++;
159  }
160 
161  try {
162  size_t zgno = (size_t)1 << 40;
164  }
165  catch (std::exception &e) {
166  PRINTMSG("FAIL: big size_t to int64_t");
167  ierr++;
168  }
169 
170  // Assignments that should not work
171  try {
172  size_t zgno = ((size_t)1 << 63) + 1 ;
174  }
175  catch (std::exception &e) {
176  PRINTMSG("GOOD: huge size_t to int64_t threw exception");
177  }
178 
179 #ifdef HAVE_ZOLTAN2_SCOTCH
180  // Test conversions into SCOTCH_Num
182 
183  SCOTCH_Num scotchIdx;
184 
185  // Assignments that should always work
186  // (since the zgno value fits in an integer)
187  try {
188  int zgno = 123;
190  }
191  catch (std::exception &e) {
192  PRINTMSG("FAIL: int to SCOTCH_Num");
193  ierr++;
194  }
195 
196  try {
197  unsigned int zgno = 123;
199  }
200  catch (std::exception &e) {
201  PRINTMSG("FAIL: unsigned int to SCOTCH_Num");
202  ierr++;
203  }
204 
205  try {
206  long zgno = 123;
208  }
209  catch (std::exception &e) {
210  PRINTMSG("FAIL: long to SCOTCH_Num");
211  ierr++;
212  }
213 
214  try {
215  size_t zgno = 123;
217  }
218  catch (std::exception &e) {
219  PRINTMSG("FAIL: size_t to SCOTCH_Num");
220  ierr++;
221  }
222 
223  if (sizeof(SCOTCH_Num) == 8) {
224 
225  try {
226  long long zgno = (long long)1 << 40;
228  }
229  catch (std::exception &e) {
230  PRINTMSG("FAIL: big unsigned int to SCOTCH_Num");
231  ierr++;
232  }
233 
234  try {
235  size_t zgno = (size_t)1 << 40;
237  }
238  catch (std::exception &e) {
239  PRINTMSG("FAIL: big size_t to SCOTCH_Num");
240  ierr++;
241  }
242  }
243 
244  // Assignments that should not work
245  if (sizeof(SCOTCH_Num) == 4) {
246  try {
247  long long zgno = (long long)1 << 40;
249  }
250  catch (std::exception &e) {
251  PRINTMSG("GOOD: big long long to 4-byte SCOTCH_Num throws exception");
252  }
253 
254  try {
255  size_t zgno = (size_t)1 << 40;
257  }
258  catch (std::exception &e) {
259  PRINTMSG("GOOD: big size_t to 4-byte SCOTCH_Num throws exception");
260  }
261  }
262 
263 #endif // HAVE_ZOLTAN2_SCOTCH
264 
265 #ifdef PARMETIS_IS_OK
266  // Test conversions into ParMETIS' idx_t
268 
269  idx_t parmetisIdx;
270 
271  // Assignments that should always work
272  // (since the zgno value fits in an integer)
273  try {
274  int zgno = 123;
275  Zoltan2::TPL_Traits<idx_t,int>::ASSIGN(parmetisIdx, zgno);
276  }
277  catch (std::exception &e) {
278  PRINTMSG("FAIL: int to ParMETIS' idx_t");
279  ierr++;
280  }
281 
282  try {
283  unsigned int zgno = 123;
285  }
286  catch (std::exception &e) {
287  PRINTMSG("FAIL: unsigned int to ParMETIS' idx_t");
288  ierr++;
289  }
290 
291  try {
292  long zgno = 123;
293  Zoltan2::TPL_Traits<idx_t,long>::ASSIGN(parmetisIdx, zgno);
294  }
295  catch (std::exception &e) {
296  PRINTMSG("FAIL: long to ParMETIS' idx_t");
297  ierr++;
298  }
299 
300  try {
301  size_t zgno = 123;
303  }
304  catch (std::exception &e) {
305  PRINTMSG("FAIL: size_t to ParMETIS' idx_t");
306  ierr++;
307  }
308 
309  if (sizeof(idx_t) == 8) {
310 
311  try {
312  long long zgno = (long long)1 << 40;
314  }
315  catch (std::exception &e) {
316  PRINTMSG("FAIL: big unsigned int to ParMETIS' idx_t");
317  ierr++;
318  }
319 
320  try {
321  size_t zgno = (size_t)1 << 40;
323  }
324  catch (std::exception &e) {
325  PRINTMSG("FAIL: big size_t to ParMETIS' idx_t");
326  ierr++;
327  }
328  }
329 
330  // Assignments that should not work
331  if (sizeof(idx_t) == 4) {
332  try {
333  long long zgno = (long long)1 << 40;
335  }
336  catch (std::exception &e) {
337  PRINTMSG("GOOD: big long long to 4-byte ParMETIS' idx_t throws exception");
338  }
339 
340  try {
341  size_t zgno = (size_t)1 << 40;
343  }
344  catch (std::exception &e) {
345  PRINTMSG("GOOD: big size_t to 4-byte ParMETIS' idx_t throws exception");
346  }
347  }
348 #endif
349 
351  // Test conversions into and from ZOLTAN_ID_PTR
352 
353  ZOLTAN_ID_PTR zoltanGID = new ZOLTAN_ID_TYPE[4];
354 
355  {
356  typedef char test_t;
357  test_t zgno = 'a';
358  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
359 
360  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
362  PRINTMSG("FAIL: NUM_ID wrong for char");
363  ierr++;
364  }
365 
367  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
368  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
369  PRINTMSG("FAIL: char to ZOLTAN_ID_PTR");
370  ierr++;
371  }
372 
373  test_t back;
375  if (back != zgno) {
376  PRINTMSG("FAIL: ZOLTAN_ID_PTR to char");
377  ierr++;
378  }
379  }
380 
381  {
382  typedef short test_t;
383  test_t zgno = 63;
384  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
385 
386  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
388  PRINTMSG("FAIL: NUM_ID wrong for short");
389  ierr++;
390  }
391 
393  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
394  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
395  PRINTMSG("FAIL: short to ZOLTAN_ID_PTR");
396  ierr++;
397  }
398 
399  test_t back;
401  if (back != zgno) {
402  PRINTMSG("FAIL: ZOLTAN_ID_PTR to short");
403  ierr++;
404  }
405  }
406 
407  {
408  typedef int test_t;
409  test_t zgno = 123;
410  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
411 
412  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
414  PRINTMSG("FAIL: NUM_ID wrong for int");
415  ierr++;
416  }
417 
419  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
420  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
421  PRINTMSG("FAIL: int to ZOLTAN_ID_PTR");
422  ierr++;
423  }
424 
425  test_t back;
427  if (back != zgno) {
428  PRINTMSG("FAIL: ZOLTAN_ID_PTR to int");
429  ierr++;
430  }
431  }
432 
433  {
434  typedef unsigned int test_t;
435  test_t zgno = 456;
436  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
437 
438  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
440  PRINTMSG("FAIL: NUM_ID wrong for unsigned int");
441  ierr++;
442  }
443 
445  if (zoltanGID[0] != zgno || zoltanGID[1] != 0 ||
446  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
447  PRINTMSG("FAIL: unsigned int to ZOLTAN_ID_PTR");
448  ierr++;
449  }
450 
451  test_t back;
453  if (back != zgno) {
454  PRINTMSG("FAIL: ZOLTAN_ID_PTR to unsigned int");
455  ierr++;
456  }
457  }
458 
459  {
460  typedef long long test_t;
461  test_t zgno = ((test_t)1 << 34) + (test_t)17;
462  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
463 
464  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
466  PRINTMSG("FAIL: NUM_ID wrong for long long");
467  ierr++;
468  }
469 
471  if (sizeof(ZOLTAN_ID_TYPE) == sizeof(unsigned int)) {
472  if (zoltanGID[0] != 17 || zoltanGID[1] != 4 ||
473  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
474  PRINTMSG("FAIL: long long to ZOLTAN_ID_PTR");
475  ierr++;
476  }
477  }
478  else if (sizeof(ZOLTAN_ID_TYPE) == sizeof(unsigned long long)) {
479  if (test_t(zoltanGID[0]) != zgno || zoltanGID[1] != 0 ||
480  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
481  PRINTMSG("FAIL: long long to ZOLTAN_ID_PTR");
482  ierr++;
483  }
484  }
485  else {
486  // should never get here
487  PRINTMSG("FAIL: unknown sizeof(ZOLTAN_ID_TYPE)");
488  ierr++;
489  }
490 
491 
492  test_t back;
494  if (back != zgno) {
495  std::cout << "back " << back << " != zgno " << zgno << std::endl;
496  PRINTMSG("FAIL: ZOLTAN_ID_PTR to long long");
497  ierr++;
498  }
499  }
500 
501  {
502  typedef unsigned long long test_t;
503  test_t zgno = ((test_t)1 << 36) + (test_t)25;
504  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
505 
506  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
508  PRINTMSG("FAIL: NUM_ID wrong for unsigned long long");
509  ierr++;
510  }
511 
513  if (sizeof(ZOLTAN_ID_TYPE) == sizeof(unsigned int)) {
514  if (zoltanGID[0] != 25 || zoltanGID[1] != 16 ||
515  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
516  PRINTMSG("FAIL: unsigned long long to ZOLTAN_ID_PTR");
517  ierr++;
518  }
519  }
520  else if (sizeof(ZOLTAN_ID_TYPE) == sizeof(unsigned long long)) {
521  if (zoltanGID[0] != zgno || zoltanGID[1] != 0 ||
522  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
523  PRINTMSG("FAIL: long long to ZOLTAN_ID_PTR");
524  ierr++;
525  }
526  }
527  else {
528  // should never get here
529  PRINTMSG("FAIL: unknown sizeof(ZOLTAN_ID_TYPE)");
530  ierr++;
531  }
532 
533 
534 
535  test_t back;
537  if (back != zgno) {
538  std::cout << "back " << back << " != zgno " << zgno << std::endl;
539  PRINTMSG("FAIL: ZOLTAN_ID_PTR to unsigned long long");
540  ierr++;
541  }
542  }
543 
544  {
545  typedef size_t test_t;
546  test_t zgno = 0;
547  for (size_t i = 0; i < 8*sizeof(test_t); i++) zgno += (test_t)1<<i;
548  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
549 
550  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
552  PRINTMSG("FAIL: NUM_ID wrong for size_t");
553  ierr++;
554  }
555 
557  for (int i = 0; i < num_gid; i++)
558  if (zoltanGID[i] != std::numeric_limits<ZOLTAN_ID_TYPE>::max()) {
559  PRINTMSG("FAIL: size_t to ZOLTAN_ID_PTR");
560  ierr++;
561  }
562  for (int i = num_gid; i < 4; i++)
563  if (zoltanGID[i] != 0) {
564  PRINTMSG("FAIL: size_t to ZOLTAN_ID_PTR");
565  ierr++;
566  }
567 
568  test_t back;
570  if (back != zgno) {
571  std::cout << "back " << back << " != zgno " << zgno << std::endl;
572  PRINTMSG("FAIL: ZOLTAN_ID_PTR to size_t");
573  ierr++;
574  }
575  }
576  delete [] zoltanGID;
577 
579 
580  if (ierr == 0)
581  std::cout << "PASS" << std::endl;
582  else
583  std::cout << "FAIL" << std::endl;
584 
585  return 0;
586 }
587 
int main(int narg, char **arg)
Definition: coloring1.cpp:164
#define MAX(a, b)
Definition: TPLTraits.cpp:21
#define PRINTMSG(s)
Definition: TPLTraits.cpp:46
static void ASSIGN(first_t &a, second_t b)
Traits class to handle conversions between gno_t/lno_t and TPL data types (e.g., ParMETIS&#39;s idx_t...
Defines the Environment class.