Zoltan2  Version of the Day
Zoltan2_PartitioningSolution.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_PARTITIONINGSOLUTION_HPP_
51 #define _ZOLTAN2_PARTITIONINGSOLUTION_HPP_
52 
54 #include <Zoltan2_Solution.hpp>
55 #include <Zoltan2_GreedyMWM.hpp>
57 #include <cmath>
58 #include <algorithm>
59 #include <vector>
60 #include <limits>
61 
62 namespace Teuchos{
63 
68 template <typename Ordinal, typename T>
69 class Zoltan2_BoxBoundaries : public ValueTypeReductionOp<Ordinal,T>
70 {
71 private:
72  Ordinal size;
73  T _EPSILON;
74 
75 public:
78  Zoltan2_BoxBoundaries ():size(0), _EPSILON (std::numeric_limits<T>::epsilon()){}
79 
86  Zoltan2_BoxBoundaries (Ordinal s_):
87  size(s_), _EPSILON (std::numeric_limits<T>::epsilon()){}
88 
91  void reduce( const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
92  {
93  for (Ordinal i=0; i < count; i++){
94  if (Z2_ABS(inBuffer[i]) > _EPSILON){
95  inoutBuffer[i] = inBuffer[i];
96  }
97  }
98  }
99 };
100 } // namespace Teuchos
101 
102 
103 namespace Zoltan2 {
104 
105 long measure_stays(partId_t *, int *, partId_t *, long *, partId_t, partId_t);
106 
107 
122 template <typename Adapter>
124 {
125 public:
126 
127 #ifndef DOXYGEN_SHOULD_SKIP_THIS
128  typedef typename Adapter::gno_t gno_t;
129  typedef typename Adapter::scalar_t scalar_t;
130  typedef typename Adapter::lno_t lno_t;
131  typedef typename Adapter::gid_t gid_t;
132  typedef typename Adapter::user_t user_t;
133 #endif
134 
152  PartitioningSolution( RCP<const Environment> &env,
153  RCP<const Comm<int> > &comm,
154  RCP<const IdentifierMap<user_t> > &idMap,
155  int userWeightDim);
156 
186  PartitioningSolution( RCP<const Environment> &env,
187  RCP<const Comm<int> > &comm,
188  RCP<const IdentifierMap<user_t> > &idMap,
189  int userWeightDim, ArrayView<ArrayRCP<partId_t> > reqPartIds,
190  ArrayView<ArrayRCP<scalar_t> > reqPartSizes);
191 
193  // Information that the algorithm may wish to query.
194 
197  size_t getTargetGlobalNumberOfParts() const { return nGlobalParts_; }
198 
201  size_t getActualGlobalNumberOfParts() const { return nGlobalPartsSolution_; }
202 
205  size_t getLocalNumberOfParts() const { return nLocalParts_; }
206 
214  scalar_t getLocalFractionOfPart() const { return localFraction_; }
215 
225  bool oneToOnePartDistribution() const { return onePartPerProc_; }
226 
246  const int *getPartDistribution() const {
247  if (partDist_.size() > 0) return &partDist_[0];
248  else return NULL;
249  }
250 
267  const partId_t *getProcDistribution() const {
268  if (procDist_.size() > 0) return &procDist_[0];
269  else return NULL;
270  }
271 
275  int getNumberOfCriteria() const { return weightDim_; }
276 
277 
284  bool criteriaHasUniformPartSizes(int idx) const { return pSizeUniform_[idx];}
285 
298  scalar_t getCriteriaPartSize(int idx, partId_t part) const {
299  if (pSizeUniform_[idx])
300  return 1.0 / nGlobalParts_;
301  else if (pCompactIndex_[idx].size())
302  return pSize_[idx][pCompactIndex_[idx][part]];
303  else
304  return pSize_[idx][part];
305  }
306 
320  bool criteriaHaveSamePartSizes(int c1, int c2) const;
321 
323  // Method used by the algorithm to set results.
324 
351  void setParts(ArrayRCP<const gno_t> &gnoList,
352  ArrayRCP<partId_t> &partList, bool dataDidNotMove);
353 
355 
367  void RemapParts();
368 
370  // Results that may be queried by the user, by migration methods,
371  // or by metric calculation methods.
372  // We return raw pointers so users don't have to learn about our
373  // pointer wrappers.
374 
377  const RCP<const Comm<int> > &getCommunicator() const { return comm_;}
378 
381  size_t getLocalNumberOfIds() const { return gids_.size(); }
382 
385  const gid_t *getIdList() const { return gids_.getRawPtr(); }
386 
389  const zoltan2_partId_t *getPartList() const { return parts_.getRawPtr();}
390 
395  const int *getProcList() const { return procs_.getRawPtr();}
396 
397 
401  this->partBoxes = outPartBoxes;
402  }
403 
406  RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > getPartBoxes(){
407  return this->partBoxes;
408  }
409 
413  const Teuchos::Comm<int> *comm,
414  ArrayRCP <partId_t> &comXAdj,
415  ArrayRCP <partId_t> &comAdj
416  ) {
417 
418  if(comXAdj_.getRawPtr() == NULL && comAdj_.getRawPtr() == NULL){
419 
420  partId_t ntasks = this->getActualGlobalNumberOfParts();
421  if (partId_t (this->getTargetGlobalNumberOfParts()) > ntasks){
422  ntasks = this->getTargetGlobalNumberOfParts();
423  }
424  RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > pBoxes = this->getGlobalBoxBoundaries(comm);
425  int dim = (*pBoxes)[0].getDim();
427  pBoxes,
428  ntasks,
429  dim);
430  grid.getAdjArrays(comXAdj_, comAdj_);
431  }
432  comAdj = comAdj_;
433  comXAdj = comXAdj_;
434 
435  }
436 
437  RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > getGlobalBoxBoundaries(
438  const Teuchos::Comm<int> *comm){
439 
440  partId_t ntasks = this->getActualGlobalNumberOfParts();
441  if (partId_t (this->getTargetGlobalNumberOfParts()) > ntasks){
442  ntasks = this->getTargetGlobalNumberOfParts();
443  }
444 
445  RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > pBoxes = this->getPartBoxes();
446 
447  int dim = (*pBoxes)[0].getDim();
448 
449 
450  scalar_t *localPartBoundaries = new scalar_t[ntasks * 2 *dim];
451 
452  memset(localPartBoundaries, 0, sizeof(scalar_t) * ntasks * 2 *dim);
453 
454  scalar_t *globalPartBoundaries = new scalar_t[ntasks * 2 *dim];
455  memset(globalPartBoundaries, 0, sizeof(scalar_t) * ntasks * 2 *dim);
456 
457  scalar_t *localPartMins = localPartBoundaries;
458  scalar_t *localPartMaxs = localPartBoundaries + ntasks * dim;
459 
460  scalar_t *globalPartMins = globalPartBoundaries;
461  scalar_t *globalPartMaxs = globalPartBoundaries + ntasks * dim;
462 
463  partId_t boxCount = pBoxes->size();
464  for (partId_t i = 0; i < boxCount; ++i){
465  partId_t pId = (*pBoxes)[i].getpId();
466  //cout << "me:" << comm->getRank() << " has:" << pId << endl;
467 
468  scalar_t *lmins = (*pBoxes)[i].getlmins();
469  scalar_t *lmaxs = (*pBoxes)[i].getlmaxs();
470 
471  for (int j = 0; j < dim; ++j){
472  localPartMins[dim * pId + j] = lmins[j];
473  localPartMaxs[dim * pId + j] = lmaxs[j];
474  /*
475  cout << "me:" << comm->getRank() <<
476  " dim * pId + j:"<< dim * pId + j <<
477  " localMin:" << localPartMins[dim * pId + j] <<
478  " localMax:" << localPartMaxs[dim * pId + j] << endl;
479  */
480  }
481  }
482 
483  Teuchos::Zoltan2_BoxBoundaries<int, scalar_t> reductionOp(ntasks * 2 *dim);
484 
485  reduceAll<int, scalar_t>(*comm, reductionOp,
486  ntasks * 2 *dim, localPartBoundaries, globalPartBoundaries
487  );
488  RCP < std::vector <coordinateModelPartBox <scalar_t, partId_t> > > pB(new std::vector <coordinateModelPartBox <scalar_t, partId_t> > (), true) ;
489  for (partId_t i = 0; i < ntasks; ++i){
491  i,
492  dim,
493  globalPartMins + dim * i,
494  globalPartMaxs + dim * i);
495 
496  /*
497  for (int j = 0; j < dim; ++j){
498  cout << "me:" << comm->getRank() <<
499  " dim * pId + j:"<< dim * i + j <<
500  " globalMin:" << globalPartMins[dim * i + j] <<
501  " globalMax:" << globalPartMaxs[dim * i + j] << endl;
502  }
503  */
504  pB->push_back(tpb);
505  }
506  delete []localPartBoundaries;
507  delete []globalPartBoundaries;
508  //RCP < std::vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > tmpRCPBox(pB, true);
509  this->partBoxes = pB;
510  return this->partBoxes;
511  }
541  template <typename Extra>
543  int numExtra,
544  ArrayRCP<Extra> &xtraInfo,
545  ArrayRCP<typename Adapter::gid_t> &imports,
546  ArrayRCP<Extra> &newXtraInfo) const;
547 
565  void getPartsForProc(int procId, double &numParts, partId_t &partMin,
566  partId_t &partMax) const
567  {
568  env_->localInputAssertion(__FILE__, __LINE__, "invalid process id",
569  procId >= 0 && procId < comm_->getSize(), BASIC_ASSERTION);
570 
571  procToPartsMap(procId, numParts, partMin, partMax);
572  }
573 
585  void getProcsForPart(partId_t partId, int &procMin, int &procMax) const
586  {
587  env_->localInputAssertion(__FILE__, __LINE__, "invalid part id",
588  partId >= 0 && partId < nGlobalParts_, BASIC_ASSERTION);
589 
590  partToProcsMap(partId, procMin, procMax);
591  }
592 
593 private:
594  void partToProc(bool doCheck, bool haveNumLocalParts, bool haveNumGlobalParts,
595  int numLocalParts, int numGlobalParts);
596 
597  void procToPartsMap(int procId, double &numParts, partId_t &partMin,
598  partId_t &partMax) const;
599 
600  void partToProcsMap(partId_t partId, int &procMin, int &procMax) const;
601 
602  void setPartDistribution();
603 
604  void setPartSizes(ArrayView<ArrayRCP<partId_t> > reqPartIds,
605  ArrayView<ArrayRCP<scalar_t> > reqPartSizes);
606 
607  void computePartSizes(int wdim, ArrayView<partId_t> ids,
608  ArrayView<scalar_t> sizes);
609 
610  void broadcastPartSizes(int wdim);
611 
612 
613  RCP<const Environment> env_; // has application communicator
614  RCP<const Comm<int> > comm_; // the problem communicator
615  RCP<const IdentifierMap<user_t> > idMap_;
616 
617  //part box boundaries as a result of geometric partitioning algorithm.
618  RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > partBoxes;
619  ArrayRCP <partId_t> comXAdj_; //communication graph xadj
620  ArrayRCP <partId_t> comAdj_; //communication graph adj.
621 
622  partId_t nGlobalParts_;// target global number of parts
623  partId_t nLocalParts_; // number of parts to be on this process
624 
625  scalar_t localFraction_; // approx fraction of a part on this process
626  int weightDim_; // if user has no weights, this is 1
627 
628  // If process p is to be assigned part p for all p, then onePartPerProc_
629  // is true. Otherwise it is false, and either procDist_ or partDist_
630  // describes the allocation of parts to processes.
631  //
632  // If parts are never split across processes, then procDist_ is defined
633  // as follows:
634  //
635  // partId = procDist_[procId]
636  // partIdNext = procDist_[procId+1]
637  // globalNumberOfParts = procDist_[numProcs]
638  //
639  // meaning that the parts assigned to process procId range from
640  // [partId, partIdNext). If partIdNext is the same as partId, then
641  // process procId has no parts.
642  //
643  // If the number parts is less than the number of processes, and the
644  // user did not specify "num_local_parts" for each of the processes, then
645  // parts are split across processes, and partDist_ is defined rather than
646  // procDist_.
647  //
648  // procId = partDist_[partId]
649  // procIdNext = partDist_[partId+1]
650  // globalNumberOfProcs = partDist_[numParts]
651  //
652  // which implies that the part partId is shared by processes in the
653  // the range [procId, procIdNext).
654  //
655  // We use std::vector so we can use upper_bound algorithm
656 
657  bool onePartPerProc_; // either this is true...
658  std::vector<int> partDist_; // or this is defined ...
659  std::vector<partId_t> procDist_; // or this is defined.
660  bool procDistEquallySpread_; // if procDist_ is used and
661  // #parts > #procs and
662  // num_local_parts is not specified,
663  // parts are evenly distributed to procs
664 
665  // In order to minimize the storage required for part sizes, we
666  // have three different representations.
667  //
668  // If the part sizes for weight dimension w are all the same, then:
669  // pSizeUniform_[w] = true
670  // pCompactIndex_[w].size() = 0
671  // pSize_[w].size() = 0
672  //
673  // and the size for part p is 1.0 / nparts.
674  //
675  // If part sizes differ for each part in weight dimension w, but there
676  // are no more than 64 distinct sizes:
677  // pSizeUniform_[w] = false
678  // pCompactIndex_[w].size() = number of parts
679  // pSize_[w].size() = number of different sizes
680  //
681  // and the size for part p is pSize_[pCompactIndex_[p]].
682  //
683  // If part sizes differ for each part in weight dimension w, and there
684  // are more than 64 distinct sizes:
685  // pSizeUniform_[w] = false
686  // pCompactIndex_[w].size() = 0
687  // pSize_[w].size() = nparts
688  //
689  // and the size for part p is pSize_[p].
690  //
691  // NOTE: If we expect to have similar cases, i.e. a long list of scalars
692  // where it is highly possible that just a few unique values appear,
693  // then we may want to make this a class. The advantage is that we
694  // save a long list of 1-byte indices instead of a long list of scalars.
695 
696  ArrayRCP<bool> pSizeUniform_;
697  ArrayRCP<ArrayRCP<unsigned char> > pCompactIndex_;
698  ArrayRCP<ArrayRCP<scalar_t> > pSize_;
699 
701  // The algorithm sets these values upon completion.
702 
703  ArrayRCP<const gid_t> gids_; // User's global IDs
704  ArrayRCP<partId_t> parts_; // part number assigned to gids_[i]
705 
706  bool haveSolution_;
707 
708  partId_t nGlobalPartsSolution_; // global number of parts in solution
709 
711  // The solution calculates this from the part assignments,
712  // unless onePartPerProc_.
713 
714  ArrayRCP<int> procs_; // process rank assigned to gids_[i]
715 };
716 
718 // Definitions
720 
721 template <typename Adapter>
723  RCP<const Environment> &env,
724  RCP<const Comm<int> > &comm,
725  RCP<const IdentifierMap<user_t> > &idMap, int userWeightDim)
726  : env_(env), comm_(comm), idMap_(idMap),
727  partBoxes(),comXAdj_(), comAdj_(),
728  nGlobalParts_(0), nLocalParts_(0),
729  localFraction_(0), weightDim_(),
730  onePartPerProc_(false), partDist_(), procDist_(),
731  procDistEquallySpread_(false),
732  pSizeUniform_(), pCompactIndex_(), pSize_(),
733  gids_(), parts_(), haveSolution_(false), nGlobalPartsSolution_(0),
734  procs_()
735 {
736  weightDim_ = (userWeightDim ? userWeightDim : 1);
737 
738  setPartDistribution();
739 
740  // We must call setPartSizes() because part sizes may have
741  // been provided by the user on other processes.
742 
743  ArrayRCP<partId_t> *noIds = new ArrayRCP<partId_t> [weightDim_];
744  ArrayRCP<scalar_t> *noSizes = new ArrayRCP<scalar_t> [weightDim_];
745  ArrayRCP<ArrayRCP<partId_t> > ids(noIds, 0, weightDim_, true);
746  ArrayRCP<ArrayRCP<scalar_t> > sizes(noSizes, 0, weightDim_, true);
747 
748  setPartSizes(ids.view(0, weightDim_), sizes.view(0, weightDim_));
749 
750  env_->memory("After construction of solution");
751 }
752 
753 template <typename Adapter>
755  RCP<const Environment> &env,
756  RCP<const Comm<int> > &comm,
757  RCP<const IdentifierMap<user_t> > &idMap, int userWeightDim,
758  ArrayView<ArrayRCP<partId_t> > reqPartIds,
759  ArrayView<ArrayRCP<scalar_t> > reqPartSizes)
760  : env_(env), comm_(comm), idMap_(idMap),
761  partBoxes(),comXAdj_(), comAdj_(),
762  nGlobalParts_(0), nLocalParts_(0),
763  localFraction_(0), weightDim_(),
764  onePartPerProc_(false), partDist_(), procDist_(),
765  procDistEquallySpread_(false),
766  pSizeUniform_(), pCompactIndex_(), pSize_(),
767  gids_(), parts_(), haveSolution_(false), nGlobalPartsSolution_(0),
768  procs_()
769 {
770  weightDim_ = (userWeightDim ? userWeightDim : 1);
771 
772  setPartDistribution();
773 
774  setPartSizes(reqPartIds, reqPartSizes);
775 
776  env_->memory("After construction of solution");
777 }
778 
779 template <typename Adapter>
781 {
782  // Did the caller define num_global_parts and/or num_local_parts?
783 
784  const ParameterList &pl = env_->getParameters();
785  size_t haveGlobalNumParts=0, haveLocalNumParts=0;
786  int numLocal=0, numGlobal=0;
787  double val;
788 
789  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("num_global_parts");
790 
791  if (pe){
792  val = pe->getValue<double>(&val); // TODO: KDD Skip this double get
793  haveGlobalNumParts = 1; // TODO: KDD Should be unnecessary once
794  numGlobal = static_cast<int>(val); // TODO: KDD paramlist handles long long.
795  nGlobalParts_ = partId_t(numGlobal); // TODO: KDD also do below.
796  }
797 
798  pe = pl.getEntryPtr("num_local_parts");
799 
800  if (pe){
801  val = pe->getValue<double>(&val);
802  haveLocalNumParts = 1;
803  numLocal = static_cast<int>(val);
804  nLocalParts_ = partId_t(numLocal);
805  }
806 
807  try{
808  // Sets onePartPerProc_, partDist_, and procDist_
809 
810  partToProc(true, haveLocalNumParts, haveGlobalNumParts,
811  numLocal, numGlobal);
812  }
814 
815  int nprocs = comm_->getSize();
816  int rank = comm_->getRank();
817 
818  if (onePartPerProc_){
819  nGlobalParts_ = nprocs;
820  nLocalParts_ = 1;
821  }
822  else if (partDist_.size() > 0){ // more procs than parts
823  nGlobalParts_ = partDist_.size() - 1;
824  int pstart = partDist_[0];
825  for (partId_t i=1; i <= nGlobalParts_; i++){
826  int pend = partDist_[i];
827  if (rank >= pstart && rank < pend){
828  int numOwners = pend - pstart;
829  nLocalParts_ = 1;
830  localFraction_ = 1.0 / numOwners;
831  break;
832  }
833  pstart = pend;
834  }
835  }
836  else if (procDist_.size() > 0){ // more parts than procs
837  nGlobalParts_ = procDist_[nprocs];
838  nLocalParts_ = procDist_[rank+1] - procDist_[rank];
839  }
840  else {
841  throw logic_error("partToProc error");
842  }
843 
844 }
845 
846 template <typename Adapter>
847  void PartitioningSolution<Adapter>::setPartSizes(
848  ArrayView<ArrayRCP<partId_t> > ids, ArrayView<ArrayRCP<scalar_t> > sizes)
849 {
850  int wdim = weightDim_;
851  bool fail=false;
852 
853  size_t *countBuf = new size_t [wdim*2];
854  ArrayRCP<size_t> counts(countBuf, 0, wdim*2, true);
855 
856  fail = ((ids.size() != wdim) || (sizes.size() != wdim));
857 
858  for (int w=0; !fail && w < wdim; w++){
859  counts[w] = ids[w].size();
860  if (ids[w].size() != sizes[w].size()) fail=true;
861  }
862 
863  env_->globalBugAssertion(__FILE__, __LINE__, "bad argument arrays", fail==0,
864  COMPLEX_ASSERTION, comm_);
865 
866  // Are all part sizes the same? This is the common case.
867 
868  ArrayRCP<scalar_t> *emptySizes= new ArrayRCP<scalar_t> [wdim];
869  pSize_ = arcp(emptySizes, 0, wdim);
870 
871  ArrayRCP<unsigned char> *emptyIndices= new ArrayRCP<unsigned char> [wdim];
872  pCompactIndex_ = arcp(emptyIndices, 0, wdim);
873 
874  bool *info = new bool [wdim];
875  pSizeUniform_ = arcp(info, 0, wdim);
876  for (int w=0; w < wdim; w++)
877  pSizeUniform_[w] = true;
878 
879  if (nGlobalParts_ == 1){
880  return; // there's only one part in the whole problem
881  }
882 
883  size_t *ptr1 = counts.getRawPtr();
884  size_t *ptr2 = counts.getRawPtr() + wdim;
885 
886  try{
887  reduceAll<int, size_t>(*comm_, Teuchos::REDUCE_MAX, wdim, ptr1, ptr2);
888  }
889  Z2_THROW_OUTSIDE_ERROR(*env_);
890 
891  bool zero = true;
892 
893  for (int w=0; w < wdim; w++)
894  if (counts[wdim+w] > 0){
895  zero = false;
896  pSizeUniform_[w] = false;
897  }
898 
899  if (zero) // Part sizes for all criteria are uniform.
900  return;
901 
902  // Compute the part sizes for criteria for which part sizes were
903  // supplied. Normalize for each criteria so part sizes sum to one.
904 
905  int nprocs = comm_->getSize();
906  int rank = comm_->getRank();
907 
908  for (int w=0; w < wdim; w++){
909  if (pSizeUniform_[w]) continue;
910 
911  // Send all ids and sizes to one process.
912  // (There is no simple gather method in Teuchos.)
913 
914  partId_t length = ids[w].size();
915  partId_t *allLength = new partId_t [nprocs];
916  Teuchos::gatherAll<int, partId_t>(*comm_, 1, &length,
917  nprocs, allLength);
918 
919  if (rank == 0){
920  int total = 0;
921  for (int i=0; i < nprocs; i++)
922  total += allLength[i];
923 
924  partId_t *partNums = new partId_t [total];
925  scalar_t *partSizes = new scalar_t [total];
926 
927  ArrayView<partId_t> idArray(partNums, total);
928  ArrayView<scalar_t> sizeArray(partSizes, total);
929 
930  if (length > 0){
931  for (int i=0; i < length; i++){
932  *partNums++ = ids[w][i];
933  *partSizes++ = sizes[w][i];
934  }
935  }
936 
937  for (int p=1; p < nprocs; p++){
938  if (allLength[p] > 0){
939  Teuchos::receive<int, partId_t>(*comm_, p,
940  allLength[p], partNums);
941  Teuchos::receive<int, scalar_t>(*comm_, p,
942  allLength[p], partSizes);
943  partNums += allLength[p];
944  partSizes += allLength[p];
945  }
946  }
947 
948  delete [] allLength;
949 
950  try{
951  computePartSizes(w, idArray, sizeArray);
952  }
954 
955  delete [] idArray.getRawPtr();
956  delete [] sizeArray.getRawPtr();
957  }
958  else{
959  delete [] allLength;
960  if (length > 0){
961  Teuchos::send<int, partId_t>(*comm_, length, ids[w].getRawPtr(), 0);
962  Teuchos::send<int, scalar_t>(*comm_, length, sizes[w].getRawPtr(), 0);
963  }
964  }
965 
966  broadcastPartSizes(w);
967  }
968 }
969 
970 template <typename Adapter>
971  void PartitioningSolution<Adapter>::broadcastPartSizes(int wdim)
972 {
973  env_->localBugAssertion(__FILE__, __LINE__, "preallocations",
974  pSize_.size()>wdim &&
975  pSizeUniform_.size()>wdim && pCompactIndex_.size()>wdim,
977 
978  int rank = comm_->getRank();
979  int nprocs = comm_->getSize();
980  partId_t nparts = nGlobalParts_;
981 
982  if (nprocs < 2)
983  return;
984 
985  char flag=0;
986 
987  if (rank == 0){
988  if (pSizeUniform_[wdim] == true)
989  flag = 1;
990  else if (pCompactIndex_[wdim].size() > 0)
991  flag = 2;
992  else
993  flag = 3;
994  }
995 
996  try{
997  Teuchos::broadcast<int, char>(*comm_, 0, 1, &flag);
998  }
999  Z2_THROW_OUTSIDE_ERROR(*env_);
1000 
1001  if (flag == 1){
1002  if (rank > 0)
1003  pSizeUniform_[wdim] = true;
1004 
1005  return;
1006  }
1007 
1008  if (flag == 2){
1009 
1010  // broadcast the indices into the size list
1011 
1012  unsigned char *idxbuf = NULL;
1013 
1014  if (rank > 0){
1015  idxbuf = new unsigned char [nparts];
1016  env_->localMemoryAssertion(__FILE__, __LINE__, nparts, idxbuf);
1017  }
1018  else{
1019  env_->localBugAssertion(__FILE__, __LINE__, "index list size",
1020  pCompactIndex_[wdim].size() == nparts, COMPLEX_ASSERTION);
1021  idxbuf = pCompactIndex_[wdim].getRawPtr();
1022  }
1023 
1024  try{
1025  // broadcast of unsigned char is not supported
1026  Teuchos::broadcast<int, char>(*comm_, 0, nparts,
1027  reinterpret_cast<char *>(idxbuf));
1028  }
1029  Z2_THROW_OUTSIDE_ERROR(*env_);
1030 
1031  if (rank > 0)
1032  pCompactIndex_[wdim] = arcp(idxbuf, 0, nparts, true);
1033 
1034  // broadcast the list of different part sizes
1035 
1036  unsigned char maxIdx=0;
1037  for (partId_t p=0; p < nparts; p++)
1038  if (idxbuf[p] > maxIdx) maxIdx = idxbuf[p];
1039 
1040  int numSizes = maxIdx + 1;
1041 
1042  scalar_t *sizeList = NULL;
1043 
1044  if (rank > 0){
1045  sizeList = new scalar_t [numSizes];
1046  env_->localMemoryAssertion(__FILE__, __LINE__, numSizes, sizeList);
1047  }
1048  else{
1049  env_->localBugAssertion(__FILE__, __LINE__, "wrong number of sizes",
1050  numSizes == pSize_[wdim].size(), COMPLEX_ASSERTION);
1051 
1052  sizeList = pSize_[wdim].getRawPtr();
1053  }
1054 
1055  try{
1056  Teuchos::broadcast<int, scalar_t>(*comm_, 0, numSizes, sizeList);
1057  }
1058  Z2_THROW_OUTSIDE_ERROR(*env_);
1059 
1060  if (rank > 0)
1061  pSize_[wdim] = arcp(sizeList, 0, numSizes, true);
1062 
1063  return;
1064  }
1065 
1066  if (flag == 3){
1067 
1068  // broadcast the size of each part
1069 
1070  scalar_t *sizeList = NULL;
1071 
1072  if (rank > 0){
1073  sizeList = new scalar_t [nparts];
1074  env_->localMemoryAssertion(__FILE__, __LINE__, nparts, sizeList);
1075  }
1076  else{
1077  env_->localBugAssertion(__FILE__, __LINE__, "wrong number of sizes",
1078  nparts == pSize_[wdim].size(), COMPLEX_ASSERTION);
1079 
1080  sizeList = pSize_[wdim].getRawPtr();
1081  }
1082 
1083  try{
1084  Teuchos::broadcast<int, scalar_t >(*comm_, 0, nparts, sizeList);
1085  }
1086  Z2_THROW_OUTSIDE_ERROR(*env_);
1087 
1088  if (rank > 0)
1089  pSize_[wdim] = arcp(sizeList, 0, nparts);
1090 
1091  return;
1092  }
1093 }
1094 
1095 template <typename Adapter>
1096  void PartitioningSolution<Adapter>::computePartSizes(int wdim,
1097  ArrayView<partId_t> ids, ArrayView<scalar_t> sizes)
1098 {
1099  int len = ids.size();
1100 
1101  if (len == 0){
1102  pSizeUniform_[wdim] = true;
1103  return;
1104  }
1105 
1106  env_->localBugAssertion(__FILE__, __LINE__, "bad array sizes",
1107  len>0 && sizes.size()==len, COMPLEX_ASSERTION);
1108 
1109  env_->localBugAssertion(__FILE__, __LINE__, "bad index",
1110  wdim>=0 && wdim<weightDim_, COMPLEX_ASSERTION);
1111 
1112  env_->localBugAssertion(__FILE__, __LINE__, "preallocations",
1113  pSize_.size()>wdim &&
1114  pSizeUniform_.size()>wdim && pCompactIndex_.size()>wdim,
1116 
1117  // Check ids and sizes and find min, max and average sizes.
1118  // If sizes are very close to uniform, call them uniform parts.
1119 
1120  partId_t nparts = nGlobalParts_;
1121  unsigned char *buf = new unsigned char [nparts];
1122  env_->localMemoryAssertion(__FILE__, __LINE__, nparts, buf);
1123  memset(buf, 0, nparts);
1124  ArrayRCP<unsigned char> partIdx(buf, 0, nparts, true);
1125 
1126  scalar_t epsilon = 10e-5 / nparts;
1127  scalar_t min=sizes[0], max=sizes[0], sum=0;
1128 
1129  for (int i=0; i < len; i++){
1130  partId_t id = ids[i];
1131  scalar_t size = sizes[i];
1132 
1133  env_->localInputAssertion(__FILE__, __LINE__, "invalid part id",
1134  id>=0 && id<nparts, BASIC_ASSERTION);
1135 
1136  env_->localInputAssertion(__FILE__, __LINE__, "invalid part size", size>=0,
1137  BASIC_ASSERTION);
1138 
1139  // TODO: we could allow users to specify multiple sizes for the same
1140  // part if we add a parameter that says what we are to do with them:
1141  // add them or take the max.
1142 
1143  env_->localInputAssertion(__FILE__, __LINE__,
1144  "multiple sizes provided for one part", partIdx[id]==0, BASIC_ASSERTION);
1145 
1146  partIdx[id] = 1; // mark that we have a size for this part
1147 
1148  if (size < min) min = size;
1149  if (size > max) max = size;
1150  sum += size;
1151  }
1152 
1153  if (sum == 0){
1154 
1155  // User has given us a list of parts of size 0, we'll set
1156  // the rest to them to equally sized parts.
1157 
1158  scalar_t *allSizes = new scalar_t [2];
1159  env_->localMemoryAssertion(__FILE__, __LINE__, 2, allSizes);
1160 
1161  ArrayRCP<scalar_t> sizeArray(allSizes, 0, 2, true);
1162 
1163  int numNonZero = nparts - len;
1164 
1165  allSizes[0] = 0.0;
1166  allSizes[1] = 1.0 / numNonZero;
1167 
1168  for (partId_t p=0; p < nparts; p++)
1169  buf[p] = 1; // index to default part size
1170 
1171  for (int i=0; i < len; i++)
1172  buf[ids[i]] = 0; // index to part size zero
1173 
1174  pSize_[wdim] = sizeArray;
1175  pCompactIndex_[wdim] = partIdx;
1176 
1177  return;
1178  }
1179 
1180  if (max - min <= epsilon){
1181  pSizeUniform_[wdim] = true;
1182  return;
1183  }
1184 
1185  // A size for parts that were not specified:
1186  scalar_t avg = sum / nparts;
1187 
1188  // We are going to merge part sizes that are very close. This takes
1189  // computation time now, but can save considerably in the storage of
1190  // all part sizes on each process. For example, a common case may
1191  // be some parts are size 1 and all the rest are size 2.
1192 
1193  scalar_t *tmp = new scalar_t [len];
1194  env_->localMemoryAssertion(__FILE__, __LINE__, len, tmp);
1195  memcpy(tmp, sizes.getRawPtr(), sizeof(scalar_t) * len);
1196  ArrayRCP<scalar_t> partSizes(tmp, 0, len, true);
1197 
1198  std::sort(partSizes.begin(), partSizes.end());
1199 
1200  // create a list of sizes that are unique within epsilon
1201 
1202  Array<scalar_t> nextUniqueSize;
1203  nextUniqueSize.push_back(partSizes[len-1]); // largest
1204  scalar_t curr = partSizes[len-1];
1205  int avgIndex = len;
1206  bool haveAvg = false;
1207  if (curr - avg <= epsilon)
1208  avgIndex = 0;
1209 
1210  for (int i=len-2; i >= 0; i--){
1211  scalar_t val = partSizes[i];
1212  if (curr - val > epsilon){
1213  nextUniqueSize.push_back(val); // the highest in the group
1214  curr = val;
1215  if (avgIndex==len && val > avg && val - avg <= epsilon){
1216  // the average would be in this group
1217  avgIndex = nextUniqueSize.size() - 1;
1218  haveAvg = true;
1219  }
1220  }
1221  }
1222 
1223  partSizes.clear();
1224 
1225  size_t numSizes = nextUniqueSize.size();
1226  int sizeArrayLen = numSizes;
1227 
1228  if (numSizes < 64){
1229  // We can store the size for every part in a compact way.
1230 
1231  // Create a list of all sizes in increasing order
1232 
1233  if (!haveAvg) sizeArrayLen++; // need to include average
1234 
1235  scalar_t *allSizes = new scalar_t [sizeArrayLen];
1236  env_->localMemoryAssertion(__FILE__, __LINE__, sizeArrayLen, allSizes);
1237  ArrayRCP<scalar_t> sizeArray(allSizes, 0, sizeArrayLen, true);
1238 
1239  int newAvgIndex = sizeArrayLen;
1240 
1241  for (int i=numSizes-1, idx=0; i >= 0; i--){
1242 
1243  if (newAvgIndex == sizeArrayLen){
1244 
1245  if (haveAvg && i==avgIndex)
1246  newAvgIndex = idx;
1247 
1248  else if (!haveAvg && avg < nextUniqueSize[i]){
1249  newAvgIndex = idx;
1250  allSizes[idx++] = avg;
1251  }
1252  }
1253 
1254  allSizes[idx++] = nextUniqueSize[i];
1255  }
1256 
1257  env_->localBugAssertion(__FILE__, __LINE__, "finding average in list",
1258  newAvgIndex < sizeArrayLen, COMPLEX_ASSERTION);
1259 
1260  for (int i=0; i < nparts; i++){
1261  buf[i] = newAvgIndex; // index to default part size
1262  }
1263 
1264  sum = (nparts - len) * allSizes[newAvgIndex];
1265 
1266  for (int i=0; i < len; i++){
1267  int id = ids[i];
1268  scalar_t size = sizes[i];
1269  int index;
1270 
1271  // Find the first size greater than or equal to this size.
1272 
1273  if (size < avg && avg - size <= epsilon)
1274  index = newAvgIndex;
1275  else{
1276  typename ArrayRCP<scalar_t>::iterator found =
1277  std::lower_bound(sizeArray.begin(), sizeArray.end(), size);
1278 
1279  env_->localBugAssertion(__FILE__, __LINE__, "size array",
1280  found != sizeArray.end(), COMPLEX_ASSERTION);
1281 
1282  index = found - sizeArray.begin();
1283  }
1284 
1285  buf[id] = index;
1286  sum += allSizes[index];
1287  }
1288 
1289  for (int i=0; i < sizeArrayLen; i++){
1290  sizeArray[i] /= sum;
1291  }
1292 
1293  pCompactIndex_[wdim] = partIdx;
1294  pSize_[wdim] = sizeArray;
1295  }
1296  else{
1297  // To have access to part sizes, we must store nparts scalar_ts on
1298  // every process. We expect this is a rare case.
1299 
1300  tmp = new scalar_t [nparts];
1301  env_->localMemoryAssertion(__FILE__, __LINE__, nparts, tmp);
1302 
1303  sum += ((nparts - len) * avg);
1304 
1305  for (int i=0; i < nparts; i++){
1306  tmp[i] = avg/sum;
1307  }
1308 
1309  for (int i=0; i < len; i++){
1310  tmp[ids[i]] = sizes[i]/sum;
1311  }
1312 
1313  pSize_[wdim] = arcp(tmp, 0, nparts);
1314  }
1315 }
1316 
1317 
1318 template <typename Adapter>
1320  ArrayRCP<const gno_t> &gnoList, ArrayRCP<partId_t> &partList,
1321  bool dataDidNotMove)
1322 {
1323  env_->debug(DETAILED_STATUS, "Entering setParts");
1324 
1325  size_t len = partList.size();
1326 
1327  // Find the actual number of parts in the solution, which can
1328  // be more or less than the nGlobalParts_ target.
1329  // (We may want to compute the imbalance of a given solution with
1330  // respect to a desired solution. This solution may have more or
1331  // fewer parts that the desired solution.)
1332 
1333  partId_t lMax=0, lMin=0, gMax, gMin;
1334 
1335  if (len > 0)
1336  IdentifierTraits<partId_t>::minMax(partList.getRawPtr(), len, lMin, lMax);
1337 
1339  lMin, lMax, gMin, gMax);
1340 
1341  nGlobalPartsSolution_ = gMax - gMin + 1;
1342 
1343  if (dataDidNotMove) {
1344  // Case where the algorithm provides the part list in the same order
1345  // that it received the gnos.
1346 
1347  if (idMap_->gnosAreGids()){
1348  gids_ = Teuchos::arcp_reinterpret_cast<const gid_t>(gnoList);
1349  }
1350  else{
1351  gid_t *gidList = new gid_t [len];
1352  env_->localMemoryAssertion(__FILE__, __LINE__, len, gidList);
1353  ArrayView<gid_t> gidView(gidList, len);
1354 
1355  const gno_t *gnos = gnoList.getRawPtr();
1356  ArrayView<gno_t> gnoView(const_cast<gno_t *>(gnos), len);
1357 
1358  try{
1359  idMap_->gidTranslate(gidView, gnoView, TRANSLATE_LIB_TO_APP);
1360  }
1362 
1363  gids_ = arcp<const gid_t>(gidList, 0, len);
1364  }
1365  parts_ = partList;
1366  }
1367  else {
1368  // Case where the algorithm moved the data.
1369  // KDDKDD Should we require the algorithm to return the parts in
1370  // KDDKDD the same order as the gnos provided by the model?
1371 
1372  // We send part information to the process that "owns" the global number.
1373 
1374  ArrayView<gid_t> emptyView;
1375  ArrayView<int> procList;
1376 
1377  if (len){
1378  int *tmp = new int [len];
1379  env_->localMemoryAssertion(__FILE__, __LINE__, len, tmp);
1380  procList = ArrayView<int>(tmp, len);
1381  }
1382 
1383  idMap_->gnoGlobalTranslate (gnoList.view(0,len), emptyView, procList);
1384 
1385  int remotelyOwned = 0;
1386  int rank = comm_->getRank();
1387  int nprocs = comm_->getSize();
1388 
1389  for (size_t i=0; !remotelyOwned && i < len; i++){
1390  if (procList[i] != rank)
1391  remotelyOwned = 1;
1392  }
1393 
1394  int anyRemotelyOwned=0;
1395 
1396  try{
1397  reduceAll<int, int>(*comm_, Teuchos::REDUCE_MAX, 1,
1398  &remotelyOwned, &anyRemotelyOwned);
1399  }
1400  Z2_THROW_OUTSIDE_ERROR(*env_);
1401 
1402  if (anyRemotelyOwned){
1403 
1404  // Send the owners of these gnos their part assignments.
1405 
1406  Array<int> countOutBuf(nprocs, 0);
1407  Array<int> countInBuf(nprocs, 0);
1408 
1409  Array<gno_t> outBuf(len*2, 0);
1410 
1411  if (len > 0){
1412  Array<lno_t> offsetBuf(nprocs+1, 0);
1413 
1414  for (size_t i=0; i < len; i++){
1415  countOutBuf[procList[i]]+=2;
1416  }
1417 
1418  offsetBuf[0] = 0;
1419  for (int i=0; i < nprocs; i++)
1420  offsetBuf[i+1] = offsetBuf[i] + countOutBuf[i];
1421 
1422  for (size_t i=0; i < len; i++){
1423  int p = procList[i];
1424  int off = offsetBuf[p];
1425  outBuf[off] = gnoList[i];
1426  outBuf[off+1] = static_cast<gno_t>(partList[i]);
1427  offsetBuf[p]+=2;
1428  }
1429  }
1430 
1431  ArrayRCP<gno_t> inBuf;
1432 
1433  try{
1434  AlltoAllv<gno_t>(*comm_, *env_,
1435  outBuf(), countOutBuf(), inBuf, countInBuf());
1436  }
1438 
1439  outBuf.clear();
1440  countOutBuf.clear();
1441 
1442  gno_t newLen = 0;
1443  for (int i=0; i < nprocs; i++)
1444  newLen += countInBuf[i];
1445 
1446  countInBuf.clear();
1447 
1448  newLen /= 2;
1449 
1450  ArrayRCP<partId_t> parts;
1451  ArrayRCP<const gno_t> myGnos;
1452 
1453  if (newLen > 0){
1454 
1455  gno_t *tmpGno = new gno_t [newLen];
1456  env_->localMemoryAssertion(__FILE__, __LINE__, newLen, tmpGno);
1457 
1458  partId_t *tmpPart = new partId_t [newLen];
1459  env_->localMemoryAssertion(__FILE__, __LINE__, newLen, tmpPart);
1460 
1461  size_t next = 0;
1462  for (lno_t i=0; i < newLen; i++){
1463  tmpGno[i] = inBuf[next++];
1464  tmpPart[i] = inBuf[next++];
1465  }
1466 
1467  parts = arcp(tmpPart, 0, newLen);
1468  myGnos = arcp(tmpGno, 0, newLen);
1469  }
1470 
1471  gnoList = myGnos;
1472  partList = parts;
1473  len = newLen;
1474  }
1475 
1476  delete [] procList.getRawPtr();
1477 
1478  if (idMap_->gnosAreGids()){
1479  gids_ = Teuchos::arcp_reinterpret_cast<const gid_t>(gnoList);
1480  }
1481  else{
1482  gid_t *gidList = new gid_t [len];
1483  env_->localMemoryAssertion(__FILE__, __LINE__, len, gidList);
1484  ArrayView<gid_t> gidView(gidList, len);
1485 
1486  const gno_t *gnos = gnoList.getRawPtr();
1487  ArrayView<gno_t> gnoView(const_cast<gno_t *>(gnos), len);
1488 
1489  try{
1490  idMap_->gidTranslate(gidView, gnoView, TRANSLATE_LIB_TO_APP);
1491  }
1493 
1494  gids_ = arcp<const gid_t>(gidList, 0, len);
1495  }
1496 
1497  parts_ = partList;
1498  }
1499 
1500  // Now determine which process gets each object, if not one-to-one.
1501 
1502  if (!onePartPerProc_){
1503 
1504  int *procs = new int [len];
1505  env_->localMemoryAssertion(__FILE__, __LINE__, len, procs);
1506  procs_ = arcp<int>(procs, 0, len);
1507 
1508  partId_t *parts = partList.getRawPtr();
1509 
1510  if (procDist_.size() > 0){ // parts are not split across procs
1511 
1512  int procId;
1513  for (size_t i=0; i < len; i++){
1514  partToProcsMap(parts[i], procs[i], procId);
1515  }
1516  }
1517  else{ // harder - we need to split the parts across multiple procs
1518 
1519  lno_t *partCounter = new lno_t [nGlobalPartsSolution_];
1520  env_->localMemoryAssertion(__FILE__, __LINE__, nGlobalPartsSolution_,
1521  partCounter);
1522 
1523  int numProcs = comm_->getSize();
1524 
1525  //MD NOTE: there was no initialization for partCounter.
1526  //I added the line below, correct me if I am wrong.
1527  memset(partCounter, 0, sizeof(lno_t) * nGlobalPartsSolution_);
1528 
1529  for (ArrayRCP<partId_t>::size_type i=0; i < partList.size(); i++)
1530  partCounter[parts[i]]++;
1531 
1532  lno_t *procCounter = new lno_t [numProcs];
1533  env_->localMemoryAssertion(__FILE__, __LINE__, numProcs, procCounter);
1534 
1535  int proc1;
1536  int proc2 = partDist_[0];
1537 
1538  for (partId_t part=1; part < nGlobalParts_; part++){
1539  proc1 = proc2;
1540  proc2 = partDist_[part+1];
1541  int numprocs = proc2 - proc1;
1542 
1543  double dNum = partCounter[part];
1544  double dProcs = numprocs;
1545 
1546  //cout << "dNum:" << dNum << " dProcs:" << dProcs << endl;
1547  double each = floor(dNum/dProcs);
1548  double extra = fmod(dNum,dProcs);
1549 
1550  for (int proc=proc1, i=0; proc<proc2; proc++, i++){
1551  if (i < extra)
1552  procCounter[proc] = lno_t(each) + 1;
1553  else
1554  procCounter[proc] = lno_t(each);
1555  }
1556  }
1557 
1558  delete [] partCounter;
1559 
1560  for (ArrayRCP<partId_t>::size_type i=0; i < partList.size(); i++){
1561  if (partList[i] >= nGlobalParts_){
1562  // Solution has more parts that targeted. These
1563  // objects just remain on this process.
1564  procs[i] = comm_->getRank();
1565  continue;
1566  }
1567  partId_t partNum = parts[i];
1568  proc1 = partDist_[partNum];
1569  proc2 = partDist_[partNum + 1];
1570 
1571  int proc;
1572  for (proc=proc1; proc < proc2; proc++){
1573  if (procCounter[proc] > 0){
1574  procs[i] = proc;
1575  procCounter[proc]--;
1576  break;
1577  }
1578  }
1579  env_->localBugAssertion(__FILE__, __LINE__, "part to proc",
1580  proc < proc2, COMPLEX_ASSERTION);
1581  }
1582 
1583  delete [] procCounter;
1584  }
1585  }
1586 
1587  // Now that parts_ info is back on home process, remap the parts.
1588  // TODO: The parts will be inconsistent with the proc assignments after
1589  // TODO: remapping. This problem will go away after we separate process
1590  // TODO: mapping from setParts. But for MueLu's use case, the part
1591  // TODO: remapping is all that matters; they do not use the process mapping.
1592  int doRemap = 0;
1593  const Teuchos::ParameterEntry *pe =
1594  env_->getParameters().getEntryPtr("remap_parts");
1595  if (pe) doRemap = pe->getValue(&doRemap);
1596  if (doRemap) RemapParts();
1597 
1598  haveSolution_ = true;
1599 
1600  env_->memory("After Solution has processed algorithm's answer");
1601  env_->debug(DETAILED_STATUS, "Exiting setParts");
1602 }
1603 
1604 template <typename Adapter>
1605  template <typename Extra>
1607  int numExtra, ArrayRCP<Extra> &xtraInfo,
1608  ArrayRCP<typename Adapter::gid_t> &imports, // output
1609  ArrayRCP<Extra> &newXtraInfo) const // output
1610 {
1611  env_->localInputAssertion(__FILE__, __LINE__, "no solution yet",
1612  haveSolution_, BASIC_ASSERTION);
1613  env_->debug(DETAILED_STATUS, "Entering convertSolutionToImportList");
1614 
1615  int numProcs = comm_->getSize();
1616  size_t localNumIds = gids_.size();
1617 
1618  // How many to each process?
1619  Array<int> counts(numProcs, 0);
1620  if (onePartPerProc_)
1621  for (size_t i=0; i < localNumIds; i++)
1622  counts[parts_[i]]++;
1623  else
1624  for (size_t i=0; i < localNumIds; i++)
1625  counts[procs_[i]]++;
1626 
1627  Array<gno_t> offsets(numProcs+1, 0);
1628  for (int i=1; i <= numProcs; i++){
1629  offsets[i] = offsets[i-1] + counts[i-1];
1630  }
1631 
1632  Array<gid_t> gidList(localNumIds);
1633  Array<Extra> numericInfo;
1634 
1635  if (numExtra > 0)
1636  numericInfo.resize(localNumIds);
1637 
1638  if (onePartPerProc_){
1639  for (size_t i=0; i < localNumIds; i++){
1640  lno_t idx = offsets[parts_[i]];
1641  gidList[idx] = gids_[i];
1642  if (numExtra > 0)
1643  numericInfo[idx] = xtraInfo[i];
1644  offsets[parts_[i]] = idx + 1;
1645  }
1646  }
1647  else {
1648  for (size_t i=0; i < localNumIds; i++){
1649  lno_t idx = offsets[procs_[i]];
1650  gidList[idx] = gids_[i];
1651  if (numExtra > 0)
1652  numericInfo[idx] = xtraInfo[i];
1653  offsets[procs_[i]] = idx + 1;
1654  }
1655  }
1656 
1657  Array<int> recvCounts(numProcs, 0);
1658 
1659  try{
1660  AlltoAllv<gid_t>(*comm_, *env_, gidList(),
1661  counts(), imports, recvCounts());
1662  }
1663  catch (std::exception &e){
1664  throw std::runtime_error("alltoallv 1");
1665  }
1666 
1667  if (numExtra > 0){
1668  try{
1669  AlltoAllv<Extra>(*comm_, *env_, xtraInfo(),
1670  counts(), newXtraInfo, recvCounts());
1671  }
1672  catch (std::exception &e){
1673  throw std::runtime_error("alltoallv 2");
1674  }
1675  }
1676 
1677  env_->debug(DETAILED_STATUS, "Exiting convertSolutionToImportList");
1678  return imports.size();
1679 }
1680 
1681 template <typename Adapter>
1683  double &numParts, partId_t &partMin, partId_t &partMax) const
1684 {
1685  if (onePartPerProc_){
1686  numParts = 1.0;
1687  partMin = partMax = procId;
1688  }
1689  else if (procDist_.size() > 0){
1690  partMin = procDist_[procId];
1691  partMax = procDist_[procId+1] - 1;
1692  numParts = procDist_[procId+1] - partMin;
1693  }
1694  else{
1695  // find the first p such that partDist_[p] > procId
1696 
1697  std::vector<int>::const_iterator entry;
1698  entry = std::upper_bound(partDist_.begin(), partDist_.end(), procId);
1699 
1700  size_t partIdx = entry - partDist_.begin();
1701  int numProcs = partDist_[partIdx] - partDist_[partIdx-1];
1702  partMin = partMax = int(partIdx) - 1;
1703  numParts = 1.0 / numProcs;
1704  }
1705 }
1706 
1707 template <typename Adapter>
1708  void PartitioningSolution<Adapter>::partToProcsMap(partId_t partId,
1709  int &procMin, int &procMax) const
1710 {
1711  if (partId >= nGlobalParts_){
1712  // setParts() may be given an initial solution which uses a
1713  // different number of parts than the desired solution. It is
1714  // still a solution. We keep it on this process.
1715  procMin = procMax = comm_->getRank();
1716  }
1717  else if (onePartPerProc_){
1718  procMin = procMax = int(partId);
1719  }
1720  else if (procDist_.size() > 0){
1721  if (procDistEquallySpread_) {
1722  // Avoid binary search.
1723  double fProcs = comm_->getSize();
1724  double fParts = nGlobalParts_;
1725  double each = fParts / fProcs;
1726  procMin = int(partId / each);
1727  while (procDist_[procMin] > partId) procMin--;
1728  while (procDist_[procMin+1] <= partId) procMin++;
1729  procMax = procMin;
1730  }
1731  else {
1732  // find the first p such that procDist_[p] > partId.
1733  // For now, do a binary search.
1734 
1735  std::vector<partId_t>::const_iterator entry;
1736  entry = std::upper_bound(procDist_.begin(), procDist_.end(), partId);
1737 
1738  size_t procIdx = entry - procDist_.begin();
1739  procMin = procMax = int(procIdx) - 1;
1740  }
1741  }
1742  else{
1743  procMin = partDist_[partId];
1744  procMax = partDist_[partId+1] - 1;
1745  }
1746 }
1747 
1748 template <typename Adapter>
1750  int c1, int c2) const
1751 {
1752  if (c1 < 0 || c1 >= weightDim_ || c2 < 0 || c2 >= weightDim_ )
1753  throw logic_error("criteriaHaveSamePartSizes error");
1754 
1755  bool theSame = false;
1756 
1757  if (c1 == c2)
1758  theSame = true;
1759 
1760  else if (pSizeUniform_[c1] == true && pSizeUniform_[c2] == true)
1761  theSame = true;
1762 
1763  else if (pCompactIndex_[c1].size() == pCompactIndex_[c2].size()){
1764  theSame = true;
1765  bool useIndex = pCompactIndex_[c1].size() > 0;
1766  if (useIndex){
1767  for (partId_t p=0; theSame && p < nGlobalParts_; p++)
1768  if (pSize_[c1][pCompactIndex_[c1][p]] !=
1769  pSize_[c2][pCompactIndex_[c2][p]])
1770  theSame = false;
1771  }
1772  else{
1773  for (partId_t p=0; theSame && p < nGlobalParts_; p++)
1774  if (pSize_[c1][p] != pSize_[c2][p])
1775  theSame = false;
1776  }
1777  }
1778 
1779  return theSame;
1780 }
1781 
1797 template <typename Adapter>
1799  bool doCheck, bool haveNumLocalParts, bool haveNumGlobalParts,
1800  int numLocalParts, int numGlobalParts)
1801 {
1802  int nprocs = comm_->getSize();
1803  ssize_t reducevals[4];
1804  ssize_t sumHaveGlobal=0, sumHaveLocal=0;
1805  ssize_t sumGlobal=0, sumLocal=0;
1806  ssize_t maxGlobal=0, maxLocal=0;
1807  ssize_t vals[4] = {haveNumGlobalParts, haveNumLocalParts,
1808  numGlobalParts, numLocalParts};
1809 
1810  partDist_.clear();
1811  procDist_.clear();
1812 
1813  if (doCheck){
1814 
1815  try{
1816  reduceAll<int, ssize_t>(*comm_, Teuchos::REDUCE_SUM, 4, vals, reducevals);
1817  }
1818  Z2_THROW_OUTSIDE_ERROR(*env_);
1819 
1820  sumHaveGlobal = reducevals[0];
1821  sumHaveLocal = reducevals[1];
1822  sumGlobal = reducevals[2];
1823  sumLocal = reducevals[3];
1824 
1825  env_->localInputAssertion(__FILE__, __LINE__,
1826  "Either all procs specify num_global/local_parts or none do",
1827  (sumHaveGlobal == 0 || sumHaveGlobal == nprocs) &&
1828  (sumHaveLocal == 0 || sumHaveLocal == nprocs),
1829  BASIC_ASSERTION);
1830  }
1831  else{
1832  if (haveNumLocalParts)
1833  sumLocal = numLocalParts * nprocs;
1834  if (haveNumGlobalParts)
1835  sumGlobal = numGlobalParts * nprocs;
1836 
1837  sumHaveGlobal = haveNumGlobalParts ? nprocs : 0;
1838  sumHaveLocal = haveNumLocalParts ? nprocs : 0;
1839 
1840  maxLocal = numLocalParts;
1841  maxGlobal = numGlobalParts;
1842  }
1843 
1844  if (!haveNumLocalParts && !haveNumGlobalParts){
1845  onePartPerProc_ = true; // default if user did not specify
1846  return;
1847  }
1848 
1849  if (haveNumGlobalParts){
1850  if (doCheck){
1851  vals[0] = numGlobalParts;
1852  vals[1] = numLocalParts;
1853  try{
1854  reduceAll<int, ssize_t>(
1855  *comm_, Teuchos::REDUCE_MAX, 2, vals, reducevals);
1856  }
1857  Z2_THROW_OUTSIDE_ERROR(*env_);
1858 
1859  maxGlobal = reducevals[0];
1860  maxLocal = reducevals[1];
1861 
1862  env_->localInputAssertion(__FILE__, __LINE__,
1863  "Value for num_global_parts is different on different processes.",
1864  maxGlobal * nprocs == sumGlobal, BASIC_ASSERTION);
1865  }
1866 
1867  if (sumLocal){
1868  env_->localInputAssertion(__FILE__, __LINE__,
1869  "Sum of num_local_parts does not equal requested num_global_parts",
1870  sumLocal == numGlobalParts, BASIC_ASSERTION);
1871 
1872  if (sumLocal == nprocs && maxLocal == 1){
1873  onePartPerProc_ = true; // user specified one part per proc
1874  return;
1875  }
1876  }
1877  else{
1878  if (maxGlobal == nprocs){
1879  onePartPerProc_ = true; // user specified num parts is num procs
1880  return;
1881  }
1882  }
1883  }
1884 
1885  // If we are here, we do not have #parts == #procs.
1886 
1887  if (sumHaveLocal == nprocs){
1888  //
1889  // We will go by the number of local parts specified.
1890  //
1891 
1892  try{
1893  procDist_.resize(nprocs+1);
1894  }
1895  catch (std::exception &e){
1896  throw(std::bad_alloc());
1897  }
1898 
1899  int *procArray = &procDist_[0];
1900 
1901  try{
1902  partId_t tmp = partId_t(numLocalParts);
1903  gatherAll<int, partId_t>(*comm_, 1, &tmp, nprocs, procArray + 1);
1904  }
1905  Z2_THROW_OUTSIDE_ERROR(*env_);
1906 
1907  procArray[0] = 0;
1908 
1909  for (int proc=0; proc < nprocs; proc++)
1910  procArray[proc+1] += procArray[proc];
1911  }
1912  else{
1913  //
1914  // We will allocate global number of parts to the processes.
1915  //
1916  double fParts = numGlobalParts;
1917  double fProcs = nprocs;
1918 
1919  if (fParts < fProcs){
1920 
1921  try{
1922  partDist_.resize(size_t(fParts+1));
1923  }
1924  catch (std::exception &e){
1925  throw(std::bad_alloc());
1926  }
1927 
1928  int *partArray = &partDist_[0];
1929 
1930  double each = floor(fProcs / fParts);
1931  double extra = fmod(fProcs, fParts);
1932  partDist_[0] = 0;
1933 
1934  for (partId_t part=0; part < numGlobalParts; part++){
1935  int numOwners = int(each + ((part<extra) ? 1 : 0));
1936  partArray[part+1] = partArray[part] + numOwners;
1937  }
1938 
1939  env_->globalBugAssertion(__FILE__, __LINE__, "#parts != #procs",
1940  partDist_[numGlobalParts] == nprocs, COMPLEX_ASSERTION, comm_);
1941  }
1942  else if (fParts > fProcs){
1943 
1944  // User did not specify local number of parts per proc;
1945  // Distribute the parts evenly among the procs.
1946 
1947  procDistEquallySpread_ = true;
1948 
1949  try{
1950  procDist_.resize(size_t(fProcs+1));
1951  }
1952  catch (std::exception &e){
1953  throw(std::bad_alloc());
1954  }
1955 
1956  int *procArray = &procDist_[0];
1957 
1958  double each = floor(fParts / fProcs);
1959  double extra = fmod(fParts, fProcs);
1960  procArray[0] = 0;
1961 
1962  for (int proc=0; proc < nprocs; proc++){
1963  partId_t numParts = partId_t(each + ((proc<extra) ? 1 : 0));
1964  procArray[proc+1] = procArray[proc] + numParts;
1965  }
1966 
1967  env_->globalBugAssertion(__FILE__, __LINE__, "#parts != #procs",
1968  procDist_[nprocs] == numGlobalParts, COMPLEX_ASSERTION, comm_);
1969  }
1970  else{
1971  env_->globalBugAssertion(__FILE__, __LINE__,
1972  "should never get here", 1, COMPLEX_ASSERTION, comm_);
1973  }
1974  }
1975 }
1976 
1978 // Remap a new part assignment vector for maximum overlap with an input
1979 // part assignment.
1980 //
1981 // Assumptions for this version:
1982 // input part assignment == processor rank for every local object.
1983 // assuming nGlobalParts_ <= num ranks
1984 // TODO: Write a version that takes the input part number as input;
1985 // this change requires input parts in adapters to be provided in
1986 // the Adapter.
1987 // TODO: For repartitioning, compare to old remapping results; see Zoltan1.
1988 
1989 template <typename Adapter>
1991 {
1992  size_t len = parts_.size();
1993 
1994  partId_t me = comm_->getRank();
1995  int np = comm_->getSize();
1996 
1997  if (np < nGlobalParts_) {
1998  if (me == 0)
1999  cout << "Remapping not yet supported for "
2000  << "num_global_parts " << nGlobalParts_
2001  << " > num procs " << np << endl;
2002  return;
2003  }
2004  // Build edges of a bipartite graph with np + nGlobalParts_ vertices,
2005  // and edges between a process vtx and any parts to which that process'
2006  // objects are assigned.
2007  // Weight edge[parts_[i]] by the number of objects that are going from
2008  // this rank to parts_[i].
2009  // We use a std::map, assuming the number of unique parts in the parts_ array
2010  // is small to keep the binary search efficient.
2011  // TODO We use the count of objects to move; should change to SIZE of objects
2012  // to move; need SIZE function in Adapter.
2013 
2014  std::map<partId_t, long> edges;
2015  long lstaying = 0; // Total num of local objects staying if we keep the
2016  // current mapping. TODO: change to SIZE of local objs
2017  long gstaying = 0; // Total num of objects staying in the current partition
2018 
2019  for (size_t i = 0; i < len; i++) {
2020  edges[parts_[i]]++; // TODO Use obj size instead of count
2021  if (parts_[i] == me) lstaying++; // TODO Use obj size instead of count
2022  }
2023 
2024  Teuchos::reduceAll<int, long>(*comm_, Teuchos::REDUCE_SUM, 1,
2025  &lstaying, &gstaying);
2026 //TODO if (gstaying == Adapter::getGlobalNumObjs()) return; // Nothing to do
2027 
2028  partId_t *remap = NULL;
2029 
2030  int nedges = edges.size();
2031 
2032  // Gather the graph to rank 0.
2033  partId_t tnVtx = np + nGlobalParts_; // total # vertices
2034  int *idx = NULL; // Pointer index into graph adjacencies
2035  int *sizes = NULL; // nedges per rank
2036  if (me == 0) {
2037  idx = new int[tnVtx+1];
2038  sizes = new int[np];
2039  }
2040  if (np > 1)
2041  Teuchos::gather<int, int>(&nedges, 1, sizes, 1, 0, *comm_);
2042  else
2043  sizes[0] = nedges;
2044 
2045  // prefix sum to build the idx array
2046  if (me == 0) {
2047  idx[0] = 0;
2048  for (int i = 0; i < np; i++)
2049  idx[i+1] = idx[i] + sizes[i];
2050  }
2051 
2052  // prepare to send edges
2053  int cnt = 0;
2054  partId_t *bufv = NULL;
2055  long *bufw = NULL;
2056  if (nedges) {
2057  bufv = new partId_t[nedges];
2058  bufw = new long[nedges];
2059  // Create buffer with edges (me, part[i]) and weight edges[parts_[i]].
2060  for (std::map<partId_t, long>::iterator it = edges.begin();
2061  it != edges.end(); it++) {
2062  bufv[cnt] = it->first; // target part
2063  bufw[cnt] = it->second; // weight
2064  cnt++;
2065  }
2066  }
2067 
2068  // Prepare to receive edges on rank 0
2069  partId_t *adj = NULL;
2070  long *wgt = NULL;
2071  if (me == 0) {
2072 //SYM adj = new partId_t[2*idx[np]]; // need 2x space to symmetrize later
2073 //SYM wgt = new long[2*idx[np]]; // need 2x space to symmetrize later
2074  adj = new partId_t[idx[np]];
2075  wgt = new long[idx[np]];
2076  }
2077 
2078  Teuchos::gatherv<int, partId_t>(bufv, cnt, adj, sizes, idx, 0, *comm_);
2079  Teuchos::gatherv<int, long>(bufw, cnt, wgt, sizes, idx, 0, *comm_);
2080  delete [] bufv;
2081  delete [] bufw;
2082 
2083  // Now have constructed graph on rank 0.
2084  // Call the matching algorithm
2085 
2086  int doRemap;
2087  if (me == 0) {
2088  // We have the "LHS" vertices of the bipartite graph; need to create
2089  // "RHS" vertices.
2090  for (int i = 0; i < idx[np]; i++) {
2091  adj[i] += np; // New RHS vertex number; offset by num LHS vertices
2092  }
2093 
2094  // Build idx for RHS vertices
2095  for (partId_t i = np; i < tnVtx; i++) {
2096  idx[i+1] = idx[i]; // No edges for RHS vertices
2097  }
2098 
2099 #ifdef KDDKDD_DEBUG
2100  cout << "IDX ";
2101  for (partId_t i = 0; i <= tnVtx; i++) cout << idx[i] << " ";
2102  cout << endl;
2103 
2104  cout << "ADJ ";
2105  for (partId_t i = 0; i < idx[tnVtx]; i++) cout << adj[i] << " ";
2106  cout << endl;
2107 
2108  cout << "WGT ";
2109  for (partId_t i = 0; i < idx[tnVtx]; i++) cout << wgt[i] << " ";
2110  cout << endl;
2111 #endif
2112 
2113  // Perform matching on the graph
2114  partId_t *match = new partId_t[tnVtx];
2115  for (partId_t i = 0; i < tnVtx; i++) match[i] = i;
2116  partId_t nmatches =
2117  Zoltan2::GreedyMWM<partId_t, long>(idx, adj, wgt, tnVtx, match);
2118 
2119 #ifdef KDDKDD_DEBUG
2120  cout << "After matching: " << nmatches << " found" << endl;
2121  for (partId_t i = 0; i < tnVtx; i++)
2122  cout << "match[" << i << "] = " << match[i]
2123  << ((match[i] != i &&
2124  (i < np && match[i] != i+np))
2125  ? " *" : " ")
2126  << endl;
2127 #endif
2128 
2129  // See whether there were nontrivial changes in the matching.
2130  bool nontrivial = false;
2131  if (nmatches) {
2132  for (partId_t i = 0; i < np; i++) {
2133  if ((match[i] != i) && (match[i] != (i+np))) {
2134  nontrivial = true;
2135  break;
2136  }
2137  }
2138  }
2139 
2140  // Process the matches
2141  if (nontrivial) {
2142  remap = new partId_t[nGlobalParts_];
2143  for (partId_t i = 0; i < nGlobalParts_; i++) remap[i] = -1;
2144 
2145  bool *used = new bool[nGlobalParts_];
2146  for (partId_t i = 0; i < nGlobalParts_; i++) used[i] = false;
2147 
2148  // First, process all matched parts
2149  for (partId_t i = 0; i < nGlobalParts_; i++) {
2150  partId_t tmp = i + np;
2151  if (match[tmp] != tmp) {
2152  remap[i] = match[tmp];
2153  used[match[tmp]] = true;
2154  }
2155  }
2156 
2157  // Second, process unmatched parts; keep same part number if possible
2158  for (partId_t i = 0; i < nGlobalParts_; i++) {
2159  if (remap[i] > -1) continue;
2160  if (!used[i]) {
2161  remap[i] = i;
2162  used[i] = true;
2163  }
2164  }
2165 
2166  // Third, process unmatched parts; give them the next unused part
2167  for (partId_t i = 0, uidx = 0; i < nGlobalParts_; i++) {
2168  if (remap[i] > -1) continue;
2169  while (used[uidx]) uidx++;
2170  remap[i] = uidx;
2171  used[uidx] = true;
2172  }
2173  delete [] used;
2174  }
2175  delete [] match;
2176 
2177 #ifdef KDDKDD_DEBUG
2178  cout << "Remap vector: ";
2179  for (partId_t i = 0; i < nGlobalParts_; i++) cout << remap[i] << " ";
2180  cout << endl;
2181 #endif
2182 
2183  long newgstaying = Zoltan2::measure_stays(remap, idx, adj, wgt,
2184  nGlobalParts_, np);
2185  doRemap = (newgstaying > gstaying);
2186  cout << "gstaying " << gstaying << " measure(input) "
2187  << Zoltan2::measure_stays(NULL, idx, adj, wgt,
2188  nGlobalParts_, np)
2189  << " newgstaying " << newgstaying
2190  << " nontrivial " << nontrivial
2191  << " doRemap " << doRemap << endl;
2192  }
2193  delete [] idx;
2194  delete [] sizes;
2195  delete [] adj;
2196  delete [] wgt;
2197 
2198  Teuchos::broadcast<int, int>(*comm_, 0, 1, &doRemap);
2199 
2200  if (doRemap) {
2201  if (me != 0) remap = new partId_t[nGlobalParts_];
2202  Teuchos::broadcast<int, partId_t>(*comm_, 0, nGlobalParts_, remap);
2203  for (size_t i = 0; i < len; i++) {
2204  parts_[i] = remap[parts_[i]];
2205  }
2206  }
2207 
2208  delete [] remap; // TODO May want to keep for repartitioning as in Zoltan
2209 }
2210 
2211 
2212 } // namespace Zoltan2
2213 
2214 #endif
An IdentifierMap manages a global space of object identifiers.
scalar_t getCriteriaPartSize(int idx, partId_t part) const
Get the size for a given weight dimension and a given part.
Defines the IdentifierMap class.
GridHash Class, Hashing Class for part boxes.
const int * getProcList() const
Returns the process list corresponding to the global ID list.
static void globalMinMax(const Comm< int > &comm, bool flag, T localMin, T localMax, T &globalMin, T &globalMax)
Find global minimum and maximum.
static void minMax(const T *values, size_t numValues, T &min, T &max)
Return the minimum and maximum of a list of values.
Defines the Solution base class.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
convert internal ids to user ids
void RemapParts()
Remap a new partition for maximum overlap with an input partition.
Just a placeholder for now.
more involved, like validate a graph
tuple id
Definition: xml2dox.py:189
size_t getLocalNumberOfParts() const
Returns the number of parts to be assigned to this process.
zoltan2_partId_t partId_t
void setParts(ArrayRCP< const gno_t > &gnoList, ArrayRCP< partId_t > &partList, bool dataDidNotMove)
The algorithm uses setParts to set the solution.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
void reduce(const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
Implement Teuchos::ValueTypeReductionOp interface.
zoltan2_partId_t partId_t
sub-steps, each method&#39;s entry and exit
coordinateModelPartBox Class, represents the boundaries of the box which is a result of a geometric p...
const partId_t * getProcDistribution() const
Return a distribution by process.
bool oneToOnePartDistribution() const
Is the part-to-process distribution is one-to-one.
size_t convertSolutionToImportList(int numExtra, ArrayRCP< Extra > &xtraInfo, ArrayRCP< typename Adapter::gid_t > &imports, ArrayRCP< Extra > &newXtraInfo) const
Create an import list from the export list.
scalar_t getLocalFractionOfPart() const
If parts are divided across processes, return the fraction of a part on this process.
int zoltan2_partId_t
a data type for part IDs
dictionary vals
Definition: xml2dox.py:186
const zoltan2_partId_t * getPartList() const
Returns the part list corresponding to the global ID list.
A PartitioningSolution is a solution to a partitioning problem.
const RCP< const Comm< int > > & getCommunicator() const
Return the communicator associated with the solution.
void getPartsForProc(int procId, double &numParts, partId_t &partMin, partId_t &partMax) const
Get the parts belonging to a process.
Zoltan2_BoxBoundaries is a reduction operation to all reduce the all box boundaries.
RCP< vector< Zoltan2::coordinateModelPartBox< scalar_t, partId_t > > > getGlobalBoxBoundaries(const Teuchos::Comm< int > *comm)
Greedy Maximal Weight Matching.
double scalar_t
void getProcsForPart(partId_t partId, int &procMin, int &procMax) const
Get the processes containing a part.
void setPartBoxes(RCP< vector< Zoltan2::coordinateModelPartBox< scalar_t, partId_t > > > outPartBoxes)
set the Part Box boundaries as a result of geometric partitioning algorithm.
const gid_t * getIdList() const
Returns the user&#39;s global ID list.
RCP< vector< Zoltan2::coordinateModelPartBox< scalar_t, partId_t > > > getPartBoxes()
returns the part box boundary list.
long measure_stays(partId_t *remap, int *idx, partId_t *adj, long *wgt, partId_t nrhs, partId_t nlhs)
int getNumberOfCriteria() const
Get the number of criteria (the weight dimension).
const int * getPartDistribution() const
Return a distribution by part.
#define Z2_THROW_OUTSIDE_ERROR(env)
Throw an error returned from outside the Zoltan2 library.
size_t getActualGlobalNumberOfParts() const
Returns the actual global number of parts provided in setParts().
void getCommunicationGraph(const Teuchos::Comm< int > *comm, ArrayRCP< partId_t > &comXAdj, ArrayRCP< partId_t > &comAdj)
returns the communication graph as a result of geometric partitioning algorithm.
bool criteriaHasUniformPartSizes(int idx) const
Determine if balancing criteria (weight dimension) has uniform part sizes. (User can specify differin...
bool criteriaHaveSamePartSizes(int c1, int c2) const
Return true if the two weight dimensions have the same part size information.
PartitioningSolution(RCP< const Environment > &env, RCP< const Comm< int > > &comm, RCP< const IdentifierMap< user_t > > &idMap, int userWeightDim)
Constructor when part sizes are not supplied.
size_t getTargetGlobalNumberOfParts() const
Returns the global number of parts desired in the solution.
void getAdjArrays(ArrayRCP< partId_t > &comXAdj_, ArrayRCP< partId_t > &comAdj_)
GridHash Class, returns the adj arrays.
size_t getLocalNumberOfIds() const
Returns the local number of Ids.