Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
DebugManager.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 // Testing the DebugManager object.
47 //
48 // Verbosity levels are
49 // NO_STATUS,
50 // BASIC_STATUS,
51 // DETAILED_STATUS,
52 // VERBOSE_DETAILED_STATUS
53 // NUM_STATUS_OUTPUT_LEVELS
54 //
55 // This test can only really be verified by reading the output.
56 // So we are testing that DebugManager doesn't crash.
57 
58 
59 #include <Zoltan2_DebugManager.hpp>
60 #include <Zoltan2_Parameters.hpp>
61 #include <Zoltan2_TestHelpers.hpp>
62 
63 #include <Teuchos_DefaultComm.hpp>
64 
65 #include <set>
66 #include <iostream>
67 #include <string>
68 #include <ostream>
69 
71 using Zoltan2::NO_STATUS;
75 
77 
78 int main(int narg, char *arg[])
79 {
80  Tpetra::ScopeGuard tscope(&narg, &arg);
81  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
82 
83  int rank = comm->getRank();
84  int nprocs = comm->getSize();
85  bool fail = false;
86 
87  std::set<string> basicMsgs, detailedMsgs, verboseMsgs;
88  std::set<string>::iterator next;
89 
90  std::ostringstream oss;
91  oss << "Proc " << rank << ": This is a ";
92 
93  basicMsgs.insert(oss.str()+string(" basic message."));
94  basicMsgs.insert(oss.str()+string("another basic message."));
95  detailedMsgs.insert(oss.str()+string(" detailed message."));
96  detailedMsgs.insert(oss.str()+string("another detailed message."));
97  verboseMsgs.insert(oss.str()+string(" verbose message."));
98  verboseMsgs.insert(oss.str()+string("another verbose message."));
99 
101  DebugManager *dm = NULL;
102 
103  // all print to std::cout
104 
105  bool iPrint = (rank%2 == 0);
106 
107  comm->barrier();
108 
109  for (int i = 0; i < numLevels; i++){
110 
111  level_t level = static_cast<level_t>(i);
112 
113  try {
114  dm = new DebugManager(rank, iPrint, std::cout, level);
115  }
116  catch(std::exception &e){
117  fail=true;
118  }
119 
120  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
121 
122  if (rank==0){
123  std::cout << "\nThere are " << nprocs << " processes. ";
124  std::cout << "Even ranks participate, output level is: " << level << std::endl;
125  }
126 
127  comm->barrier();
128 
129  try{
130  for (next=basicMsgs.begin(); next != basicMsgs.end(); ++next){
131  dm->print(BASIC_STATUS, *next);
132  }
133  comm->barrier();
134  for (next=detailedMsgs.begin(); next != detailedMsgs.end(); ++next){
135  dm->print(DETAILED_STATUS, *next);
136  }
137  comm->barrier();
138  for (next=verboseMsgs.begin(); next != verboseMsgs.end(); ++next){
139  dm->print(VERBOSE_DETAILED_STATUS, *next);
140  }
141  comm->barrier();
142  }
143  catch(std::exception &e){
144  fail=true;
145  }
146 
147  TEST_FAIL_AND_EXIT(*comm, !fail, "print to standard output", 1);
148 
149  delete dm;
150  }
151 
152  // Node zero prints to a file
153 
154  iPrint = (rank == 0);
155  comm->barrier();
156 
157  for (int i = 0; i < numLevels; i++){
158 
159  level_t level = static_cast<level_t>(i);
160 
161  std::ios_base::openmode flags = std::ios_base::out & std::ios_base::trunc;
162 
163  std::ofstream outF("testFile.txt", flags);
164 
165  try {
166  dm = new DebugManager(rank, iPrint, outF, level);
167  }
168  catch(std::exception &e){
169  fail=true;
170  }
171 
172  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
173 
174  if (rank==0){
175  std::cout << "\nThere are " << nprocs << " processes. ";
176  std::cout << "Rank zero only participates, output level is: ";
177  std::cout << level << std::endl;
178  }
179 
180  try {
181  for (next=basicMsgs.begin(); next != basicMsgs.end(); ++next){
182  dm->print(BASIC_STATUS, *next);
183  }
184  comm->barrier();
185 
186  for (next=detailedMsgs.begin(); next != detailedMsgs.end(); ++next){
187  dm->print(DETAILED_STATUS, *next);
188  }
189  comm->barrier();
190 
191  for (next=verboseMsgs.begin(); next != verboseMsgs.end(); ++next){
192  dm->print(VERBOSE_DETAILED_STATUS, *next);
193  }
194  comm->barrier();
195  }
196  catch(std::exception &e){
197  fail=true;
198  }
199 
200  delete dm;
201 
202  TEST_FAIL_AND_EXIT(*comm, !fail, "print to a file", 1);
203 
204  outF.close();
205 
206  comm->barrier();
207 
208  if (rank == 0){
209  std::ifstream inF("testFile.txt");
210  string s;
211  while (getline(inF, s)){
212  std::cout << s << std::endl;
213  }
214  inF.close();
215  system("rm testFile.txt"); // \todo fix for windows
216  }
217 
218  comm->barrier();
219  }
220 
221  if (rank==0)
222  std::cout << "PASS" << std::endl;
223 }
void print(MessageOutputLevel debugLevel, const std::string &output)
Print a debug or status message, if this process is one of those that is supposed to be doing output...
MessageOutputLevel
The amount of debugging or status output to print.
Defines Parameter related enumerators, declares functions.
int main(int narg, char *arg[])
Debug output manager for Zoltan2.
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
common code used by tests
sub-steps, each method&#39;s entry and exit
Zoltan2::MessageOutputLevel level_t
don&#39;t display status/debug messages
static const std::string fail
the status at each high level step
DebugManager contains the methods that perform output of debug and status messages.
include more detail about sub-steps