Zoltan2  Version of the Day
Zoltan2_Parameters.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 
50 #include <Zoltan2_Parameters.hpp>
52 
53 #include <Teuchos_StringInputSource.hpp>
54 #include <Teuchos_XMLParser.hpp>
55 #include <Teuchos_XMLObject.hpp>
56 #include <Teuchos_XMLParameterListReader.hpp>
57 #include <Teuchos_ValidatorXMLConverterDB.hpp>
58 
59 // A generated header file in {zoltan2-binary-directory}/src
60 // which defines ZOLTAN2_XML_PARAMETER_STRING.
61 
63 
64 using namespace std;
65 
66 namespace Zoltan2 {
67 
76 void createAllParameters(Teuchos::ParameterList &pList)
77 {
78  // An XML converter for IntegerRangeListValidator
79  // needs to be added to the converter database.
80 
83 
84  RCP<const irl_t> intRangeValidatorP = rcp(new irl_t); // dummy
85  RCP<irlConverter_t > converter = rcp(new irlConverter_t);
86  Teuchos::ValidatorXMLConverterDB::addConverter(
87  intRangeValidatorP,
88  converter);
89 
90  // Create a Teuchos::ParameterList from an XML string.
91  // To add a parameter to Zoltan2, edit zoltan2/data/parameters.xml.
92 
93  std::string xmlParameterString(ZOLTAN2_XML_PARAMETER_STRING);
94  Teuchos::StringInputSource src(xmlParameterString);
95 
96  Teuchos::XMLObject xmlObj;
97  ostringstream errMsg;
98 
99  Teuchos::XMLParser parser(src.stream());
100 
101  try{
102  xmlObj = parser.parse();
103  }
104  catch (std::exception &e){
105  errMsg << e.what() << " invalid xml";
106  }
107 
108  if (errMsg.str().size() == 0){
109  try{
110  Teuchos::XMLParameterListReader rdr;
111  pList = rdr.toParameterList(xmlObj);
112  }
113  catch (std::exception &e){
114  errMsg << e.what() << " invalid parameter list";
115  }
116  }
117 
118  if (errMsg.str().size() > 0)
119  throw std::logic_error(errMsg.str().c_str());
120 }
121 
146  const Teuchos::ParameterList &plSome, // in: user's parameters
147  const Teuchos::ParameterList &plAll, // in: validators for all params
148  Teuchos::ParameterList &plVal) // out: validators for user's params
149 {
150  ParameterList::ConstIterator next = plSome.begin();
151 
152  while (next != plSome.end()){
153 
154  const std::string &name = next->first;
155  const ParameterEntry &entrySome = plSome.getEntry(name);
156  const ParameterEntry &entryAll = plAll.getEntry(name);
157 
158  if (entrySome.isList()){
159  const ParameterList &sublistSome = plSome.sublist(name); // get
160  const ParameterList &sublistAll = plAll.sublist(name); // get
161  ParameterList &sublistVal = plVal.sublist(name); // create & get
162  setValidatorsInList(sublistSome, sublistAll, sublistVal);
163  }
164  else{
165  plVal.setEntry(name, entryAll);
166  }
167 
168  ++next;
169  }
170 }
171 
179  const Teuchos::ParameterList &plIn,
180  Teuchos::ParameterList &plOut)
181 {
182  ParameterList allParameters;
183 
184  try{
185  createAllParameters(allParameters);
186  }
188 
189  setValidatorsInList(plIn, allParameters, plOut);
190 }
191 
192 // Why isn't there a Teuchos method that does this?
193 
195  const Teuchos::ParameterList &pl,
196  std::ostream &os,
197  std::string listNames)
198 {
199  using std::string;
200 
201  if (listNames.size() == 0)
202  listNames = string("top");
203 
204  Array<string> subLists;
205  ParameterList::ConstIterator next = pl.begin();
206 
207  while (next != pl.end()){
208  const string &name = next->first;
209  const ParameterEntry &entry = pl.getEntry(name);
210 
211  if (entry.isList()){
212  subLists.append(name);
213  }
214  else{
215  string doc = entry.docString();
216  os << "List: "<< listNames << ", parameter: " << name << "\n";
217  if (doc.size())
218  os << doc << "\n";
219  }
220 
221  ++next;
222  }
223 
224  for (int i=0; i < subLists.size(); i++){
225  string newListName = listNames + string("/") + subLists[i];
226  const ParameterList &sublist = pl.sublist(subLists[i]);
227  printListDocumentation(sublist, os, newListName);
228  }
229 }
230 
231 
232 } //namespace Zoltan2
#define ZOLTAN2_XML_PARAMETER_STRING
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Defines Parameter related enumerators, declares functions.
XML conversion code for IntegerRangeListValidator.
void createValidatorList(const Teuchos::ParameterList &plIn, Teuchos::ParameterList &plOut)
Create a list by adding validators to the users parameter list.
void createAllParameters(Teuchos::ParameterList &pList)
Create a list of all Zoltan2 parameters and validators.
void printListDocumentation(const Teuchos::ParameterList &pl, std::ostream &os, std::string listNames)
A ParameterList validator for integer range lists.
Define IntegerRangeList validator.
static void setValidatorsInList(const Teuchos::ParameterList &plSome, const Teuchos::ParameterList &plAll, Teuchos::ParameterList &plVal)
Create a parameter list that can validate a specific list of parameters.