Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_SphynxProblem.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Sphynx
6 // Copyright 2020 National Technology & Engineering
7 // Solutions of Sandia, LLC (NTESS).
8 //
9 // Under the terms of Contract DE-NA0003525 with NTESS,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Seher Acer (sacer@sandia.gov)
40 // Erik Boman (egboman@sandia.gov)
41 // Siva Rajamanickam (srajama@sandia.gov)
42 // Karen Devine (kddevin@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef _ZOLTAN2_SPHYNXPROBLEM_HPP_
48 #define _ZOLTAN2_SPHYNXPROBLEM_HPP_
49 
50 
52 // This file contains the implementation of SphynxProblem.
53 //
54 // SphynxProblem is a subset of PartitioningProblem in Zoltan2Core. This subset
55 // only consists of the functionality and data members needed by Sphynx.
56 //
57 // SphynxProblem acts as an interface between user and the Sphynx algorithm.
58 // User creates the SphynxProblem object on her adapter and calls solve() to
59 // get a partitioning solution.
60 //
62 
64 #include "Zoltan2_Sphynx.hpp"
66 
67 namespace Zoltan2 {
68 
69 
72  static void getSphynxValidParameters(ParameterList & pl)
73  {
74 
75  RCP<Teuchos::StringValidator> sphynx_preconditionner_type_method_Validator =
76  Teuchos::rcp( new Teuchos::StringValidator(Teuchos::tuple<std::string>( "muelu", "jacobi", "polynomial")));
77 
78  pl.set("sphynx_preconditioner_type", "polynomial", "Sphynx preconditioner type", sphynx_preconditionner_type_method_Validator);
79 
80  RCP<Teuchos::StringValidator> sphynx_initial_guess_method_Validator =
81  Teuchos::rcp( new Teuchos::StringValidator(Teuchos::tuple<std::string>( "random", "constants")));
82 
83  pl.set("sphynx_initial_guess", "random", "Sphynx initial guess", sphynx_initial_guess_method_Validator);
84 
85  RCP<Teuchos::StringValidator> sphynx_eigensolver_Validator =
86  Teuchos::rcp( new Teuchos::StringValidator(Teuchos::tuple<std::string>( "LOBPCG", "randomized", "GeneralizedDavidson", "BlockDavidson", "BlockKrylovSchur")));
87 
88  pl.set("sphynx_eigensolver", "LOBPCG", "Sphynx eigensolver", sphynx_eigensolver_Validator);
89 
90  RCP<Teuchos::StringValidator> sphynx_problem_type_method_Validator =
91  Teuchos::rcp( new Teuchos::StringValidator(Teuchos::tuple<std::string>( "combinatorial", "normalized", "generalized")));
92 
93  pl.set("sphynx_problem_type", "combinatorial", "Sphynx problem type", sphynx_problem_type_method_Validator);
94 
95  RCP<Teuchos::EnhancedNumberValidator<int>> sphynx_verbosity_validator =
96  Teuchos::rcp( new Teuchos::EnhancedNumberValidator<int>(0, 1) );
97  pl.set("sphynx_verbosity", 0, "Sphynx verbosity.", sphynx_verbosity_validator);
98 
99  pl.set("sphynx_ortho_freq", 0, "Sphynx orthogonalization frequency");
100  pl.set("sphynx_res_freq", 0, "Sphynx residual frequency");
101  pl.set("sphynx_tolerance", 1e-1, "Sphynx tolerance");
102  pl.set("sphynx_max_iterations", 1000, "Sphynx max iterations");
103  pl.set("sphynx_block_size", 0, "Sphynx block size");
104  // bool parameter
105  pl.set("sphynx_skip_preprocessing", false, "Sphynx skip preprocessing.", Environment::getBoolValidator());
106  pl.set("sphynx_use_full_ortho", true, "Sphynx use full ortho.", Environment::getBoolValidator());
107  }
108 
110  const Teuchos::ParameterList &plSome, // in: user's parameters
111  const Teuchos::ParameterList &plAll, // in: validators for all params
112  Teuchos::ParameterList &plVal) // out: validators for user's params
113  {
114  ParameterList::ConstIterator next = plSome.begin();
115 
116  while (next != plSome.end()){
117 
118  const std::string &name = next->first;
119  const ParameterEntry &entrySome = plSome.getEntry(name);
120  const ParameterEntry &entryAll = plAll.getEntry(name);
121 
122  if (entrySome.isList()){
123  plVal.sublist(name); // create & get
124  // Don't set validators for sublists; sublists are for TPL's parameters
125  }
126  else{
127  plVal.setEntry(name, entryAll);
128  }
129 
130  ++next;
131  }
132  }
133 
134  template <typename Adapter>
135  class SphynxProblem : public PartitioningProblem<Adapter>
136  {
137 
138  public:
139 
140  using part_t = typename Adapter::part_t;
141  using weight_t = typename Adapter::scalar_t;
142  using scalar_t = double; // Sphynx with scalar_t=double obtains better cutsize
143  using lno_t = typename Adapter::lno_t;
144  using gno_t = typename Adapter::gno_t;
145  using node_t = typename Adapter::node_t;
146  using mvector_t = typename Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t>;
147  typedef typename Adapter::base_adapter_t base_adapter_t; // CHeck to Remove
148 
152 
153  // Constructor where Teuchos communicator is specified
154  SphynxProblem(Adapter *A,
155  Teuchos::ParameterList *p,
156  RCP<Teuchos::ParameterList> sphynxParams,
157  const RCP<const Teuchos::Comm<int> > &comm):
158  PartitioningProblem<Adapter>(A, p, comm), sphynxParams_(sphynxParams)
159  {
160  // Validation of SphynxParameter
161  ParameterList validParams;
162  try{
163  ParameterList allParameters;
164  getSphynxValidParameters(allParameters);
165 
166  setSphynxValidatorsInList(*(sphynxParams_.get()), allParameters, validParams);
167  }
169 
170  sphynxParams_->validateParametersAndSetDefaults(validParams, 0);
171 
172  int nparts = -1;
173  const Teuchos::ParameterEntry *pe = this->params_->getEntryPtr("num_global_parts");
174  if(pe)
175  nparts = pe->getValue<int>(&nparts);
176 
177  if(nparts == -1)
178  throw std::runtime_error("\nUser did not set num_global_parts"
179  "in the parameter list!n");
180  }
181 
182 #ifdef HAVE_ZOLTAN2_MPI
183  // Constructor where MPI communicator can be specified
184  SphynxProblem(Adapter *A, ParameterList *p, RCP<Teuchos::ParameterList> sphynxParams, MPI_Comm mpicomm):
185  SphynxProblem(A, p,sphynxParams,
186  rcp<const Comm<int> >(new Teuchos::MpiComm<int>(
187  Teuchos::opaqueWrapper(mpicomm))))
188  {}
189 #endif
190 
191  // Constructor where communicator is the Teuchos default.
192  SphynxProblem(Adapter *A, ParameterList *p, RCP<Teuchos::ParameterList> sphynxParams):
193  SphynxProblem(A, p,sphynxParams, Tpetra::getDefaultComm())
194  {}
195 
196  // Destructor
198 
202 
203  void createAlgorithm() override;
204  void processAlgorithmName(const std::string& algorithm, const std::string& defString, const std::string& model,
205  Environment &env, bool& removeSelfEdges, bool& isGraphType, bool& needConsecutiveGlobalIds) override;
206  RCP<mvector_t> getSphynxEigenvectors();
207  void setUserEigenvectors(const RCP<mvector_t> &userEvects);
208 
212 
213 
215  return *(this->solution_.getRawPtr());
216  };
217 
221 
222  private:
223  Teuchos::RCP<Teuchos::ParameterList> envParams_;
224  RCP<ParameterList> sphynxParams_;
225  RCP<mvector_t> eigenVectors_;
226 
227  };
228 
232 
233  template <typename Adapter>
235  const std::string &algorithm, const std::string &defString,
236  const std::string &model, Environment &env, bool &removeSelfEdges,
237  bool &isGraphType, bool &needConsecutiveGlobalIds)
238  {
239  this->algName_ = std::string("sphynx");
240  }
241 
242  template <typename Adapter>
244  {
245  // Create the algorithm
246  if (this->algName_ == std::string("sphynx")) {
247  this->algorithm_ = Teuchos::rcp(new Zoltan2::Sphynx<Adapter>(this->envConst_,
248  this->params_,
249  this->sphynxParams_,
250  this->comm_,
251  this->inputAdapter_));
252  if( this->eigenVectors_!=Teuchos::null ){
253  Teuchos::rcp_dynamic_cast<Zoltan2::Sphynx<Adapter>>(this->algorithm_)->setUserEigenvectors(eigenVectors_);
254  }
255  }
256  else {
257  throw std::logic_error("partitioning algorithm not supported");
258  }
259  }
260 
262  // Allows the user to manually set eigenvectors for the Sphynx partitioner
263  // to use rather than solving for them with Anasazi. Mainly intended
264  // for debugging purposes.
266  template <typename Adapter>
267  void SphynxProblem<Adapter>::setUserEigenvectors(const RCP<mvector_t> &userEvects)
268  {
269  eigenVectors_ = userEvects;
270  }
271 
273  // Returns an RCP containing a deep copy of the eigenvectors used by Sphynx.
275  template <typename Adapter>
276  Teuchos::RCP<Tpetra::MultiVector<double, typename Adapter::lno_t, typename Adapter::gno_t, typename Adapter::node_t> >
278  {
279  if(this->algorithm_!=Teuchos::null){
280  return Teuchos::rcp_dynamic_cast<Zoltan2::Sphynx<Adapter>>(this->algorithm_)->getSphynxEigenvectors();
281  }
282  else{
283  return Teuchos::null;
284  }
285  }
286 } // namespace Zoltan2
287 
288 #endif
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Adapter::base_adapter_t base_adapter_t
static RCP< Teuchos::BoolParameterEntryValidator > getBoolValidator()
Exists to make setting up validators less cluttered.
SphynxProblem(Adapter *A, ParameterList *p, RCP< Teuchos::ParameterList > sphynxParams)
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:18
typename Zoltan2::InputTraits< ztcrsmatrix_t >::node_t node_t
Defines the PartitioningSolution class.
static void setSphynxValidatorsInList(const Teuchos::ParameterList &plSome, const Teuchos::ParameterList &plAll, Teuchos::ParameterList &plVal)
void setUserEigenvectors(const RCP< mvector_t > &userEvects)
SparseMatrixAdapter_t::part_t part_t
A PartitioningSolution is a solution to a partitioning problem.
SphynxProblem(Adapter *A, Teuchos::ParameterList *p, RCP< Teuchos::ParameterList > sphynxParams, const RCP< const Teuchos::Comm< int > > &comm)
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:17
RCP< PartitioningSolution< Adapter > > solution_
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
static void getSphynxValidParameters(ParameterList &pl)
Set up validators specific to this algorithm.
PartitioningProblem sets up partitioning problems for the user.
typename Adapter::node_t node_t
typename Tpetra::MultiVector< scalar_t, lno_t, gno_t, node_t > mvector_t
RCP< ParameterList > params_
void processAlgorithmName(const std::string &algorithm, const std::string &defString, const std::string &model, Environment &env, bool &removeSelfEdges, bool &isGraphType, bool &needConsecutiveGlobalIds) override
Defines the PartitioningProblem class.
typename Adapter::scalar_t weight_t
RCP< mvector_t > getSphynxEigenvectors()
const PartitioningSolution< Adapter > & getSolution()