Zoltan2
Zoltan2_AlgQuotient.hpp
Go to the documentation of this file.
1// @HEADER
2//
3// ***********************************************************************
4//
5// Zoltan2: A package of combinatorial algorithms for scientific computing
6// Copyright 2012 Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Karen Devine (kddevin@sandia.gov)
39// Erik Boman (egboman@sandia.gov)
40// Siva Rajamanickam (srajama@sandia.gov)
41//
42// ***********************************************************************
43//
44// @HEADER
45#ifndef _ZOLTAN2_ALGQUOTIENT_HPP_
46#define _ZOLTAN2_ALGQUOTIENT_HPP_
47
49#include <Zoltan2_Algorithm.hpp>
51#include <Zoltan2_Util.hpp>
52
53
56//
57// This algorithm
58// - creates a CommGraphModel (communication graph model)
59// - partitions the CommGraphModel using ParMETIS, and
60// - transfers the solution (which is only stored on active ranks of
61// CommGraphModel) to all MPI ranks.
62//
63// Please see model/Zoltan2_CommGraphModel.hpp for more details.
65
66namespace Zoltan2 {
67
68template <typename Adapter>
69class AlgQuotient : public Algorithm<Adapter>
70{
71public:
73 typedef typename Adapter::part_t part_t;
74 typedef typename Adapter::user_t user_t;
75 typedef typename Adapter::userCoord_t userCoord_t;
77
78
86 AlgQuotient(const RCP<const Environment> &env__,
87 const RCP<const Comm<int> > &problemComm__,
88 const RCP<const IdentifierAdapter<user_t> > &adapter__) :
89 env(env__), problemComm(problemComm__), adapter(adapter__)
90 {
91 std::string errStr = "cannot build CommGraphModel from IdentifierAdapter, ";
92 errStr += "AlgQuotient requires Graph Adapter";
93 throw std::runtime_error(errStr);
94 }
95
96 AlgQuotient(const RCP<const Environment> &env__,
97 const RCP<const Comm<int> > &problemComm__,
98 const RCP<const VectorAdapter<user_t> > &adapter__) :
99 env(env__), problemComm(problemComm__), adapter(adapter__)
100 {
101 std::string errStr = "cannot build CommGraphModel from VectorAdapter, ";
102 errStr += "AlgQuotient requires Graph Adapter";
103 throw std::runtime_error(errStr);
104 }
105
106 AlgQuotient(const RCP<const Environment> &env__,
107 const RCP<const Comm<int> > &problemComm__,
108 const RCP<const MatrixAdapter<user_t,userCoord_t> > &adapter__) :
109 env(env__), problemComm(problemComm__), adapter(adapter__)
110 {
111 std::string errStr = "cannot build CommGraphModel from MatrixAdapter, ";
112 errStr += "AlgQuotient has not been implemented for Matrix Adapter yet.";
113 throw std::runtime_error(errStr);
114 }
115
116 AlgQuotient(const RCP<const Environment> &env__,
117 const RCP<const Comm<int> > &problemComm__,
118 const RCP<const MeshAdapter<user_t> > &adapter__) :
119 env(env__), problemComm(problemComm__), adapter(adapter__)
120 {
121 std::string errStr = "cannot build CommGraphModel from MeshAdapter, ";
122 errStr += "AlgQuotient has not been implemented for Mesh Adapter yet.";
123 throw std::runtime_error(errStr);
124 }
125
126 AlgQuotient(const RCP<const Environment> &env__,
127 const RCP<const Comm<int> > &problemComm__,
128 const RCP<const GraphAdapter<user_t,userCoord_t> > &adapter__) :
129 env(env__), problemComm(problemComm__), adapter(adapter__)
130 {
131 buildModel();
132 this->innerAlgorithm = rcp(new AlgParMETIS<Adapter, graphModel_t>(env,
133 problemComm,
134 model));
135 }
136
139 static void getValidParameters(ParameterList & pl)
140 {
141 pl.set("quotient_threshold", 1, "threshold for the number of vertices on the active ranks",
143 }
144
145 void partition(const RCP<PartitioningSolution<Adapter> > &solution);
146 void migrateBack(const RCP<PartitioningSolution<Adapter> > &solution);
147
148private:
149
150 void buildModel();
151
152 const RCP<const Environment> env;
153 const RCP<const Comm<int> > problemComm;
154 const RCP<const base_adapter_t> adapter;
155 RCP<graphModel_t> model;
156
157 RCP<Algorithm<Adapter>> innerAlgorithm; // algorithm to partition the quotient graph
158 RCP<PartitioningSolution<Adapter>> quotientSolution; // the solution stored on the active ranks
159
160};
161
162
164template <typename Adapter>
165void AlgQuotient<Adapter>::buildModel()
166{
167 this->env->debug(DETAILED_STATUS, " building communication graph model");
168 this->model = rcp(new CommGraphModel<base_adapter_t>(
169 this->adapter, this->env, this->problemComm));
170 this->env->debug(DETAILED_STATUS, " communication graph model built");
171}
172
173
174template <typename Adapter>
176 const RCP<PartitioningSolution<Adapter> > &solution
177)
178{
179 HELLO;
180
181 // Create a different object for pre-migration solution
183 try{
184 soln = new PartitioningSolution<Adapter>(env, problemComm, 1);
185 }
187 quotientSolution = rcp(soln);
188
189 try {
190 this->innerAlgorithm->partition(quotientSolution);
191 }
193
194 // Migrate the solution
195 migrateBack(solution);
196
197 env->memory("Zoltan2-Quotient: After creating solution");
198
199}
200
201// Pre-condition:
202// The partitioning solution in quotientSolution is only stored on active MPI ranks,
203// which is a single rank if commGraphModel has less than 'threshold_' vertices.
204// Post-condition:
205// The partitioning solution in output parameter 'solution' is distributed to all MPI ranks,
206// so that each rank gets a partitioning vector with the size of the number of local vertices,
207// and each entry in a goven rank should have the same part value.
208template <typename Adapter>
210 const RCP<PartitioningSolution<Adapter> > &solution
211)
212{
213 int me = problemComm->getRank();
214 int nActiveRanks = model->getNumActiveRanks();
215 int dRank = model->getDestinationRank();
216
217 // Receive the (single entry) partitioning solution for this MPI rank
218 Teuchos::ArrayRCP<part_t> parts(1);
219 RCP<CommRequest<int>> *requests = new RCP<CommRequest<int>>[1];
220 requests[0] = Teuchos::ireceive<int, part_t>(*problemComm, parts, dRank);
221 if(me < nActiveRanks){
222
223 const part_t *qtntSlnView = quotientSolution->getPartListView();
224
225 int sRank = model->getStartRank();
226 int eRank = model->getEndRank();
227
228 ArrayView<size_t> vtxdist;
229 model->getVertexDist(vtxdist);
230 for(int i = sRank; i < eRank; i++)
231 Teuchos::send<int, part_t>(*problemComm, 1, &qtntSlnView[i-sRank], i);
232 }
233 Teuchos::waitAll<int>(*problemComm, Teuchos::arrayView(requests, 1));
234
235 // Extend the single-entry solution for all local vertices
236 size_t numLocalVertices = adapter->getLocalNumIDs();
237 Teuchos::ArrayRCP<part_t> extendedParts(numLocalVertices);
238 for(size_t i = 0; i < numLocalVertices; i++)
239 extendedParts[i] = parts[0];
240
241 // Set the extended partitioning solution
242 solution->setParts(extendedParts);
243
244}
245
246
247} // namespace Zoltan2
248
250
251
252#endif
253
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:74
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Defines the PartitioningSolution class.
#define HELLO
A gathering of useful namespace methods.
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const MeshAdapter< user_t > > &adapter__)
CommGraphModel< typename Adapter::base_adapter_t > graphModel_t
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const VectorAdapter< user_t > > &adapter__)
void partition(const RCP< PartitioningSolution< Adapter > > &solution)
Partitioning method.
static void getValidParameters(ParameterList &pl)
Set up validators specific to this algorithm.
void migrateBack(const RCP< PartitioningSolution< Adapter > > &solution)
Adapter::base_adapter_t base_adapter_t
Adapter::userCoord_t userCoord_t
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const IdentifierAdapter< user_t > > &adapter__)
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const GraphAdapter< user_t, userCoord_t > > &adapter__)
AlgQuotient(const RCP< const Environment > &env__, const RCP< const Comm< int > > &problemComm__, const RCP< const MatrixAdapter< user_t, userCoord_t > > &adapter__)
Algorithm defines the base class for all algorithms.
CommGraphModel defines the interface required for communication graph.
static RCP< Teuchos::AnyNumberParameterEntryValidator > getAnyIntValidator()
Exists to make setting up validators less cluttered.
GraphAdapter defines the interface for graph-based user data.
IdentifierAdapter defines the interface for identifiers.
MatrixAdapter defines the adapter interface for matrices.
MeshAdapter defines the interface for mesh input.
A PartitioningSolution is a solution to a partitioning problem.
VectorAdapter defines the interface for vector input.
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Created by mbenlioglu on Aug 31, 2020.
@ DETAILED_STATUS
sub-steps, each method's entry and exit
SparseMatrixAdapter_t::part_t part_t