Zoltan2
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"
65 
66 namespace Zoltan2 {
67 
68  template <typename Adapter>
70  {
71 
72  public:
73 
74  using part_t = typename Adapter::part_t;
75  using weight_t = typename Adapter::scalar_t;
76 
80 
81  // Constructor where Teuchos communicator is specified
82  SphynxProblem(Teuchos::RCP<Adapter> A,
83  Teuchos::RCP<Teuchos::ParameterList> p,
84  const Teuchos::RCP<const Teuchos::Comm<int> > &comm):
85  inputAdapter_(A),
86  params_(p),
87  comm_(comm),
88  solution_(),
89  numberOfWeights_(),
90  numberOfCriteria_()
91  {
92 
93  numberOfWeights_ = this->inputAdapter_->getNumWeightsPerID();
94  numberOfCriteria_ = (numberOfWeights_ > 1) ? numberOfWeights_ : 1;
95 
96  Teuchos::ArrayRCP<part_t> *noIds =
97  new Teuchos::ArrayRCP<part_t> [numberOfCriteria_];
98  Teuchos::ArrayRCP<weight_t> *noSizes =
99  new Teuchos::ArrayRCP<weight_t> [numberOfCriteria_];
100 
101  partIds_ = Teuchos::arcp(noIds, 0, numberOfCriteria_, true);
102  partSizes_ = Teuchos::arcp(noSizes, 0, numberOfCriteria_, true);
103 
104  int nparts = -1;
105  const Teuchos::ParameterEntry *pe = params_->getEntryPtr("num_global_parts");
106  if(pe)
107  nparts = pe->getValue<int>(&nparts);
108 
109  if(nparts == -1)
110  throw std::runtime_error("\nUser did not set num_global_parts"
111  "in the parameter list!n");
112 
113 
114  envParams_ = Teuchos::rcp(new Teuchos::ParameterList());
115  envParams_->set("num_global_parts", nparts);
116 
117  env_ = Teuchos::rcp(new Environment(*envParams_, comm_));
118  envConst_ = Teuchos::rcp_const_cast<const Environment>(env_);
119 
120  }
121 
122 #ifdef HAVE_ZOLTAN2_MPI
123  // Constructor where MPI communicator can be specified
124  SphynxProblem(Teuchos::RCP<Adapter> A,
125  Teuchos::RCP<Teuchos::ParameterList> p,
126  MPI_Comm mpicomm):
127  SphynxProblem(A, p, Teuchos::rcp<const Teuchos::Comm<int>>
128  (new Teuchos::MpiComm<int>
129  (Teuchos::opaqueWrapper(mpicomm))))
130  {}
131 #endif
132 
133  // Constructor where communicator is the Teuchos default.
134  SphynxProblem(Teuchos::RCP<Adapter> A,
135  Teuchos::RCP<Teuchos::ParameterList> p):
136  SphynxProblem(A, p, Tpetra::getDefaultComm())
137  {}
138 
139  // Destructor
141 
145 
146  void solve();
147 
148 
152 
153 
155  return *(solution_.getRawPtr());
156  };
157 
161 
162  private:
163 
164  Teuchos::RCP<Adapter> inputAdapter_;
165  Teuchos::RCP<Teuchos::ParameterList> params_;
166  Teuchos::RCP<const Teuchos::Comm<int>> comm_;
167  Teuchos::RCP<Algorithm<Adapter> > algorithm_;
168 
169  Teuchos::RCP<Teuchos::ParameterList> envParams_;
170  Teuchos::RCP<Environment> env_;
171  Teuchos::RCP<const Environment> envConst_;
172 
173  Teuchos::RCP<PartitioningSolution<Adapter> > solution_;
174 
175  int numberOfWeights_; // What user provides
176  int numberOfCriteria_; // What Sphynx uses
177 
178  Teuchos::ArrayRCP<Teuchos::ArrayRCP<part_t> > partIds_;
179  Teuchos::ArrayRCP<Teuchos::ArrayRCP<weight_t> > partSizes_;
180 
181  };
182 
186 
187  template <typename Adapter>
189  {
190  this->algorithm_ = Teuchos::rcp(new Zoltan2::Sphynx<Adapter>(this->envConst_,
191  this->params_,
192  this->comm_,
193  this->inputAdapter_));
194 
195 
196  PartitioningSolution<Adapter> *soln = NULL;
197 
198  try{
199 
200  soln = new PartitioningSolution<Adapter>(this->envConst_, this->comm_, numberOfWeights_,
201  partIds_.view(0, numberOfCriteria_),
202  partSizes_.view(0, numberOfCriteria_), this->algorithm_);
203  }
205 
206  solution_ = Teuchos::rcp(soln);
207 
208  // Call the algorithm
209 
210  try {
211  this->algorithm_->partition(solution_);
212  }
214  }
215 
216 
217 } // namespace Zoltan2
218 
219 #endif
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Defines the PartitioningSolution class.
The user parameters, debug, timing and memory profiling output objects, and error checking methods.
A PartitioningSolution is a solution to a partitioning problem.
typename Adapter::part_t part_t
SphynxProblem(Teuchos::RCP< Adapter > A, Teuchos::RCP< Teuchos::ParameterList > p)
SphynxProblem(Teuchos::RCP< Adapter > A, Teuchos::RCP< Teuchos::ParameterList > p, const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
const PartitioningSolution< Adapter > & getSolution()
typename Adapter::scalar_t weight_t
Created by mbenlioglu on Aug 31, 2020.
SparseMatrixAdapter_t::part_t part_t