Zoltan2
Zoltan2_AlgBlock.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_ALGBLOCK_HPP_
46#define _ZOLTAN2_ALGBLOCK_HPP_
47
48#include <Zoltan2_Algorithm.hpp>
51
52#include <bitset>
53#include <sstream>
54#include <string>
55
60namespace Zoltan2{
61
71};
72
90template <typename Adapter>
91class AlgBlock : public Algorithm<Adapter>
92{
93
94private:
95 const RCP<const Environment> env;
96 const RCP<const Comm<int> > problemComm;
97 const RCP<const IdentifierModel<typename Adapter::base_adapter_t> > ids;
98
99public:
100 typedef typename Adapter::lno_t lno_t; // local ids
101 typedef typename Adapter::gno_t gno_t; // global ids
102 typedef typename Adapter::scalar_t scalar_t; // scalars
103 typedef typename Adapter::part_t part_t; // part numbers
104
105 // Constructor
107 const RCP<const Environment> &env_,
108 const RCP<const Comm<int> > &problemComm_,
110 ) :
111 env(env_), problemComm(problemComm_), ids(ids_)
112 {}
113
114 // Partitioning method
115 void partition(const RCP<PartitioningSolution<Adapter> > &solution)
116 {
117 env->debug(DETAILED_STATUS, std::string("Entering AlgBlock"));
118
119 int rank = env->myRank_;
120 int nprocs = env->numProcs_;
121
123 // From the IdentifierModel we need:
124 // the number of gnos
125 // number of weights per gno
126 // the weights
127
128 size_t numGnos = ids->getLocalNumIdentifiers();
129
130 ArrayView<const gno_t> idList;
131 typedef StridedData<lno_t, scalar_t> input_t;
132 ArrayView<input_t> wgtList;
133
134 ids->getIdentifierList(idList, wgtList);
135
136 // If user supplied no weights, we use uniform weights.
137 bool uniformWeights = (wgtList.size() == 0);
138
140 // Partitioning problem parameters of interest:
141 // objective
142 // imbalance_tolerance
143
144 const Teuchos::ParameterList &pl = env->getParameters();
145 const Teuchos::ParameterEntry *pe;
146
147 pe = pl.getEntryPtr("partitioning_objective");
148 if (pe) {
149 std::string po = pe->getValue<std::string>(&po);
150 if (po == std::string("balance_object_count"))
151 uniformWeights = true; // User requests that we ignore weights
152 }
153
154 double imbalanceTolerance=1.1;
155 pe = pl.getEntryPtr("imbalance_tolerance");
156 if (pe) imbalanceTolerance = pe->getValue<double>(&imbalanceTolerance);
157
159 // From the Solution we get part information:
160 // number of parts and part sizes
161
162 size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
163
164 Array<scalar_t> part_sizes(numGlobalParts);
165
166 if (solution->criteriaHasUniformPartSizes(0))
167 for (unsigned int i=0; i<numGlobalParts; i++)
168 part_sizes[i] = 1.0 / numGlobalParts;
169 else
170 for (unsigned int i=0; i<numGlobalParts; i++)
171 part_sizes[i] = solution->getCriteriaPartSize(0, i);
172
173 for (unsigned int i=1; i<numGlobalParts; i++)
174 part_sizes[i] += part_sizes[i-1];
175
176 // TODO assertion that last part sizes is about equal to 1.0
177
178
180 // The algorithm
181 //
182 // Block partitioning algorithm lifted from zoltan/src/simple/block.c
183 // The solution is:
184 // a list of part numbers in gno order
185 // an imbalance for each weight
186
187 scalar_t wtsum(0);
188
189 if (!uniformWeights) {
190 for (size_t i=0; i<numGnos; i++)
191 wtsum += wgtList[0][i]; // [] operator knows stride
192 }
193 else
194 wtsum = static_cast<scalar_t>(numGnos);
195
196 Array<scalar_t> scansum(nprocs+1, 0);
197
198 Teuchos::gatherAll<int, scalar_t>(*problemComm, 1, &wtsum, nprocs,
199 scansum.getRawPtr()+1);
200
201 /* scansum = sum of weights on lower processors, excluding self. */
202
203 for (int i=2; i<=nprocs; i++)
204 scansum[i] += scansum[i-1];
205
206 scalar_t globalTotalWeight = scansum[nprocs];
207
208 if (env->getDebugLevel() >= VERBOSE_DETAILED_STATUS) {
209 std::ostringstream oss("Part sizes: ");
210 for (unsigned int i=0; i < numGlobalParts; i++)
211 oss << part_sizes[i] << " ";
212 oss << std::endl << std::endl << "Weights : ";
213 for (int i=0; i <= nprocs; i++)
214 oss << scansum[i] << " ";
215 oss << std::endl;
216 env->debug(VERBOSE_DETAILED_STATUS, oss.str());
217 }
218
219 /* Loop over objects and assign part. */
220 part_t part = 0;
221 wtsum = scansum[rank];
222 Array<scalar_t> partTotal(numGlobalParts, 0);
223 ArrayRCP<part_t> gnoPart= arcp(new part_t[numGnos], 0, numGnos);
224
225 env->memory("Block algorithm memory");
226
227 for (size_t i=0; i<numGnos; i++){
228 scalar_t gnoWeight = (uniformWeights ? 1.0 : wgtList[0][i]);
229 /* wtsum is now sum of all lower-ordered object */
230 /* determine new part number for this object,
231 using the "center of gravity" */
232 while (unsigned(part)<numGlobalParts-1 &&
233 (wtsum+0.5*gnoWeight) > part_sizes[part]*globalTotalWeight)
234 part++;
235 gnoPart[i] = part;
236 partTotal[part] += gnoWeight;
237 wtsum += gnoWeight;
238 }
239
241 // Done
242
243 solution->setParts(gnoPart);
244
245 env->debug(DETAILED_STATUS, std::string("Exiting AlgBlock"));
246 }
247};
248
249} // namespace Zoltan2
250
251#endif
Defines the IdentifierModel interface.
Defines the PartitioningSolution class.
Adapter::part_t part_t
Adapter::gno_t gno_t
Adapter::scalar_t scalar_t
AlgBlock(const RCP< const Environment > &env_, const RCP< const Comm< int > > &problemComm_, const RCP< const IdentifierModel< typename Adapter::base_adapter_t > > &ids_)
void partition(const RCP< PartitioningSolution< Adapter > > &solution)
Partitioning method.
Adapter::lno_t lno_t
Algorithm defines the base class for all algorithms.
IdentifierModel defines the interface for all identifier models.
A PartitioningSolution is a solution to a partitioning problem.
The StridedData class manages lists of weights or coordinates.
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:17
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:18
Created by mbenlioglu on Aug 31, 2020.
@ DETAILED_STATUS
sub-steps, each method's entry and exit
@ VERBOSE_DETAILED_STATUS
include more detail about sub-steps
blockParams
The boolean parameters of interest to the Block algorithm.
@ block_balanceTotalMaximum
@ block_minMaximumWeight
SparseMatrixAdapter_t::part_t part_t