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 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
46 
47 // Unit test for Zoltan2_TPLTraits.hpp
48 // Passes various zgno_t types to ASSIGN.
49 // Some combinations should work without error;
50 // for these, this test FAILS if ASSIGN throws an error.
51 // Some combinations should throw an error;
52 // for these, this test says it is GOOD if ASSIGN throws an error.
53 
54 #include <Teuchos_RCP.hpp>
55 #include <Zoltan2_Environment.hpp>
56 #include <Zoltan2_TPLTraits.hpp>
57 
58 #define MAX(a,b) ((a) > (b) ? (a) : (b))
59 
60 #ifdef HAVE_ZOLTAN2_SCOTCH
61 // stdint.h for int64_t in scotch header
62 #include <stdint.h>
63 extern "C"{
64 #ifndef HAVE_ZOLTAN2_MPI
65 #include "scotch.h"
66 #else
67 #include "ptscotch.h"
68 #endif
69 }
70 #endif // HAVE_ZOLTAN2_SCOTCH
71 
72 #ifdef HAVE_ZOLTAN2_PARMETIS
73 
74 extern "C"{
75 #include "parmetis.h"
76 }
77 
78 #define PARMETIS_IS_OK 1
79 
80 #endif // HAVE_ZOLTAN2_MPI
81 
82 
83 #define PRINTMSG(s) \
84  std::cout << (s) << " " << __FILE__ << ":" << __LINE__ << std::endl
85 
86 int main(int narg, char *arg[])
87 {
88  Tpetra::ScopeGuard tscope(&narg, &arg);
89 
90  int ierr = 0;
91 
93  // Test conversions into integers
94 
95  // Assignments that should always work
96  // (since the zgno value fits in an integer)
97  int intIdx;
98  try {
99  int zgno = 123;
101  }
102  catch (std::exception &e) {
103  PRINTMSG("FAIL: int to int");
104  ierr++;
105  }
106 
107  try {
108  unsigned int zgno = 123;
110  }
111  catch (std::exception &e) {
112  PRINTMSG("FAIL: unsigned int to int");
113  ierr++;
114  }
115 
116  try {
117  long zgno = 123;
119  }
120  catch (std::exception &e) {
121  PRINTMSG("FAIL: long to int");
122  ierr++;
123  }
124 
125  try {
126  size_t zgno = 123;
128  }
129  catch (std::exception &e) {
130  PRINTMSG("FAIL: size_t to int");
131  ierr++;
132  }
133 
134  // Assignments that should not work
135  try {
136  long long zgno = (long long)1 << 40;
138  }
139  catch (std::exception &e) {
140  PRINTMSG("GOOD: big long long to int throws exception");
141  }
142 
143  try {
144  size_t zgno = (size_t)1 << 40;
146  }
147  catch (std::exception &e) {
148  PRINTMSG("GOOD: big size_t to int throws exception");
149  }
150 
151  try {
152  unsigned zgno = (1 << 31) + 1;
154  }
155  catch (std::exception &e) {
156  PRINTMSG("GOOD: huge unsigned to int throws exception");
157  }
158 
160  // Test conversions into size_t
161 
162  // Assignments that should always work
163 
164  size_t sizetIdx;
165  try {
166  long long zgno = (long long)1 << 40;
168  }
169  catch (std::exception &e) {
170  PRINTMSG("FAIL: big long long to size_t");
171  ierr++;
172  }
173 
174  try {
175  size_t zgno = (size_t)1 << 40;
177  }
178  catch (std::exception &e) {
179  PRINTMSG("FAIL: big size_t to size_t");
180  ierr++;
181  }
182 
184  // Test conversions into int64_t
185 
186  // Assignments that should always work
187 
188  int64_t int64Idx;
189  try {
190  long long zgno = (long long)1 << 40;
192  }
193  catch (std::exception &e) {
194  PRINTMSG("FAIL: big long long to int64_t");
195  ierr++;
196  }
197 
198  try {
199  size_t zgno = (size_t)1 << 40;
201  }
202  catch (std::exception &e) {
203  PRINTMSG("FAIL: big size_t to int64_t");
204  ierr++;
205  }
206 
207  // Assignments that should not work
208  try {
209  size_t zgno = ((size_t)1 << 63) + 1 ;
211  }
212  catch (std::exception &e) {
213  PRINTMSG("GOOD: huge size_t to int64_t threw exception");
214  }
215 
216 #ifdef HAVE_ZOLTAN2_SCOTCH
217  // Test conversions into SCOTCH_Num
219 
220  SCOTCH_Num scotchIdx;
221 
222  // Assignments that should always work
223  // (since the zgno value fits in an integer)
224  try {
225  int zgno = 123;
227  }
228  catch (std::exception &e) {
229  PRINTMSG("FAIL: int to SCOTCH_Num");
230  ierr++;
231  }
232 
233  try {
234  unsigned int zgno = 123;
236  }
237  catch (std::exception &e) {
238  PRINTMSG("FAIL: unsigned int to SCOTCH_Num");
239  ierr++;
240  }
241 
242  try {
243  long zgno = 123;
245  }
246  catch (std::exception &e) {
247  PRINTMSG("FAIL: long to SCOTCH_Num");
248  ierr++;
249  }
250 
251  try {
252  size_t zgno = 123;
254  }
255  catch (std::exception &e) {
256  PRINTMSG("FAIL: size_t to SCOTCH_Num");
257  ierr++;
258  }
259 
260  if (sizeof(SCOTCH_Num) == 8) {
261 
262  try {
263  long long zgno = (long long)1 << 40;
265  }
266  catch (std::exception &e) {
267  PRINTMSG("FAIL: big unsigned int to SCOTCH_Num");
268  ierr++;
269  }
270 
271  try {
272  size_t zgno = (size_t)1 << 40;
274  }
275  catch (std::exception &e) {
276  PRINTMSG("FAIL: big size_t to SCOTCH_Num");
277  ierr++;
278  }
279  }
280 
281  // Assignments that should not work
282  if (sizeof(SCOTCH_Num) == 4) {
283  try {
284  long long zgno = (long long)1 << 40;
286  }
287  catch (std::exception &e) {
288  PRINTMSG("GOOD: big long long to 4-byte SCOTCH_Num throws exception");
289  }
290 
291  try {
292  size_t zgno = (size_t)1 << 40;
294  }
295  catch (std::exception &e) {
296  PRINTMSG("GOOD: big size_t to 4-byte SCOTCH_Num throws exception");
297  }
298  }
299 
300 #endif // HAVE_ZOLTAN2_SCOTCH
301 
302 #ifdef PARMETIS_IS_OK
303  // Test conversions into ParMETIS' idx_t
305 
306  idx_t parmetisIdx;
307 
308  // Assignments that should always work
309  // (since the zgno value fits in an integer)
310  try {
311  int zgno = 123;
312  Zoltan2::TPL_Traits<idx_t,int>::ASSIGN(parmetisIdx, zgno);
313  }
314  catch (std::exception &e) {
315  PRINTMSG("FAIL: int to ParMETIS' idx_t");
316  ierr++;
317  }
318 
319  try {
320  unsigned int zgno = 123;
322  }
323  catch (std::exception &e) {
324  PRINTMSG("FAIL: unsigned int to ParMETIS' idx_t");
325  ierr++;
326  }
327 
328  try {
329  long zgno = 123;
330  Zoltan2::TPL_Traits<idx_t,long>::ASSIGN(parmetisIdx, zgno);
331  }
332  catch (std::exception &e) {
333  PRINTMSG("FAIL: long to ParMETIS' idx_t");
334  ierr++;
335  }
336 
337  try {
338  size_t zgno = 123;
340  }
341  catch (std::exception &e) {
342  PRINTMSG("FAIL: size_t to ParMETIS' idx_t");
343  ierr++;
344  }
345 
346  if (sizeof(idx_t) == 8) {
347 
348  try {
349  long long zgno = (long long)1 << 40;
351  }
352  catch (std::exception &e) {
353  PRINTMSG("FAIL: big unsigned int to ParMETIS' idx_t");
354  ierr++;
355  }
356 
357  try {
358  size_t zgno = (size_t)1 << 40;
360  }
361  catch (std::exception &e) {
362  PRINTMSG("FAIL: big size_t to ParMETIS' idx_t");
363  ierr++;
364  }
365  }
366 
367  // Assignments that should not work
368  if (sizeof(idx_t) == 4) {
369  try {
370  long long zgno = (long long)1 << 40;
372  }
373  catch (std::exception &e) {
374  PRINTMSG("GOOD: big long long to 4-byte ParMETIS' idx_t throws exception");
375  }
376 
377  try {
378  size_t zgno = (size_t)1 << 40;
380  }
381  catch (std::exception &e) {
382  PRINTMSG("GOOD: big size_t to 4-byte ParMETIS' idx_t throws exception");
383  }
384  }
385 #endif
386 
388  // Test conversions into and from ZOLTAN_ID_PTR
389 
390  ZOLTAN_ID_PTR zoltanGID = new ZOLTAN_ID_TYPE[4];
391 
392  {
393  typedef char test_t;
394  test_t zgno = 'a';
395  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
396 
397  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
399  PRINTMSG("FAIL: NUM_ID wrong for char");
400  ierr++;
401  }
402 
404  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
405  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
406  PRINTMSG("FAIL: char to ZOLTAN_ID_PTR");
407  ierr++;
408  }
409 
410  test_t back;
412  if (back != zgno) {
413  PRINTMSG("FAIL: ZOLTAN_ID_PTR to char");
414  ierr++;
415  }
416  }
417 
418  {
419  typedef short test_t;
420  test_t zgno = 63;
421  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
422 
423  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
425  PRINTMSG("FAIL: NUM_ID wrong for short");
426  ierr++;
427  }
428 
430  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
431  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
432  PRINTMSG("FAIL: short to ZOLTAN_ID_PTR");
433  ierr++;
434  }
435 
436  test_t back;
438  if (back != zgno) {
439  PRINTMSG("FAIL: ZOLTAN_ID_PTR to short");
440  ierr++;
441  }
442  }
443 
444  {
445  typedef int test_t;
446  test_t zgno = 123;
447  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
448 
449  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
451  PRINTMSG("FAIL: NUM_ID wrong for int");
452  ierr++;
453  }
454 
456  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
457  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
458  PRINTMSG("FAIL: int to ZOLTAN_ID_PTR");
459  ierr++;
460  }
461 
462  test_t back;
464  if (back != zgno) {
465  PRINTMSG("FAIL: ZOLTAN_ID_PTR to int");
466  ierr++;
467  }
468  }
469 
470  {
471  typedef unsigned int test_t;
472  test_t zgno = 456;
473  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
474 
475  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
477  PRINTMSG("FAIL: NUM_ID wrong for unsigned int");
478  ierr++;
479  }
480 
482  if (zoltanGID[0] != zgno || zoltanGID[1] != 0 ||
483  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
484  PRINTMSG("FAIL: unsigned int to ZOLTAN_ID_PTR");
485  ierr++;
486  }
487 
488  test_t back;
490  if (back != zgno) {
491  PRINTMSG("FAIL: ZOLTAN_ID_PTR to unsigned int");
492  ierr++;
493  }
494  }
495 
496  {
497  typedef long long test_t;
498  test_t zgno = ((test_t)1 << 34) + (test_t)17;
499  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
500 
501  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
503  PRINTMSG("FAIL: NUM_ID wrong for long long");
504  ierr++;
505  }
506 
508  if (sizeof(ZOLTAN_ID_TYPE) == sizeof(unsigned int)) {
509  if (zoltanGID[0] != 17 || zoltanGID[1] != 4 ||
510  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
511  PRINTMSG("FAIL: long long to ZOLTAN_ID_PTR");
512  ierr++;
513  }
514  }
515  else if (sizeof(ZOLTAN_ID_TYPE) == sizeof(unsigned long long)) {
516  if (test_t(zoltanGID[0]) != zgno || zoltanGID[1] != 0 ||
517  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
518  PRINTMSG("FAIL: long long to ZOLTAN_ID_PTR");
519  ierr++;
520  }
521  }
522  else {
523  // should never get here
524  PRINTMSG("FAIL: unknown sizeof(ZOLTAN_ID_TYPE)");
525  ierr++;
526  }
527 
528 
529  test_t back;
531  if (back != zgno) {
532  std::cout << "back " << back << " != zgno " << zgno << std::endl;
533  PRINTMSG("FAIL: ZOLTAN_ID_PTR to long long");
534  ierr++;
535  }
536  }
537 
538  {
539  typedef unsigned long long test_t;
540  test_t zgno = ((test_t)1 << 36) + (test_t)25;
541  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
542 
543  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
545  PRINTMSG("FAIL: NUM_ID wrong for unsigned long long");
546  ierr++;
547  }
548 
550  if (sizeof(ZOLTAN_ID_TYPE) == sizeof(unsigned int)) {
551  if (zoltanGID[0] != 25 || zoltanGID[1] != 16 ||
552  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
553  PRINTMSG("FAIL: unsigned long long to ZOLTAN_ID_PTR");
554  ierr++;
555  }
556  }
557  else if (sizeof(ZOLTAN_ID_TYPE) == sizeof(unsigned long long)) {
558  if (zoltanGID[0] != zgno || zoltanGID[1] != 0 ||
559  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
560  PRINTMSG("FAIL: long long to ZOLTAN_ID_PTR");
561  ierr++;
562  }
563  }
564  else {
565  // should never get here
566  PRINTMSG("FAIL: unknown sizeof(ZOLTAN_ID_TYPE)");
567  ierr++;
568  }
569 
570 
571 
572  test_t back;
574  if (back != zgno) {
575  std::cout << "back " << back << " != zgno " << zgno << std::endl;
576  PRINTMSG("FAIL: ZOLTAN_ID_PTR to unsigned long long");
577  ierr++;
578  }
579  }
580 
581  {
582  typedef size_t test_t;
583  test_t zgno = 0;
584  for (size_t i = 0; i < 8*sizeof(test_t); i++) zgno += (test_t)1<<i;
585  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
586 
587  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
589  PRINTMSG("FAIL: NUM_ID wrong for size_t");
590  ierr++;
591  }
592 
594  for (int i = 0; i < num_gid; i++)
595  if (zoltanGID[i] != std::numeric_limits<ZOLTAN_ID_TYPE>::max()) {
596  PRINTMSG("FAIL: size_t to ZOLTAN_ID_PTR");
597  ierr++;
598  }
599  for (int i = num_gid; i < 4; i++)
600  if (zoltanGID[i] != 0) {
601  PRINTMSG("FAIL: size_t to ZOLTAN_ID_PTR");
602  ierr++;
603  }
604 
605  test_t back;
607  if (back != zgno) {
608  std::cout << "back " << back << " != zgno " << zgno << std::endl;
609  PRINTMSG("FAIL: ZOLTAN_ID_PTR to size_t");
610  ierr++;
611  }
612  }
613  delete [] zoltanGID;
614 
616 
617  if (ierr == 0)
618  std::cout << "PASS" << std::endl;
619  else
620  std::cout << "FAIL" << std::endl;
621 
622  return 0;
623 }
624 
int main(int narg, char *arg[])
#define MAX(a, b)
Definition: TPLTraits.cpp:58
#define PRINTMSG(s)
Definition: TPLTraits.cpp:83
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.