Zoltan2
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 
70 using namespace std;
72 using Zoltan2::NO_STATUS;
76 
78 
79 int main(int argc, char *argv[])
80 {
81  Teuchos::GlobalMPISession session(&argc, &argv);
82  Teuchos::RCP<const Teuchos::Comm<int> > comm =
83  Teuchos::DefaultComm<int>::getComm();
84 
85  int rank = comm->getRank();
86  int nprocs = comm->getSize();
87  bool fail = false;
88 
89  set<string> basicMsgs, detailedMsgs, verboseMsgs;
90  set<string>::iterator next;
91 
92  ostringstream oss;
93  oss << "Proc " << rank << ": This is a ";
94 
95  basicMsgs.insert(oss.str()+string(" basic message."));
96  basicMsgs.insert(oss.str()+string("another basic message."));
97  detailedMsgs.insert(oss.str()+string(" detailed message."));
98  detailedMsgs.insert(oss.str()+string("another detailed message."));
99  verboseMsgs.insert(oss.str()+string(" verbose message."));
100  verboseMsgs.insert(oss.str()+string("another verbose message."));
101 
103  DebugManager *dm = NULL;
104 
105  // all print to cout
106 
107  bool iPrint = (rank%2 == 0);
108 
109  comm->barrier();
110 
111  for (int i = 0; i < numLevels; i++){
112 
113  level_t level = static_cast<level_t>(i);
114 
115  try {
116  dm = new DebugManager(rank, iPrint, std::cout, level);
117  }
118  catch(std::exception &e){
119  fail=true;
120  }
121 
122  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
123 
124  if (rank==0){
125  std::cout << "\nThere are " << nprocs << " processes. ";
126  std::cout << "Even ranks participate, output level is: " << level << std::endl;
127  }
128 
129  comm->barrier();
130 
131  try{
132  for (next=basicMsgs.begin(); next != basicMsgs.end(); ++next){
133  dm->print(BASIC_STATUS, *next);
134  }
135  comm->barrier();
136  for (next=detailedMsgs.begin(); next != detailedMsgs.end(); ++next){
137  dm->print(DETAILED_STATUS, *next);
138  }
139  comm->barrier();
140  for (next=verboseMsgs.begin(); next != verboseMsgs.end(); ++next){
141  dm->print(VERBOSE_DETAILED_STATUS, *next);
142  }
143  comm->barrier();
144  }
145  catch(std::exception &e){
146  fail=true;
147  }
148 
149  TEST_FAIL_AND_EXIT(*comm, !fail, "print to standard output", 1);
150 
151  delete dm;
152  }
153 
154  // Node zero prints to a file
155 
156  iPrint = (rank == 0);
157  comm->barrier();
158 
159  for (int i = 0; i < numLevels; i++){
160 
161  level_t level = static_cast<level_t>(i);
162 
163  ios_base::openmode flags = ios_base::out & ios_base::trunc;
164 
165  ofstream outF("testFile.txt", flags);
166 
167  try {
168  dm = new DebugManager(rank, iPrint, outF, level);
169  }
170  catch(std::exception &e){
171  fail=true;
172  }
173 
174  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
175 
176  if (rank==0){
177  std::cout << "\nThere are " << nprocs << " processes. ";
178  std::cout << "Rank zero only participates, output level is: ";
179  std::cout << level << std::endl;
180  }
181 
182  try {
183  for (next=basicMsgs.begin(); next != basicMsgs.end(); ++next){
184  dm->print(BASIC_STATUS, *next);
185  }
186  comm->barrier();
187 
188  for (next=detailedMsgs.begin(); next != detailedMsgs.end(); ++next){
189  dm->print(DETAILED_STATUS, *next);
190  }
191  comm->barrier();
192 
193  for (next=verboseMsgs.begin(); next != verboseMsgs.end(); ++next){
194  dm->print(VERBOSE_DETAILED_STATUS, *next);
195  }
196  comm->barrier();
197  }
198  catch(std::exception &e){
199  fail=true;
200  }
201 
202  delete dm;
203 
204  TEST_FAIL_AND_EXIT(*comm, !fail, "print to a file", 1);
205 
206  outF.close();
207 
208  comm->barrier();
209 
210  if (rank == 0){
211  ifstream inF("testFile.txt");
212  string s;
213  while (getline(inF, s)){
214  std::cout << s << std::endl;
215  }
216  inF.close();
217  system("rm testFile.txt"); // \todo fix for windows
218  }
219 
220  comm->barrier();
221  }
222 
223  if (rank==0)
224  std::cout << "PASS" << std::endl;
225 }
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.
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
int main(int argc, char *argv[])
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