Zoltan2  Version of the Day
Zoltan2_AlgRCB_methods.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_METHODS_HPP_
51 #define _ZOLTAN2_ALGRCB_METHODS_HPP_
52 
54 #include <Zoltan2_XpetraTraits.hpp>
55 #include <Zoltan2_Metric.hpp>
56 
57 #include <Tpetra_Vector.hpp>
58 
59 #include <sstream>
60 #include <string>
61 #include <cmath>
62 #include <bitset>
63 
64 namespace Teuchos{
65 
72 template <typename Ordinal, typename T>
73  class Zoltan2_RCBOperation : public ValueTypeReductionOp<Ordinal,T>
74 {
75 private:
76  Ordinal numSum_, numMin_, numMax_;
77 
78 public:
81  Zoltan2_RCBOperation():numSum_(0), numMin_(0), numMax_(0){}
82 
89  Zoltan2_RCBOperation(Ordinal nsum, Ordinal nmin, Ordinal nmax):
90  numSum_(nsum), numMin_(nmin), numMax_(nmax){}
91 
94  void reduce( const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
95  {
96  Ordinal next=0;
97  for (Ordinal i=0; i < numSum_; i++, next++)
98  inoutBuffer[next] += inBuffer[next];
99 
100  for (Ordinal i=0; i < numMin_; i++, next++)
101  if (inoutBuffer[next] > inBuffer[next])
102  inoutBuffer[next] = inBuffer[next];
103 
104  for (Ordinal i=0; i < numMax_; i++, next++)
105  if (inoutBuffer[next] < inBuffer[next])
106  inoutBuffer[next] = inBuffer[next];
107  }
108 
111  ArrayRCP<T> getInitializedBuffer(T minExtreme, T maxExtreme) const
112  {
113  Ordinal len = numSum_+numMin_+numMax_;
114  T *buf = new T [len];
115  if (!buf)
116  throw std::bad_alloc();
117 
118  Ordinal next=0;
119  for (Ordinal i=0; i < numSum_; i++)
120  buf[next++] = 0;
121  for (Ordinal i=0; i < numMin_; i++)
122  buf[next++] = minExtreme;
123  for (Ordinal i=0; i < numMax_; i++)
124  buf[next++] = maxExtreme;
125 
126  ArrayRCP<T> returnBuf = arcp(buf, 0, len, true);
127  return returnBuf;
128  }
129 };
130 } // namespace Teuchos
131 
132 namespace Zoltan2{
133 
136 enum rcbParams {
146 };
147 
156  unsetFlag = 0xfd,
157  leftFlag = 0xfe,
158  rightFlag = 0xff
159 };
160 
161 // TODO: RCB has several helper methods.
162 // Do we want a convention for naming algorithm methods? Do
163 // we want sub-namespaces for algorithms?
164 
178 template <typename Adapter>
180  const RCP<const Environment> &env,
181  partId_t part0,
182  partId_t part1,
183  const RCP<PartitioningSolution<Adapter> > &solution,
184  ArrayRCP<double> &fractionLeft,
185  partId_t &numPartsLeftHalf)
186 {
187  env->debug(DETAILED_STATUS, string("Entering getFractionLeft"));
188  partId_t numParts = part1 - part0 + 1;
189  // TODO In LDRD, substitute call to machine model for
190  // TODO computation of numPartsLeftHalf below.
191  numPartsLeftHalf = numParts / 2;
192  partId_t left0 = part0; // First part in left half
193  partId_t left1 = left0 + numPartsLeftHalf - 1; // Last part in left half
194  partId_t right0 = left1 + 1; // First part in right half
195  partId_t right1 = part1; // Last part in right half
196 
197  int weightDim = solution->getNumberOfCriteria();
198  fractionLeft = arcp(new double [weightDim], 0, weightDim);
199 
200  for (int wdim=0; wdim<weightDim; wdim++){
201  if (solution->criteriaHasUniformPartSizes(wdim)){
202  fractionLeft[wdim] = double(numPartsLeftHalf) / double(numParts);
203  }
204  else{
205  fractionLeft[wdim] = 0;
206  for(int partId=left0; partId <= left1; partId++){
207  fractionLeft[wdim] += solution->getCriteriaPartSize(wdim, partId);
208  }
209  double total = fractionLeft[wdim];
210  for(int partId=right0; partId <= right1; partId++){
211  total += solution->getCriteriaPartSize(wdim, partId);
212  }
213  fractionLeft[wdim] /= total;
214  }
215  }
216  env->debug(DETAILED_STATUS, string("Exiting getFractionLeft"));
217 }
218 
237 template <typename mvector_t>
239  const RCP<const Environment> &env,
240  const RCP<Comm<int> > &comm,
241  int coordDim,
242  const RCP<mvector_t> &vectors,
243  ArrayView<typename mvector_t::local_ordinal_type> index,
244  int &dimension, // output
245  typename mvector_t::scalar_type &minCoord, // output
246  typename mvector_t::scalar_type &maxCoord) // output
247 {
248  env->debug(DETAILED_STATUS, string("Entering getCutDimension"));
249  typedef typename mvector_t::scalar_type scalar_t;
250  typedef typename mvector_t::local_ordinal_type lno_t;
251 
252  int nprocs = comm->getSize();
253  bool useIndices = index.size() > 0;
254  lno_t numLocalCoords = 0;
255 
256  if (useIndices)
257  numLocalCoords = index.size();
258  else
259  numLocalCoords = vectors->getLocalLength();
260 
261  Array<scalar_t> spans(coordDim*2);
262  int next = 0;
263 
264  if (useIndices){
265  if (numLocalCoords > 0){
266  for (int dim=0; dim < coordDim; dim++){
267  const scalar_t *val = vectors->getData(dim).getRawPtr();
268  scalar_t v = val[index[0]];
269  scalar_t min = v;
270  scalar_t max = v;
271  for (lno_t i=1; i < numLocalCoords; i++){
272  v = val[index[i]];
273  if (v > max)
274  max = v;
275  else if (v < min)
276  min = v;
277  }
278 
279  spans[next++] = min;
280  spans[next++] = max * -1.0;
281  }
282  }
283  else{
284  for (int dim=0; dim < coordDim; dim++){
285  spans[next++] = numeric_limits<scalar_t>::max();
286  spans[next++] = numeric_limits<scalar_t>::min() * -1.0;
287  }
288  }
289  }
290  else{
291  for (int dim=0; dim < coordDim; dim++){
292  const scalar_t *val = vectors->getData(dim).getRawPtr();
293  pair<scalar_t, scalar_t> minMax =
294  z2LocalMinMax<scalar_t>(val, numLocalCoords);
295  spans[next++] = minMax.first;
296  spans[next++] = minMax.second * -1.0;
297  }
298  }
299 
300  if (nprocs > 1){
301  Array<scalar_t> globalValues(coordDim*2);
302  try{
303  reduceAll<int, scalar_t>(*comm, Teuchos::REDUCE_MIN, coordDim*2,
304  spans.getRawPtr(), globalValues.getRawPtr());
305  }
307 
308  for (int i=0; i < coordDim*2; i++)
309  spans[i] = globalValues[i];
310  }
311 
312  scalar_t maxSpan = 0;
313  dimension = -1;
314  next = 0;
315 
316  for (int dim=0; dim < coordDim; dim++){
317  scalar_t min = spans[next++];
318  scalar_t max = spans[next++] * -1.0;
319  scalar_t newSpan = max - min;
320  if (newSpan > maxSpan){
321  maxSpan = newSpan;
322  dimension = dim;
323  minCoord = min;
324  maxCoord = max;
325  }
326  }
327  env->debug(DETAILED_STATUS, string("Exiting getCutDimension"));
328 }
329 
346 template <typename mvector_t>
348  const RCP<const Environment> &env, const RCP<Comm<int> > &comm,
349  ArrayView<unsigned char> lrflags,
350  RCP<mvector_t> &vectors, // on return is the new data
351  int &leftNumProcs) // on return is num procs with left data
352 {
353  env->debug(DETAILED_STATUS, string("Entering migrateData"));
354  typedef typename mvector_t::scalar_type scalar_t;
355  typedef typename mvector_t::local_ordinal_type lno_t;
356  typedef typename mvector_t::global_ordinal_type gno_t;
357 
358  int nprocs = comm->getSize();
359  size_t nobj = vectors->getLocalLength();
360  size_t nGlobalObj = vectors->getGlobalLength();
361 
362  env->localBugAssertion(__FILE__, __LINE__, "migrateData input",
363  nprocs>1 && size_t(lrflags.size())==nobj, DEBUG_MODE_ASSERTION);
364 
365  gno_t myNumLeft= 0, numLeft;
366  for (size_t i=0; i < nobj; i++)
367  if (lrflags[i] == leftFlag)
368  myNumLeft++;
369 
370  try{
371  reduceAll<int, gno_t>(
372  *comm, Teuchos::REDUCE_SUM, 1, &myNumLeft, &numLeft);
373  }
375 
376  scalar_t leftFraction = scalar_t(numLeft)/scalar_t(nGlobalObj);
377  leftNumProcs = static_cast<int>(floor((nprocs*leftFraction)+.5));
378 
379  if (leftNumProcs == 0)
380  leftNumProcs++;
381  else if (leftNumProcs == nprocs)
382  leftNumProcs--;
383 
385  // Get a list of my new global numbers.
386 
387  Array<int> sendCount(nprocs, 0);
388  Array<int> recvCount(nprocs, 0);
389  Array<gno_t> sendBuf(nobj, 0);
390 
391  if (nobj > 0){
392  int *procId = new int [nobj];
393  env->localMemoryAssertion(__FILE__, __LINE__, nobj, procId) ;
394  int leftProc0 = 0;
395  int rightProc0 = leftProc0 + leftNumProcs;
396 
397  int nextLeftProc = leftProc0;
398  int nextRightProc = rightProc0;
399  int *p = procId;
400 
401  for (size_t i=0; i < nobj; i++){
402  if (lrflags[i] == leftFlag){
403  if (nextLeftProc == rightProc0)
404  nextLeftProc = leftProc0;
405 
406  sendCount[nextLeftProc]++;
407  *p++ = nextLeftProc++;
408  }
409  else{
410  if (nextRightProc == nprocs)
411  nextRightProc = rightProc0;
412  sendCount[nextRightProc]++;
413 
414  *p++ = nextRightProc++;
415  }
416  }
417 
418  gno_t *sendOffset = new gno_t [nprocs];
419  env->localMemoryAssertion(__FILE__, __LINE__, nprocs, sendOffset) ;
420  sendOffset[0] = 0;
421  for (int i=0; i < nprocs-1; i++)
422  sendOffset[i+1] = sendOffset[i] + sendCount[i];
423 
424  ArrayView<const gno_t> gnoList = vectors->getMap()->getNodeElementList();
425 
426  for (size_t i=0; i < nobj; i++){
427  int proc = procId[i];
428  lno_t offset = sendOffset[proc]++;
429 
430  sendBuf[offset] = gnoList[i];
431  }
432 
433  delete [] sendOffset;
434  delete [] procId;
435  }
436 
437  ArrayRCP<gno_t> recvBuf;
438 
439  try{
440  AlltoAllv<gno_t>(*comm, *env,
441  sendBuf(), sendCount(),
442  recvBuf, recvCount());
443  }
445 
446  sendCount.clear();
447  sendBuf.clear();
448 
450  // Migrate the multivector of data.
451 
452  gno_t numMyNewGnos = 0;
453  for (int i=0; i < nprocs; i++)
454  numMyNewGnos += recvCount[i];
455 
456  recvCount.clear();
457 
458  RCP<const mvector_t> newMultiVector;
459  RCP<const mvector_t> constInput = rcp_const_cast<const mvector_t>(vectors);
460 
461  try{
462  newMultiVector = XpetraTraits<mvector_t>::doMigration(
463  constInput, numMyNewGnos, recvBuf.getRawPtr());
464  }
466 
467  vectors = rcp_const_cast<mvector_t>(newMultiVector);
468  env->memory("Former problem data replaced with new data");
469  env->debug(DETAILED_STATUS, string("Exiting migrateData"));
470 }
471 
472 template <typename lno_t, typename scalar_t>
474  multiCriteriaNorm mcnorm,
475  ArrayView<StridedData<lno_t, scalar_t> > weights)
476 {
477  scalar_t wgtValue = 1.0;
478  size_t weightDim = weights.size();
479 
480  if (weightDim > 1){
481  Array<scalar_t> coordWeights(weightDim, 1.0);
482  for (size_t wdim=0; wdim < weightDim; wdim++){
483  if (weights[wdim].size() > 0)
484  coordWeights[wdim] = weights[wdim][id];
485  }
486 
487  wgtValue = normedWeight<scalar_t>(coordWeights.view(0,weightDim), mcnorm);
488  }
489  else if (weights[0].size() > 0){
490  wgtValue = weights[0][id];
491  }
492 
493  return wgtValue;
494 }
495 
513 template <typename scalar_t>
514  bool emptyPartsCheck( const RCP<const Environment> &env,
515  const ArrayView<double> fractionLeft,
516  scalar_t minCoord, scalar_t maxCoord,
517  leftRightFlag &lrf,
518  scalar_t &cutValue)
519 {
520  env->debug(DETAILED_STATUS, string("Entering emptyPartsCheck"));
521  // initialize return values
522  lrf = leftFlag;
523  cutValue = 0.0;
524 
525  size_t weightDim = fractionLeft.size();
526  int numEmptyRight = 0, numEmptyLeft = 0;
527 
528  for (size_t i=0; i < weightDim; i++){
529  if (fractionLeft[i] == 0.0)
530  numEmptyLeft++;
531  else if (fractionLeft[i] == 1.0)
532  numEmptyRight++;
533  }
534 
535  if (numEmptyRight + numEmptyLeft == 0)
536  return false;
537 
538  env->localInputAssertion(__FILE__, __LINE__,
539  "partitioning not solvable - conflicting part size criteria",
540  (numEmptyRight==0) || (numEmptyLeft==0), BASIC_ASSERTION);
541 
542  if (numEmptyLeft > 0){
543  lrf = leftFlag;
544  cutValue = minCoord;
545  }
546  else{
547  lrf = rightFlag;
548  cutValue = maxCoord;
549  }
550 
551  env->debug(DETAILED_STATUS, string("Exiting emptyPartsCheck"));
552  return true;
553 }
554 
567 template <typename lno_t, typename gno_t, typename scalar_t>
569  const RCP<const Environment> &env,
570  const RCP<Comm<int> > &comm,
571  scalar_t totalWeightLeft,
572  scalar_t targetLeftScalar,
573  int cutLocation,
574  ArrayView<scalar_t> localSums,
575  ArrayView<scalar_t> globalSums,
576  ArrayView<lno_t> index,
577  ArrayView<StridedData<lno_t, scalar_t> > weights,
578  multiCriteriaNorm mcnorm,
579  ArrayView<unsigned char> lrFlags,
580  scalar_t &globalWeightMovedRight) // output
581 {
582  env->debug(DETAILED_STATUS, string("Entering testCoordinatesOnRightBoundary"));
583  int nprocs = comm->getSize();
584  int rank = comm->getRank();
585 
586  globalWeightMovedRight = 0.0;
587 
588  scalar_t localBoundarySum = localSums[cutLocation];
589 
590  scalar_t total = totalWeightLeft;
591  for (int i=0; i <= cutLocation; i++)
592  total += globalSums[i];
593 
594  scalar_t totalMoveRight = total - targetLeftScalar;
595  scalar_t localMoveRight = localBoundarySum;
596  scalar_t actualWeightMovedRight = 0.0;
597 
598  Array<scalar_t> scansum(nprocs+1, 0.0);
599  Teuchos::gatherAll<int, scalar_t>(*comm, 1,
600  &localBoundarySum, nprocs, scansum.getRawPtr()+1);
601  for (int i=2; i<=nprocs; i++)
602  scansum[i] += scansum[i-1];
603 
604  if (localBoundarySum > 0.0){
605 
606  scalar_t sumMine = scansum[rank]; // sum of ranks preceding me
607  scalar_t diff = scansum[nprocs] - sumMine;
608  localMoveRight = 0;
609 
610  if (diff <= totalMoveRight)
611  localMoveRight = localBoundarySum; // all
612  else{
613  scalar_t leftPart = diff - totalMoveRight;
614  if (leftPart < localBoundarySum)
615  localMoveRight = localBoundarySum - leftPart;
616  }
617  }
618 
619  if (localMoveRight > 0.0){
620 
621  bool moveAll = (localMoveRight >= localBoundarySum);
622 
623  if (moveAll){
624  actualWeightMovedRight = localBoundarySum;
625  for (lno_t i=0; i < lrFlags.size(); i++){
626  if (lrFlags[i] == cutLocation){
627  lrFlags[i] = rightFlag;
628  }
629  }
630  }
631  else{
632  int weightDim = weights.size();
633  int useIndices = index.size() > 0;
634 
635  for (lno_t i=0; i < lrFlags.size(); i++){
636  if (lrFlags[i] == cutLocation){
637  lrFlags[i] = rightFlag;
638 
639  lno_t idx = (useIndices ? index[i] : i);
640 
641  actualWeightMovedRight += getCoordWeight<lno_t, scalar_t>(
642  idx, mcnorm, weights.view(0, weightDim));
643 
644  if (actualWeightMovedRight >= localMoveRight)
645  break;
646  }
647  } // next coordinate
648  }
649  }
650 
651  try{
652  reduceAll<int, scalar_t>(
653  *comm, Teuchos::REDUCE_SUM, 1, &actualWeightMovedRight,
654  &globalWeightMovedRight);
655  }
657 
658  env->debug(DETAILED_STATUS, string("Exiting testCoordinatesOnRightBoundary"));
659  return;
660 }
661 
709 template <typename mvector_t>
711  const RCP<const Environment> &env,
712  const RCP<Comm<int> > &comm,
713  const std::bitset<NUM_RCB_PARAMS> &params,
714  int numTestCuts,
715  typename mvector_t::scalar_type tolerance,
716  int cutDim,
717  int coordDim,
718  const RCP<mvector_t> &vectors,
719  ArrayView<typename mvector_t::local_ordinal_type> index,
720  ArrayView<double> fractionLeft,
721  ArrayView<bool> uniformWeights,
722  typename mvector_t::scalar_type coordGlobalMin,
723  typename mvector_t::scalar_type coordGlobalMax,
724  typename mvector_t::scalar_type &cutValue, // output
725  ArrayView<unsigned char> lrFlags, // output
726  typename mvector_t::scalar_type &totalWeightLeft, // output
727  typename mvector_t::scalar_type &totalWeightRight, // output
728  typename mvector_t::local_ordinal_type &localCountLeft, // output
729  typename mvector_t::scalar_type &imbalance) // output
730 {
731  env->debug(DETAILED_STATUS, string("Entering BSPfindCut"));
732 
733  // initialize output
734  bool useIndices = index.size() > 0;
735  int numAllCoords = vectors->getLocalLength();
736  int numCoords = (useIndices ? index.size() : numAllCoords);
737  cutValue = totalWeightLeft = totalWeightRight = imbalance = 0.0;
738  localCountLeft = 0;
739 
740  for (int i=0; i < numCoords; i++)
741  lrFlags[i] = unsetFlag;
742 
743  typedef typename mvector_t::scalar_type scalar_t;
744  typedef typename mvector_t::local_ordinal_type lno_t;
745  typedef typename mvector_t::global_ordinal_type gno_t;
747 
748  bool multiplePartSizeSpecs = params.test(rcb_multiplePartSizeSpecs);
749  bool rectilinearBlocks = params.test(rcb_rectilinearBlocks);
750  bool averageCuts = params.test(rcb_averageCuts);
751 
752  // A coordinate is considered to be on a cut if it is within
753  // this distance of the cut.
754 
755  double epsilon = (coordGlobalMax - coordGlobalMin) * 10e-9;
756 
757  // Find the coordinate values and weights.
758 
759  int weightDim = uniformWeights.size(); // at least one
760  int numNonUniformWeights = 0;
761  for (int i=0; i < weightDim; i++){
762  if (!uniformWeights[i])
763  numNonUniformWeights++;
764  }
765 
766  if (env->getDebugLevel() >= DETAILED_STATUS){
767  ostringstream info;
768  info << "Weight dim " << weightDim << ", Fraction left:";
769  for (int i=0; i < weightDim; i++)
770  info << " " << fractionLeft[i];
771  info << endl << "Dimension " << cutDim << " [";
772  info << coordGlobalMin << ", " << coordGlobalMax << "]";
773  info << endl << "# test cuts " << numTestCuts;
774  info << ", tolerance " << tolerance << endl;
775  env->debug(DETAILED_STATUS, info.str());
776  }
777 
778  const scalar_t *coordValue = vectors->getData(cutDim).getRawPtr();
779 
780  // An empty input_t object implies uniform weights.
781 
782  input_t *wgtinfo = new input_t [weightDim];
783  env->localMemoryAssertion(__FILE__, __LINE__, weightDim, wgtinfo);
784  ArrayRCP<input_t> weight(wgtinfo, 0, weightDim, true);
785 
786  if (numNonUniformWeights > 0){
787  for (int wdim = 0, widx=coordDim; wdim < weightDim; wdim++){
788  if (!uniformWeights[wdim]){
789  weight[wdim] = input_t(vectors->getData(widx++), 1);
790  }
791  }
792  }
793 
794  // Multicriteria norm
795 
797 
798  if (params.test(rcb_minMaximumWeight))
799  mcnorm = normMinimizeMaximumWeight;
800  else if (params.test(rcb_minTotalWeight))
801  mcnorm = normMinimizeTotalWeight;
802 
803  // Goal is globally find one cut that comes close to leaving
804  // partSizeLeft*totalWeight on the left side.
805 
806  Epetra_SerialDenseVector partSizeLeft(
807  View, fractionLeft.getRawPtr(), weightDim);
808 
809  // Where do we make the first test cuts?
810  //
811  // min 1 2 3 max
812  //
813  // Space is [min, max].
814  // If there are three test cuts: 1, 2 and 3.
815  // 4 regions: [min,1] [1,2] [2,3] [3,max].
816  // 5 boundaries: min, 1, 2, 3, and max.
817 
818  int numRegions = numTestCuts + 1;
819  int numBoundaries = numTestCuts + 2;
820  int endBoundary = numBoundaries - 1;
821  vector<scalar_t> boundaries(numBoundaries);
822  vector<scalar_t> searchBoundaries(numBoundaries);
823 
824  int numSums = numBoundaries+numRegions;
825 
827  numSums, // number of sums
828  numRegions, // number of mins
829  numRegions); // number of maxes
830 
831  typename std::vector<scalar_t>::iterator foundCut;
832 
833  bool done=false;
834  bool fail=false;
835  scalar_t min = coordGlobalMin;
836  scalar_t max = coordGlobalMax;
837  lno_t numRemaining = numCoords;
838  lno_t prevNumRemaining = numCoords;
839  size_t numGlobalPoints = vectors->getGlobalLength();
840  size_t sanityCheck = numGlobalPoints;
841 
842  double totalWeight = 0;
843  double targetLeftScalar = 0;
844  double targetLeftNorm = 0;
845  Epetra_SerialDenseVector targetLeftVector(weightDim);
846 
847  while (!done && !fail && sanityCheck--){
848 
849  // Create regions into which coordinates will be placed.
850  scalar_t diff = (max - min) / numRegions;
851  boundaries[0] = min;
852  boundaries[endBoundary] = max;
853  for (int i=0; i < endBoundary; i++){
854  searchBoundaries[i+1] = boundaries[i+1] = boundaries[i] + diff;
855  }
856 
857  // Move ends slightly so we catch points on boundary.
858  searchBoundaries[0] = min - epsilon;
859  searchBoundaries[endBoundary] = max + epsilon;
860 
861  // Save region and boundary sums, and region min and max.
862 
863  ArrayRCP<scalar_t> localSums = reductionOp.getInitializedBuffer(
864  searchBoundaries[endBoundary], searchBoundaries[0]);
865 
866  ArrayRCP<scalar_t> globalSums = reductionOp.getInitializedBuffer(
867  searchBoundaries[endBoundary], searchBoundaries[0]);
868 
869  scalar_t *sums = localSums.getRawPtr();
870  scalar_t *regionMin = sums + numSums;
871  scalar_t *regionMax = regionMin + numRegions;
872 
873  if (numRemaining > 0){
874 
875  // Assign each of my points to a region.
876  // lower_bound() finds the first cut f, such that f >= coordValue[i].
877  // So for now, objects that are on the cut boundary go into the
878  // region on the "left" side.
879 
880  for (lno_t i=0; i < numCoords; i++){
881 
882  if (lrFlags[i] != unsetFlag)
883  continue;
884 
885  int inRegion = 0;
886  int idx = (useIndices ? index[i] : i);
887  scalar_t value = coordValue[idx];
888 
889  if (numRegions > 2){
890 
891  foundCut = std::lower_bound(
892  searchBoundaries.begin(), searchBoundaries.end(), value);
893 
894  env->localBugAssertion(__FILE__, __LINE__, "search cuts",
895  foundCut != searchBoundaries.end(), BASIC_ASSERTION);
896 
897  inRegion = foundCut - searchBoundaries.begin() - 1;
898  }
899  else{
900  if (value <= boundaries[1])
901  inRegion = 0;
902  else
903  inRegion = 1;
904  }
905 
906  int sumIdx = 1 + inRegion*2;
907 
908  if (value >= boundaries[inRegion+1]-epsilon){
909  // "on" right boundary of this region
910  sumIdx++;
911  }
912  else if (inRegion==0 && (value < min + epsilon)){
913  // in region 0 but far left boundary
914  sumIdx--;
915  }
916 
917  lrFlags[i] = (unsigned char)sumIdx;
918 
919  if (value < regionMin[inRegion])
920  regionMin[inRegion] = value;
921  if (value > regionMax[inRegion])
922  regionMax[inRegion] = value;
923 
924  sums[sumIdx] += getCoordWeight<lno_t, scalar_t>(idx,
925  mcnorm, weight.view(0, weightDim));
926 
927  } // next coord
928  }
929 
930  try{
931  reduceAll<int, scalar_t>(*comm, reductionOp,
932  localSums.size(), localSums.getRawPtr(), globalSums.getRawPtr());
933  }
935 
936  sums = globalSums.getRawPtr();
937  regionMin = sums + numSums;
938  regionMax = regionMin + numRegions;
939 
940  if (env->getDebugLevel() >= DETAILED_STATUS){
941  ostringstream info;
942  info << " Region " << min << " - " << max << endl;
943  info << " Remaining to classify: " << numRemaining << endl;
944  info << " Boundaries: ";
945  for (int i=0; i < numBoundaries; i++)
946  info << boundaries[i] << " ";
947  info << endl << " For searching: ";
948  for (int i=0; i < numBoundaries; i++)
949  info << searchBoundaries[i] << " ";
950  info << endl << " Global sums: ";
951  double sumTotal=0;
952  for (int i=0; i < numSums; i++){
953  sumTotal += sums[i];
954  info << sums[i] << " ";
955  }
956  info << " total: " << sumTotal << endl;
957  env->debug(DETAILED_STATUS, info.str());
958  }
959 
960  if (totalWeight == 0){ // first time through only
961 
962  for (int i=0; i < numSums; i++)
963  totalWeight += sums[i];
964 
965  partSizeLeft.Scale(totalWeight);
966  targetLeftVector = partSizeLeft;
967 
968  targetLeftScalar = targetLeftVector[0];
969  targetLeftNorm = targetLeftVector.Norm2();
970  totalWeightLeft = 0;
971  totalWeightRight = 0;
972  }
973 
974  int cutLocation=0;
975  scalar_t testDiff=0, prevTestDiff=0, target=0;
976 
977  if (multiplePartSizeSpecs){
978  // more complex: if we have multiple weight dimensions, the
979  // weights are non-uniform, and the part sizes requested
980  // for each each weight dimension differ, then we may not
981  // be able to reach the imbalance tolerance.
982  //
983  // TODO: discuss how to (or whether to) handle this case.
984  //
985  // For now we look at this imbalance:
986  //
987  // |target - actual|^2 / |target|^2
988 
989  target = targetLeftNorm;
990  Epetra_SerialDenseVector testVec(weightDim);
991  for (int i=0; i < weightDim; i++)
992  testVec[i] = totalWeightLeft;
993  Epetra_SerialDenseVector diffVec = testVec;
994  diffVec.Scale(-1.0);
995  diffVec += targetLeftVector;
996 
997  testDiff = diffVec.Norm2(); // imbalance numerator
998  prevTestDiff = testDiff;
999  cutLocation= -1;
1000 
1001  while (++cutLocation< numSums){
1002 
1003  for (int i=0; i < weightDim; i++)
1004  testVec[i] += sums[cutLocation];
1005 
1006  diffVec = testVec;
1007  diffVec.Scale(-1.0);
1008  diffVec += targetLeftVector;
1009 
1010  testDiff = diffVec.Norm2();
1011 
1012  if (testDiff >= target)
1013  break;
1014 
1015  prevTestDiff = testDiff;
1016  }
1017  }
1018  else{ // the part sizes for each weight dimension are the same
1019 
1020  target = targetLeftScalar;
1021  testDiff = totalWeightLeft;
1022  prevTestDiff = testDiff;
1023  cutLocation = -1;
1024 
1025  while (++cutLocation < numSums){
1026 
1027  testDiff += sums[cutLocation];
1028  if (testDiff >= target)
1029  break;
1030 
1031  prevTestDiff = testDiff;
1032  }
1033  }
1034 
1035  scalar_t diffLeftCut = target - prevTestDiff;
1036  scalar_t diffRightCut = testDiff - target;
1037 
1038  if (diffLeftCut < diffRightCut){
1039  imbalance = diffLeftCut / target;
1040  if (imbalance <= tolerance){
1041  env->debug(DETAILED_STATUS, " Done, tolerance met");
1042  done = true;
1043  cutLocation--;
1044  }
1045  }
1046  else{
1047  imbalance = diffRightCut / target;
1048  if (imbalance <= tolerance){
1049  env->debug(DETAILED_STATUS, " Done, tolerance met");
1050  done = true;
1051  }
1052  }
1053 
1054  bool cutLocIsRegion = (cutLocation % 2 == 1);
1055  bool cutLocIsBoundary = !cutLocIsRegion;
1056 
1057  if (env->getDebugLevel() >= DETAILED_STATUS){
1058  ostringstream info;
1059  info << " Best cut location: " << cutLocation;
1060  if (cutLocIsRegion) info << " just after a region." << endl;
1061  else info << " just after a boundary." << endl;
1062  env->debug(DETAILED_STATUS, info.str());
1063  }
1064 
1065  if (!done && cutLocIsBoundary){
1066 
1067  done = true; // can not divide space any more
1068 
1069  env->debug(DETAILED_STATUS, " Done, cutting at a region boundary");
1070 
1071  if (rectilinearBlocks){
1072  // Can not divide boundary points into two
1073  // different regions to achieve balance.
1074  fail = true;
1075  }
1076  else {
1077  // Maybe by moving some of the points right, we can
1078  // obtain a better balance. If so, the lrFlag for
1079  // the points moved right will be updated here.
1080 
1081  scalar_t globalWeightMovedRight(0);
1082 
1083  testCoordinatesOnRightBoundary<lno_t, gno_t, scalar_t>(
1084  env, comm, totalWeightLeft, targetLeftScalar, cutLocation,
1085  localSums.view(0, numSums), globalSums.view(0, numSums),
1086  index, weight.view(0, weightDim), mcnorm, lrFlags,
1087  globalWeightMovedRight);
1088 
1089  scalar_t newSum = testDiff - globalWeightMovedRight;
1090  globalSums[cutLocation] -= globalWeightMovedRight;
1091 
1092  if (newSum > target)
1093  imbalance = (target - newSum) / target;
1094  else
1095  imbalance = (newSum - target) / target;
1096 
1097  if (imbalance > tolerance)
1098  fail = true;
1099  }
1100  }
1101 
1102  int rightmostLeftNum=0, leftmostRightNum=0;
1103 
1104  if (!done){
1105  // Best cut is following a region. Narrow down the boundaries.
1106 
1107  int regionNumber = (cutLocation - 1) / 2;
1108 
1109  min = regionMin[regionNumber];
1110  max = regionMax[regionNumber];
1111 
1112  rightmostLeftNum = cutLocation - 1;
1113  leftmostRightNum = cutLocation + 1;
1114  }
1115  else{
1116  rightmostLeftNum = cutLocation;
1117  leftmostRightNum = cutLocation + 1;
1118 
1119  if (cutLocIsRegion && averageCuts){
1120  int regionNumber = (cutLocation - 1) / 2;
1121  cutValue = (
1122  boundaries[regionNumber+1] + // boundary to right
1123  regionMax[regionNumber]) // rightmost point in region
1124  / 2.0;
1125  }
1126  }
1127 
1128  for (int i=0; i <= rightmostLeftNum; i++){
1129  totalWeightLeft += globalSums[i];
1130  }
1131 
1132  for (lno_t i=0; i < numCoords; i++){
1133  if (lrFlags[i] != leftFlag && lrFlags[i] != rightFlag){
1134  if (lrFlags[i] <= rightmostLeftNum){
1135  lrFlags[i] = leftFlag;
1136  numRemaining--;
1137  localCountLeft++;
1138  }
1139  else if (lrFlags[i] >= leftmostRightNum){
1140  lrFlags[i] = rightFlag;
1141  numRemaining--;
1142  }
1143  else{
1144  lrFlags[i] = unsetFlag; // still to be determined
1145  }
1146  }
1147  }
1148 
1149  if (env->getDebugLevel() >= VERBOSE_DETAILED_STATUS && numCoords < 100){
1150  // For large numCoords, building this message
1151  // takes an extraordinarily long time.
1152  ostringstream ossLeft;
1153  ostringstream ossRight;
1154  ossLeft << "left: ";
1155  ossRight << "right: ";
1156  for (lno_t i=0; i < numCoords; i++){
1157  if (lrFlags[i] == unsetFlag)
1158  continue;
1159  lno_t idx = (useIndices ? index[i] : i);
1160  scalar_t val = coordValue[idx];
1161  if (lrFlags[i] == leftFlag)
1162  ossLeft << val << " ";
1163  else if (lrFlags[i] == rightFlag)
1164  ossRight << val << " ";
1165  else
1166  env->localBugAssertion(__FILE__, __LINE__,
1167  "left/right flags", false, BASIC_ASSERTION);
1168  }
1169  ostringstream msg;
1170  msg << ossLeft.str() << endl << ossRight.str() << endl;
1171  env->debug(VERBOSE_DETAILED_STATUS, msg.str());
1172  }
1173 
1174  prevNumRemaining = numRemaining;
1175  } // while !done
1176 
1177  totalWeightRight = totalWeight - totalWeightLeft;
1178 
1179  if (fail)
1180  env->debug(BASIC_STATUS, "Warning: tolerance not achieved in sub-step");
1181 
1182  // TODO: If fail the warn that tolerance was not met.
1183 
1184  env->globalInputAssertion(__FILE__, __LINE__, "partitioning not solvable",
1185  done, DEBUG_MODE_ASSERTION, comm);
1186 
1187  env->memory("End of bisection");
1188 
1189  if (env->getDebugLevel() >= DETAILED_STATUS){
1190  ostringstream oss;
1191  oss << "Exiting BSPfindCut, ";
1192  oss << "# iterations: " << numGlobalPoints - sanityCheck;
1193  env->debug(DETAILED_STATUS, oss.str());
1194  }
1195 
1196  return;
1197 }
1198 
1226 template <typename mvector_t, typename Adapter>
1228  const RCP<const Environment> &env,
1229  const RCP<Comm<int> > &comm,
1230  const std::bitset<NUM_RCB_PARAMS> &params,
1231  int numTestCuts,
1232  typename mvector_t::scalar_type tolerance,
1233  int coordDim,
1234  const RCP<mvector_t> &vectors,
1235  const ArrayView<bool> uniformWeights,
1236  multiCriteriaNorm mcnorm,
1237  const RCP<PartitioningSolution<Adapter> > &solution,
1238  partId_t part0,
1239  partId_t part1,
1240  ArrayView<unsigned char> lrflags, // output
1241  int &cutDimension, // output
1242  typename mvector_t::scalar_type &cutValue, // output
1243  typename mvector_t::scalar_type &imbalance, // output
1244  partId_t &numPartsLeftHalf, // output
1245  typename mvector_t::scalar_type &weightLeftHalf, // output
1246  typename mvector_t::scalar_type &weightRightHalf // output
1247  )
1248 {
1249  env->debug(DETAILED_STATUS, string("Entering determineCut"));
1250  typedef typename mvector_t::scalar_type scalar_t;
1251  typedef typename mvector_t::local_ordinal_type lno_t;
1253 
1254  lno_t numLocalCoords = vectors->getLocalLength();
1255 
1256  // initialize return values
1257  cutDimension = 0;
1258  cutValue = imbalance = weightLeftHalf = weightRightHalf = 0.0;
1259  numPartsLeftHalf = 0;
1260 
1262  // Pick a cut direction.
1263 
1264  scalar_t globalMinCoord, globalMaxCoord;
1265  ArrayView<lno_t> emptyIndex;
1266 
1267  getCutDimension<mvector_t>(env, comm, coordDim, vectors, emptyIndex,
1268  cutDimension, globalMinCoord, globalMaxCoord);
1269 
1271  // Compute part sizes for the two parts.
1272 
1273  ArrayRCP<double> fractionLeft;
1274  size_t weightDim = uniformWeights.size();
1275 
1276  getFractionLeft<Adapter>(env, part0, part1, solution,
1277  fractionLeft, numPartsLeftHalf);
1278 
1279  // Special case of empty left or empty right.
1280 
1281  leftRightFlag lrf;
1282 
1283  bool emptyParts = emptyPartsCheck(env,
1284  fractionLeft.view(0, weightDim), // input
1285  globalMinCoord, globalMaxCoord, // input
1286  lrf, cutValue); // output
1287 
1288  if (emptyParts){
1289 
1290  for (lno_t i=0; i < lrflags.size(); i++)
1291  lrflags[i] = lrf;
1292 
1293  imbalance = 0.0; // perfect balance
1294  scalar_t totalWeight = 0.0;
1295  int numNonUniform = 0;
1296 
1297  for (size_t i=0; i < weightDim; i++)
1298  if (!uniformWeights[i])
1299  numNonUniform++;
1300 
1301  int wgt1 = vectors->getNumVectors() - numNonUniform;
1302 
1303  if (weightDim == 1){
1304  if (numNonUniform == 0)
1305  totalWeight = numLocalCoords;
1306  else{
1307  const scalar_t *val = vectors->getData(wgt1).getRawPtr();
1308  for (lno_t i=0; i < numLocalCoords; i++)
1309  totalWeight += val[i];
1310  }
1311  }
1312  else{ // need to add up total normed weight
1313  Array<input_t> wgts(weightDim);
1314  for (size_t i=0; i < weightDim; i++){
1315  if (!uniformWeights[i]){
1316  wgts[i] = input_t(vectors->getData(wgt1++), 1);
1317  }
1318  }
1319 
1320  partId_t numParts, numNonemptyParts;
1321  ArrayRCP<MetricValues<scalar_t> > metrics;
1322  ArrayRCP<scalar_t> weightSums;
1323 
1324  globalSumsByPart<scalar_t, unsigned char, lno_t>(
1325  env, comm, lrflags,
1326  wgts.view(0, weightDim), mcnorm,
1327  numParts, numNonemptyParts, metrics, weightSums);
1328 
1329  totalWeight = weightSums[1]; // sum normed weight
1330  }
1331 
1332  if (lrf == leftFlag){
1333  numPartsLeftHalf = part1 - part0 + 1;
1334  weightLeftHalf = totalWeight;
1335  weightRightHalf = 0;
1336  }
1337  else{
1338  numPartsLeftHalf = 0;
1339  weightRightHalf = totalWeight;
1340  weightLeftHalf = 0;
1341  }
1342 
1343  env->debug(DETAILED_STATUS, string("Exiting determineCut"));
1344  return;
1345  }
1346 
1348  // Divide the coordinates into balanced left and right
1349  // halves.
1350 
1351  ArrayView<lno_t> emptyIndices;
1352  lno_t localCountLeft;
1353 
1354  try{
1355  BSPfindCut<mvector_t>( env, comm,
1356  params, numTestCuts, tolerance,
1357  cutDimension, coordDim, vectors, emptyIndices,
1358  fractionLeft.view(0, weightDim), uniformWeights.view(0, weightDim),
1359  globalMinCoord, globalMaxCoord,
1360  cutValue, lrflags.view(0, numLocalCoords),
1361  weightLeftHalf, weightRightHalf, localCountLeft, imbalance);
1362  }
1364  env->debug(DETAILED_STATUS, string("Exiting determineCut"));
1365 }
1366 
1367 
1391 template <typename mvector_t, typename Adapter>
1393  const RCP<const Environment> &env,
1394  int depth,
1395  const std::bitset<NUM_RCB_PARAMS> &params,
1396  int numTestCuts,
1397  typename mvector_t::scalar_type tolerance,
1398  int coordDim,
1399  const RCP<mvector_t> &vectors,
1400  ArrayView<typename mvector_t::local_ordinal_type> index,
1401  const ArrayView<bool> uniformWeights,
1402  const RCP<PartitioningSolution<Adapter> > &solution,
1403  partId_t part0,
1404  partId_t part1,
1405  ArrayView<partId_t> partNum) // output
1406 {
1407  env->debug(DETAILED_STATUS, string("Entering serialRCB"));
1408  env->timerStart(MICRO_TIMERS, "serialRCB", depth, 2);
1409 
1410  typedef typename mvector_t::scalar_type scalar_t;
1411  typedef typename mvector_t::local_ordinal_type lno_t;
1412 
1413  RCP<Comm<int> > comm(new Teuchos::SerialComm<int>);
1414 
1415  lno_t numLocalCoords=0;
1416  bool useIndices;
1417  bool firstCall=false;
1418 
1419  if (index.size() == 0){
1420  // First time through there are no indices.
1421  useIndices = false;
1422  numLocalCoords = vectors->getLocalLength();
1423  firstCall = true;
1424  }
1425  else{
1426  useIndices = true;
1427  numLocalCoords = index.size();
1428  }
1429 
1430  if (env->getDebugLevel() >= DETAILED_STATUS){
1431  ostringstream info;
1432  info << " Number of coordinates: " << numLocalCoords << endl;
1433  info << " Use index: " << useIndices << endl;
1434  env->debug(DETAILED_STATUS, info.str());
1435  }
1436 
1438  // Are we done?
1439 
1440  if (part1 == part0){
1441  if (useIndices)
1442  for (lno_t i=0; i < numLocalCoords; i++)
1443  partNum[index[i]] = part0;
1444  else
1445  for (lno_t i=0; i < numLocalCoords; i++)
1446  partNum[i] = part0;
1447 
1448  env->memory("serial RCB end");
1449  env->timerStop(MICRO_TIMERS, "serialRCB", depth, 2);
1450  env->debug(DETAILED_STATUS, string("Exiting serialRCB"));
1451 
1452  return;
1453  }
1454 
1456  // Pick a cut direction
1457 
1458  int cutDimension;
1459  scalar_t minCoord, maxCoord;
1460 
1461  try{
1462  getCutDimension(env, comm, coordDim, vectors, index,
1463  cutDimension, minCoord, maxCoord);
1464  }
1466 
1468  // Compute relative sizes of the two halves.
1469 
1470  ArrayRCP<double> fractionLeft;
1471  partId_t numPartsLeftHalf;
1472 
1473  try{
1474  getFractionLeft<Adapter>(env, part0, part1, solution,
1475  fractionLeft, numPartsLeftHalf);
1476  }
1478 
1479  // Check for special case of empty left or empty right.
1480 
1481  int weightDim = uniformWeights.size();
1482  scalar_t imbalance, cutValue; //unused for now
1483  leftRightFlag lrf;
1484 
1485  bool emptyPart = emptyPartsCheck(env, fractionLeft.view(0, weightDim),
1486  minCoord, maxCoord, lrf, cutValue);
1487 
1488  if (emptyPart){ // continue only on the side that is not empty
1489 
1490  if (lrf == rightFlag) // right is empty
1491  part1 = part0 + numPartsLeftHalf - 1;
1492  else
1493  part0 = part0 + numPartsLeftHalf;
1494 
1495  if (part0 == part1){
1496  if (useIndices){
1497  for (lno_t i=0; i < numLocalCoords; i++){
1498  partNum[index[i]] = part0;
1499  }
1500  }
1501  else{
1502  for (lno_t i=0; i < numLocalCoords; i++){
1503  partNum[i] = part0;
1504  }
1505  }
1506 
1507  imbalance = 0.0; // perfect
1508  env->timerStop(MICRO_TIMERS, "serialRCB", depth, 2);
1509  env->debug(DETAILED_STATUS, string("Exiting serialRCB"));
1510  return;
1511  }
1512  }
1513 
1515  // Divide into balanced left and right halves.
1516 
1517  scalar_t totalLeft=0, totalRight=0;
1518  lno_t localCountLeft=0, localCountRight=0;
1519  unsigned char *flags = new unsigned char [numLocalCoords];
1520  env->localMemoryAssertion(__FILE__, __LINE__, numLocalCoords, flags) ;
1521  ArrayRCP<unsigned char> lrflags(flags, 0, numLocalCoords, true);
1522 
1523  try{
1524  BSPfindCut<mvector_t>( env, comm,
1525  params, numTestCuts, tolerance,
1526  cutDimension, coordDim, vectors, index,
1527  fractionLeft.view(0, weightDim), uniformWeights.view(0, weightDim),
1528  minCoord, maxCoord,
1529  cutValue, lrflags.view(0, numLocalCoords),
1530  totalLeft, totalRight, localCountLeft, imbalance);
1531  }
1533 
1534  if (firstCall)
1535  env->memory("serial RCB start");
1536 
1538  // Adjust indices for left half and right half
1539 
1540  localCountRight = numLocalCoords - localCountLeft;
1541 
1542  if (localCountLeft){
1543 
1544  lno_t *newIndex = new lno_t [localCountLeft];
1545  env->localMemoryAssertion(__FILE__, __LINE__, localCountLeft, newIndex);
1546  ArrayView<lno_t> leftIndices(newIndex, localCountLeft);
1547 
1548  if (useIndices){
1549  for (lno_t i=0, ii=0; i < numLocalCoords; i++)
1550  if (lrflags[i] == leftFlag)
1551  newIndex[ii++] = index[i];
1552  }
1553  else{
1554  for (lno_t i=0, ii=0; i < numLocalCoords; i++)
1555  if (lrflags[i] == leftFlag)
1556  newIndex[ii++] = i;
1557  }
1558 
1559  partId_t newPart1 = part0 + numPartsLeftHalf - 1;
1560 
1561  env->timerStop(MICRO_TIMERS, "serialRCB", depth, 2);
1562 
1563  serialRCB<mvector_t, Adapter>(env, depth+1, params, numTestCuts, tolerance,
1564  coordDim, vectors, leftIndices,
1565  uniformWeights.view(0, weightDim), solution,
1566  part0, newPart1, partNum);
1567 
1568  env->timerStart(MICRO_TIMERS, "serialRCB", depth, 2);
1569 
1570  delete [] newIndex;
1571  }
1572 
1573 
1574  if (localCountRight){
1575 
1576  lno_t *newIndex = new lno_t [localCountRight];
1577  env->localMemoryAssertion(__FILE__, __LINE__, localCountRight, newIndex);
1578  ArrayView<lno_t> rightIndices(newIndex, localCountRight);
1579 
1580  if (useIndices){
1581  for (lno_t i=0, ii=0; i < numLocalCoords; i++)
1582  if (lrflags[i] == rightFlag){
1583  newIndex[ii++] = index[i];
1584  }
1585  }
1586  else{
1587  for (lno_t i=0, ii=0; i < numLocalCoords; i++)
1588  if (lrflags[i] == rightFlag)
1589  newIndex[ii++] = i;
1590  }
1591 
1592  partId_t newPart0 = part0 + numPartsLeftHalf;
1593 
1594  env->timerStop(MICRO_TIMERS, "serialRCB", depth, 2);
1595 
1596  serialRCB<mvector_t, Adapter>(env, depth+1, params, numTestCuts, tolerance,
1597  coordDim, vectors, rightIndices,
1598  uniformWeights.view(0, weightDim), solution,
1599  newPart0, part1, partNum);
1600 
1601  env->timerStart(MICRO_TIMERS, "serialRCB", depth, 2);
1602 
1603  delete [] newIndex;
1604  }
1605 
1606  env->timerStop(MICRO_TIMERS, "serialRCB", depth, 2);
1607  env->debug(DETAILED_STATUS, string("Exiting serialRCB"));
1608 }
1609 
1610 }// namespace Zoltan2
1611 
1612 #endif
1613 
checks for logic errors
fast typical checks for valid arguments
bool emptyPartsCheck(const RCP< const Environment > &env, const ArrayView< double > fractionLeft, scalar_t minCoord, scalar_t maxCoord, leftRightFlag &lrf, scalar_t &cutValue)
Solve partitioning if there are empty parts.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Metric class and namespace methods to compute quality metrics.
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
ArrayRCP< T > getInitializedBuffer(T minExtreme, T maxExtreme) const
Convenience method.
tuple id
Definition: xml2dox.py:189
void BSPfindCut(const RCP< const Environment > &env, const RCP< Comm< int > > &comm, const std::bitset< NUM_RCB_PARAMS > &params, int numTestCuts, typename mvector_t::scalar_type tolerance, int cutDim, int coordDim, const RCP< mvector_t > &vectors, ArrayView< typename mvector_t::local_ordinal_type > index, ArrayView< double > fractionLeft, ArrayView< bool > uniformWeights, typename mvector_t::scalar_type coordGlobalMin, typename mvector_t::scalar_type coordGlobalMax, typename mvector_t::scalar_type &cutValue, ArrayView< unsigned char > lrFlags, typename mvector_t::scalar_type &totalWeightLeft, typename mvector_t::scalar_type &totalWeightRight, typename mvector_t::local_ordinal_type &localCountLeft, typename mvector_t::scalar_type &imbalance)
Find the point in space that divides the data evenly with respect to the weights, part sizes...
Zoltan2_RCBOperation()
Default Constructor.
Zoltan2_RCBOperation(Ordinal nsum, Ordinal nmin, Ordinal nmax)
Constructor.
zoltan2_partId_t partId_t
rcbParams
The boolean parameters of interest to the RCB algorithm.
scalar_t getCoordWeight(lno_t id, multiCriteriaNorm mcnorm, ArrayView< StridedData< lno_t, scalar_t > > weights)
Traits of Xpetra classes, including migration method.
Defines the PartitioningSolution class.
void getFractionLeft(const RCP< const Environment > &env, partId_t part0, partId_t part1, const RCP< PartitioningSolution< Adapter > > &solution, ArrayRCP< double > &fractionLeft, partId_t &numPartsLeftHalf)
Determine the fraction of work in the left half.
Time the substeps of an entity.
void determineCut(const RCP< const Environment > &env, const RCP< Comm< int > > &comm, const std::bitset< NUM_RCB_PARAMS > &params, int numTestCuts, typename mvector_t::scalar_type tolerance, int coordDim, const RCP< mvector_t > &vectors, const ArrayView< bool > uniformWeights, multiCriteriaNorm mcnorm, const RCP< PartitioningSolution< Adapter > > &solution, partId_t part0, partId_t part1, ArrayView< unsigned char > lrflags, int &cutDimension, typename mvector_t::scalar_type &cutValue, typename mvector_t::scalar_type &imbalance, partId_t &numPartsLeftHalf, typename mvector_t::scalar_type &weightLeftHalf, typename mvector_t::scalar_type &weightRightHalf)
Divide the coordinates into a &quot;left&quot; half and &quot;right&quot; half.
sub-steps, each method&#39;s entry and exit
void testCoordinatesOnRightBoundary(const RCP< const Environment > &env, const RCP< Comm< int > > &comm, scalar_t totalWeightLeft, scalar_t targetLeftScalar, int cutLocation, ArrayView< scalar_t > localSums, ArrayView< scalar_t > globalSums, ArrayView< lno_t > index, ArrayView< StridedData< lno_t, scalar_t > > weights, multiCriteriaNorm mcnorm, ArrayView< unsigned char > lrFlags, scalar_t &globalWeightMovedRight)
Move boundary coordinates to the right.
A PartitioningSolution is a solution to a partitioning problem.
The StridedData class manages lists of weights or coordinates.
leftRightFlag
During partitioning, flags are stored in unsigned char arrays.
static RCP< const User > doMigration(const RCP< const User > &from, lno_t numLocalRows, const gno_t *myNewRows)
Migrate the object Given a user object and a new row distribution, create and return a new user objec...
the status at each high level step
double scalar_t
void migrateData(const RCP< const Environment > &env, const RCP< Comm< int > > &comm, ArrayView< unsigned char > lrflags, RCP< mvector_t > &vectors, int &leftNumProcs)
Migrate coordinates and weights to new processes.
void serialRCB(const RCP< const Environment > &env, int depth, const std::bitset< NUM_RCB_PARAMS > &params, int numTestCuts, typename mvector_t::scalar_type tolerance, int coordDim, const RCP< mvector_t > &vectors, ArrayView< typename mvector_t::local_ordinal_type > index, const ArrayView< bool > uniformWeights, const RCP< PartitioningSolution< Adapter > > &solution, partId_t part0, partId_t part1, ArrayView< partId_t > partNum)
Perform RCB on the local process only.
void getCutDimension(const RCP< const Environment > &env, const RCP< Comm< int > > &comm, int coordDim, const RCP< mvector_t > &vectors, ArrayView< typename mvector_t::local_ordinal_type > index, int &dimension, typename mvector_t::scalar_type &minCoord, typename mvector_t::scalar_type &maxCoord)
Choose a coordinate dimension for the next cut.
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
void reduce(const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
Implement Teuchos::ValueTypeReductionOp interface.
A Teuchos::MPIComm reduction operation.