Zoltan2
Zoltan2_Parameters.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #include <Zoltan2_Parameters.hpp>
00051 #include <Zoltan2_IntegerRangeList.hpp>
00052 
00053 #include <Teuchos_StringInputSource.hpp>
00054 #include <Teuchos_XMLParser.hpp>
00055 #include <Teuchos_XMLObject.hpp>
00056 #include <Teuchos_XMLParameterListReader.hpp>
00057 #include <Teuchos_ValidatorXMLConverterDB.hpp>
00058 
00059 // A generated header file in {zoltan2-binary-directory}/src
00060 // which defines ZOLTAN2_XML_PARAMETER_STRING.
00061 
00062 #include <Zoltan2_XML_Parameters.hpp>  
00063 
00064 using namespace std;
00065 
00066 namespace Zoltan2 {
00067 
00076 void createAllParameters(Teuchos::ParameterList &pList)
00077 {
00078   // An XML converter for IntegerRangeListValidator
00079   // needs to be added to the converter database.
00080 
00081   typedef Zoltan2::IntegerRangeListValidator<int> irl_t;
00082   typedef Zoltan2::IntegerRangeListValidatorXMLConverter<int> irlConverter_t;
00083 
00084   RCP<const irl_t> intRangeValidatorP = rcp(new irl_t); // dummy
00085   RCP<irlConverter_t > converter = rcp(new irlConverter_t);
00086   Teuchos::ValidatorXMLConverterDB::addConverter(
00087         intRangeValidatorP,
00088         converter);
00089 
00090   // Create a Teuchos::ParameterList from an XML string.
00091   // To add a parameter to Zoltan2, edit zoltan2/data/parameters.xml.
00092 
00093   std::string xmlParameterString(ZOLTAN2_XML_PARAMETER_STRING);
00094   Teuchos::StringInputSource src(xmlParameterString);
00095 
00096   Teuchos::XMLObject xmlObj;
00097   ostringstream errMsg;
00098 
00099   Teuchos::XMLParser parser(src.stream());
00100 
00101   try{
00102     xmlObj = parser.parse();
00103   }
00104   catch (std::exception &e){
00105     errMsg << e.what() << " invalid xml";
00106   }
00107 
00108   if (errMsg.str().size() == 0){
00109     try{
00110       Teuchos::XMLParameterListReader rdr;
00111       pList = rdr.toParameterList(xmlObj);
00112     }
00113     catch (std::exception &e){
00114       errMsg << e.what() << " invalid parameter list";
00115     }
00116   }
00117 
00118   if (errMsg.str().size() > 0)
00119     throw std::logic_error(errMsg.str().c_str());
00120 }
00121 
00145 static void setValidatorsInList(
00146   const Teuchos::ParameterList &plSome,   // in: user's parameters
00147   const Teuchos::ParameterList &plAll,    // in: validators for all params
00148   Teuchos::ParameterList &plVal)          // out: validators for user's params
00149 {
00150   ParameterList::ConstIterator next = plSome.begin();
00151 
00152   while (next != plSome.end()){
00153 
00154     const std::string &name = next->first;
00155     const ParameterEntry &entrySome = plSome.getEntry(name);
00156     const ParameterEntry &entryAll = plAll.getEntry(name);
00157 
00158     if (entrySome.isList()){
00159       const ParameterList &sublistSome = plSome.sublist(name);   // get
00160       const ParameterList &sublistAll = plAll.sublist(name);     // get
00161       ParameterList &sublistVal = plVal.sublist(name);     // create & get
00162       setValidatorsInList(sublistSome, sublistAll, sublistVal);
00163     }
00164     else{
00165       plVal.setEntry(name, entryAll);
00166     }
00167 
00168     ++next;
00169   }
00170 }
00171 
00178 void createValidatorList(
00179    const Teuchos::ParameterList &plIn,
00180    Teuchos::ParameterList &plOut)
00181 {
00182   ParameterList allParameters;
00183 
00184   try{
00185     createAllParameters(allParameters);
00186   }
00187   Z2_FORWARD_EXCEPTIONS
00188 
00189   setValidatorsInList(plIn, allParameters, plOut);
00190 }
00191 
00192 // Why isn't there a Teuchos method that does this?
00193 
00194 void printListDocumentation(
00195   const Teuchos::ParameterList &pl,
00196   std::ostream &os,
00197   std::string listNames)
00198 {
00199   using std::string;
00200 
00201   if (listNames.size() == 0)
00202     listNames = string("top");
00203 
00204   Array<string> subLists;
00205   ParameterList::ConstIterator next = pl.begin();
00206 
00207   while (next != pl.end()){
00208     const string &name = next->first;
00209     const ParameterEntry &entry = pl.getEntry(name);
00210 
00211     if (entry.isList()){
00212       subLists.append(name);
00213     }
00214     else{
00215       string doc = entry.docString();
00216       os << "List: "<< listNames << ", parameter: " << name << "\n";
00217       if (doc.size())
00218         os << doc << "\n";
00219     }
00220 
00221     ++next;
00222   }
00223 
00224   for (int i=0; i < subLists.size(); i++){
00225     string newListName = listNames + string("/") + subLists[i];
00226     const ParameterList &sublist = pl.sublist(subLists[i]);
00227     printListDocumentation(sublist, os, newListName);
00228   }
00229 }
00230 
00231 
00232 }  //namespace Zoltan2