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 
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("partitioning_objective", "minimize_cut_edge_weight");
145  myParams.set("imbalance_tolerance", 1.2);
146 
147  Environment *env = NULL;
148 
149  try{
150  env = new Environment(myParams, comm);
151  }
152  catch(std::exception &e){
153  std::cerr << e.what() << std::endl;
154  fail=2000;
155  }
156 
157  if (!fail){
158  try{
159  env->debug(Zoltan2::BASIC_STATUS, "A basic debugging message.");
160  }
161  catch(std::exception &e){
162  std::cerr << e.what() << std::endl;
163  fail=3000;
164  }
165  }
166 
167  if (!fail){
168  try{
169  env->memory("Memory info");
170  env->memory("Memory info next");
171  env->memory("Memory info after");
172  }
173  catch(std::exception &e){
174  std::cerr << e.what() << std::endl;
175  fail=3002;
176  }
177  }
178 
179  if (checkErrorCode(comm, fail))
180  return 1;
181 
182  if (!fail && env->myRank_ != rank)
183  fail = 2001;
184 
185  if (!fail && env->numProcs_ != nprocs)
186  fail = 2002;
187 
188  if (!fail && env->comm_->getSize() != nprocs)
189  fail = 2003;
190 
191  if (!fail){
192  const Teuchos::ParameterList &pl1 = env->getParameters();
193  const ParameterEntry *dl = pl1.getEntryPtr("debug_level");
194 
195  if (!dl){
196  fail = 2004;
197  }
198  else if (!(dl->isType<int>())){
199  fail = 2013;
200  }
201  else{
202  int value;
203  int &val = dl->getValue<int>(&value);
204  if (val != Zoltan2::DETAILED_STATUS)
205  fail = 2005;
206  }
207  }
208 
210  fail = 2008;
211 
212  if (checkErrorCode(comm, fail))
213  return 1;
214 
215  if (rank==0){
216  std::cout << "\nA test parameter list" << std::endl;
217  const Teuchos::ParameterList &envParams = env->getParameters();
218  try{
219  envParams.print();
220  }
221  catch(std::exception &e){
222  std::cerr << e.what() << std::endl;
223  fail=2013;
224  }
225  }
226 
227  if (checkErrorCode(comm, fail))
228  return 1;
229 
231  // Given an existing Environment, get its parameters and
232  // add some new parameters and create a new Environment.
233 
234  RCP<const Comm<int> > oldComm = env->comm_;
235  const Teuchos::ParameterList &oldParams = env->getUnvalidatedParameters();
236 
237  Teuchos::ParameterList newParams = oldParams;
238  newParams.set("error_check_level", "debug_mode_assertions");
239  newParams.remove("memory_output_file");
240  newParams.set("imbalance_tolerance", 1.05);
241  newParams.set("algorithm", "phg");
242  newParams.set("partitioning_objective", "minimize_cut_edge_weight");
243 
244  RCP<Environment> newEnv;
245 
246  try{
247  newEnv = Teuchos::rcp(new Environment(newParams, oldComm));
248  }
249  catch(std::exception &e){
250  std::cerr << e.what() << std::endl;
251  fail=3000;
252  }
253 
254  if (checkErrorCode(comm, fail))
255  return 1;
256 
257  if (!fail && newEnv->errorCheckLevel_ != Zoltan2::DEBUG_MODE_ASSERTION)
258  fail = 3001;
259 
260  if (!fail && rank==0){
261  std::cout << "\nA few changes/additions to the list" << std::endl;
262  const Teuchos::ParameterList &envParams = newEnv->getParameters();
263  try{
264  envParams.print();
265  }
266  catch(std::exception &e){
267  std::cerr << e.what() << std::endl;
268  fail=3003;
269  }
270  }
271 
272  if (checkErrorCode(comm, fail))
273  return 1;
274 
275  delete env;
276 
277  if (rank==0)
278  std::cout << "PASS" << std::endl;
279 
280  return 0;
281 }
checks for logic errors
void memory(const char *msg) const
Print a message and the kilobytes in use by this process.
fast typical checks for valid arguments
Defines Parameter related enumerators, declares functions.
const Teuchos::ParameterList & getUnvalidatedParameters() const
Returns a const reference to the user&#39;s original list.
bool doMemoryProfiling() const
Return true if memory usage output was requested, even if this process is not printing out memory use...
void debug(MessageOutputLevel level, const char *msg) const
Send a message to the debug output manager.
common code used by tests
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_)
int myRank_
mpi rank (relative to comm_)
Comm_t comm_
communicator for environment
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...
const Teuchos::ParameterList & getParameters() const
Returns a reference to the user&#39;s parameter list.
bool doStatus() const
Return true if debug output was requested, even if this process is not printing out debug messages...
AssertionLevel errorCheckLevel_
level of error checking to do