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