Zoltan2
Loading...
Searching...
No Matches
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
67namespace Zoltan2 {
68
69
72static 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
81 RCP<Teuchos::StringValidator> sphynx_initial_guess_method_Validator =
82 Teuchos::rcp( new Teuchos::StringValidator(Teuchos::tuple<std::string>( "random", "constants")));
83
84 pl.set("sphynx_initial_guess", "random", "Sphynx initial guess", sphynx_initial_guess_method_Validator);
85
86 RCP<Teuchos::StringValidator> sphynx_eigensolver_Validator =
87 Teuchos::rcp( new Teuchos::StringValidator(Teuchos::tuple<std::string>( "LOBPCG", "randomized")));
88
89 pl.set("sphynx_eigensolver", "LOBPCG", "Sphynx eigensolver", sphynx_eigensolver_Validator);
90
91 RCP<Teuchos::StringValidator> sphynx_problem_type_method_Validator =
92 Teuchos::rcp( new Teuchos::StringValidator(Teuchos::tuple<std::string>( "combinatorial", "normalized", "generalized")));
93
94 pl.set("sphynx_problem_type", "combinatorial", "Sphynx problem type", sphynx_problem_type_method_Validator);
95
96 RCP<Teuchos::EnhancedNumberValidator<int>> sphynx_verbosity_validator =
97 Teuchos::rcp( new Teuchos::EnhancedNumberValidator<int>(0, 1) );
98 pl.set("sphynx_verbosity", 0, "Sphynx verbosity.", sphynx_verbosity_validator);
99
100 pl.set("sphynx_max_iterations", 1000, "Sphynx max iterations");
101 pl.set("sphynx_block_size", 0, "Sphynx block size");
102 // bool parameter
103 pl.set("sphynx_skip_preprocessing", false, "Sphynx skip preprocessing.", Environment::getBoolValidator());
104 pl.set("sphynx_use_full_ortho", true, "Sphynx use full ortho.", Environment::getBoolValidator());
105}
106
108 const Teuchos::ParameterList &plSome, // in: user's parameters
109 const Teuchos::ParameterList &plAll, // in: validators for all params
110 Teuchos::ParameterList &plVal) // out: validators for user's params
111{
112 ParameterList::ConstIterator next = plSome.begin();
113
114 while (next != plSome.end()){
115
116 const std::string &name = next->first;
117 const ParameterEntry &entrySome = plSome.getEntry(name);
118 const ParameterEntry &entryAll = plAll.getEntry(name);
119
120 if (entrySome.isList()){
121 plVal.sublist(name); // create & get
122 // Don't set validators for sublists; sublists are for TPL's parameters
123 }
124 else{
125 plVal.setEntry(name, entryAll);
126 }
127
128 ++next;
129 }
130}
131
132 template <typename Adapter>
133 class SphynxProblem : public PartitioningProblem<Adapter>
134 {
135
136 public:
137
138 using part_t = typename Adapter::part_t;
139 using weight_t = typename Adapter::scalar_t;
140 using scalar_t = double; // Sphynx with scalar_t=double obtains better cutsize
141 using lno_t = typename Adapter::lno_t;
142 using gno_t = typename Adapter::gno_t;
143 using node_t = typename Adapter::node_t;
144 using mvector_t = typename Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t>;
145 typedef typename Adapter::base_adapter_t base_adapter_t; // CHeck to Remove
146
150
151 // Constructor where Teuchos communicator is specified
152 SphynxProblem(Adapter *A,
153 Teuchos::ParameterList *p,
154 RCP<Teuchos::ParameterList> sphynxParams,
155 const RCP<const Teuchos::Comm<int> > &comm):
156 PartitioningProblem<Adapter>(A, p, comm), sphynxParams_(sphynxParams)
157 {
158 // Validation of SphynxParameter
159 ParameterList validParams;
160 try{
161 ParameterList allParameters;
162 getSphynxValidParameters(allParameters);
163
164 setSphynxValidatorsInList(*(sphynxParams_.get()), allParameters, validParams);
165 }
167
168 sphynxParams_->validateParametersAndSetDefaults(validParams, 0);
169 this->env_->convertStringToInt(*sphynxParams_.get());
170
171 int nparts = -1;
172 const Teuchos::ParameterEntry *pe = this->params_->getEntryPtr("num_global_parts");
173 if(pe)
174 nparts = pe->getValue<int>(&nparts);
175
176 if(nparts == -1)
177 throw std::runtime_error("\nUser did not set num_global_parts"
178 "in the parameter list!n");
179 }
180
181#ifdef HAVE_ZOLTAN2_MPI
182 // Constructor where MPI communicator can be specified
183 SphynxProblem(Adapter *A, ParameterList *p, RCP<Teuchos::ParameterList> sphynxParams, MPI_Comm mpicomm):
184 SphynxProblem(A, p,sphynxParams,
185 rcp<const Comm<int> >(new Teuchos::MpiComm<int>(
186 Teuchos::opaqueWrapper(mpicomm))))
187 {}
188#endif
189
190 // Constructor where communicator is the Teuchos default.
191 SphynxProblem(Adapter *A, ParameterList *p, RCP<Teuchos::ParameterList> sphynxParams):
192 SphynxProblem(A, p,sphynxParams, Tpetra::getDefaultComm())
193 {}
194
195 // Destructor
197
201
202 void createAlgorithm() override;
203 void processAlgorithmName(const std::string& algorithm, const std::string& defString, const std::string& model,
204 Environment &env, bool& removeSelfEdges, bool& isGraphType, bool& needConsecutiveGlobalIds) override;
205 RCP<mvector_t> getSphynxEigenvectors();
206 void setUserEigenvectors(const RCP<mvector_t> &userEvects);
207
211
212
214 return *(this->solution_.getRawPtr());
215 };
216
220
221 private:
222 Teuchos::RCP<Teuchos::ParameterList> envParams_;
223 RCP<ParameterList> sphynxParams_;
224 RCP<mvector_t> eigenVectors_;
225
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
287} // namespace Zoltan2
288
289#endif
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Defines the PartitioningProblem class.
Defines the PartitioningSolution class.
The user parameters, debug, timing and memory profiling output objects, and error checking methods.
static RCP< Teuchos::BoolParameterEntryValidator > getBoolValidator()
Exists to make setting up validators less cluttered.
PartitioningProblem(Adapter *A, ParameterList *p, const RCP< const Teuchos::Comm< int > > &comm)
Constructor where Teuchos communicator is specified.
RCP< PartitioningSolution< Adapter > > solution_
A PartitioningSolution is a solution to a partitioning problem.
RCP< const Environment > envConst_
RCP< const Adapter > inputAdapter_
RCP< Environment > env_
RCP< ParameterList > params_
RCP< const Comm< int > > comm_
RCP< Algorithm< Adapter > > algorithm_
typename Adapter::node_t node_t
typename Adapter::part_t part_t
void setUserEigenvectors(const RCP< mvector_t > &userEvects)
SphynxProblem(Adapter *A, Teuchos::ParameterList *p, RCP< Teuchos::ParameterList > sphynxParams, const RCP< const Teuchos::Comm< int > > &comm)
typename Tpetra::MultiVector< scalar_t, lno_t, gno_t, node_t > mvector_t
typename Adapter::lno_t lno_t
SphynxProblem(Adapter *A, ParameterList *p, RCP< Teuchos::ParameterList > sphynxParams)
typename Adapter::gno_t gno_t
const PartitioningSolution< Adapter > & getSolution()
RCP< mvector_t > getSphynxEigenvectors()
void processAlgorithmName(const std::string &algorithm, const std::string &defString, const std::string &model, Environment &env, bool &removeSelfEdges, bool &isGraphType, bool &needConsecutiveGlobalIds) override
Adapter::base_adapter_t base_adapter_t
typename Adapter::scalar_t weight_t
Created by mbenlioglu on Aug 31, 2020.
static void getSphynxValidParameters(ParameterList &pl)
Set up validators specific to this algorithm.
static void setSphynxValidatorsInList(const Teuchos::ParameterList &plSome, const Teuchos::ParameterList &plAll, Teuchos::ParameterList &plVal)