Zoltan2  Version of the Day
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 
50 
51 #include <sstream>
52 #include <string>
53 #include <bitset>
54 
60 
61 namespace Zoltan2{
62 
72 };
73 
93 template <typename Adapter>
94 void AlgBlock(
95  const RCP<const Environment> &env,
96  const RCP<Comm<int> > &problemComm,
98  RCP<PartitioningSolution<Adapter> > &solution
99 )
100 {
101  using std::string;
102  using std::ostringstream;
103  typedef typename Adapter::lno_t lno_t; // local ids
104  typedef typename Adapter::gno_t gno_t; // global ids
105  typedef typename Adapter::scalar_t scalar_t; // scalars
106 
107  env->debug(DETAILED_STATUS, string("Entering AlgBlock"));
108 
109  int rank = env->myRank_;
110  int nprocs = env->numProcs_;
111 
113  // Partitioning problem parameters of interest:
114  // objective
115  // imbalance_tolerance
116 
117  std::bitset<NUM_BLOCK_PARAMS> params;
118  double imbalanceTolerance=1.1;
119 
120  const Teuchos::ParameterList &pl = env->getParameters();
121 
122  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("partitioning_objective");
123 
124  if (pe){
125  string po;
126  po = pe->getValue<string>(&po);
127  if (po == string("balance_object_count"))
128  params.set(block_balanceCount);
129  else if (po == string("multicriteria_minimize_total_weight"))
130  params.set(block_minTotalWeight);
131  else if (po == string("multicriteria_minimize_maximum_weight"))
132  params.set(block_minMaximumWeight);
133  else if (po == string("multicriteria_balance_total_maximum"))
134  params.set(block_balanceTotalMaximum);
135  }
136  else
137  params.set(block_balanceWeight);
138 
139  pe = pl.getEntryPtr("imbalance_tolerance");
140 
141  if (pe)
142  imbalanceTolerance = pe->getValue<double>(&imbalanceTolerance);
143 
145  // From the IdentifierModel we need:
146  // the number of gnos
147  // number of weights per gno
148  // the weights
149 
150  size_t numGnos = ids->getLocalNumIdentifiers();
151 
152  ArrayView<const gno_t> idList;
154  ArrayView<input_t> wgtList;
155 
156  ids->getIdentifierList(idList, wgtList);
157 
158  // If user supplied no weights, we use uniform weights.
159  Array<input_t> uwArray(1);
160  if (wgtList.size() == 0)
161  wgtList = uwArray.view(0,1);
162 
163  bool uniformWeights = (wgtList[0].size() == 0);
164 
166  // From the Solution we get part information.
167 
168  size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
169 
170  size_t numLocalParts = solution->getLocalNumberOfParts();
171 
172  env->localInputAssertion(__FILE__, __LINE__,
173  "can only compute one part per proc",
174  numLocalParts == 1, BASIC_ASSERTION);
175 
177  // The algorithm
178  //
179  // Block partitioning algorithm lifted from zoltan/src/simple/block.c
180  // The solution is:
181  // a list of part numbers in gno order
182  // an imbalance for each weight
183 
184  scalar_t wtsum(0);
185 
186  if (!uniformWeights){
187  for (size_t i=0; i<numGnos; i++)
188  wtsum += wgtList[0][i]; // [] operator knows stride
189  }
190  else
191  wtsum = static_cast<scalar_t>(numGnos);
192 
193  Array<scalar_t> scansum(nprocs+1, 0);
194 
195  Teuchos::gatherAll<int, scalar_t>(*problemComm, 1, &wtsum, nprocs,
196  scansum.getRawPtr()+1);
197 
198  /* scansum = sum of weights on lower processors, excluding self. */
199 
200  for (int i=2; i<=nprocs; i++)
201  scansum[i] += scansum[i-1];
202 
203  scalar_t globalTotalWeight = scansum[nprocs];
204 
205  /* part_sizes - inclusive sum */
206 
207  Array<scalar_t> part_sizes(numGlobalParts);
208 
209  if (!solution->criteriaHasUniformPartSizes(0))
210  for (unsigned int i=0; i<numGlobalParts; i++)
211  part_sizes[i] = solution->getCriteriaPartSize(0, i);
212  else
213  for (unsigned int i=0; i<numGlobalParts; i++)
214  part_sizes[i] = 1.0 / numGlobalParts;
215 
216  for (unsigned int i=1; i<numGlobalParts; i++)
217  part_sizes[i] += part_sizes[i-1];
218 
219  // TODO assertion that last part sizes is about equal to 1.0
220 
221  if (env->getDebugLevel() >= VERBOSE_DETAILED_STATUS) {
222  ostringstream oss("Part sizes: ");
223  for (unsigned int i=0; i < numGlobalParts; i++)
224  oss << part_sizes[i] << " ";
225  oss << "\n";
226  oss << std::endl << "Weights : ";
227  for (int i=0; i <= nprocs; i++)
228  oss << scansum[i] << " ";
229  oss << "\n";
230  env->debug(VERBOSE_DETAILED_STATUS, oss.str());
231  }
232 
233  /* Loop over objects and assign partition. */
234  partId_t part = 0;
235  wtsum = scansum[rank];
236  Array<scalar_t> partTotal(numGlobalParts, 0);
237  ArrayRCP<partId_t> gnoPart= arcp(new partId_t [numGnos], 0, numGnos);
238 
239  env->memory("Block algorithm memory");
240 
241  for (size_t i=0; i<numGnos; i++){
242  scalar_t gnoWeight = (uniformWeights ? 1.0 : wgtList[0][i]);
243  /* wtsum is now sum of all lower-ordered object */
244  /* determine new partition number for this object,
245  using the "center of gravity" */
246  while (unsigned(part)<numGlobalParts-1 &&
247  (wtsum+0.5*gnoWeight) > part_sizes[part]*globalTotalWeight)
248  part++;
249  gnoPart[i] = part;
250  partTotal[part] += gnoWeight;
251  wtsum += gnoWeight;
252  }
253 
255  // Done
256 
257  ArrayRCP<const gno_t> gnos = arcpFromArrayView(idList);
258 
259  solution->setParts(gnos, gnoPart, true);
260 
261  env->debug(DETAILED_STATUS, string("Exiting AlgBlock"));
262 }
263 
264 } // namespace Zoltan2
265 
266 #endif
void AlgBlock(const RCP< const Environment > &env, const RCP< Comm< int > > &problemComm, const RCP< const IdentifierModel< typename Adapter::base_adapter_t > > &ids, RCP< PartitioningSolution< Adapter > > &solution)
fast typical checks for valid arguments
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
zoltan2_partId_t partId_t
Defines the PartitioningSolution class.
zoltan2_partId_t partId_t
sub-steps, each method&#39;s entry and exit
list idList
Match up parameters to validators.
Defines the IdentifierModel interface.
int zoltan2_partId_t
a data type for part IDs
A PartitioningSolution is a solution to a partitioning problem.
The StridedData class manages lists of weights or coordinates.
double scalar_t
IdentifierModel defines the interface for all identifier models.
include more detail about sub-steps
blockParams
The boolean parameters of interest to the Block algorithm.