Zoltan2
TPLTraits.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00046 
00047 // Unit test for Zoltan2_TPLTraits.hpp
00048 // Passes various zgno_t types to ASSIGN_TPL_T.
00049 // Some combinations should work without error; 
00050 // for these, this test FAILS if ASSIGN_TPL_T throws an error.
00051 // Some combinations should throw an error; 
00052 // for these, this test says it is GOOD if ASSIGN_TPL_T throws an error.
00053 
00054 #include <Teuchos_GlobalMPISession.hpp>   
00055 #include <Teuchos_RCP.hpp>
00056 #include <Zoltan2_Environment.hpp>
00057 #include <Zoltan2_TPLTraits.hpp>
00058 
00059 #ifdef HAVE_ZOLTAN2_SCOTCH
00060 // stdint.h for int64_t in scotch header
00061 #include <stdint.h>
00062 extern "C"{
00063 #ifndef HAVE_ZOLTAN2_MPI
00064 #include "scotch.h"
00065 #else
00066 #include "ptscotch.h"
00067 #endif
00068 }
00069 #endif   // HAVE_ZOLTAN2_SCOTCH
00070 
00071 #ifdef HAVE_ZOLTAN2_PARMETIS
00072 
00073 #ifndef HAVE_ZOLTAN2_MPI
00074 // ParMETIS requires compilation with MPI.  
00075 // If MPI is not available, make compilation fail.
00076 #error "TPL ParMETIS requires compilation with MPI.  Configure with -DTPL_ENABLE_MPI:BOOL=ON or -DZoltan2_ENABLE_ParMETIS:BOOL=OFF"
00077   
00078 #else
00079 
00080 extern "C"{
00081 #include "parmetis.h"
00082 }
00083 
00084 #if (PARMETIS_MAJOR_VERSION < 4)
00085 // Zoltan2 requires ParMETIS v4.x.  
00086 // Make compilation fail for earlier versions of ParMETIS.
00087 #error "Specified version of ParMETIS is not compatible with Zoltan2; upgrade to ParMETIS v4 or later, or build Zoltan2 without ParMETIS."
00088   
00089 #else
00090 
00091 // MPI and ParMETIS version requirements are met.  Proceed.
00092 #define PARMETIS_IS_OK 1
00093 
00094 #endif  // ParMETIS version check
00095 #endif  // HAVE_ZOLTAN2_MPI
00096 #endif  // HAVE_ZOLTAN2_PARMETIS
00097 
00098 
00099 
00100 #define PRINTMSG(s) \
00101   std::cout << (s) << " " << __FILE__ << ":" << __LINE__ << std::endl
00102 
00103 int main(int argc, char *argv[])
00104 {
00105   Teuchos::GlobalMPISession session(&argc, &argv);
00106   Teuchos::RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
00107 
00108   int ierr = 0;
00109 
00111   // Test conversions into integers
00112 
00113   // Assignments that should always work 
00114   // (since the zgno value fits in an integer)
00115   int intIdx;
00116   try {
00117     int zgno = 123;
00118     Zoltan2::TPL_Traits<int,int>::ASSIGN_TPL_T(intIdx, zgno, env);
00119   }
00120   catch (std::exception &e) {
00121     PRINTMSG("FAIL: int to int");
00122     ierr++;
00123   }
00124 
00125   try {
00126     unsigned int zgno = 123;
00127     Zoltan2::TPL_Traits<int,unsigned int>::ASSIGN_TPL_T(intIdx, zgno, env);
00128   }
00129   catch (std::exception &e) {
00130     PRINTMSG("FAIL: unsigned int to int");
00131     ierr++;
00132   }
00133 
00134   try {
00135     long zgno = 123;
00136     Zoltan2::TPL_Traits<int,long>::ASSIGN_TPL_T(intIdx, zgno, env);
00137   }
00138   catch (std::exception &e) {
00139     PRINTMSG("FAIL: long to int");
00140     ierr++;
00141   }
00142  
00143   try {
00144     size_t zgno = 123;
00145     Zoltan2::TPL_Traits<int,size_t>::ASSIGN_TPL_T(intIdx, zgno, env);
00146   }
00147   catch (std::exception &e) {
00148     PRINTMSG("FAIL: size_t to int");
00149     ierr++;
00150   }
00151 
00152   // Assignments that should not work
00153   try {
00154     long long zgno = (long long)1 << 40;
00155     Zoltan2::TPL_Traits<int,long long>::ASSIGN_TPL_T(intIdx, zgno, env);
00156   }
00157   catch (std::exception &e) {
00158     PRINTMSG("GOOD: big long long to int throws exception");
00159   }
00160 
00161   try {
00162     size_t zgno = (size_t)1 << 40;
00163     Zoltan2::TPL_Traits<int,size_t>::ASSIGN_TPL_T(intIdx, zgno, env);
00164   }
00165   catch (std::exception &e) {
00166     PRINTMSG("GOOD: big size_t to int throws exception");
00167   }
00168 
00169   try {
00170     unsigned zgno = (1 << 31) + 1;
00171     Zoltan2::TPL_Traits<int,unsigned>::ASSIGN_TPL_T(intIdx, zgno, env);
00172   }
00173   catch (std::exception &e) {
00174     PRINTMSG("GOOD: huge unsigned to int throws exception");
00175   }
00176 
00178   // Test conversions into size_t
00179 
00180   // Assignments that should always work 
00181 
00182   size_t sizetIdx;
00183   try {
00184     long long zgno = (long long)1 << 40;
00185     Zoltan2::TPL_Traits<size_t,long long>::ASSIGN_TPL_T(sizetIdx, zgno, env);
00186   }
00187   catch (std::exception &e) {
00188     PRINTMSG("FAIL: big long long to size_t");
00189     ierr++;
00190   }
00191  
00192   try {
00193     size_t zgno = (size_t)1 << 40;
00194     Zoltan2::TPL_Traits<size_t,size_t>::ASSIGN_TPL_T(sizetIdx, zgno, env);
00195   }
00196   catch (std::exception &e) {
00197     PRINTMSG("FAIL: big size_t to size_t");
00198     ierr++;
00199   }
00200 
00202   // Test conversions into int64_t
00203 
00204   // Assignments that should always work 
00205 
00206   int64_t int64Idx;
00207   try {
00208     long long zgno = (long long)1 << 40;
00209     Zoltan2::TPL_Traits<int64_t,long long>::ASSIGN_TPL_T(int64Idx, zgno, env);
00210   }
00211   catch (std::exception &e) {
00212     PRINTMSG("FAIL: big long long to int64_t");
00213     ierr++;
00214   }
00215  
00216   try {
00217     size_t zgno = (size_t)1 << 40;
00218     Zoltan2::TPL_Traits<int64_t,size_t>::ASSIGN_TPL_T(int64Idx, zgno, env);
00219   }
00220   catch (std::exception &e) {
00221     PRINTMSG("FAIL: big size_t to int64_t");
00222     ierr++;
00223   }
00224 
00225   // Assignments that should not work
00226   try {
00227     size_t zgno = ((size_t)1 << 63) + 1 ;
00228     Zoltan2::TPL_Traits<int64_t,size_t>::ASSIGN_TPL_T(int64Idx, zgno, env);
00229   }
00230   catch (std::exception &e) {
00231     PRINTMSG("GOOD: huge size_t to int64_t threw exception");
00232   }
00233 
00234 #ifdef HAVE_ZOLTAN2_SCOTCH
00235 
00236   // Test conversions into SCOTCH_Num
00237 
00238   SCOTCH_Num scotchIdx;
00239 
00240   // Assignments that should always work 
00241   // (since the zgno value fits in an integer)
00242   try {
00243     int zgno = 123;
00244     Zoltan2::TPL_Traits<SCOTCH_Num,int>::ASSIGN_TPL_T(scotchIdx, zgno, env);
00245   }
00246   catch (std::exception &e) {
00247     PRINTMSG("FAIL: int to SCOTCH_Num");
00248     ierr++;
00249   }
00250 
00251   try {
00252     unsigned int zgno = 123;
00253     Zoltan2::TPL_Traits<SCOTCH_Num,unsigned int>::ASSIGN_TPL_T(scotchIdx, zgno, env);
00254   }
00255   catch (std::exception &e) {
00256     PRINTMSG("FAIL: unsigned int to SCOTCH_Num");
00257     ierr++;
00258   }
00259 
00260   try {
00261     long zgno = 123;
00262     Zoltan2::TPL_Traits<SCOTCH_Num,long>::ASSIGN_TPL_T(scotchIdx, zgno, env);
00263   }
00264   catch (std::exception &e) {
00265     PRINTMSG("FAIL: long to SCOTCH_Num");
00266     ierr++;
00267   }
00268  
00269   try {
00270     size_t zgno = 123;
00271     Zoltan2::TPL_Traits<SCOTCH_Num,size_t>::ASSIGN_TPL_T(scotchIdx, zgno, env);
00272   }
00273   catch (std::exception &e) {
00274     PRINTMSG("FAIL: size_t to SCOTCH_Num");
00275     ierr++;
00276   }
00277 
00278   if (sizeof(SCOTCH_Num) == 8) {
00279 
00280     try {
00281       long long zgno = (long long)1 << 40;
00282       Zoltan2::TPL_Traits<SCOTCH_Num,long long>::ASSIGN_TPL_T(scotchIdx, zgno, env);
00283     }
00284     catch (std::exception &e) {
00285       PRINTMSG("FAIL: big unsigned int to SCOTCH_Num");
00286       ierr++;
00287     }
00288 
00289     try {
00290       size_t zgno = (size_t)1 << 40;
00291       Zoltan2::TPL_Traits<SCOTCH_Num,size_t>::ASSIGN_TPL_T(scotchIdx, zgno, env);
00292     }
00293     catch (std::exception &e) {
00294       PRINTMSG("FAIL: big size_t to SCOTCH_Num");
00295       ierr++;
00296     }
00297   }
00298 
00299   // Assignments that should not work
00300   if (sizeof(SCOTCH_Num) == 4) {
00301     try {
00302       long long zgno = (long long)1 << 40;
00303       Zoltan2::TPL_Traits<SCOTCH_Num,long long>::ASSIGN_TPL_T(scotchIdx, zgno, env);
00304     }
00305     catch (std::exception &e) {
00306       PRINTMSG("GOOD: big long long to 4-byte SCOTCH_Num throws exception");
00307     }
00308 
00309     try {
00310       size_t zgno = (size_t)1 << 40;
00311       Zoltan2::TPL_Traits<SCOTCH_Num,size_t>::ASSIGN_TPL_T(scotchIdx, zgno, env);
00312     }
00313     catch (std::exception &e) {
00314       PRINTMSG("GOOD: big size_t to 4-byte SCOTCH_Num throws exception");
00315     }
00316   }
00317 
00318 #endif  // HAVE_ZOLTAN2_SCOTCH
00319 
00320 #ifdef PARMETIS_IS_OK
00321 
00322   // Test conversions into ParMETIS' idx_t
00323 
00324   idx_t parmetisIdx;
00325 
00326   // Assignments that should always work 
00327   // (since the zgno value fits in an integer)
00328   try {
00329     int zgno = 123;
00330     Zoltan2::TPL_Traits<idx_t,int>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
00331   }
00332   catch (std::exception &e) {
00333     PRINTMSG("FAIL: int to ParMETIS' idx_t");
00334     ierr++;
00335   }
00336 
00337   try {
00338     unsigned int zgno = 123;
00339     Zoltan2::TPL_Traits<idx_t,unsigned int>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
00340   }
00341   catch (std::exception &e) {
00342     PRINTMSG("FAIL: unsigned int to ParMETIS' idx_t");
00343     ierr++;
00344   }
00345 
00346   try {
00347     long zgno = 123;
00348     Zoltan2::TPL_Traits<idx_t,long>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
00349   }
00350   catch (std::exception &e) {
00351     PRINTMSG("FAIL: long to ParMETIS' idx_t");
00352     ierr++;
00353   }
00354  
00355   try {
00356     size_t zgno = 123;
00357     Zoltan2::TPL_Traits<idx_t,size_t>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
00358   }
00359   catch (std::exception &e) {
00360     PRINTMSG("FAIL: size_t to ParMETIS' idx_t");
00361     ierr++;
00362   }
00363 
00364   if (sizeof(idx_t) == 8) {
00365 
00366     try {
00367       long long zgno = (long long)1 << 40;
00368       Zoltan2::TPL_Traits<idx_t,long long>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
00369     }
00370     catch (std::exception &e) {
00371       PRINTMSG("FAIL: big unsigned int to ParMETIS' idx_t");
00372       ierr++;
00373     }
00374 
00375     try {
00376       size_t zgno = (size_t)1 << 40;
00377       Zoltan2::TPL_Traits<idx_t,size_t>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
00378     }
00379     catch (std::exception &e) {
00380       PRINTMSG("FAIL: big size_t to ParMETIS' idx_t");
00381       ierr++;
00382     }
00383   }
00384 
00385   // Assignments that should not work
00386   if (sizeof(idx_t) == 4) {
00387     try {
00388       long long zgno = (long long)1 << 40;
00389       Zoltan2::TPL_Traits<idx_t,long long>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
00390     }
00391     catch (std::exception &e) {
00392       PRINTMSG("GOOD: big long long to 4-byte ParMETIS' idx_t throws exception");
00393     }
00394 
00395     try {
00396       size_t zgno = (size_t)1 << 40;
00397       Zoltan2::TPL_Traits<idx_t,size_t>::ASSIGN_TPL_T(parmetisIdx, zgno, env);
00398     }
00399     catch (std::exception &e) {
00400       PRINTMSG("GOOD: big size_t to 4-byte ParMETIS' idx_t throws exception");
00401     }
00402   }
00403 #endif
00404 
00405 
00406   if (ierr == 0)
00407     std::cout << "PASS" << std::endl;
00408   else
00409     std::cout << "FAIL" << std::endl;
00410 
00411   return 0;
00412 }
00413