Zoltan2 Version of the Day
Environment.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 // Testing Zoltan2::Environment
00051 #include <Zoltan2_Environment.hpp>
00052 #include <Zoltan2_Parameters.hpp>
00053 #include <Zoltan2_TestHelpers.hpp>
00054 #include <Teuchos_ParameterList.hpp>
00055 #include <Teuchos_DefaultComm.hpp>
00056 
00057 using std::string;
00058 using Teuchos::ParameterEntry;
00059 using Teuchos::RCP;
00060 using Teuchos::Comm;
00061 using Zoltan2::Environment;
00062 
00063 int checkErrorCode(Teuchos::RCP<const Teuchos::Comm<int> > &comm, int code)
00064 {
00065   int rank = comm->getRank();
00066   if (code > 0) 
00067     std::cerr << "Proc " << rank << " error: " << code << std::endl;
00068   comm->barrier();
00069 
00070   // Will return 1 if any process has a non-zero code
00071   TEST_FAIL_AND_RETURN_VALUE(*comm, code==0, "test failure", 1);
00072 
00073   return 0;
00074 }
00075 
00076 int main(int argc, char *argv[])
00077 {
00078   Teuchos::GlobalMPISession session(&argc, &argv);
00079   Teuchos::RCP<const Comm<int> > comm =
00080     Teuchos::DefaultComm<int>::getComm();
00081 
00082   int rank = comm->getRank();
00083   int nprocs = comm->getSize();
00084   int fail = 0;
00085 
00087   // Create a default Environment and test it
00088 
00089   Environment *defEnv = NULL;
00090 
00091   try{
00092     defEnv = new Environment;
00093   }
00094   catch(std::exception &e){
00095     std::cerr << e.what() << std::endl;
00096     fail=1000;
00097   }
00098 
00099   if (checkErrorCode(comm, fail))
00100     return 1;
00101 
00102   if (!fail && defEnv->myRank_ != rank)
00103     fail = 1001;
00104 
00105   if (!fail && defEnv->numProcs_ != nprocs)
00106     fail = 1002;
00107 
00108   if (!fail && defEnv->comm_->getSize() != nprocs)
00109     fail = 1003;
00110 
00111   if (!fail && defEnv->doStatus() != true)
00112     fail = 1005;
00113   
00114   if (!fail && defEnv->doTiming() != false)
00115     fail = 1006;
00116 
00117   if (!fail && defEnv->doMemoryProfiling() != false)
00118     fail = 1007;
00119 
00120   if (!fail && defEnv->errorCheckLevel_ != Zoltan2::BASIC_ASSERTION)
00121     fail = 1008;
00122 
00123   if (checkErrorCode(comm, fail))
00124     return 1;
00125 
00126   delete defEnv;
00127 
00129   // Set a few parameters and create an Environment
00130 
00131   Teuchos::ParameterList myParams("testParameterList");
00132 
00133   myParams.set("debug_level", "detailed_status");        
00134   myParams.set("debug_procs", "all");   
00135   myParams.set("debug_output_stream", "std::cout");
00136 
00137   if (nprocs > 3)
00138     myParams.set("memory_procs", "0-1,3"); 
00139   else
00140     myParams.set("memory_procs", "0"); 
00141 
00142   myParams.set("memory_output_file", "memInfo.txt");
00143 
00144   myParams.set("speed_versus_quality", "speed");
00145   myParams.set("memory_versus_speed", "memory");
00146 
00147   myParams.set("topology", "2,6,6");
00148   myParams.set("randomize_input", "true");
00149   myParams.set("partitioning_objective", "minimize_cut_edge_weight");
00150   myParams.set("imbalance_tolerance", 1.2);
00151 
00152   Environment *env = NULL;
00153 
00154   try{
00155     env = new Environment(myParams, comm);
00156   }
00157   catch(std::exception &e){
00158     std::cerr << e.what() << std::endl;
00159     fail=2000;
00160   }
00161 
00162   if (!fail){
00163      try{
00164        env->debug(Zoltan2::BASIC_STATUS, "A basic debugging message.");
00165      }
00166      catch(std::exception &e){
00167        std::cerr << e.what() << std::endl;
00168        fail=3000;
00169      }
00170   }
00171 
00172   if (!fail){
00173      try{
00174        env->memory("Memory info");
00175        env->memory("Memory info next");
00176        env->memory("Memory info after");
00177      }
00178      catch(std::exception &e){
00179        std::cerr << e.what() << std::endl;
00180        fail=3002;
00181      }
00182   }
00183 
00184   if (checkErrorCode(comm, fail))
00185     return 1;
00186 
00187   if (!fail && env->myRank_ != rank)
00188     fail = 2001;
00189 
00190   if (!fail && env->numProcs_ != nprocs)
00191     fail = 2002;
00192 
00193   if (!fail && env->comm_->getSize() != nprocs)
00194     fail = 2003;
00195 
00196   if (!fail){
00197     const Teuchos::ParameterList &pl1 = env->getParameters();
00198     const ParameterEntry *dl = pl1.getEntryPtr("debug_level");
00199   
00200     if (!dl){
00201       fail = 2004;
00202     }
00203     else if (!(dl->isType<int>())){
00204       fail = 2013;
00205     }
00206     else{
00207       int value;
00208       int &val = dl->getValue<int>(&value);
00209       if (val != Zoltan2::DETAILED_STATUS) 
00210         fail = 2005;
00211     }
00212   }
00213 
00214   if (!fail && env->errorCheckLevel_ != Zoltan2::BASIC_ASSERTION)
00215     fail = 2008;
00216 
00217   if (checkErrorCode(comm, fail))
00218     return 1;
00219 
00220   if (rank==0){
00221     std::cout << "\nA test parameter list" << std::endl;
00222     const Teuchos::ParameterList &envParams = env->getParameters();
00223     try{
00224       envParams.print();
00225     }
00226     catch(std::exception &e){
00227       std::cerr << e.what() << std::endl;
00228       fail=2013;
00229     }
00230   }
00231 
00232   if (checkErrorCode(comm, fail))
00233     return 1;
00234 
00236   // Given an existing Environment, get its parameters and
00237   // add some new parameters and create a new Environment.
00238 
00239   RCP<const Comm<int> > oldComm = env->comm_;
00240   const Teuchos::ParameterList &oldParams = env->getUnvalidatedParameters();
00241   
00242   Teuchos::ParameterList newParams = oldParams;
00243   newParams.set("error_check_level", "debug_mode_assertions");
00244   newParams.set("memory_versus_speed", "speed");
00245   newParams.remove("memory_output_file");
00246   newParams.set("imbalance_tolerance", "1.05");
00247   newParams.set("algorithm", "phg");
00248   newParams.set("partitioning_objective", "minimize_cut_edge_weight");
00249   
00250   RCP<Environment> newEnv;
00251 
00252   try{
00253     newEnv = Teuchos::rcp(new Environment(newParams, oldComm));
00254   }
00255   catch(std::exception &e){
00256     std::cerr << e.what() << std::endl;
00257     fail=3000;
00258   }
00259 
00260   if (checkErrorCode(comm, fail))
00261     return 1;
00262 
00263   if (!fail && newEnv->errorCheckLevel_ != Zoltan2::DEBUG_MODE_ASSERTION)
00264     fail = 3001;
00265 
00266   if (!fail && rank==0){
00267     std::cout << "\nA few changes/additions to the list" << std::endl;
00268     const Teuchos::ParameterList &envParams = newEnv->getParameters();
00269     try{
00270       envParams.print();
00271     }
00272     catch(std::exception &e){
00273       std::cerr << e.what() << std::endl;
00274       fail=3003;
00275     }
00276   }
00277 
00278   if (checkErrorCode(comm, fail))
00279     return 1;
00280 
00281   delete env;
00282 
00283   if (rank==0)
00284     std::cout << "PASS" << std::endl;
00285 
00286   return 0;
00287 }