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