Zoltan 2 Version 0.5
DebugManager.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 the DebugManager object.
00047 //
00048 // Verbosity levels are
00049 //  NO_STATUS,
00050 //  BASIC_STATUS,
00051 //  DETAILED_STATUS,
00052 //  VERBOSE_DETAILED_STATUS
00053 //  NUM_STATUS_OUTPUT_LEVELS
00054 //
00055 //  This test can only really be verified by reading the output.
00056 //  So we are testing that DebugManager doesn't crash.
00057 
00058 
00059 #include <Zoltan2_DebugManager.hpp>
00060 #include <Zoltan2_Parameters.hpp>
00061 #include <Zoltan2_TestHelpers.hpp>
00062 
00063 #include <Teuchos_DefaultComm.hpp>
00064 
00065 #include <set>
00066 #include <iostream>
00067 #include <string>
00068 #include <ostream>
00069 
00070 using namespace std;
00071 using Zoltan2::DebugManager;
00072 using Zoltan2::NO_STATUS;
00073 using Zoltan2::BASIC_STATUS;
00074 using Zoltan2::DETAILED_STATUS;
00075 using Zoltan2::VERBOSE_DETAILED_STATUS;
00076 
00077 typedef Zoltan2::MessageOutputLevel level_t;
00078 
00079 int main(int argc, char *argv[])
00080 {
00081   Teuchos::GlobalMPISession session(&argc, &argv);
00082   Teuchos::RCP<const Teuchos::Comm<int> > comm =
00083     Teuchos::DefaultComm<int>::getComm();
00084 
00085   int rank = comm->getRank();
00086   int nprocs = comm->getSize();
00087   bool fail = false;
00088 
00089   set<string> basicMsgs, detailedMsgs, verboseMsgs; 
00090   set<string>::iterator next;
00091 
00092   ostringstream oss;
00093   oss << "Proc " << rank << ": This is a ";
00094 
00095   basicMsgs.insert(oss.str()+string(" basic message."));
00096   basicMsgs.insert(oss.str()+string("another basic message."));
00097   detailedMsgs.insert(oss.str()+string(" detailed message."));
00098   detailedMsgs.insert(oss.str()+string("another detailed message."));
00099   verboseMsgs.insert(oss.str()+string(" verbose message."));
00100   verboseMsgs.insert(oss.str()+string("another verbose message."));
00101 
00102   level_t numLevels = Zoltan2::NUM_STATUS_OUTPUT_LEVELS;
00103   DebugManager *dm = NULL;
00104 
00105   // all print to cout
00106 
00107   bool iPrint = (rank%2 == 0);
00108 
00109   comm->barrier();
00110 
00111   for (int i = 0; i < numLevels; i++){
00112 
00113     level_t level = static_cast<level_t>(i);
00114   
00115     try {
00116       dm = new DebugManager(rank, iPrint, std::cout, level);
00117     }
00118     catch(std::exception &e){
00119       fail=true;
00120     }
00121 
00122     TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
00123 
00124     if (rank==0){
00125       std::cout << "\nThere are " << nprocs << " processes. ";
00126       std::cout << "Even ranks participate, output level is: " << level << std::endl;
00127     }
00128 
00129     comm->barrier();
00130 
00131     try{
00132       for (next=basicMsgs.begin(); next != basicMsgs.end(); ++next){
00133         dm->print(BASIC_STATUS, *next);
00134       }
00135       comm->barrier();
00136       for (next=detailedMsgs.begin(); next != detailedMsgs.end(); ++next){
00137         dm->print(DETAILED_STATUS, *next);
00138       }
00139       comm->barrier();
00140       for (next=verboseMsgs.begin(); next != verboseMsgs.end(); ++next){
00141         dm->print(VERBOSE_DETAILED_STATUS, *next);
00142       }
00143       comm->barrier();
00144     }
00145     catch(std::exception &e){
00146       fail=true;
00147     }
00148 
00149     TEST_FAIL_AND_EXIT(*comm, !fail, "print to standard output", 1);
00150 
00151     delete dm;
00152   }
00153 
00154   // Node zero prints to a file
00155 
00156   iPrint = (rank == 0);
00157   comm->barrier();
00158 
00159   for (int i = 0; i < numLevels; i++){
00160 
00161     level_t level = static_cast<level_t>(i);
00162 
00163     ios_base::openmode flags = ios_base::out & ios_base::trunc;
00164 
00165     ofstream outF("testFile.txt", flags);
00166   
00167     try {
00168       dm = new DebugManager(rank, iPrint, outF, level);
00169     }
00170     catch(std::exception &e){
00171       fail=true;
00172     }
00173 
00174     TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
00175 
00176     if (rank==0){
00177       std::cout << "\nThere are " << nprocs << " processes. ";
00178       std::cout << "Rank zero only participates, output level is: ";
00179       std::cout << level << std::endl;
00180     }
00181 
00182     try {
00183       for (next=basicMsgs.begin(); next != basicMsgs.end(); ++next){
00184         dm->print(BASIC_STATUS, *next);
00185       }
00186       comm->barrier();
00187 
00188       for (next=detailedMsgs.begin(); next != detailedMsgs.end(); ++next){
00189         dm->print(DETAILED_STATUS, *next);
00190       }
00191       comm->barrier();
00192 
00193       for (next=verboseMsgs.begin(); next != verboseMsgs.end(); ++next){
00194         dm->print(VERBOSE_DETAILED_STATUS, *next);
00195       }
00196       comm->barrier();
00197     }
00198     catch(std::exception &e){
00199       fail=true;
00200     }
00201 
00202     delete dm;
00203 
00204     TEST_FAIL_AND_EXIT(*comm, !fail, "print to a file", 1);
00205 
00206     outF.close();
00207 
00208     comm->barrier();
00209 
00210     if (rank == 0){
00211       ifstream inF("testFile.txt");
00212       string s;
00213       while (getline(inF, s)){
00214         std::cout << s << std::endl;
00215       }
00216       inF.close();
00217       system("rm testFile.txt");  // \todo fix for windows
00218     }
00219 
00220     comm->barrier();
00221   }
00222 
00223   if (rank==0)
00224     std::cout << "PASS" << std::endl;
00225 }