Zoltan2
AllParameters.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 parameters. Serial test.
47 
48 #include <Zoltan2_config.h>
49 #include <Zoltan2_Environment.hpp>
50 #include <Teuchos_ParameterList.hpp>
51 #include <Teuchos_DefaultComm.hpp>
52 #include <string>
53 
54 using namespace std;
55 using std::cout;
56 using std::cerr;
57 using std::endl;
58 using std::string;
59 
60 //
61 // For all parameters:
62 // parameter name, a valid value, an invalid value
63 
64 // FileNameValidator - a number is invalid
65 
66 #define NUMFN 3
67 static string fnParams[NUMFN][3]={
68  {"debug_output_file", "temp.txt", "5"},
69  {"timer_output_file", "timerInfo.txt", "10.3"},
70  {"memory_output_file", "memory.txt", "3.33"}
71 };
72 
73 // Value is a particular string
74 #ifdef HAVE_ZOLTAN2_PULP
75 #define NUMSTR 35
76 #else
77 #define NUMSTR 33
78 #endif
79 
80 static string strParams[NUMSTR][3]={
81  {"error_check_level", "basic_assertions", "invalid_assertion_request"},
82  {"debug_level", "basic_status", "invalid_status"},
83  {"timer_type", "no_timers", "invalid_timers"},
84  {"debug_output_stream", "cout", "invalid_stream"},
85  {"timer_output_stream", "/dev/null", "invalid_stream"},
86  {"memory_output_stream", "cerr", "invalid_stream"},
87  {"debug_procs", "all", "not_a_valid_list_of_any_type"},
88  {"mj_parts", "2,3,4", "not_a_valid_list_of_any_type"},
89  {"memory_procs", "2-10", "not_a_valid_list_of_any_type"},
90  {"order_method", "rcm", "invalid_method"},
91  {"order_method_type", "local", "invalid_method_type"},
92  {"order_package", "amd", "invalid_package"},
93  {"partitioning_objective", "balance_object_weight", "invalid_objective"},
94  {"partitioning_approach", "repartition", "invalid_approach"},
95  {"objects_to_partition", "graph_vertices", "invalid_objects"},
96  {"model", "graph", "invalid_model"},
97  {"algorithm", "rcb", "invalid_algorithm"},
98  {"symmetrize_input", "transpose", "invalid_option"},
99  {"symmetrize_input", "transpose", "invalid_option"},
100  {"mj_concurrent_part_count", "0", "invalid_value"}, // AnyNumberParameterEntryValidator
101  {"mj_recursion_depth", "0", "invalid_value"}, // AnyNumberParameterEntryValidator
102  {"mapping_type", "0", "invalid_value"}, // AnyNumberParameterEntryValidator
103  {"imbalance_tolerance", "1.1", "invalid_option"}, // AnyNumberParameterEntryValidator
104  {"mj_minimum_migration_imbalance", "1.1", "invalid_option"}, // AnyNumberParameterEntryValidator
105 #ifdef HAVE_ZOLTAN2_PULP
106  {"pulp_vert_imbalance", "1.1", "invalid_option"}, // AnyNumberParameterEntryValidator
107  {"pulp_edge_imbalance", "1.1", "invalid_option"}, // AnyNumberParameterEntryValidator
108 #endif // HAVE_ZOLTAN2_PULP
109  {"scotch_imbalance_ratio", "1.1", "invalid_option"}, // AnyNumberParameterEntryValidator
110  {"compute_metrics", "false", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
111  {"rectilinear", "false", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
112  {"subset_graph", "false", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
113  {"mj_enable_rcb", "true", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
114  {"mj_keep_part_boxes", "true", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
115  {"num_global_parts", "1", "invalid_value"}, // EnhancedNumberValidator
116  {"num_local_parts", "0", "invalid_value"}, // EnhancedNumberValidator
117  {"mj_migration_option", "2", "invalid_value"}, // EnhancedNumberValidator
118 };
119 
120 template <typename T>
121 int testInvalidValue( Teuchos::ParameterList &pl,
122  string paramName, T badValue)
123 {
124  Teuchos::ParameterList validParameters;
125  pl.set(paramName, badValue);
126  cout << endl;
127  cout << paramName << " = " << badValue << endl;
128 
129  bool failed = false;
130  try{
131  Zoltan2::createValidatorList(pl, validParameters);
132  pl.validateParametersAndSetDefaults(validParameters);
133  }
134  catch(std::exception &e){
135  cout << "Correctly generated an error:" << endl;
136  cout << e.what() << endl;
137  failed = true;
138  }
139 
140  if (!failed){
141  cerr << "Bad parameter value was not detected in parameter list." << endl;
142  return 1;
143  }
144  return 0;
145 }
146 
147 // this we can remove later
148 // kept here temporarily for reference
150 {
151  // Testing AnyNumberParameterEntryValidator
152 
153  // Create a parameter list
154  Teuchos::ParameterList valid("valid parameter list");
155 
156  // Create parameter using validator
157  typedef Teuchos::AnyNumberParameterEntryValidator validator_t;
158  Teuchos::RCP<const validator_t> anyNumVal = Teuchos::rcp(new validator_t);
159 
161  // Initial test: set parameter to 0.5 in valid parameter list.
162  // Need to use the *expected* data type here.
163  std::cout << "set good default value" << std::endl;
164 
165  std::string parameterName("parameterName");
166  try {
167  valid.set(parameterName, 5.0, "parameterDoc", anyNumVal);
168  }
169  catch (std::exception &e) {
170  std::cout << "FAIL error setting good default value "
171  << e.what() << std::endl;
172  return -1;
173  }
174 
175  double dd = valid.getEntry(parameterName).getValue<double>(&dd);
176  std::cout << "good default value <double> = " << dd << std::endl;
177 
178  // User creates his own parameter list and passes things of various types.
179  // The user list must be validated against the valid list.
180  Teuchos::ParameterList user("user");
181 
182  // This one should work
183  std::cout << "test good user value" << std::endl;
184  user.set(parameterName, "0.123");
185  try {
186  user.validateParametersAndSetDefaults(valid);
187  }
188  catch (std::exception &e) {
189  std::cout << "FAIL " << e.what() << std::endl;
190  return -1;
191  }
192 
193  dd = user.getEntry(parameterName).getValue<double>(&dd);
194  std::cout << "good user value <double> = " << dd << std::endl;
195 
196  // This one should not work; the user's string is not a number
197  std::cout << "test bogus user value" << std::endl;
198  bool aok = false;
199 
200  // MDM note - the fail point will now be on user.set
201  // std::stod will throw on this since we have added the validator
202  try {
203  user.set(parameterName, "bogus");
204  }
205  catch(std::exception &e) {
206  // correct behavior
207  std::cout << "Parameter list correctly rejected bogus user value."
208  << std::endl;
209  aok = true;
210  }
211 
212  if (!aok) {
213  std::cout << "FAIL parameter list accepted a bogus user value"
214  << std::endl;
215  return -1;
216  }
217 
218  // Test the valid with a bogus default value. This operation should also
219  // not work. The validator should catch the bogus input.
220  std::cout << "set bogus default value" << std::endl;
221 
222  std::string parameterNameToo("parameterNameToo");
223  aok = false;
224  try {
225  valid.set(parameterNameToo, "bogus", "parameterDoc", anyNumVal);
226  }
227  catch (std::exception &e) {
228  // correct behavior
229  std::cout << "Parameter list correctly rejected bogus default value."
230  << std::endl;
231  aok = true;
232  }
233 
234  if (!aok) {
235  std::cout << "FAIL parameter list accepted a bogus default value"
236  << std::endl;
237  return -1;
238  }
239 
240  std::cout << "PASS" << std::endl;
241  return 0;
242 }
243 
244  // Print out all the documentation
245 
246 int main(int argc, char *argv[])
247 {
248  Teuchos::GlobalMPISession session(&argc, &argv);
249  Teuchos::RCP<const Teuchos::Comm<int> > comm =
250  Teuchos::DefaultComm<int>::getComm();
251 
252  int rank = comm->getRank();
253 
254  if (rank > 0)
255  return 0;
256 
257  // short term reference - to delete later
258  // this was an example proposed for issue #612
259  // just keeping it here as a reference point to be deleted in the future
260  int tempTest = testForIssue612();
261  if( tempTest != 0 ) {
262  return tempTest;
263  }
264 
265  // Create a valid parameter list.
266 
267  Teuchos::ParameterList validParameters;
268  Teuchos::ParameterList myParams("testParameterList");
269 
270  for (int i=0; i < NUMSTR; i++){
271  myParams.set(strParams[i][0], strParams[i][1]);
272  }
273 
274  for (int i=0; i < NUMFN; i++){
275  myParams.set(fnParams[i][0], fnParams[i][1]);
276  }
277 
278  Teuchos::ParameterList origParams(myParams);
279 
280  // Normally an application would not call this. The
281  // Environment object will validate the entered parameters.
282 
283  try{
284  Zoltan2::createValidatorList(myParams, validParameters);
285  myParams.validateParametersAndSetDefaults(validParameters);
287  }
288  catch(std::exception &e){
289  std::cerr << "Validate parameters generated an error:" << endl;
290  std::cerr << e.what() << endl;
291  std::cerr << "FAIL" << endl;
292  return 1;
293  }
294 
295  cout << endl;
296  cout << "Parameters after validation: " << endl;
297  cout << myParams << endl;
298 
299  // Try invalid parameter values
300  for (int i=0; i < NUMSTR; i++){
301  Teuchos::ParameterList badParams(origParams);
302  int fail =
303  testInvalidValue<string>(badParams, strParams[i][0], strParams[i][2]);
304  if (fail){
305  cout << "FAIL" << endl;
306  return 1;
307  }
308  }
309 
310  for (int i=0; i < NUMFN; i++){
311  Teuchos::ParameterList badParams(origParams);
312  istringstream iss(fnParams[i][2]);
313  double badVal;
314  iss >> badVal;
315  int fail =
316  testInvalidValue<double>(badParams, fnParams[i][0], badVal);
317  if (fail){
318  cout << "FAIL" << endl;
319  return 1;
320  }
321  }
322 
323 
324  // Print out all the documentation
325 
326  cout << endl;
327  cout << "Parameter documentation:" << endl;
328  Zoltan2::printListDocumentation(validParameters, cout, std::string());
329 
330  cout << "PASS" << endl;
331  return 0;
332 }
paramName
Definition: xml2dox.py:207
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...
#define NUMSTR
void printListDocumentation(const Teuchos::ParameterList &pl, std::ostream &os, std::string listNames)
static string fnParams[NUMFN][3]
int main(int argc, char *argv[])
static const std::string fail
#define NUMFN
Defines the Environment class.
int testInvalidValue(Teuchos::ParameterList &pl, string paramName, T badValue)
static string strParams[NUMSTR][3]
int testForIssue612()