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
45 //
46 // Testing Zoltan2::Environment
51 #include <Zoltan2_Environment.hpp>
52 #include <Zoltan2_Parameters.hpp>
53 #include <Zoltan2_TestHelpers.hpp>
54 #include <Teuchos_ParameterList.hpp>
55 #include <Teuchos_DefaultComm.hpp>
56 
57 using std::string;
58 using Teuchos::ParameterEntry;
59 using Teuchos::RCP;
60 using Teuchos::Comm;
62 
63 int checkErrorCode(Teuchos::RCP<const Teuchos::Comm<int> > &comm, int code)
64 {
65  int rank = comm->getRank();
66  if (code > 0)
67  std::cerr << "Proc " << rank << " error: " << code << std::endl;
68  comm->barrier();
69 
70  // Will return 1 if any process has a non-zero code
71  TEST_FAIL_AND_RETURN_VALUE(*comm, code==0, "test failure", 1);
72 
73  return 0;
74 }
75 
76 int main(int argc, char *argv[])
77 {
78  Teuchos::GlobalMPISession session(&argc, &argv);
79  Teuchos::RCP<const Comm<int> > comm =
80  Teuchos::DefaultComm<int>::getComm();
81 
82  int rank = comm->getRank();
83  int nprocs = comm->getSize();
84  int fail = 0;
85 
87  // Create a default Environment and test it
88 
89  Environment *defEnv = NULL;
90 
91  try{
92  defEnv = new Environment;
93  }
94  catch(std::exception &e){
95  std::cerr << e.what() << std::endl;
96  fail=1000;
97  }
98 
99  if (checkErrorCode(comm, fail))
100  return 1;
101 
102  if (!fail && defEnv->myRank_ != rank)
103  fail = 1001;
104 
105  if (!fail && defEnv->numProcs_ != nprocs)
106  fail = 1002;
107 
108  if (!fail && defEnv->comm_->getSize() != nprocs)
109  fail = 1003;
110 
111  if (!fail && defEnv->doStatus() != true)
112  fail = 1005;
113 
114  if (!fail && defEnv->doTiming() != false)
115  fail = 1006;
116 
117  if (!fail && defEnv->doMemoryProfiling() != false)
118  fail = 1007;
119 
120  if (!fail && defEnv->errorCheckLevel_ != Zoltan2::BASIC_ASSERTION)
121  fail = 1008;
122 
123  if (checkErrorCode(comm, fail))
124  return 1;
125 
126  delete defEnv;
127 
129  // Set a few parameters and create an Environment
130 
131  Teuchos::ParameterList myParams("testParameterList");
132 
133  myParams.set("debug_level", "detailed_status");
134  myParams.set("debug_procs", "all");
135  myParams.set("debug_output_stream", "std::cout");
136 
137  if (nprocs > 3)
138  myParams.set("memory_procs", "0-1,3");
139  else
140  myParams.set("memory_procs", "0");
141 
142  myParams.set("memory_output_file", "memInfo.txt");
143 
144  myParams.set("speed_versus_quality", "speed");
145  myParams.set("memory_versus_speed", "memory");
146 
147  myParams.set("topology", "2,6,6");
148  myParams.set("randomize_input", "true");
149  myParams.set("partitioning_objective", "minimize_cut_edge_weight");
150  myParams.set("imbalance_tolerance", 1.2);
151 
152  Environment *env = NULL;
153 
154  try{
155  env = new Environment(myParams, comm);
156  }
157  catch(std::exception &e){
158  std::cerr << e.what() << std::endl;
159  fail=2000;
160  }
161 
162  if (!fail){
163  try{
164  env->debug(Zoltan2::BASIC_STATUS, "A basic debugging message.");
165  }
166  catch(std::exception &e){
167  std::cerr << e.what() << std::endl;
168  fail=3000;
169  }
170  }
171 
172  if (!fail){
173  try{
174  env->memory("Memory info");
175  env->memory("Memory info next");
176  env->memory("Memory info after");
177  }
178  catch(std::exception &e){
179  std::cerr << e.what() << std::endl;
180  fail=3002;
181  }
182  }
183 
184  if (checkErrorCode(comm, fail))
185  return 1;
186 
187  if (!fail && env->myRank_ != rank)
188  fail = 2001;
189 
190  if (!fail && env->numProcs_ != nprocs)
191  fail = 2002;
192 
193  if (!fail && env->comm_->getSize() != nprocs)
194  fail = 2003;
195 
196  if (!fail){
197  const Teuchos::ParameterList &pl1 = env->getParameters();
198  const ParameterEntry *dl = pl1.getEntryPtr("debug_level");
199 
200  if (!dl){
201  fail = 2004;
202  }
203  else if (!(dl->isType<int>())){
204  fail = 2013;
205  }
206  else{
207  int value;
208  int &val = dl->getValue<int>(&value);
209  if (val != Zoltan2::DETAILED_STATUS)
210  fail = 2005;
211  }
212  }
213 
214  if (!fail && env->errorCheckLevel_ != Zoltan2::BASIC_ASSERTION)
215  fail = 2008;
216 
217  if (checkErrorCode(comm, fail))
218  return 1;
219 
220  if (rank==0){
221  std::cout << "\nA test parameter list" << std::endl;
222  const Teuchos::ParameterList &envParams = env->getParameters();
223  try{
224  envParams.print();
225  }
226  catch(std::exception &e){
227  std::cerr << e.what() << std::endl;
228  fail=2013;
229  }
230  }
231 
232  if (checkErrorCode(comm, fail))
233  return 1;
234 
236  // Given an existing Environment, get its parameters and
237  // add some new parameters and create a new Environment.
238 
239  RCP<const Comm<int> > oldComm = env->comm_;
240  const Teuchos::ParameterList &oldParams = env->getUnvalidatedParameters();
241 
242  Teuchos::ParameterList newParams = oldParams;
243  newParams.set("error_check_level", "debug_mode_assertions");
244  newParams.set("memory_versus_speed", "speed");
245  newParams.remove("memory_output_file");
246  newParams.set("imbalance_tolerance", "1.05");
247  newParams.set("algorithm", "phg");
248  newParams.set("partitioning_objective", "minimize_cut_edge_weight");
249 
250  RCP<Environment> newEnv;
251 
252  try{
253  newEnv = Teuchos::rcp(new Environment(newParams, oldComm));
254  }
255  catch(std::exception &e){
256  std::cerr << e.what() << std::endl;
257  fail=3000;
258  }
259 
260  if (checkErrorCode(comm, fail))
261  return 1;
262 
263  if (!fail && newEnv->errorCheckLevel_ != Zoltan2::DEBUG_MODE_ASSERTION)
264  fail = 3001;
265 
266  if (!fail && rank==0){
267  std::cout << "\nA few changes/additions to the list" << std::endl;
268  const Teuchos::ParameterList &envParams = newEnv->getParameters();
269  try{
270  envParams.print();
271  }
272  catch(std::exception &e){
273  std::cerr << e.what() << std::endl;
274  fail=3003;
275  }
276  }
277 
278  if (checkErrorCode(comm, fail))
279  return 1;
280 
281  delete env;
282 
283  if (rank==0)
284  std::cout << "PASS" << std::endl;
285 
286  return 0;
287 }
const Teuchos::ParameterList & getParameters() const
Returns a reference to the user&#39;s parameter list.
checks for logic errors
bool doStatus() const
Return true if debug output was requested, even if this process is not printing out debug messages...
fast typical checks for valid arguments
Defines Parameter related enumerators, declares functions.
common code used by tests
bool doMemoryProfiling() const
Return true if memory usage output was requested, even if this process is not printing out memory use...
sub-steps, each method&#39;s entry and exit
int checkErrorCode(Teuchos::RCP< const Teuchos::Comm< int > > &comm, int code)
Definition: Environment.cpp:63
int numProcs_
number of processes (relative to comm_)
void debug(MessageOutputLevel level, const char *msg) const
Send a message to the debug output manager.
int myRank_
mpi rank (relative to comm_)
Comm_t comm_
communicator for environment
const Teuchos::ParameterList & getUnvalidatedParameters() const
Returns a const reference to the user&#39;s original list.
void memory(const char *msg) const
Print a message and the kilobytes in use by this process.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
static const std::string fail
the status at each high level step
#define TEST_FAIL_AND_RETURN_VALUE(comm, ok, s, rc)
Defines the Environment class.
int main(int argc, char *argv[])
Definition: Environment.cpp:76
bool doTiming() const
Return true if timing was requested, even if this process is not printing out timing messages...
AssertionLevel errorCheckLevel_
level of error checking to do