Zoltan2  Version of the Day
Zoltan2_AlgRCB.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 
50 #ifndef _ZOLTAN2_ALGRCB_HPP_
51 #define _ZOLTAN2_ALGRCB_HPP_
52 
55 #include <Zoltan2_Exceptions.hpp>
56 
57 #include <Teuchos_ParameterList.hpp>
58 
59 namespace Zoltan2{
60 
82 template <typename Adapter>
83 void AlgRCB(
84  const RCP<const Environment> &env,
85  RCP<Comm<int> > &problemComm,
86  const RCP<const CoordinateModel<
87  typename Adapter::base_adapter_t> > &coords,
88  RCP<PartitioningSolution<Adapter> > &solution
89 )
90 {
91 #ifndef INCLUDE_ZOLTAN2_EXPERIMENTAL
92 
93  Z2_THROW_EXPERIMENTAL("Zoltan2 RCB is strictly experimental software "
94  "due to performance problems in its use of Tpetra.")
95 
96 #else // INCLUDE_ZOLTAN2_EXPERIMENTAL
97 
98  typedef typename Adapter::node_t node_t;
99  typedef typename Adapter::lno_t lno_t;
100  typedef typename Adapter::gno_t gno_t;
101  typedef typename Adapter::scalar_t scalar_t;
102 
103  // Make a copy of communicator because
104  // we subdivide the communicator during the algorithm.
105 
106  RCP<Comm<int> > comm = problemComm->duplicate();
107 
108  std::bitset<NUM_RCB_PARAMS> params;
109 
110  env->debug(DETAILED_STATUS, string("Entering AlgPartRCB"));
111 
112  const Teuchos::ParameterList &pl = env->getParameters();
113 
114  env->timerStart(BOTH_TIMERS, "RCB set up");
115 
117  // Partitioning problem parameters of interest:
118  // objective
119  // imbalance_tolerance
120 
122  string obj;
123 
124  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("partitioning_objective");
125  if (pe)
126  obj = pe->getValue(&obj);
127 
128  if (!pe){
129  params.set(rcb_balanceWeight);
130  mcnorm = normBalanceTotalMaximum;
131  }
132  else if (obj == string("balance_object_count")){
133  params.set(rcb_balanceCount);
134  }
135  else if (obj == string("multicriteria_minimize_total_weight")){
136  params.set(rcb_minTotalWeight);
137  mcnorm = normMinimizeTotalWeight;
138  }
139  else if (obj == string("multicriteria_minimize_maximum_weight")){
140  params.set(rcb_minMaximumWeight);
141  mcnorm = normMinimizeMaximumWeight;
142  }
143  else if (obj == string("multicriteria_balance_total_maximum")){
144  params.set(rcb_balanceTotalMaximum);
145  mcnorm = normBalanceTotalMaximum;
146  }
147  else{
148  params.set(rcb_balanceWeight);
149  mcnorm = normBalanceTotalMaximum;
150  }
151 
152  scalar_t imbalanceTolerance = .1;
153  pe = pl.getEntryPtr("imbalance_tolerance");
154  if (pe){
155  double tol;
156  tol = pe->getValue(&tol);
157  imbalanceTolerance = tol - 1.0;
158  }
159 
160  if (imbalanceTolerance <= 0)
161  imbalanceTolerance = 10e-4; // TODO - what's a good choice
162 
164  // Geometric partitioning problem parameters of interest:
165  // average_cuts
166  // rectilinear_blocks
167  // bisection_num_test_cuts (experimental)
168 
169  int val = 0;
170  pe = pl.getEntryPtr("average_cuts");
171  if (pe)
172  val = pe->getValue(&val);
173 
174  if (val == 1)
175  params.set(rcb_averageCuts);
176 
177  val = 0;
178  pe = pl.getEntryPtr("rectilinear_blocks");
179  if (pe)
180  val = pe->getValue(&val);
181 
182  if (val == 1)
183  params.set(rcb_rectilinearBlocks);
184 
185  int numTestCuts = 1;
186  pe = pl.getEntryPtr("bisection_num_test_cuts");
187  if (pe)
188  numTestCuts = pe->getValue(&numTestCuts);
189 
191  // From the CoordinateModel we need:
192  // coordinate values
193  // coordinate weights, if any
194  // coordinate global Ids
195 
197 
198  int coordDim = coords->getCoordinateDim();
199  int weightDim = coords->getCoordinateWeightDim();
200  size_t numLocalCoords = coords->getLocalNumCoordinates();
201  global_size_t numGlobalCoords = coords->getGlobalNumCoordinates();
202 
203  ArrayView<const gno_t> gnos;
204  ArrayView<input_t> xyz;
205  ArrayView<input_t> wgts;
206 
207  coords->getCoordinates(gnos, xyz, wgts);
208 
209  Array<ArrayRCP<const scalar_t> > values(coordDim);
210  for (int dim=0; dim < coordDim; dim++){
211  ArrayRCP<const scalar_t> ar;
212  xyz[dim].getInputArray(ar);
213  values[dim] = ar;
214  }
215 
216  int criteriaDim = (weightDim ? weightDim : 1);
217  bool ignoreWeights = params.test(rcb_balanceCount);
218 
219  if (criteriaDim > 1 && ignoreWeights)
220  criteriaDim = 1;
221 
222  ArrayRCP<bool> uniformWeights(new bool [criteriaDim], 0, criteriaDim, true);
223  Array<ArrayRCP<const scalar_t> > weights(criteriaDim);
224 
225  if (weightDim == 0 || ignoreWeights)
226  uniformWeights[0] = true;
227  else{
228  for (int wdim = 0; wdim < weightDim; wdim++){
229  if (wgts[wdim].size() == 0){
230  uniformWeights[wdim] = true;
231  }
232  else{
233  uniformWeights[wdim] = false;
234  ArrayRCP<const scalar_t> ar;
235  wgts[wdim].getInputArray(ar);
236  weights[wdim] = ar;
237  }
238  }
239  }
240 
241  if (env->doStatus() && (numGlobalCoords < 500)){
242  ostringstream oss;
243  oss << "Problem: ";
244  for (size_t i=0; i < numLocalCoords; i++){
245  oss << gnos[i] << " (";
246  for (int dim=0; dim < coordDim; dim++)
247  oss << (xyz[dim])[i] << " ";
248  oss << ") ";
249  }
250 
251  env->debug(VERBOSE_DETAILED_STATUS, oss.str());
252  }
253 
255  // From the Solution we get part information.
256  // If the part sizes for a given criteria are not uniform,
257  // then they are values that sum to 1.0.
258 
259  size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
260 
261  Array<bool> uniformParts(criteriaDim);
262  Array<ArrayRCP<scalar_t> > partSizes(criteriaDim);
263 
264  for (int wdim = 0; wdim < criteriaDim; wdim++){
265  if (solution->criteriaHasUniformPartSizes(wdim)){
266  uniformParts[wdim] = true;
267  }
268  else{
269  scalar_t *tmp = new scalar_t [numGlobalParts];
270  env->localMemoryAssertion(__FILE__, __LINE__, numGlobalParts, tmp) ;
271 
272  for (size_t i=0; i < numGlobalParts; i++){
273  tmp[i] = solution->getCriteriaPartSize(wdim, i);
274  }
275 
276  partSizes[wdim] = arcp(tmp, 0, numGlobalParts);
277  }
278  }
279 
280  // It may not be possible to solve the partitioning problem
281  // if we have multiple weight dimensions with part size
282  // arrays that differ. So let's be aware of this possibility.
283 
284  bool multiplePartSizeSpecs = false;
285 
286  if (criteriaDim > 1){
287  for (int wdim1 = 0; wdim1 < criteriaDim; wdim1++)
288  for (int wdim2 = wdim1+1; wdim2 < criteriaDim; wdim2++)
289  if (!solution->criteriaHaveSamePartSizes(wdim1, wdim2)){
290  multiplePartSizeSpecs = true;
291  break;
292  }
293  }
294 
295  if (multiplePartSizeSpecs)
296  params.set(rcb_multiplePartSizeSpecs);
297 
299  // Create the distributed data for the algorithm.
300  //
301  // It is a multivector containing one vector for each coordinate
302  // dimension, plus a vector for each weight dimension that is not
303  // uniform.
304 
305  typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
306  typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> mvector_t;
307 
308  int multiVectorDim = coordDim;
309  for (int wdim = 0; wdim < criteriaDim; wdim++)
310  if (!uniformWeights[wdim]) multiVectorDim++;
311 
312  gno_t gnoMin, gnoMax;
313  coords->getIdentifierMap()->getGnoRange(gnoMin, gnoMax);
314 
315  RCP<map_t> map;
316  try{
317  map = rcp(new map_t(numGlobalCoords, gnos, gnoMin, comm));
318  }
320 
321  typedef ArrayView<const scalar_t> coordList_t;
322 
323  coordList_t *avList = new coordList_t [multiVectorDim];
324 
325  for (int dim=0; dim < coordDim; dim++)
326  avList[dim] = values[dim].view(0, numLocalCoords);
327 
328  for (int wdim=0, idx=coordDim; wdim < criteriaDim; wdim++)
329  if (!uniformWeights[wdim])
330  avList[idx++] = weights[wdim].view(0, numLocalCoords);
331 
332  ArrayRCP<const ArrayView<const scalar_t> > vectors =
333  arcp(avList, 0, multiVectorDim);
334 
335  RCP<mvector_t> mvector;
336 
337  try{
338  mvector = rcp(new mvector_t(
339  map, vectors.view(0, multiVectorDim), multiVectorDim));
340  }
342 
343  env->timerStop(BOTH_TIMERS, "RCB set up");
344 
347  // The algorithm
350 
351  partId_t part0 = 0;
352  partId_t part1 = numGlobalParts-1;
353  int sanityCheck = numGlobalParts;
354  int groupSize = comm->getSize();
355  int rank = comm->getRank();
356 
357  long imbalanceReductionFactor(1);
358  long nparts = numGlobalParts;
359  while ((nparts >>= 1) != 0) imbalanceReductionFactor++;
360 
361  imbalanceTolerance /= imbalanceReductionFactor;
362 
363  int iteration = 1;
364 
365  env->memory("RCB algorithm set up");
366 
367  env->timerStart(MACRO_TIMERS, "Parallel RCB");
368 
369  while (part1>part0 && groupSize>1 && numGlobalCoords>0 && sanityCheck--){
370 
372  // Which coordinates are left and which are right?
373 
374  Array<unsigned char> lrflags(numLocalCoords);
375  scalar_t cutValue=0; // TODO eventually save this for user
376  int cutDimension=0;
377  scalar_t imbalance=0, weightLeft=0, weightRight=0;
378  partId_t leftHalfNumParts=0;
379 
380  env->timerStart(MICRO_TIMERS, "Find cut", iteration, 2);
381 
382  try{
383  determineCut<mvector_t, Adapter>(env, comm,
384  params, numTestCuts, imbalanceTolerance,
385  coordDim, mvector,
386  uniformWeights.view(0,criteriaDim), mcnorm, solution,
387  part0, part1,
388  lrflags.view(0, numLocalCoords),
389  cutDimension, cutValue, imbalance, leftHalfNumParts,
390  weightLeft, weightRight);
391  }
393 
394  env->timerStop(MICRO_TIMERS, "Find cut", iteration, 2);
395 
396  // Do we have empty left or right halves?
397 
398  bool skipLeft = (weightLeft == 0);
399  bool skipRight = (weightRight == 0);
400 
402  // Migrate the multivector of data.
403 
404  int leftHalfNumProcs=0;
405 
406  env->timerStart(MICRO_TIMERS, "Migrate", iteration, 2);
407 
408  try{ // on return mvector has my new data
409 
410  migrateData<mvector_t>( env, comm, lrflags.view(0,numLocalCoords),
411  mvector, leftHalfNumProcs);
412  }
414 
415  env->timerStop(MICRO_TIMERS, "Migrate", iteration, 2);
416 
417  env->localBugAssertion(__FILE__, __LINE__, "num procs in half",
418  leftHalfNumProcs > 0 && leftHalfNumProcs < groupSize,
420 
421  bool inLeftHalf = (rank < leftHalfNumProcs);
422 
423  if ((inLeftHalf && skipLeft) || (!inLeftHalf && skipRight)){
424  groupSize = 1;
425  numLocalCoords = 0;
426  continue;
427  }
428 
430  // Divide into two subgroups.
431 
432  env->timerStart(MICRO_TIMERS, "Create sub group, sub data", iteration, 2);
433 
434  int *ids = NULL;
435 
436  if (rank < leftHalfNumProcs){
437  groupSize = leftHalfNumProcs;
438  ids = new int [groupSize];
439  env->localMemoryAssertion(__FILE__, __LINE__, groupSize, ids);
440  for (int i=0; i < groupSize; i++)
441  ids[i] = i;
442  part1 = part0 + leftHalfNumParts - 1;
443  }
444  else {
445  groupSize = comm->getSize() - leftHalfNumProcs;
446  rank -= leftHalfNumProcs;
447  ids = new int [groupSize];
448  env->localMemoryAssertion(__FILE__, __LINE__, groupSize, ids);
449  for (int i=0; i < groupSize; i++)
450  ids[i] = i + leftHalfNumProcs;
451  part0 += leftHalfNumParts;
452  }
453 
454  ArrayView<const int> idView(ids, groupSize);
455  comm = comm->createSubcommunicator(idView);
456 
457  delete [] ids;
458 
460  // Create a new multivector for my smaller group.
461 
462  ArrayView<const gno_t> gnoList = mvector->getMap()->getNodeElementList();
463  size_t localSize = mvector->getLocalLength();
464 
465  // Tpetra will calculate the globalSize.
466  size_t globalSize = Teuchos::OrdinalTraits<size_t>::invalid();
467 
468  RCP<map_t> subMap;
469  try{
470  subMap= rcp(new map_t(globalSize, gnoList, 0, comm));
471  }
473 
474  coordList_t *avSubList = new coordList_t [multiVectorDim];
475 
476  for (int dim=0; dim < multiVectorDim; dim++)
477  avSubList[dim] = mvector->getData(dim).view(0, localSize);
478 
479  ArrayRCP<const ArrayView<const scalar_t> > subVectors =
480  arcp(avSubList, 0, multiVectorDim);
481 
482  RCP<mvector_t> subMvector;
483 
484  try{
485  subMvector = rcp(new mvector_t(
486  subMap, subVectors.view(0, multiVectorDim), multiVectorDim));
487  }
489 
490  env->timerStop(MICRO_TIMERS, "Create sub group, sub data", iteration, 2);
491 
492  mvector = subMvector;
493 
494  numLocalCoords = mvector->getLocalLength();
495  numGlobalCoords = mvector->getGlobalLength();
496 
497  iteration++;
498 
499  env->memory("New subgroup data created");
500  }
501 
502  env->timerStop(MACRO_TIMERS, "Parallel RCB");
503 
504  env->localBugAssertion(__FILE__, __LINE__, "partitioning failure",
505  sanityCheck, BASIC_ASSERTION);
506 
507  ArrayRCP<partId_t> partId;
508 
509  if (numLocalCoords > 0){
510  partId_t *tmp = new partId_t [numLocalCoords];
511  env->localMemoryAssertion(__FILE__, __LINE__, numLocalCoords, tmp);
512  partId = arcp(tmp, 0, numLocalCoords, true);
513  }
514 
515  env->memory("Solution array created");
516 
517  if ((part1 > part0) && (numLocalCoords > 0)){ // Serial partitioning
518 
519  // scalar_t cutValue; TODO
520  // int cutDimension;
521  // scalar_t imbalance;
522 
523  env->timerStart(MACRO_TIMERS, "Serial RCB");
524 
525  try{
526  ArrayView<lno_t> emptyIndex;
527 
528  serialRCB<mvector_t, Adapter>(env, 1, params,
529  numTestCuts, imbalanceTolerance,
530  coordDim, mvector, emptyIndex,
531  uniformWeights.view(0,criteriaDim), solution,
532  part0, part1, partId.view(0,numLocalCoords));
533  }
535 
536  env->timerStop(MACRO_TIMERS, "Serial RCB");
537 
538  }
539  else{
540  for (lno_t i=0; i < partId.size(); i++)
541  partId[i] = part0;
542  }
543 
545  // Done: update the solution
546 
547  ArrayRCP<const gno_t> gnoList =
548  arcpFromArrayView(mvector->getMap()->getNodeElementList());
549 
550  if (env->getDebugLevel() >= VERBOSE_DETAILED_STATUS &&
551  (numGlobalCoords < 500)){
552  ostringstream oss;
553  oss << "Solution: ";
554  for (gno_t i=0; i < gnoList.size(); i++)
555  oss << gnoList[i] << " (" << partId[i] << ") ";
556 
557  env->debug(VERBOSE_DETAILED_STATUS, oss.str());
558  }
559 
560  solution->setParts(gnoList, partId, false);
561 #endif // INCLUDE_ZOLTAN2_EXPERIMENTAL
562 }
563 
564 } // namespace Zoltan2
565 
566 #endif
Defines exception handling macros.
Time an algorithm (or other entity) as a whole.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
size_t global_size_t
zoltan2_partId_t partId_t
Time the substeps of an entity.
sub-steps, each method&#39;s entry and exit
void AlgRCB(const RCP< const Environment > &env, RCP< Comm< int > > &problemComm, const RCP< const CoordinateModel< typename Adapter::base_adapter_t > > &coords, RCP< PartitioningSolution< Adapter > > &solution)
Recursive coordinate bisection algorithm.
Run both MACRO and MICRO timers.
This class provides geometric coordinates with optional weights to the Zoltan2 algorithm.
A PartitioningSolution is a solution to a partitioning problem.
The StridedData class manages lists of weights or coordinates.
double scalar_t
#define Z2_THROW_EXPERIMENTAL(mystr)
Throw an error when experimental code is requested but not compiled.
Defines the CoordinateModel classes.
multiCriteriaNorm
Enumerator used in code for multicriteria norm choice.
#define Z2_THROW_OUTSIDE_ERROR(env)
Throw an error returned from outside the Zoltan2 library.
include more detail about sub-steps
Methods written for the RCB algorithm.
Kokkos::DefaultNode::DefaultNodeType node_t