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 #ifdef HAVE_ZOLTAN2_SCOTCH
60 // stdint.h for int64_t in scotch header
61 #include <stdint.h>
62 extern "C"{
63 #ifndef HAVE_ZOLTAN2_MPI
64 #include "scotch.h"
65 #else
66 #include "ptscotch.h"
67 #endif
68 }
69 #endif // HAVE_ZOLTAN2_SCOTCH
70 
71 #ifdef HAVE_ZOLTAN2_PARMETIS
72 
73 #ifndef HAVE_ZOLTAN2_MPI
74 // ParMETIS requires compilation with MPI.
75 // If MPI is not available, make compilation fail.
76 #error "TPL ParMETIS requires compilation with MPI. Configure with -DTPL_ENABLE_MPI:BOOL=ON or -DZoltan2_ENABLE_ParMETIS:BOOL=OFF"
77 
78 #else
79 
80 extern "C"{
81 #include "parmetis.h"
82 }
83 
84 #if (PARMETIS_MAJOR_VERSION < 4)
85 // Zoltan2 requires ParMETIS v4.x.
86 // Make compilation fail for earlier versions of ParMETIS.
87 #error "Specified version of ParMETIS is not compatible with Zoltan2; upgrade to ParMETIS v4 or later, or build Zoltan2 without ParMETIS."
88 
89 #else
90 
91 // MPI and ParMETIS version requirements are met. Proceed.
92 #define PARMETIS_IS_OK 1
93 
94 #endif // ParMETIS version check
95 #endif // HAVE_ZOLTAN2_MPI
96 #endif // HAVE_ZOLTAN2_PARMETIS
97 
98 
99 
100 #define PRINTMSG(s) \
101  std::cout << (s) << " " << __FILE__ << ":" << __LINE__ << std::endl
102 
103 int main(int argc, char *argv[])
104 {
105  Teuchos::GlobalMPISession session(&argc, &argv);
106  Teuchos::RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
107 
108  int ierr = 0;
109 
111  // Test conversions into integers
112 
113  // Assignments that should always work
114  // (since the zgno value fits in an integer)
115  int intIdx;
116  try {
117  int zgno = 123;
119  }
120  catch (std::exception &e) {
121  PRINTMSG("FAIL: int to int");
122  ierr++;
123  }
124 
125  try {
126  unsigned int zgno = 123;
128  }
129  catch (std::exception &e) {
130  PRINTMSG("FAIL: unsigned int to int");
131  ierr++;
132  }
133 
134  try {
135  long zgno = 123;
137  }
138  catch (std::exception &e) {
139  PRINTMSG("FAIL: long to int");
140  ierr++;
141  }
142 
143  try {
144  size_t zgno = 123;
146  }
147  catch (std::exception &e) {
148  PRINTMSG("FAIL: size_t to int");
149  ierr++;
150  }
151 
152  // Assignments that should not work
153  try {
154  long long zgno = (long long)1 << 40;
156  }
157  catch (std::exception &e) {
158  PRINTMSG("GOOD: big long long to int throws exception");
159  }
160 
161  try {
162  size_t zgno = (size_t)1 << 40;
164  }
165  catch (std::exception &e) {
166  PRINTMSG("GOOD: big size_t to int throws exception");
167  }
168 
169  try {
170  unsigned zgno = (1 << 31) + 1;
172  }
173  catch (std::exception &e) {
174  PRINTMSG("GOOD: huge unsigned to int throws exception");
175  }
176 
178  // Test conversions into size_t
179 
180  // Assignments that should always work
181 
182  size_t sizetIdx;
183  try {
184  long long zgno = (long long)1 << 40;
186  }
187  catch (std::exception &e) {
188  PRINTMSG("FAIL: big long long to size_t");
189  ierr++;
190  }
191 
192  try {
193  size_t zgno = (size_t)1 << 40;
195  }
196  catch (std::exception &e) {
197  PRINTMSG("FAIL: big size_t to size_t");
198  ierr++;
199  }
200 
202  // Test conversions into int64_t
203 
204  // Assignments that should always work
205 
206  int64_t int64Idx;
207  try {
208  long long zgno = (long long)1 << 40;
210  }
211  catch (std::exception &e) {
212  PRINTMSG("FAIL: big long long to int64_t");
213  ierr++;
214  }
215 
216  try {
217  size_t zgno = (size_t)1 << 40;
219  }
220  catch (std::exception &e) {
221  PRINTMSG("FAIL: big size_t to int64_t");
222  ierr++;
223  }
224 
225  // Assignments that should not work
226  try {
227  size_t zgno = ((size_t)1 << 63) + 1 ;
229  }
230  catch (std::exception &e) {
231  PRINTMSG("GOOD: huge size_t to int64_t threw exception");
232  }
233 
234 #ifdef HAVE_ZOLTAN2_SCOTCH
235  // Test conversions into SCOTCH_Num
237 
238  SCOTCH_Num scotchIdx;
239 
240  // Assignments that should always work
241  // (since the zgno value fits in an integer)
242  try {
243  int zgno = 123;
245  }
246  catch (std::exception &e) {
247  PRINTMSG("FAIL: int to SCOTCH_Num");
248  ierr++;
249  }
250 
251  try {
252  unsigned int zgno = 123;
254  }
255  catch (std::exception &e) {
256  PRINTMSG("FAIL: unsigned int to SCOTCH_Num");
257  ierr++;
258  }
259 
260  try {
261  long zgno = 123;
263  }
264  catch (std::exception &e) {
265  PRINTMSG("FAIL: long to SCOTCH_Num");
266  ierr++;
267  }
268 
269  try {
270  size_t zgno = 123;
272  }
273  catch (std::exception &e) {
274  PRINTMSG("FAIL: size_t to SCOTCH_Num");
275  ierr++;
276  }
277 
278  if (sizeof(SCOTCH_Num) == 8) {
279 
280  try {
281  long long zgno = (long long)1 << 40;
283  }
284  catch (std::exception &e) {
285  PRINTMSG("FAIL: big unsigned int to SCOTCH_Num");
286  ierr++;
287  }
288 
289  try {
290  size_t zgno = (size_t)1 << 40;
292  }
293  catch (std::exception &e) {
294  PRINTMSG("FAIL: big size_t to SCOTCH_Num");
295  ierr++;
296  }
297  }
298 
299  // Assignments that should not work
300  if (sizeof(SCOTCH_Num) == 4) {
301  try {
302  long long zgno = (long long)1 << 40;
304  }
305  catch (std::exception &e) {
306  PRINTMSG("GOOD: big long long to 4-byte SCOTCH_Num throws exception");
307  }
308 
309  try {
310  size_t zgno = (size_t)1 << 40;
312  }
313  catch (std::exception &e) {
314  PRINTMSG("GOOD: big size_t to 4-byte SCOTCH_Num throws exception");
315  }
316  }
317 
318 #endif // HAVE_ZOLTAN2_SCOTCH
319 
320 #ifdef PARMETIS_IS_OK
321  // Test conversions into ParMETIS' idx_t
323 
324  idx_t parmetisIdx;
325 
326  // Assignments that should always work
327  // (since the zgno value fits in an integer)
328  try {
329  int zgno = 123;
330  Zoltan2::TPL_Traits<idx_t,int>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
331  }
332  catch (std::exception &e) {
333  PRINTMSG("FAIL: int to ParMETIS' idx_t");
334  ierr++;
335  }
336 
337  try {
338  unsigned int zgno = 123;
340  }
341  catch (std::exception &e) {
342  PRINTMSG("FAIL: unsigned int to ParMETIS' idx_t");
343  ierr++;
344  }
345 
346  try {
347  long zgno = 123;
348  Zoltan2::TPL_Traits<idx_t,long>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
349  }
350  catch (std::exception &e) {
351  PRINTMSG("FAIL: long to ParMETIS' idx_t");
352  ierr++;
353  }
354 
355  try {
356  size_t zgno = 123;
357  Zoltan2::TPL_Traits<idx_t,size_t>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
358  }
359  catch (std::exception &e) {
360  PRINTMSG("FAIL: size_t to ParMETIS' idx_t");
361  ierr++;
362  }
363 
364  if (sizeof(idx_t) == 8) {
365 
366  try {
367  long long zgno = (long long)1 << 40;
369  }
370  catch (std::exception &e) {
371  PRINTMSG("FAIL: big unsigned int to ParMETIS' idx_t");
372  ierr++;
373  }
374 
375  try {
376  size_t zgno = (size_t)1 << 40;
377  Zoltan2::TPL_Traits<idx_t,size_t>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
378  }
379  catch (std::exception &e) {
380  PRINTMSG("FAIL: big size_t to ParMETIS' idx_t");
381  ierr++;
382  }
383  }
384 
385  // Assignments that should not work
386  if (sizeof(idx_t) == 4) {
387  try {
388  long long zgno = (long long)1 << 40;
390  }
391  catch (std::exception &e) {
392  PRINTMSG("GOOD: big long long to 4-byte ParMETIS' idx_t throws exception");
393  }
394 
395  try {
396  size_t zgno = (size_t)1 << 40;
397  Zoltan2::TPL_Traits<idx_t,size_t>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
398  }
399  catch (std::exception &e) {
400  PRINTMSG("GOOD: big size_t to 4-byte ParMETIS' idx_t throws exception");
401  }
402  }
403 #endif
404 
406  if (ierr == 0)
407  std::cout << "PASS" << std::endl;
408  else
409  std::cout << "FAIL" << std::endl;
410 
411  return 0;
412 }
413 
static void ASSIGN_TPL_T(tpl_t &a, zno_t b, const RCP< const Environment > &env)
int main(int argc, char *argv[])
Definition: TPLTraits.cpp:103
#define PRINTMSG(s)
Definition: TPLTraits.cpp:100
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
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.