Zoltan2  Version of the Day
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 
127  myRank_(0), numProcs_(1), comm_(), errorCheckLevel_(BASIC_ASSERTION),
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 std::string;
151  using Teuchos::Array;
152  using Teuchos::rcp;
153  using Teuchos::RCP;
154  using Teuchos::ParameterList;
155 
156  bool emptyList = (params_.begin() == params_.end());
157 
158  if (!emptyList){
159 
160  ParameterList validParams;
161 
162  try{
163  createValidatorList(params_, validParams);
164  }
166 
167  // Note that since validParams only contains parameters that
168  // appear in params_, there are no defaults being set. We
169  // call ParameterList::validateParametersAndSetDefaults() instead of
170  // ParameterList::validateParameters() because we want the
171  // the validators' validateAndModify() to be called instead
172  // of validate(). validateAndModify() "fixes" some of the
173  // parameters for us.
174 
175  params_.validateParametersAndSetDefaults(validParams);
176 
177  // For all of the string to integer parameters, convert
178  // them to the integer. I would have
179  // expected validateAndModify() to do this.
180 
181  convertStringToInt(params_);
182  }
183 
185 
186  // Set up for debugging/status output.
187  // By default: if no output stream is specified, then node zero
188  // outputs BASIC_STATUS to std::cout.
189 
190 #ifndef Z2_OMIT_ALL_STATUS_MESSAGES
191  int &level = params_.get<int>("debug_level", NUM_STATUS_OUTPUT_LEVELS);
192  string &fname = params_.get<string>("debug_output_file", Z2_UNSET_STRING);
193  int &os = params_.get<int>("debug_output_stream", NUM_OUTPUT_STREAMS);
194 
195  if (fname==Z2_UNSET_STRING && os==NUM_OUTPUT_STREAMS)
196  os = COUT_STREAM; // default output target
197  if (level == NUM_STATUS_OUTPUT_LEVELS)
198  level = BASIC_STATUS; // default level of verbosity
199 
200  bool iPrint = (myRank_ == 0); // default reporter
201 
202  const Array<int> *reporters =
203  params_.getPtr<Array<int> >("debug_procs");
204  if (reporters)
205  iPrint = IsInRangeList(myRank_, *reporters);
206 
207  try{
208  makeDebugManager(myRank_, iPrint, level, fname, os, debugOut_);
209  }
210  catch (std::exception &e){
211  std::ostringstream oss;
212  oss << myRank_ << ": unable to create debug output manager";
213  oss << " (" << e.what() << ")";
214  throw std::runtime_error(oss.str());
215  }
216 #endif
217 
218  // Set up for memory usage output.
219 
220 #ifndef Z2_OMIT_ALL_PROFILING
221  string &f2 =
222  params_.get<string>("memory_output_file", Z2_UNSET_STRING);
223  int &os2 =
224  params_.get<int>("memory_output_stream", NUM_OUTPUT_STREAMS);
225 
226  const Array<int> *reporters2 =
227  params_.getPtr<Array<int> >("memory_procs");
228 
229  bool doMemory = true;
230 
231  if (f2 != Z2_UNSET_STRING || os2 != NUM_OUTPUT_STREAMS || reporters2 != NULL){
232  // user indicated they want memory usage information
233  long numKbytes = 0;
234  if (myRank_ == 0)
235  numKbytes = getProcessKilobytes();
236 
237  Teuchos::broadcast<int, long>(*comm_, 0, 1, &numKbytes);
238 
239  if (numKbytes == 0){
240  // This is not a Linux system with proc/pid/statm.
241  f2 = Z2_UNSET_STRING;
242  os2 = NUM_OUTPUT_STREAMS;
243  reporters2 = NULL;
244  this->debug(BASIC_STATUS,
245  string("Warning: memory profiling requested but not available."));
246  doMemory = false; // can't do it
247  }
248  }
249  else{
250  doMemory = false; // not requested
251  }
252 
253  if (doMemory){
254  iPrint = (myRank_ == 0); // default
255  if (reporters2)
256  iPrint = IsInRangeList(myRank_, *reporters2);
257 
258  try{
259  makeMetricOutputManager<long>(myRank_, iPrint, f2, os2, memoryOut_,
260  string("KB"), 10, memoryOutputFile_);
261  }
262  catch (std::exception &e){
263  std::ostringstream oss;
264  oss << myRank_ << ": unable to create memory profiling output manager";
265  oss << " (" << e.what() << ")";
266  throw std::runtime_error(oss.str());
267  }
268 
269  memoryOn_ = true;
270  }
271 #endif
272 
273 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
275 #else
276  errorCheckLevel_ = static_cast<AssertionLevel>(
277  params_.get<int>("error_check_level", BASIC_ASSERTION));
278 #endif
279 }
280 
281 void Environment::convertStringToInt(Teuchos::ParameterList &params)
282 {
283  using Teuchos::ParameterList;
284  using Teuchos::ParameterEntry;
285  using Teuchos::RCP;
286  using Teuchos::rcp_dynamic_cast;
287  using std::string;
288  ParameterList::ConstIterator next = params.begin();
289 
290  // Data type of these parameters will now change from string to int
291 
292  string validatorName("StringIntegralValidator(int)");
293  typedef Teuchos::StringToIntegralParameterEntryValidator<int> s2i_t;
294 
295  while (next != params.end()){
296 
297  const std::string &name = next->first;
298  ParameterEntry &entry = params.getEntry(name);
299 
300  if (entry.isList()){
301  ParameterList *dummy = NULL;
302  ParameterList &pl = entry.getValue<ParameterList>(dummy);
303  convertStringToInt(pl);
304  }
305  else{
306  if ((entry.validator()).get()){
307  if (entry.validator()->getXMLTypeName() == validatorName){
308  string dummy("");
309  string &entryValue = entry.getValue<string>(&dummy);
310  RCP<const s2i_t> s2i =
311  Teuchos::rcp_dynamic_cast<const s2i_t>(entry.validator(), true);
312  int val = s2i->getIntegralValue(entryValue);
313  entry.setValue<int>(val);
314  }
315  }
316  }
317  ++next;
318  }
319 }
320 
321 } //namespace Zoltan2
322 
323 #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&lt;int&gt; 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