Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 narg, char *arg[])
77 {
78  Tpetra::ScopeGuard tscope(&narg, &arg);
79  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
80 
81  int rank = comm->getRank();
82  int nprocs = comm->getSize();
83  int fail = 0;
84 
86  // Test the Environment(comm) constructor - no param list
87 
88  Environment *defEnv = NULL;
89 
90  try{
91  defEnv = new Environment(comm);
92  }
93  catch(std::exception &e){
94  std::cerr << e.what() << std::endl;
95  fail=1000;
96  }
97 
98  if (checkErrorCode(comm, fail))
99  return 1;
100 
101  if (!fail && defEnv->myRank_ != rank)
102  fail = 1001;
103 
104  if (!fail && defEnv->numProcs_ != nprocs)
105  fail = 1002;
106 
107  if (!fail && defEnv->comm_->getSize() != nprocs)
108  fail = 1003;
109 
110  if (!fail && defEnv->doStatus() != true)
111  fail = 1005;
112 
113  if (!fail && defEnv->doTiming() != false)
114  fail = 1006;
115 
116  if (!fail && defEnv->doMemoryProfiling() != false)
117  fail = 1007;
118 
119  if (!fail && defEnv->errorCheckLevel_ != Zoltan2::BASIC_ASSERTION)
120  fail = 1008;
121 
122  if (checkErrorCode(comm, fail))
123  return 1;
124 
125  delete defEnv;
126 
128  // Set a few parameters and create an Environment
129 
130  Teuchos::ParameterList myParams("testParameterList");
131 
132  myParams.set("debug_level", "detailed_status");
133  myParams.set("debug_procs", "all");
134  myParams.set("debug_output_stream", "std::cout");
135 
136  if (nprocs > 3)
137  myParams.set("memory_procs", "0-1,3");
138  else
139  myParams.set("memory_procs", "0");
140 
141  myParams.set("memory_output_file", "memInfo.txt");
142 
143  myParams.set("partitioning_objective", "minimize_cut_edge_weight");
144  myParams.set("imbalance_tolerance", 1.2);
145 
146  Environment *env = NULL;
147 
148  try{
149  env = new Environment(myParams, comm);
150  }
151  catch(std::exception &e){
152  std::cerr << e.what() << std::endl;
153  fail=2000;
154  }
155 
156  if (!fail){
157  try{
158  env->debug(Zoltan2::BASIC_STATUS, "A basic debugging message.");
159  }
160  catch(std::exception &e){
161  std::cerr << e.what() << std::endl;
162  fail=3000;
163  }
164  }
165 
166  if (!fail){
167  try{
168  env->memory("Memory info");
169  env->memory("Memory info next");
170  env->memory("Memory info after");
171  }
172  catch(std::exception &e){
173  std::cerr << e.what() << std::endl;
174  fail=3002;
175  }
176  }
177 
178  if (checkErrorCode(comm, fail))
179  return 1;
180 
181  if (!fail && env->myRank_ != rank)
182  fail = 2001;
183 
184  if (!fail && env->numProcs_ != nprocs)
185  fail = 2002;
186 
187  if (!fail && env->comm_->getSize() != nprocs)
188  fail = 2003;
189 
190  if (!fail){
191  const Teuchos::ParameterList &pl1 = env->getParameters();
192  const ParameterEntry *dl = pl1.getEntryPtr("debug_level");
193 
194  if (!dl){
195  fail = 2004;
196  }
197  else if (!(dl->isType<int>())){
198  fail = 2013;
199  }
200  else{
201  int value;
202  int &val = dl->getValue<int>(&value);
203  if (val != Zoltan2::DETAILED_STATUS)
204  fail = 2005;
205  }
206  }
207 
208  if (!fail && env->errorCheckLevel_ != Zoltan2::BASIC_ASSERTION)
209  fail = 2008;
210 
211  if (checkErrorCode(comm, fail))
212  return 1;
213 
214  if (rank==0){
215  std::cout << "\nA test parameter list" << std::endl;
216  const Teuchos::ParameterList &envParams = env->getParameters();
217  try{
218  envParams.print();
219  }
220  catch(std::exception &e){
221  std::cerr << e.what() << std::endl;
222  fail=2013;
223  }
224  }
225 
226  if (checkErrorCode(comm, fail))
227  return 1;
228 
230  // Given an existing Environment, get its parameters and
231  // add some new parameters and create a new Environment.
232 
233  RCP<const Comm<int> > oldComm = env->comm_;
234  const Teuchos::ParameterList &oldParams = env->getUnvalidatedParameters();
235 
236  Teuchos::ParameterList newParams = oldParams;
237  newParams.set("error_check_level", "debug_mode_assertions");
238  newParams.remove("memory_output_file");
239  newParams.set("imbalance_tolerance", 1.05);
240  newParams.set("algorithm", "phg");
241  newParams.set("partitioning_objective", "minimize_cut_edge_weight");
242 
243  RCP<Environment> newEnv;
244 
245  try{
246  newEnv = Teuchos::rcp(new Environment(newParams, oldComm));
247  }
248  catch(std::exception &e){
249  std::cerr << e.what() << std::endl;
250  fail=3000;
251  }
252 
253  if (checkErrorCode(comm, fail))
254  return 1;
255 
256  if (!fail && newEnv->errorCheckLevel_ != Zoltan2::DEBUG_MODE_ASSERTION)
257  fail = 3001;
258 
259  if (!fail && rank==0){
260  std::cout << "\nA few changes/additions to the list" << std::endl;
261  const Teuchos::ParameterList &envParams = newEnv->getParameters();
262  try{
263  envParams.print();
264  }
265  catch(std::exception &e){
266  std::cerr << e.what() << std::endl;
267  fail=3003;
268  }
269  }
270 
271  if (checkErrorCode(comm, fail))
272  return 1;
273 
274  delete env;
275 
276  if (rank==0)
277  std::cout << "PASS" << std::endl;
278 
279  return 0;
280 }
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.
int main(int narg, char *arg[])
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.
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