Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_AlgQuotient.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef _ZOLTAN2_ALGQUOTIENT_HPP_
11 #define _ZOLTAN2_ALGQUOTIENT_HPP_
12 
14 #include <Zoltan2_Algorithm.hpp>
16 #include <Zoltan2_Util.hpp>
17 
18 
21 //
22 // This algorithm
23 // - creates a CommGraphModel (communication graph model)
24 // - partitions the CommGraphModel using ParMETIS, and
25 // - transfers the solution (which is only stored on active ranks of
26 // CommGraphModel) to all MPI ranks.
27 //
28 // Please see model/Zoltan2_CommGraphModel.hpp for more details.
30 
31 namespace Zoltan2 {
32 
33 template <typename Adapter>
34 class AlgQuotient : public Algorithm<Adapter>
35 {
36 public:
38  typedef typename Adapter::part_t part_t;
39  typedef typename Adapter::user_t user_t;
40  typedef typename Adapter::userCoord_t userCoord_t;
42 
43 
51  AlgQuotient(const RCP<const Environment> &env__,
52  const RCP<const Comm<int> > &problemComm__,
53  const RCP<const IdentifierAdapter<user_t> > &adapter__,
54  const modelFlag_t& graphFlags_) :
55  env(env__), problemComm(problemComm__), adapter(adapter__), graphFlags(graphFlags_)
56  {
57  std::string errStr = "cannot build CommGraphModel from IdentifierAdapter, ";
58  errStr += "AlgQuotient requires Graph Adapter";
59  throw std::runtime_error(errStr);
60  }
61 
62  AlgQuotient(const RCP<const Environment> &env__,
63  const RCP<const Comm<int> > &problemComm__,
64  const RCP<const VectorAdapter<user_t> > &adapter__,
65  const modelFlag_t& graphFlags_) :
66  env(env__), problemComm(problemComm__), adapter(adapter__), graphFlags(graphFlags_)
67  {
68  std::string errStr = "cannot build CommGraphModel from VectorAdapter, ";
69  errStr += "AlgQuotient requires Graph Adapter";
70  throw std::runtime_error(errStr);
71  }
72 
73  AlgQuotient(const RCP<const Environment> &env__,
74  const RCP<const Comm<int> > &problemComm__,
75  const RCP<const MatrixAdapter<user_t,userCoord_t> > &adapter__,
76  const modelFlag_t& graphFlags_) :
77  env(env__), problemComm(problemComm__), adapter(adapter__), graphFlags(graphFlags_)
78  {
79  std::string errStr = "cannot build CommGraphModel from MatrixAdapter, ";
80  errStr += "AlgQuotient has not been implemented for Matrix Adapter yet.";
81  throw std::runtime_error(errStr);
82  }
83 
84  AlgQuotient(const RCP<const Environment> &env__,
85  const RCP<const Comm<int> > &problemComm__,
86  const RCP<const MeshAdapter<user_t> > &adapter__,
87  const modelFlag_t& graphFlags_) :
88  env(env__), problemComm(problemComm__), adapter(adapter__), graphFlags(graphFlags_)
89  {
90  std::string errStr = "cannot build CommGraphModel from MeshAdapter, ";
91  errStr += "AlgQuotient has not been implemented for Mesh Adapter yet.";
92  throw std::runtime_error(errStr);
93  }
94 
95  AlgQuotient(const RCP<const Environment> &env__,
96  const RCP<const Comm<int> > &problemComm__,
97  const RCP<const GraphAdapter<user_t,userCoord_t> > &adapter__,
98  const modelFlag_t& graphFlags_) :
99  env(env__), problemComm(problemComm__), adapter(adapter__), graphFlags(graphFlags_)
100  {
101  this->innerAlgorithm =
102  rcp(new AlgParMETIS<Adapter, graphModel_t>(env, problemComm, adapter, graphFlags));
103  }
104 
107  static void getValidParameters(ParameterList & pl)
108  {
109  pl.set("quotient_threshold", 1, "threshold for the number of vertices on the active ranks",
111  }
112 
113  void partition(const RCP<PartitioningSolution<Adapter> > &solution);
114  void migrateBack(const RCP<PartitioningSolution<Adapter> > &solution);
115 
116 private:
117 
118  const RCP<const Environment> env;
119  const RCP<const Comm<int> > problemComm;
120  const RCP<const base_adapter_t> adapter;
121  modelFlag_t graphFlags;
122 
123  RCP<Algorithm<Adapter>> innerAlgorithm; // algorithm to partition the quotient graph
124  RCP<PartitioningSolution<Adapter>> quotientSolution; // the solution stored on the active ranks
125 
126 };
127 
128 
130 template <typename Adapter>
132  const RCP<PartitioningSolution<Adapter> > &solution
133 )
134 {
135  HELLO;
136 
137  // Create a different object for pre-migration solution
138  PartitioningSolution<Adapter> *soln = NULL;
139  try{
140  soln = new PartitioningSolution<Adapter>(env, problemComm, 1);
141  }
143  quotientSolution = rcp(soln);
144 
145  try {
146  this->innerAlgorithm->partition(quotientSolution);
147  }
149 
150  // Migrate the solution
151  migrateBack(solution);
152 
153  env->memory("Zoltan2-Quotient: After creating solution");
154 
155 }
156 
157 // Pre-condition:
158 // The partitioning solution in quotientSolution is only stored on active MPI ranks,
159 // which is a single rank if commGraphModel has less than 'threshold_' vertices.
160 // Post-condition:
161 // The partitioning solution in output parameter 'solution' is distributed to all MPI ranks,
162 // so that each rank gets a partitioning vector with the size of the number of local vertices,
163 // and each entry in a goven rank should have the same part value.
164 template <typename Adapter>
166  const RCP<PartitioningSolution<Adapter> > &solution
167 )
168 {
169  const auto model = rcp(new CommGraphModel<base_adapter_t>(
170  this->adapter, this->env, this->problemComm));
171  int me = problemComm->getRank();
172  int nActiveRanks = model->getNumActiveRanks();
173  int dRank = model->getDestinationRank();
174 
175  // Receive the (single entry) partitioning solution for this MPI rank
176  Teuchos::ArrayRCP<part_t> parts(1);
177  RCP<CommRequest<int>> *requests = new RCP<CommRequest<int>>[1];
178  requests[0] = Teuchos::ireceive<int, part_t>(*problemComm, parts, dRank);
179  if (me < nActiveRanks) {
180 
181  const part_t *qtntSlnView = quotientSolution->getPartListView();
182 
183  int sRank = model->getStartRank();
184  int eRank = model->getEndRank();
185 
186  ArrayView<size_t> vtxdist;
187  model->getVertexDist(vtxdist);
188  for (int i = sRank; i < eRank; i++)
189  Teuchos::send<int, part_t>(*problemComm, 1, &qtntSlnView[i - sRank], i);
190  }
191  Teuchos::waitAll<int>(*problemComm, Teuchos::arrayView(requests, 1));
192 
193  // Extend the single-entry solution for all local vertices
194  size_t numLocalVertices = adapter->getLocalNumIDs();
195  Teuchos::ArrayRCP<part_t> extendedParts(numLocalVertices);
196  for(size_t i = 0; i < numLocalVertices; i++)
197  extendedParts[i] = parts[0];
198 
199  // Set the extended partitioning solution
200  solution->setParts(extendedParts);
201 
202 }
203 
204 
205 } // namespace Zoltan2
206 
208 
209 
210 #endif
211 
#define HELLO
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
IdentifierAdapter defines the interface for identifiers.
CommGraphModel< typename Adapter::base_adapter_t > graphModel_t
void migrateBack(const RCP< PartitioningSolution< Adapter > > &solution)
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
MatrixAdapter defines the adapter interface for matrices.
GraphAdapter defines the interface for graph-based user data.
MeshAdapter defines the interface for mesh input.
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const MatrixAdapter< user_t, userCoord_t > > &adapter__, const modelFlag_t &graphFlags_)
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const MeshAdapter< user_t > > &adapter__, const modelFlag_t &graphFlags_)
Defines the PartitioningSolution class.
void partition(const RCP< PartitioningSolution< Adapter > > &solution)
Partitioning method.
SparseMatrixAdapter_t::part_t part_t
CommGraphModel defines the interface required for communication graph.
A PartitioningSolution is a solution to a partitioning problem.
VectorAdapter defines the interface for vector input.
Algorithm defines the base class for all algorithms.
static RCP< Teuchos::AnyNumberParameterEntryValidator > getAnyIntValidator()
Exists to make setting up validators less cluttered.
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const VectorAdapter< user_t > > &adapter__, const modelFlag_t &graphFlags_)
Adapter::userCoord_t userCoord_t
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const IdentifierAdapter< user_t > > &adapter__, const modelFlag_t &graphFlags_)
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const GraphAdapter< user_t, userCoord_t > > &adapter__, const modelFlag_t &graphFlags_)
static void getValidParameters(ParameterList &pl)
Set up validators specific to this algorithm.
A gathering of useful namespace methods.
Adapter::base_adapter_t base_adapter_t
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:39