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 36
72 #else
73 #define NUMSTR 34
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 #endif // HAVE_ZOLTAN2_PULP
105  {"scotch_imbalance_ratio", "1.1", "invalid_option"}, // AnyNumberParameterEntryValidator
106  {"compute_metrics", "false", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
107  {"rectilinear", "false", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
108  {"subset_graph", "false", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
109  {"mj_enable_rcb", "true", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
110  {"mj_keep_part_boxes", "true", "invalid_bool_setting"}, // BoolParameterEntryValidator - accepts true/false/"true"/"false"
111  {"num_global_parts", "1", "invalid_value"}, // EnhancedNumberValidator
112  {"num_local_parts", "0", "invalid_value"}, // EnhancedNumberValidator
113  {"mj_premigration_option", "1", "invalid_value"}, // EnhancedNumberValidator
114  {"mj_migration_option", "2", "invalid_value"}, // EnhancedNumberValidator
115 };
116 
117 template <typename T>
118 int testInvalidValue( Teuchos::ParameterList &pl,
119  string paramName, T badValue)
120 {
121  Teuchos::ParameterList validParameters;
122  pl.set(paramName, badValue);
123  std::cout << std::endl;
124  std::cout << paramName << " = " << badValue << std::endl;
125 
126  bool failed = false;
127  try{
128  Zoltan2::createValidatorList(pl, validParameters);
129  pl.validateParametersAndSetDefaults(validParameters);
130  }
131  catch(std::exception &e){
132  std::cout << "Correctly generated an error:" << std::endl;
133  std::cout << e.what() << std::endl;
134  failed = true;
135  }
136 
137  if (!failed){
138  std::cerr << "Bad parameter value was not detected in parameter list." << std::endl;
139  return 1;
140  }
141  return 0;
142 }
143 
144 // this we can remove later
145 // kept here temporarily for reference
147 {
148  // Testing AnyNumberParameterEntryValidator
149 
150  // Create a parameter list
151  Teuchos::ParameterList valid("valid parameter list");
152 
153  // Create parameter using validator
154  typedef Teuchos::AnyNumberParameterEntryValidator validator_t;
155  Teuchos::RCP<const validator_t> anyNumVal = Teuchos::rcp(new validator_t);
156 
158  // Initial test: set parameter to 0.5 in valid parameter list.
159  // Need to use the *expected* data type here.
160  std::cout << "set good default value" << std::endl;
161 
162  std::string parameterName("parameterName");
163  try {
164  valid.set(parameterName, 5.0, "parameterDoc", anyNumVal);
165  }
166  catch (std::exception &e) {
167  std::cout << "FAIL error setting good default value "
168  << e.what() << std::endl;
169  return -1;
170  }
171 
172  double dd = valid.getEntry(parameterName).getValue<double>(&dd);
173  std::cout << "good default value <double> = " << dd << std::endl;
174 
175  // User creates his own parameter list and passes things of various types.
176  // The user list must be validated against the valid list.
177  Teuchos::ParameterList user("user");
178 
179  // This one should work
180  std::cout << "test good user value" << std::endl;
181  user.set(parameterName, "0.123");
182  try {
183  user.validateParametersAndSetDefaults(valid);
184  }
185  catch (std::exception &e) {
186  std::cout << "FAIL " << e.what() << std::endl;
187  return -1;
188  }
189 
190  dd = user.getEntry(parameterName).getValue<double>(&dd);
191  std::cout << "good user value <double> = " << dd << std::endl;
192 
193  // This one should not work; the user's string is not a number
194  std::cout << "test bogus user value" << std::endl;
195  bool aok = false;
196 
197  // MDM note - the fail point will now be on user.set
198  // std::stod will throw on this since we have added the validator
199  try {
200  user.set(parameterName, "bogus");
201  }
202  catch(std::exception &e) {
203  // correct behavior
204  std::cout << "Parameter list correctly rejected bogus user value."
205  << std::endl;
206  aok = true;
207  }
208 
209  if (!aok) {
210  std::cout << "FAIL parameter list accepted a bogus user value"
211  << std::endl;
212  return -1;
213  }
214 
215  // Test the valid with a bogus default value. This operation should also
216  // not work. The validator should catch the bogus input.
217  std::cout << "set bogus default value" << std::endl;
218 
219  std::string parameterNameToo("parameterNameToo");
220  aok = false;
221  try {
222  valid.set(parameterNameToo, "bogus", "parameterDoc", anyNumVal);
223  }
224  catch (std::exception &e) {
225  // correct behavior
226  std::cout << "Parameter list correctly rejected bogus default value."
227  << std::endl;
228  aok = true;
229  }
230 
231  if (!aok) {
232  std::cout << "FAIL parameter list accepted a bogus default value"
233  << std::endl;
234  return -1;
235  }
236 
237  std::cout << "PASS" << std::endl;
238  return 0;
239 }
240 
241  // Print out all the documentation
242 
243 int main(int narg, char *arg[])
244 {
245  Tpetra::ScopeGuard tscope(&narg, &arg);
246  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
247 
248  int rank = comm->getRank();
249 
250  if (rank > 0)
251  return 0;
252 
253  // short term reference - to delete later
254  // this was an example proposed for issue #612
255  // just keeping it here as a reference point to be deleted in the future
256  int tempTest = testForIssue612();
257  if( tempTest != 0 ) {
258  return tempTest;
259  }
260 
261  // Create a valid parameter list.
262 
263  Teuchos::ParameterList validParameters;
264  Teuchos::ParameterList myParams("testParameterList");
265 
266  for (int i=0; i < NUMSTR; i++){
267  myParams.set(strParams[i][0], strParams[i][1]);
268  }
269 
270  for (int i=0; i < NUMFN; i++){
271  myParams.set(fnParams[i][0], fnParams[i][1]);
272  }
273 
274  Teuchos::ParameterList origParams(myParams);
275 
276  // Normally an application would not call this. The
277  // Environment object will validate the entered parameters.
278 
279  try{
280  Zoltan2::createValidatorList(myParams, validParameters);
281  myParams.validateParametersAndSetDefaults(validParameters);
283  }
284  catch(std::exception &e){
285  std::cerr << "Validate parameters generated an error:" << std::endl;
286  std::cerr << e.what() << std::endl;
287  std::cerr << "FAIL" << std::endl;
288  return 1;
289  }
290 
291  std::cout << std::endl;
292  std::cout << "Parameters after validation: " << std::endl;
293  std::cout << myParams << std::endl;
294 
295  // Try invalid parameter values
296  for (int i=0; i < NUMSTR; i++){
297  Teuchos::ParameterList badParams(origParams);
298  int fail =
299  testInvalidValue<string>(badParams, strParams[i][0], strParams[i][2]);
300  if (fail){
301  std::cout << "FAIL" << std::endl;
302  return 1;
303  }
304  }
305 
306  for (int i=0; i < NUMFN; i++){
307  Teuchos::ParameterList badParams(origParams);
308  std::istringstream iss(fnParams[i][2]);
309  double badVal;
310  iss >> badVal;
311  int fail =
312  testInvalidValue<double>(badParams, fnParams[i][0], badVal);
313  if (fail){
314  std::cout << "FAIL" << std::endl;
315  return 1;
316  }
317  }
318 
319 
320  // Print out all the documentation
321 
322  std::cout << std::endl;
323  std::cout << "Parameter documentation:" << std::endl;
324  Zoltan2::printListDocumentation(validParameters, std::cout, std::string());
325 
326  std::cout << "PASS" << std::endl;
327  return 0;
328 }
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&lt;int&gt; to integer...
int main(int narg, char *arg[])
#define NUMSTR
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()
tuple paramName
Definition: xml2dox.py:207