Zoltan2
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_TPL_T.
49 // Some combinations should work without error;
50 // for these, this test FAILS if ASSIGN_TPL_T throws an error.
51 // Some combinations should throw an error;
52 // for these, this test says it is GOOD if ASSIGN_TPL_T throws an error.
53 
54 #include <Teuchos_GlobalMPISession.hpp>
55 #include <Teuchos_RCP.hpp>
56 #include <Zoltan2_Environment.hpp>
57 #include <Zoltan2_TPLTraits.hpp>
58 
59 #define MAX(a,b) ((a) > (b) ? (a) : (b))
60 
61 #ifdef HAVE_ZOLTAN2_SCOTCH
62 // stdint.h for int64_t in scotch header
63 #include <stdint.h>
64 extern "C"{
65 #ifndef HAVE_ZOLTAN2_MPI
66 #include "scotch.h"
67 #else
68 #include "ptscotch.h"
69 #endif
70 }
71 #endif // HAVE_ZOLTAN2_SCOTCH
72 
73 #ifdef HAVE_ZOLTAN2_PARMETIS
74 
75 #ifndef HAVE_ZOLTAN2_MPI
76 // ParMETIS requires compilation with MPI.
77 // If MPI is not available, make compilation fail.
78 #error "TPL ParMETIS requires compilation with MPI. Configure with -DTPL_ENABLE_MPI:BOOL=ON or -DZoltan2_ENABLE_ParMETIS:BOOL=OFF"
79 
80 #else
81 
82 extern "C"{
83 #include "parmetis.h"
84 }
85 
86 #if (PARMETIS_MAJOR_VERSION < 4)
87 // Zoltan2 requires ParMETIS v4.x.
88 // Make compilation fail for earlier versions of ParMETIS.
89 #error "Specified version of ParMETIS is not compatible with Zoltan2; upgrade to ParMETIS v4 or later, or build Zoltan2 without ParMETIS."
90 
91 #else
92 
93 // MPI and ParMETIS version requirements are met. Proceed.
94 #define PARMETIS_IS_OK 1
95 
96 #endif // ParMETIS version check
97 #endif // HAVE_ZOLTAN2_MPI
98 #endif // HAVE_ZOLTAN2_PARMETIS
99 
100 
101 
102 #define PRINTMSG(s) \
103  std::cout << (s) << " " << __FILE__ << ":" << __LINE__ << std::endl
104 
105 int main(int argc, char *argv[])
106 {
107  Teuchos::GlobalMPISession session(&argc, &argv);
108 
109  int ierr = 0;
110 
112  // Test conversions into integers
113 
114  // Assignments that should always work
115  // (since the zgno value fits in an integer)
116  int intIdx;
117  try {
118  int zgno = 123;
120  }
121  catch (std::exception &e) {
122  PRINTMSG("FAIL: int to int");
123  ierr++;
124  }
125 
126  try {
127  unsigned int zgno = 123;
129  }
130  catch (std::exception &e) {
131  PRINTMSG("FAIL: unsigned int to int");
132  ierr++;
133  }
134 
135  try {
136  long zgno = 123;
138  }
139  catch (std::exception &e) {
140  PRINTMSG("FAIL: long to int");
141  ierr++;
142  }
143 
144  try {
145  size_t zgno = 123;
147  }
148  catch (std::exception &e) {
149  PRINTMSG("FAIL: size_t to int");
150  ierr++;
151  }
152 
153  // Assignments that should not work
154  try {
155  long long zgno = (long long)1 << 40;
157  }
158  catch (std::exception &e) {
159  PRINTMSG("GOOD: big long long to int throws exception");
160  }
161 
162  try {
163  size_t zgno = (size_t)1 << 40;
165  }
166  catch (std::exception &e) {
167  PRINTMSG("GOOD: big size_t to int throws exception");
168  }
169 
170  try {
171  unsigned zgno = (1 << 31) + 1;
173  }
174  catch (std::exception &e) {
175  PRINTMSG("GOOD: huge unsigned to int throws exception");
176  }
177 
179  // Test conversions into size_t
180 
181  // Assignments that should always work
182 
183  size_t sizetIdx;
184  try {
185  long long zgno = (long long)1 << 40;
187  }
188  catch (std::exception &e) {
189  PRINTMSG("FAIL: big long long to size_t");
190  ierr++;
191  }
192 
193  try {
194  size_t zgno = (size_t)1 << 40;
196  }
197  catch (std::exception &e) {
198  PRINTMSG("FAIL: big size_t to size_t");
199  ierr++;
200  }
201 
203  // Test conversions into int64_t
204 
205  // Assignments that should always work
206 
207  int64_t int64Idx;
208  try {
209  long long zgno = (long long)1 << 40;
211  }
212  catch (std::exception &e) {
213  PRINTMSG("FAIL: big long long to int64_t");
214  ierr++;
215  }
216 
217  try {
218  size_t zgno = (size_t)1 << 40;
220  }
221  catch (std::exception &e) {
222  PRINTMSG("FAIL: big size_t to int64_t");
223  ierr++;
224  }
225 
226  // Assignments that should not work
227  try {
228  size_t zgno = ((size_t)1 << 63) + 1 ;
230  }
231  catch (std::exception &e) {
232  PRINTMSG("GOOD: huge size_t to int64_t threw exception");
233  }
234 
235 #ifdef HAVE_ZOLTAN2_SCOTCH
236  // Test conversions into SCOTCH_Num
238 
239  SCOTCH_Num scotchIdx;
240 
241  // Assignments that should always work
242  // (since the zgno value fits in an integer)
243  try {
244  int zgno = 123;
246  }
247  catch (std::exception &e) {
248  PRINTMSG("FAIL: int to SCOTCH_Num");
249  ierr++;
250  }
251 
252  try {
253  unsigned int zgno = 123;
255  }
256  catch (std::exception &e) {
257  PRINTMSG("FAIL: unsigned int to SCOTCH_Num");
258  ierr++;
259  }
260 
261  try {
262  long zgno = 123;
264  }
265  catch (std::exception &e) {
266  PRINTMSG("FAIL: long to SCOTCH_Num");
267  ierr++;
268  }
269 
270  try {
271  size_t zgno = 123;
273  }
274  catch (std::exception &e) {
275  PRINTMSG("FAIL: size_t to SCOTCH_Num");
276  ierr++;
277  }
278 
279  if (sizeof(SCOTCH_Num) == 8) {
280 
281  try {
282  long long zgno = (long long)1 << 40;
284  }
285  catch (std::exception &e) {
286  PRINTMSG("FAIL: big unsigned int to SCOTCH_Num");
287  ierr++;
288  }
289 
290  try {
291  size_t zgno = (size_t)1 << 40;
293  }
294  catch (std::exception &e) {
295  PRINTMSG("FAIL: big size_t to SCOTCH_Num");
296  ierr++;
297  }
298  }
299 
300  // Assignments that should not work
301  if (sizeof(SCOTCH_Num) == 4) {
302  try {
303  long long zgno = (long long)1 << 40;
305  }
306  catch (std::exception &e) {
307  PRINTMSG("GOOD: big long long to 4-byte SCOTCH_Num throws exception");
308  }
309 
310  try {
311  size_t zgno = (size_t)1 << 40;
313  }
314  catch (std::exception &e) {
315  PRINTMSG("GOOD: big size_t to 4-byte SCOTCH_Num throws exception");
316  }
317  }
318 
319 #endif // HAVE_ZOLTAN2_SCOTCH
320 
321 #ifdef PARMETIS_IS_OK
322  // Test conversions into ParMETIS' idx_t
324 
325  idx_t parmetisIdx;
326 
327  // Assignments that should always work
328  // (since the zgno value fits in an integer)
329  try {
330  int zgno = 123;
332  }
333  catch (std::exception &e) {
334  PRINTMSG("FAIL: int to ParMETIS' idx_t");
335  ierr++;
336  }
337 
338  try {
339  unsigned int zgno = 123;
341  }
342  catch (std::exception &e) {
343  PRINTMSG("FAIL: unsigned int to ParMETIS' idx_t");
344  ierr++;
345  }
346 
347  try {
348  long zgno = 123;
350  }
351  catch (std::exception &e) {
352  PRINTMSG("FAIL: long to ParMETIS' idx_t");
353  ierr++;
354  }
355 
356  try {
357  size_t zgno = 123;
359  }
360  catch (std::exception &e) {
361  PRINTMSG("FAIL: size_t to ParMETIS' idx_t");
362  ierr++;
363  }
364 
365  if (sizeof(idx_t) == 8) {
366 
367  try {
368  long long zgno = (long long)1 << 40;
370  }
371  catch (std::exception &e) {
372  PRINTMSG("FAIL: big unsigned int to ParMETIS' idx_t");
373  ierr++;
374  }
375 
376  try {
377  size_t zgno = (size_t)1 << 40;
379  }
380  catch (std::exception &e) {
381  PRINTMSG("FAIL: big size_t to ParMETIS' idx_t");
382  ierr++;
383  }
384  }
385 
386  // Assignments that should not work
387  if (sizeof(idx_t) == 4) {
388  try {
389  long long zgno = (long long)1 << 40;
391  }
392  catch (std::exception &e) {
393  PRINTMSG("GOOD: big long long to 4-byte ParMETIS' idx_t throws exception");
394  }
395 
396  try {
397  size_t zgno = (size_t)1 << 40;
399  }
400  catch (std::exception &e) {
401  PRINTMSG("GOOD: big size_t to 4-byte ParMETIS' idx_t throws exception");
402  }
403  }
404 #endif
405 
407  // Test conversions into and from ZOLTAN_ID_PTR
408 
409  ZOLTAN_ID_PTR zoltanGID = new ZOLTAN_ID_TYPE[4];
410 
411  {
412  typedef char test_t;
413  test_t zgno = 'a';
414  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
415 
416  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
418  PRINTMSG("FAIL: NUM_ID wrong for char");
419  ierr++;
420  }
421 
423  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
424  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
425  PRINTMSG("FAIL: char to ZOLTAN_ID_PTR");
426  ierr++;
427  }
428 
429  test_t back;
431  if (back != zgno) {
432  PRINTMSG("FAIL: ZOLTAN_ID_PTR to char");
433  ierr++;
434  }
435  }
436 
437  {
438  typedef short test_t;
439  test_t zgno = 63;
440  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
441 
442  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
444  PRINTMSG("FAIL: NUM_ID wrong for short");
445  ierr++;
446  }
447 
449  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
450  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
451  PRINTMSG("FAIL: short to ZOLTAN_ID_PTR");
452  ierr++;
453  }
454 
455  test_t back;
457  if (back != zgno) {
458  PRINTMSG("FAIL: ZOLTAN_ID_PTR to short");
459  ierr++;
460  }
461  }
462 
463  {
464  typedef int test_t;
465  test_t zgno = 123;
466  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
467 
468  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
470  PRINTMSG("FAIL: NUM_ID wrong for int");
471  ierr++;
472  }
473 
475  if (zoltanGID[0] != ZOLTAN_ID_TYPE(zgno) || zoltanGID[1] != 0 ||
476  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
477  PRINTMSG("FAIL: int to ZOLTAN_ID_PTR");
478  ierr++;
479  }
480 
481  test_t back;
483  if (back != zgno) {
484  PRINTMSG("FAIL: ZOLTAN_ID_PTR to int");
485  ierr++;
486  }
487  }
488 
489  {
490  typedef unsigned int test_t;
491  test_t zgno = 456;
492  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
493 
494  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
496  PRINTMSG("FAIL: NUM_ID wrong for unsigned int");
497  ierr++;
498  }
499 
501  if (zoltanGID[0] != zgno || zoltanGID[1] != 0 ||
502  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
503  PRINTMSG("FAIL: unsigned int to ZOLTAN_ID_PTR");
504  ierr++;
505  }
506 
507  test_t back;
509  if (back != zgno) {
510  PRINTMSG("FAIL: ZOLTAN_ID_PTR to unsigned int");
511  ierr++;
512  }
513  }
514 
515  {
516  typedef long long test_t;
517  test_t zgno = ((test_t)1 << 34) + (test_t)17;
518  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
519 
520  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
522  PRINTMSG("FAIL: NUM_ID wrong for long long");
523  ierr++;
524  }
525 
527  if (zoltanGID[0] != 17 || zoltanGID[1] != 4 ||
528  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
529  PRINTMSG("FAIL: long long to ZOLTAN_ID_PTR");
530  ierr++;
531  }
532 
533  test_t back;
535  if (back != zgno) {
536  std::cout << "back " << back << " != zgno " << zgno << std::endl;
537  PRINTMSG("FAIL: ZOLTAN_ID_PTR to long long");
538  ierr++;
539  }
540  }
541 
542  {
543  typedef unsigned long long test_t;
544  test_t zgno = ((test_t)1 << 36) + (test_t)25;
545  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
546 
547  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
549  PRINTMSG("FAIL: NUM_ID wrong for unsigned long long");
550  ierr++;
551  }
552 
554  if (zoltanGID[0] != 25 || zoltanGID[1] != 16 ||
555  zoltanGID[2] != 0 || zoltanGID[3] != 0) {
556  PRINTMSG("FAIL: unsigned long long to ZOLTAN_ID_PTR");
557  ierr++;
558  }
559 
560  test_t back;
562  if (back != zgno) {
563  std::cout << "back " << back << " != zgno " << zgno << std::endl;
564  PRINTMSG("FAIL: ZOLTAN_ID_PTR to unsigned long long");
565  ierr++;
566  }
567  }
568 
569  {
570  typedef size_t test_t;
571  test_t zgno = 0;
572  for (size_t i = 0; i < 8*sizeof(test_t); i++) zgno += (test_t)1<<i;
573  zoltanGID[0] = 0; zoltanGID[1] = 0; zoltanGID[2] = 0; zoltanGID[3] = 0;
574 
575  int num_gid = MAX(1, sizeof(test_t) / sizeof(ZOLTAN_ID_TYPE));
577  PRINTMSG("FAIL: NUM_ID wrong for size_t");
578  ierr++;
579  }
580 
582  for (int i = 0; i < num_gid; i++)
583  if (zoltanGID[i] != std::numeric_limits<ZOLTAN_ID_TYPE>::max()) {
584  PRINTMSG("FAIL: size_t to ZOLTAN_ID_PTR");
585  ierr++;
586  }
587  for (int i = num_gid; i < 4; i++)
588  if (zoltanGID[i] != 0) {
589  PRINTMSG("FAIL: size_t to ZOLTAN_ID_PTR");
590  ierr++;
591  }
592 
593  test_t back;
595  if (back != zgno) {
596  std::cout << "back " << back << " != zgno " << zgno << std::endl;
597  PRINTMSG("FAIL: ZOLTAN_ID_PTR to size_t");
598  ierr++;
599  }
600  }
601  delete [] zoltanGID;
602 
604 
605  if (ierr == 0)
606  std::cout << "PASS" << std::endl;
607  else
608  std::cout << "FAIL" << std::endl;
609 
610  return 0;
611 }
612 
int main(int argc, char *argv[])
Definition: TPLTraits.cpp:105
#define MAX(a, b)
Definition: TPLTraits.cpp:59
#define PRINTMSG(s)
Definition: TPLTraits.cpp:102
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.
static void ASSIGN_TPL_T(tpl_t &a, zno_t b)