Zoltan2
Zoltan2_Environment.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
49 #ifndef _ZOLTAN2_ENVIRONMENT_CPP_
50 #define _ZOLTAN2_ENVIRONMENT_CPP_
51 
52 #include <Zoltan2_Environment.hpp>
54 #include <Zoltan2_Util.hpp>
55 
56 #include <Teuchos_StandardParameterEntryValidators.hpp>
57 #include <Teuchos_RCP.hpp>
58 
59 #include <sstream>
60 #include <ostream>
61 
62 namespace Zoltan2 {
63 
65 // Namespace definitions used by this class.
66 
77 void makeDebugManager(int rank, bool iPrint,
78  int level, std::string fname, int ost,
79  Teuchos::RCP<DebugManager> &mgr)
80 {
81  MessageOutputLevel lvl = static_cast<MessageOutputLevel>(level);
82 
83  if (fname != Z2_UNSET_STRING){
84  std::ofstream *dbgFile = new std::ofstream;
85  if (iPrint){
86  std::string newFname;
87  addNumberToFileName(rank, fname, newFname);
88  try{
89  dbgFile->open(newFname.c_str(), std::ios::out|std::ios::trunc);
90  }
91  catch(std::exception &e){
92  throw std::runtime_error(e.what());
93  }
94  }
95  mgr = Teuchos::rcp(new DebugManager(rank, iPrint, *dbgFile, lvl));
96  return;
97  }
98 
99  OSType os = static_cast<OSType>(ost);
100 
101  if (os == COUT_STREAM)
102  mgr = Teuchos::rcp(new DebugManager(rank, iPrint, std::cout, lvl));
103  else if (os == CERR_STREAM)
104  mgr = Teuchos::rcp(new DebugManager(rank, iPrint, std::cerr, lvl));
105  else if (os == NULL_STREAM)
106  mgr = Teuchos::rcp(new DebugManager(rank, false, std::cout, lvl));
107 }
108 
110 // Environment definitions
111 
112 Environment::Environment( Teuchos::ParameterList &problemParams,
113  const Teuchos::RCP<const Teuchos::Comm<int> > &comm):
114  myRank_(comm->getRank()), numProcs_(comm->getSize()), comm_(comm),
115  errorCheckLevel_(BASIC_ASSERTION),
116  unvalidatedParams_(problemParams), params_(problemParams),
117  debugOut_(), timerOut_(), timingOn_(false), memoryOut_(), memoryOn_(false),
118  memoryOutputFile_()
119 {
120  try{
121  commitParameters();
122  }
124 }
125 
128  unvalidatedParams_("emptyList"), params_("emptyList"),
129  debugOut_(), timerOut_(), timingOn_(false), memoryOut_(), memoryOn_(false),
130  memoryOutputFile_()
131 {
132  comm_ = Teuchos::DefaultComm<int>::getComm();
133  myRank_ = comm_->getRank();
134  numProcs_ = comm_->getSize();
135 
136  try{
137  commitParameters();
138  }
140 }
141 
143 {
144  if (!memoryOutputFile_.is_null())
145  memoryOutputFile_->close();
146 }
147 
148 void Environment::commitParameters()
149 {
150  using Teuchos::Array;
151  using Teuchos::ParameterList;
152 
153  bool emptyList = (params_.begin() == params_.end());
154 
155  if (!emptyList){
156 
157  ParameterList validParams;
158 
159  try{
160  createValidatorList(params_, validParams);
161  }
163 
164  // Note that since validParams only contains parameters that
165  // appear in params_, there are no defaults being set. We
166  // call ParameterList::validateParametersAndSetDefaults() instead of
167  // ParameterList::validateParameters() because we want the
168  // the validators' validateAndModify() to be called instead
169  // of validate(). validateAndModify() "fixes" some of the
170  // parameters for us.
171  // Note: depth==0 --> do not validate sublists,
172  // since they are for TPL parameters
173 
174  params_.validateParametersAndSetDefaults(validParams, 0);
175 
176  // For all of the string to integer parameters, convert
177  // them to the integer. I would have
178  // expected validateAndModify() to do this.
179 
180  convertStringToInt(params_);
181  }
182 
184 
185  // Set up for debugging/status output.
186  // By default: if no output stream is specified, then node zero
187  // outputs BASIC_STATUS to std::cout.
188 
189 #ifndef Z2_OMIT_ALL_STATUS_MESSAGES
190  int &level = params_.get<int>("debug_level", NUM_STATUS_OUTPUT_LEVELS);
191  std::string &fname = params_.get<std::string>("debug_output_file", Z2_UNSET_STRING);
192  int &os = params_.get<int>("debug_output_stream", NUM_OUTPUT_STREAMS);
193 
194  if (fname==Z2_UNSET_STRING && os==NUM_OUTPUT_STREAMS)
195  os = COUT_STREAM; // default output target
196  if (level == NUM_STATUS_OUTPUT_LEVELS)
197  level = BASIC_STATUS; // default level of verbosity
198 
199  bool iPrint = (myRank_ == 0); // default reporter
200 
201  const Array<int> *reporters =
202  params_.getPtr<Array<int> >("debug_procs");
203  if (reporters)
204  iPrint = IsInRangeList(myRank_, *reporters);
205 
206  try{
207  makeDebugManager(myRank_, iPrint, level, fname, os, debugOut_);
208  }
209  catch (std::exception &e){
210  std::ostringstream oss;
211  oss << myRank_ << ": unable to create debug output manager";
212  oss << " (" << e.what() << ")";
213  throw std::runtime_error(oss.str());
214  }
215 #endif
216 
217  // Set up for memory usage output.
218 
219 #ifndef Z2_OMIT_ALL_PROFILING
220  std::string &f2 =
221  params_.get<std::string>("memory_output_file", Z2_UNSET_STRING);
222  int &os2 =
223  params_.get<int>("memory_output_stream", NUM_OUTPUT_STREAMS);
224 
225  const Array<int> *reporters2 =
226  params_.getPtr<Array<int> >("memory_procs");
227 
228  bool doMemory = true;
229 
230  if (f2 != Z2_UNSET_STRING || os2 != NUM_OUTPUT_STREAMS || reporters2 != NULL){
231  // user indicated they want memory usage information
232  long numKbytes = 0;
233  if (myRank_ == 0)
234  numKbytes = getProcessKilobytes();
235 
236  Teuchos::broadcast<int, long>(*comm_, 0, 1, &numKbytes);
237 
238  if (numKbytes == 0){
239  // This is not a Linux system with proc/pid/statm.
240  f2 = Z2_UNSET_STRING;
241  os2 = NUM_OUTPUT_STREAMS;
242  reporters2 = NULL;
243  this->debug(BASIC_STATUS,
244  std::string("Warning: memory profiling requested but not available."));
245  doMemory = false; // can't do it
246  }
247  }
248  else{
249  doMemory = false; // not requested
250  }
251 
252  if (doMemory){
253  iPrint = (myRank_ == 0); // default
254  if (reporters2)
255  iPrint = IsInRangeList(myRank_, *reporters2);
256 
257  try{
258  makeMetricOutputManager<long>(myRank_, iPrint, f2, os2, memoryOut_,
259  std::string("KB"), 10, memoryOutputFile_);
260  }
261  catch (std::exception &e){
262  std::ostringstream oss;
263  oss << myRank_ << ": unable to create memory profiling output manager";
264  oss << " (" << e.what() << ")";
265  throw std::runtime_error(oss.str());
266  }
267 
268  memoryOn_ = true;
269  }
270 #endif
271 
272 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
274 #else
275  errorCheckLevel_ = static_cast<AssertionLevel>(
276  params_.get<int>("error_check_level", BASIC_ASSERTION));
277 #endif
278 }
279 
280 void Environment::convertStringToInt(Teuchos::ParameterList &params)
281 {
282  using Teuchos::ParameterList;
283  using Teuchos::ParameterEntry;
284  using Teuchos::RCP;
285  using Teuchos::rcp_dynamic_cast;
286  ParameterList::ConstIterator next = params.begin();
287 
288  // Data type of these parameters will now change from string to int
289 
290  std::string validatorName("StringIntegralValidator(int)");
291  typedef Teuchos::StringToIntegralParameterEntryValidator<int> s2i_t;
292 
293  while (next != params.end()){
294 
295  const std::string &name = next->first;
296  ParameterEntry &entry = params.getEntry(name);
297 
298  if (entry.isList()){
299  ParameterList *dummy = NULL;
300  ParameterList &pl = entry.getValue<ParameterList>(dummy);
301  convertStringToInt(pl);
302  }
303  else{
304  if ((entry.validator()).get()){
305  if (entry.validator()->getXMLTypeName() == validatorName){
306  std::string dummy("");
307  std::string &entryValue = entry.getValue<std::string>(&dummy);
308  RCP<const s2i_t> s2i =
309  Teuchos::rcp_dynamic_cast<const s2i_t>(entry.validator(), true);
310  int val = s2i->getIntegralValue(entryValue);
311  entry.setValue<int>(val);
312  }
313  }
314  }
315  ++next;
316  }
317 }
318 
319 } //namespace Zoltan2
320 
321 #endif
Environment()
Default Constructor.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
MessageOutputLevel
The amount of debugging or status output to print.
void makeDebugManager(int rank, bool iPrint, int level, std::string fname, int ost, Teuchos::RCP< DebugManager > &mgr)
Create an output manager for debugging or status information.
void createValidatorList(const Teuchos::ParameterList &plIn, Teuchos::ParameterList &plOut)
Create a list by adding validators to the users parameter list.
static void convertStringToInt(Teuchos::ParameterList &params)
Convert parameters of type Teuchos::StringToIntegralParameterEntryValidator<int> to integer...
/dev/null: do actions but don&#39;t output results
long getProcessKilobytes()
AssertionLevel
Level of error checking or assertions desired.
int numProcs_
number of processes (relative to comm_)
void debug(MessageOutputLevel level, const char *msg) const
Send a message to the debug output manager.
no assertion checks will be done
int myRank_
mpi rank (relative to comm_)
OSType
Output stream types.
Comm_t comm_
communicator for environment
void addNumberToFileName(int number, std::string fname, std::string &newf)
Helper method to add number to a file name.
Definition: Zoltan2_IO.cpp:56
#define Z2_UNSET_STRING
A value to indicate a string parameter that was not set by the user.
bool IsInRangeList(const Integral val, const Teuchos::Array< Integral > &valList, bool sorted=true)
A helper function that determines if a value is in the list.
the status at each high level step
Define IntegerRangeList validator.
DebugManager contains the methods that perform output of debug and status messages.
Defines the Environment class.
A gathering of useful namespace methods.
AssertionLevel errorCheckLevel_
level of error checking to do