Zoltan2 Version of the Day
IdentifierTraits.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 // TODO: doxygen comments
00047 
00048 // TODO: just testing that the test compiles and looks reasonable
00049 //   We need to test the validity of the values returned in check_traits.
00050 
00051 #include <Zoltan2_TestHelpers.hpp>
00052 #include <Zoltan2_IdentifierTraits.hpp>
00053 #include <ostream>
00054 #include <string>
00055 #include <algorithm>
00056 #include <utility>
00057 #include <Teuchos_GlobalMPISession.hpp>   
00058 
00059 using namespace std;
00060 
00061 // TODO - add answers to check_traits to verify the results
00062 template <typename T> 
00063 void check_traits(
00064   Teuchos::RCP<const Teuchos::Comm<int> > &tcomm , T &val, T &higherVal)
00065 {
00066   const Teuchos::Comm<int> *comm = tcomm.get();
00067   typedef Zoltan2::IdentifierTraits<T> id;
00068   T valueList[2] = {val, higherVal};
00069 
00070   double k(0);
00071   if (id::hasUniqueKey())
00072     k = id::key(val);
00073 
00074   std::cout << "ID type: " << id::name() << std::endl;
00075   std::cout << "Value: " << id::stringify(val) << std::endl;
00076   std::cout << "\"Higher\" value: " << id::stringify(higherVal) << std::endl;
00077   std::cout << "Int hash code (non-unique): " << id::hashCode(val) << std::endl;
00078   std::cout << "Supports unique key: " << id::hasUniqueKey() << std::endl;
00079   if (id::hasUniqueKey())
00080     std::cout << "Key value: " << k << std::endl;
00081   std::cout << "Is Teuchos Global Ordinal: " << id::isGlobalOrdinal() << std::endl;
00082   std::cout << "Is valid id type: " << id::is_valid_id_type() << std::endl;
00083 
00085 
00086   bool supported = true;
00087   T lo, hi;
00088   std::cout << "minMax: ";
00089   try{
00090     id::minMax(valueList, 2, lo, hi);
00091   }
00092   catch(...){
00093     supported = false;
00094   }
00095 
00096   if (supported)
00097     std::cout << id::stringify(lo) << ", " << id::stringify(hi) << std::endl;
00098   else
00099     std::cout << "not supported" << std::endl;
00100 
00102 
00103   supported = true;
00104   T gmin, gmax;
00105   std::cout << "global minMax: ";
00106   try{
00107     id::globalMinMax(*comm, false, lo, hi, gmin, gmax);
00108   }
00109   catch(...){
00110     supported = false;
00111   }
00112 
00113   if (supported)
00114     std::cout << id::stringify(lo) << ", " << id::stringify(hi) << std::endl;
00115   else
00116     std::cout << "not supported" << std::endl;
00117 
00119 
00120   supported = true;
00121   T diff;
00122   std::cout << "Difference : ";
00123   try{
00124     diff = id::difference(val, higherVal);
00125   }
00126   catch(...){
00127     supported = false;
00128   }
00129 
00130   if (supported)
00131     std::cout << id::stringify(diff) << std::endl;
00132   else
00133     std::cout << "not supported" << std::endl;
00134 
00136 
00137   supported = true;
00138   bool consec=false; 
00139   std::cout << "Are consecutive: ";
00140   try{
00141     consec = id::areConsecutive(valueList, 2);
00142   }
00143   catch(...){
00144     supported = false;
00145   }
00146 
00147   if (supported)
00148     std::cout << consec << std::endl;
00149   else
00150     std::cout << "not supported" << std::endl;
00151   
00152   std::cout << std::endl;
00153 }
00154 int main(int argc, char *argv[])
00155 {
00156   Teuchos::GlobalMPISession session(&argc, &argv);
00157   Teuchos::RCP<const Teuchos::Comm<int> > nullComm;
00158   Teuchos::RCP<const Teuchos::Comm<int> > comm =
00159     Teuchos::DefaultComm<int>::getDefaultSerialComm(nullComm);
00160 
00161   int rank = session.getRank();
00162 
00163   if (rank == 0){
00164     char c='a';
00165     char c_other ='v';
00166     unsigned char uc='A';
00167     unsigned char uc_other ='Z';
00168     short int si=1024;
00169     short int si_other =11024;
00170     unsigned short int usi=1024;
00171     unsigned short int usi_other =11024;
00172     int i=1024;
00173     int i_other=11024;
00174     unsigned int ui=1024;
00175     unsigned int ui_other=11024;
00176     long int li=1024;
00177     long int li_other=11024;
00178     long unsigned int lui =1024;
00179     long unsigned int lui_other =11024;
00180 #ifdef HAVE_ZOLTAN2_LONG_LONG
00181     long long int lli = 3000000000;
00182     long long int lli_other = 3102400000000;
00183     unsigned long long int ulli = 3000000000;
00184     unsigned long long int ulli_other = 3102400000000;
00185 #endif
00186     std::pair<int, int> pairVals(1024, 1024);
00187     std::pair<int, int> pairVals_other(11024, 11024);
00188 
00189     std::pair<long, long> pairValsLong(1024, 1024);
00190     std::pair<long, long> pairValsLong_other(11024, 11024);
00191 
00192     string strVal("right front wheel");
00193     string strVal_other("left front wheel");
00194 
00195     check_traits(comm, c, c_other);
00196     check_traits(comm, uc, uc_other);
00197     check_traits(comm, si, si_other);
00198     check_traits(comm, usi, usi_other);
00199     check_traits(comm, i, i_other);
00200     check_traits(comm, ui, ui_other);
00201     check_traits(comm, li, li_other);
00202     check_traits(comm, lui, lui_other);
00203 #ifdef HAVE_ZOLTAN2_LONG_LONG
00204     check_traits(comm, lli, lli_other);
00205     check_traits(comm, ulli, ulli_other);
00206 #endif
00207     check_traits(comm, pairVals, pairVals_other);
00208     check_traits(comm, pairValsLong, pairValsLong_other);
00209     check_traits(comm, strVal, strVal_other);
00210 
00211     std::cout << "PASS" << std::endl;
00212   }
00213 
00214   return 0;
00215 }
00216