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 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 //
11 // Testing the DebugManager object.
12 //
13 // Verbosity levels are
14 // NO_STATUS,
15 // BASIC_STATUS,
16 // DETAILED_STATUS,
17 // VERBOSE_DETAILED_STATUS
18 // NUM_STATUS_OUTPUT_LEVELS
19 //
20 // This test can only really be verified by reading the output.
21 // So we are testing that DebugManager doesn't crash.
22 
23 
24 #include <Zoltan2_DebugManager.hpp>
25 #include <Zoltan2_Parameters.hpp>
26 #include <Zoltan2_TestHelpers.hpp>
27 
28 #include <Teuchos_DefaultComm.hpp>
29 
30 #include <set>
31 #include <iostream>
32 #include <string>
33 #include <ostream>
34 
36 using Zoltan2::NO_STATUS;
40 
42 
43 int main(int narg, char *arg[])
44 {
45  Tpetra::ScopeGuard tscope(&narg, &arg);
46  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
47 
48  int rank = comm->getRank();
49  int nprocs = comm->getSize();
50  bool fail = false;
51 
52  std::set<string> basicMsgs, detailedMsgs, verboseMsgs;
53  std::set<string>::iterator next;
54 
55  std::ostringstream oss;
56  oss << "Proc " << rank << ": This is a ";
57 
58  basicMsgs.insert(oss.str()+string(" basic message."));
59  basicMsgs.insert(oss.str()+string("another basic message."));
60  detailedMsgs.insert(oss.str()+string(" detailed message."));
61  detailedMsgs.insert(oss.str()+string("another detailed message."));
62  verboseMsgs.insert(oss.str()+string(" verbose message."));
63  verboseMsgs.insert(oss.str()+string("another verbose message."));
64 
66  DebugManager *dm = NULL;
67 
68  // all print to std::cout
69 
70  bool iPrint = (rank%2 == 0);
71 
72  comm->barrier();
73 
74  for (int i = 0; i < numLevels; i++){
75 
76  level_t level = static_cast<level_t>(i);
77 
78  try {
79  dm = new DebugManager(rank, iPrint, std::cout, level);
80  }
81  catch(std::exception &e){
82  fail=true;
83  }
84 
85  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
86 
87  if (rank==0){
88  std::cout << "\nThere are " << nprocs << " processes. ";
89  std::cout << "Even ranks participate, output level is: " << level << std::endl;
90  }
91 
92  comm->barrier();
93 
94  try{
95  for (next=basicMsgs.begin(); next != basicMsgs.end(); ++next){
96  dm->print(BASIC_STATUS, *next);
97  }
98  comm->barrier();
99  for (next=detailedMsgs.begin(); next != detailedMsgs.end(); ++next){
100  dm->print(DETAILED_STATUS, *next);
101  }
102  comm->barrier();
103  for (next=verboseMsgs.begin(); next != verboseMsgs.end(); ++next){
104  dm->print(VERBOSE_DETAILED_STATUS, *next);
105  }
106  comm->barrier();
107  }
108  catch(std::exception &e){
109  fail=true;
110  }
111 
112  TEST_FAIL_AND_EXIT(*comm, !fail, "print to standard output", 1);
113 
114  delete dm;
115  }
116 
117  // Node zero prints to a file
118 
119  iPrint = (rank == 0);
120  comm->barrier();
121 
122  for (int i = 0; i < numLevels; i++){
123 
124  level_t level = static_cast<level_t>(i);
125 
126  std::ios_base::openmode flags = std::ios_base::out & std::ios_base::trunc;
127 
128  std::ofstream outF("testFile.txt", flags);
129 
130  try {
131  dm = new DebugManager(rank, iPrint, outF, level);
132  }
133  catch(std::exception &e){
134  fail=true;
135  }
136 
137  TEST_FAIL_AND_EXIT(*comm, !fail, "constructor", 1);
138 
139  if (rank==0){
140  std::cout << "\nThere are " << nprocs << " processes. ";
141  std::cout << "Rank zero only participates, output level is: ";
142  std::cout << level << std::endl;
143  }
144 
145  try {
146  for (next=basicMsgs.begin(); next != basicMsgs.end(); ++next){
147  dm->print(BASIC_STATUS, *next);
148  }
149  comm->barrier();
150 
151  for (next=detailedMsgs.begin(); next != detailedMsgs.end(); ++next){
152  dm->print(DETAILED_STATUS, *next);
153  }
154  comm->barrier();
155 
156  for (next=verboseMsgs.begin(); next != verboseMsgs.end(); ++next){
157  dm->print(VERBOSE_DETAILED_STATUS, *next);
158  }
159  comm->barrier();
160  }
161  catch(std::exception &e){
162  fail=true;
163  }
164 
165  delete dm;
166 
167  TEST_FAIL_AND_EXIT(*comm, !fail, "print to a file", 1);
168 
169  outF.close();
170 
171  comm->barrier();
172 
173  if (rank == 0){
174  std::ifstream inF("testFile.txt");
175  string s;
176  while (getline(inF, s)){
177  std::cout << s << std::endl;
178  }
179  inF.close();
180  system("rm testFile.txt"); // \todo fix for windows
181  }
182 
183  comm->barrier();
184  }
185 
186  if (rank==0)
187  std::cout << "PASS" << std::endl;
188 }
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)
int main(int narg, char **arg)
Definition: coloring1.cpp:164
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