50 #ifndef _ZOLTAN2_PARTITIONINGSOLUTION_HPP_
51 #define _ZOLTAN2_PARTITIONINGSOLUTION_HPP_
68 template <
typename Ordinal,
typename T>
87 size(s_), _EPSILON (std::numeric_limits<T>::epsilon()){}
91 void reduce(
const Ordinal count,
const T inBuffer[], T inoutBuffer[])
const
93 for (Ordinal i=0; i < count; i++){
94 if (
Z2_ABS(inBuffer[i]) > _EPSILON){
95 inoutBuffer[i] = inBuffer[i];
122 template <
typename Adapter>
127 #ifndef DOXYGEN_SHOULD_SKIP_THIS
131 typedef typename Adapter::gid_t gid_t;
153 RCP<
const Comm<int> > &comm,
187 RCP<
const Comm<int> > &comm,
189 int userWeightDim, ArrayView<ArrayRCP<partId_t> > reqPartIds,
190 ArrayView<ArrayRCP<scalar_t> > reqPartSizes);
247 if (partDist_.size() > 0)
return &partDist_[0];
268 if (procDist_.size() > 0)
return &procDist_[0];
299 if (pSizeUniform_[idx])
300 return 1.0 / nGlobalParts_;
301 else if (pCompactIndex_[idx].size())
302 return pSize_[idx][pCompactIndex_[idx][part]];
304 return pSize_[idx][part];
351 void setParts(ArrayRCP<const gno_t> &gnoList,
352 ArrayRCP<partId_t> &partList,
bool dataDidNotMove);
385 const gid_t *
getIdList()
const {
return gids_.getRawPtr(); }
401 this->partBoxes = outPartBoxes;
406 RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > >
getPartBoxes(){
407 return this->partBoxes;
413 const Teuchos::Comm<int> *comm,
414 ArrayRCP <partId_t> &comXAdj,
415 ArrayRCP <partId_t> &comAdj
418 if(comXAdj_.getRawPtr() == NULL && comAdj_.getRawPtr() == NULL){
424 RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > pBoxes = this->
getGlobalBoxBoundaries(comm);
425 int dim = (*pBoxes)[0].getDim();
438 const Teuchos::Comm<int> *comm){
445 RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > pBoxes = this->
getPartBoxes();
447 int dim = (*pBoxes)[0].getDim();
452 memset(localPartBoundaries, 0,
sizeof(
scalar_t) * ntasks * 2 *dim);
455 memset(globalPartBoundaries, 0,
sizeof(
scalar_t) * ntasks * 2 *dim);
457 scalar_t *localPartMins = localPartBoundaries;
458 scalar_t *localPartMaxs = localPartBoundaries + ntasks * dim;
460 scalar_t *globalPartMins = globalPartBoundaries;
461 scalar_t *globalPartMaxs = globalPartBoundaries + ntasks * dim;
464 for (
partId_t i = 0; i < boxCount; ++i){
465 partId_t pId = (*pBoxes)[i].getpId();
468 scalar_t *lmins = (*pBoxes)[i].getlmins();
469 scalar_t *lmaxs = (*pBoxes)[i].getlmaxs();
471 for (
int j = 0; j < dim; ++j){
472 localPartMins[dim * pId + j] = lmins[j];
473 localPartMaxs[dim * pId + j] = lmaxs[j];
485 reduceAll<int, scalar_t>(*comm, reductionOp,
486 ntasks * 2 *dim, localPartBoundaries, globalPartBoundaries
489 for (
partId_t i = 0; i < ntasks; ++i){
493 globalPartMins + dim * i,
494 globalPartMaxs + dim * i);
506 delete []localPartBoundaries;
507 delete []globalPartBoundaries;
509 this->partBoxes = pB;
510 return this->partBoxes;
541 template <
typename Extra>
544 ArrayRCP<Extra> &xtraInfo,
545 ArrayRCP<typename Adapter::gid_t> &imports,
546 ArrayRCP<Extra> &newXtraInfo)
const;
568 env_->localInputAssertion(__FILE__, __LINE__,
"invalid process id",
571 procToPartsMap(procId, numParts, partMin, partMax);
587 env_->localInputAssertion(__FILE__, __LINE__,
"invalid part id",
590 partToProcsMap(partId, procMin, procMax);
594 void partToProc(
bool doCheck,
bool haveNumLocalParts,
bool haveNumGlobalParts,
595 int numLocalParts,
int numGlobalParts);
597 void procToPartsMap(
int procId,
double &numParts,
partId_t &partMin,
600 void partToProcsMap(
partId_t partId,
int &procMin,
int &procMax)
const;
602 void setPartDistribution();
604 void setPartSizes(ArrayView<ArrayRCP<partId_t> > reqPartIds,
605 ArrayView<ArrayRCP<scalar_t> > reqPartSizes);
607 void computePartSizes(
int wdim, ArrayView<partId_t> ids,
608 ArrayView<scalar_t> sizes);
610 void broadcastPartSizes(
int wdim);
613 RCP<const Environment> env_;
614 RCP<const Comm<int> > comm_;
615 RCP<const IdentifierMap<user_t> > idMap_;
618 RCP < vector <Zoltan2::coordinateModelPartBox <scalar_t, partId_t> > > partBoxes;
619 ArrayRCP <partId_t> comXAdj_;
620 ArrayRCP <partId_t> comAdj_;
657 bool onePartPerProc_;
658 std::vector<int> partDist_;
659 std::vector<partId_t> procDist_;
660 bool procDistEquallySpread_;
696 ArrayRCP<bool> pSizeUniform_;
697 ArrayRCP<ArrayRCP<unsigned char> > pCompactIndex_;
698 ArrayRCP<ArrayRCP<scalar_t> > pSize_;
703 ArrayRCP<const gid_t> gids_;
704 ArrayRCP<partId_t> parts_;
714 ArrayRCP<int> procs_;
721 template <
typename Adapter>
723 RCP<const Environment> &env,
724 RCP<
const Comm<int> > &comm,
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),
736 weightDim_ = (userWeightDim ? userWeightDim : 1);
738 setPartDistribution();
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);
748 setPartSizes(ids.view(0, weightDim_), sizes.view(0, weightDim_));
750 env_->memory(
"After construction of solution");
753 template <
typename Adapter>
755 RCP<const Environment> &env,
756 RCP<
const Comm<int> > &comm,
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),
770 weightDim_ = (userWeightDim ? userWeightDim : 1);
772 setPartDistribution();
774 setPartSizes(reqPartIds, reqPartSizes);
776 env_->memory(
"After construction of solution");
779 template <
typename Adapter>
784 const ParameterList &pl = env_->getParameters();
785 size_t haveGlobalNumParts=0, haveLocalNumParts=0;
786 int numLocal=0, numGlobal=0;
789 const Teuchos::ParameterEntry *pe = pl.getEntryPtr(
"num_global_parts");
792 val = pe->getValue<
double>(&val);
793 haveGlobalNumParts = 1;
794 numGlobal =
static_cast<int>(val);
795 nGlobalParts_ =
partId_t(numGlobal);
798 pe = pl.getEntryPtr(
"num_local_parts");
801 val = pe->getValue<
double>(&val);
802 haveLocalNumParts = 1;
803 numLocal =
static_cast<int>(val);
810 partToProc(
true, haveLocalNumParts, haveGlobalNumParts,
811 numLocal, numGlobal);
815 int nprocs = comm_->getSize();
816 int rank = comm_->getRank();
818 if (onePartPerProc_){
819 nGlobalParts_ = nprocs;
822 else if (partDist_.size() > 0){
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;
830 localFraction_ = 1.0 / numOwners;
836 else if (procDist_.size() > 0){
837 nGlobalParts_ = procDist_[nprocs];
838 nLocalParts_ = procDist_[rank+1] - procDist_[rank];
841 throw logic_error(
"partToProc error");
846 template <
typename Adapter>
847 void PartitioningSolution<Adapter>::setPartSizes(
848 ArrayView<ArrayRCP<partId_t> > ids, ArrayView<ArrayRCP<scalar_t> > sizes)
850 int wdim = weightDim_;
853 size_t *countBuf =
new size_t [wdim*2];
854 ArrayRCP<size_t> counts(countBuf, 0, wdim*2,
true);
856 fail = ((ids.size() != wdim) || (sizes.size() != wdim));
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;
863 env_->globalBugAssertion(__FILE__, __LINE__,
"bad argument arrays", fail==0,
868 ArrayRCP<scalar_t> *emptySizes=
new ArrayRCP<scalar_t> [wdim];
869 pSize_ = arcp(emptySizes, 0, wdim);
871 ArrayRCP<unsigned char> *emptyIndices=
new ArrayRCP<unsigned char> [wdim];
872 pCompactIndex_ = arcp(emptyIndices, 0, wdim);
874 bool *info =
new bool [wdim];
875 pSizeUniform_ = arcp(info, 0, wdim);
876 for (
int w=0; w < wdim; w++)
877 pSizeUniform_[w] =
true;
879 if (nGlobalParts_ == 1){
883 size_t *ptr1 = counts.getRawPtr();
884 size_t *ptr2 = counts.getRawPtr() + wdim;
887 reduceAll<int, size_t>(*comm_, Teuchos::REDUCE_MAX, wdim, ptr1, ptr2);
893 for (
int w=0; w < wdim; w++)
894 if (counts[wdim+w] > 0){
896 pSizeUniform_[w] =
false;
905 int nprocs = comm_->getSize();
906 int rank = comm_->getRank();
908 for (
int w=0; w < wdim; w++){
909 if (pSizeUniform_[w])
continue;
916 Teuchos::gatherAll<int, partId_t>(*comm_, 1, &length,
921 for (
int i=0; i < nprocs; i++)
922 total += allLength[i];
927 ArrayView<partId_t> idArray(partNums, total);
928 ArrayView<scalar_t> sizeArray(partSizes, total);
931 for (
int i=0; i < length; i++){
932 *partNums++ = ids[w][i];
933 *partSizes++ = sizes[w][i];
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];
951 computePartSizes(w, idArray, sizeArray);
955 delete [] idArray.getRawPtr();
956 delete [] sizeArray.getRawPtr();
961 Teuchos::send<int, partId_t>(*comm_, length, ids[w].getRawPtr(), 0);
962 Teuchos::send<int, scalar_t>(*comm_, length, sizes[w].getRawPtr(), 0);
966 broadcastPartSizes(w);
970 template <
typename Adapter>
971 void PartitioningSolution<Adapter>::broadcastPartSizes(
int wdim)
973 env_->localBugAssertion(__FILE__, __LINE__,
"preallocations",
974 pSize_.size()>wdim &&
975 pSizeUniform_.size()>wdim && pCompactIndex_.size()>wdim,
978 int rank = comm_->getRank();
979 int nprocs = comm_->getSize();
988 if (pSizeUniform_[wdim] ==
true)
990 else if (pCompactIndex_[wdim].size() > 0)
997 Teuchos::broadcast<int, char>(*comm_, 0, 1, &flag);
1003 pSizeUniform_[wdim] =
true;
1012 unsigned char *idxbuf = NULL;
1015 idxbuf =
new unsigned char [nparts];
1016 env_->localMemoryAssertion(__FILE__, __LINE__, nparts, idxbuf);
1019 env_->localBugAssertion(__FILE__, __LINE__,
"index list size",
1021 idxbuf = pCompactIndex_[wdim].getRawPtr();
1026 Teuchos::broadcast<int, char>(*comm_, 0, nparts,
1027 reinterpret_cast<char *
>(idxbuf));
1032 pCompactIndex_[wdim] = arcp(idxbuf, 0, nparts,
true);
1036 unsigned char maxIdx=0;
1037 for (
partId_t p=0; p < nparts; p++)
1038 if (idxbuf[p] > maxIdx) maxIdx = idxbuf[p];
1040 int numSizes = maxIdx + 1;
1045 sizeList =
new scalar_t [numSizes];
1046 env_->localMemoryAssertion(__FILE__, __LINE__, numSizes, sizeList);
1049 env_->localBugAssertion(__FILE__, __LINE__,
"wrong number of sizes",
1052 sizeList = pSize_[wdim].getRawPtr();
1056 Teuchos::broadcast<int, scalar_t>(*comm_, 0, numSizes, sizeList);
1061 pSize_[wdim] = arcp(sizeList, 0, numSizes,
true);
1074 env_->localMemoryAssertion(__FILE__, __LINE__, nparts, sizeList);
1077 env_->localBugAssertion(__FILE__, __LINE__,
"wrong number of sizes",
1080 sizeList = pSize_[wdim].getRawPtr();
1084 Teuchos::broadcast<int, scalar_t >(*comm_, 0, nparts, sizeList);
1089 pSize_[wdim] = arcp(sizeList, 0, nparts);
1095 template <
typename Adapter>
1096 void PartitioningSolution<Adapter>::computePartSizes(
int wdim,
1097 ArrayView<partId_t> ids, ArrayView<scalar_t> sizes)
1099 int len = ids.size();
1102 pSizeUniform_[wdim] =
true;
1106 env_->localBugAssertion(__FILE__, __LINE__,
"bad array sizes",
1109 env_->localBugAssertion(__FILE__, __LINE__,
"bad index",
1112 env_->localBugAssertion(__FILE__, __LINE__,
"preallocations",
1113 pSize_.size()>wdim &&
1114 pSizeUniform_.size()>wdim && pCompactIndex_.size()>wdim,
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);
1127 scalar_t min=sizes[0], max=sizes[0], sum=0;
1129 for (
int i=0; i < len; i++){
1133 env_->localInputAssertion(__FILE__, __LINE__,
"invalid part id",
1136 env_->localInputAssertion(__FILE__, __LINE__,
"invalid part size", size>=0,
1143 env_->localInputAssertion(__FILE__, __LINE__,
1144 "multiple sizes provided for one part", partIdx[
id]==0,
BASIC_ASSERTION);
1148 if (size < min) min = size;
1149 if (size > max) max = size;
1159 env_->localMemoryAssertion(__FILE__, __LINE__, 2, allSizes);
1161 ArrayRCP<scalar_t> sizeArray(allSizes, 0, 2,
true);
1163 int numNonZero = nparts - len;
1166 allSizes[1] = 1.0 / numNonZero;
1168 for (
partId_t p=0; p < nparts; p++)
1171 for (
int i=0; i < len; i++)
1174 pSize_[wdim] = sizeArray;
1175 pCompactIndex_[wdim] = partIdx;
1180 if (max - min <= epsilon){
1181 pSizeUniform_[wdim] =
true;
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);
1198 std::sort(partSizes.begin(), partSizes.end());
1202 Array<scalar_t> nextUniqueSize;
1203 nextUniqueSize.push_back(partSizes[len-1]);
1206 bool haveAvg =
false;
1207 if (curr - avg <= epsilon)
1210 for (
int i=len-2; i >= 0; i--){
1212 if (curr - val > epsilon){
1213 nextUniqueSize.push_back(val);
1215 if (avgIndex==len && val > avg && val - avg <= epsilon){
1217 avgIndex = nextUniqueSize.size() - 1;
1225 size_t numSizes = nextUniqueSize.size();
1226 int sizeArrayLen = numSizes;
1233 if (!haveAvg) sizeArrayLen++;
1236 env_->localMemoryAssertion(__FILE__, __LINE__, sizeArrayLen, allSizes);
1237 ArrayRCP<scalar_t> sizeArray(allSizes, 0, sizeArrayLen,
true);
1239 int newAvgIndex = sizeArrayLen;
1241 for (
int i=numSizes-1, idx=0; i >= 0; i--){
1243 if (newAvgIndex == sizeArrayLen){
1245 if (haveAvg && i==avgIndex)
1248 else if (!haveAvg && avg < nextUniqueSize[i]){
1250 allSizes[idx++] = avg;
1254 allSizes[idx++] = nextUniqueSize[i];
1257 env_->localBugAssertion(__FILE__, __LINE__,
"finding average in list",
1260 for (
int i=0; i < nparts; i++){
1261 buf[i] = newAvgIndex;
1264 sum = (nparts - len) * allSizes[newAvgIndex];
1266 for (
int i=0; i < len; i++){
1273 if (size < avg && avg - size <= epsilon)
1274 index = newAvgIndex;
1276 typename ArrayRCP<scalar_t>::iterator found =
1277 std::lower_bound(sizeArray.begin(), sizeArray.end(), size);
1279 env_->localBugAssertion(__FILE__, __LINE__,
"size array",
1282 index = found - sizeArray.begin();
1286 sum += allSizes[index];
1289 for (
int i=0; i < sizeArrayLen; i++){
1290 sizeArray[i] /= sum;
1293 pCompactIndex_[wdim] = partIdx;
1294 pSize_[wdim] = sizeArray;
1301 env_->localMemoryAssertion(__FILE__, __LINE__, nparts, tmp);
1303 sum += ((nparts - len) * avg);
1305 for (
int i=0; i < nparts; i++){
1309 for (
int i=0; i < len; i++){
1310 tmp[ids[i]] = sizes[i]/sum;
1313 pSize_[wdim] = arcp(tmp, 0, nparts);
1318 template <
typename Adapter>
1320 ArrayRCP<const gno_t> &gnoList, ArrayRCP<partId_t> &partList,
1321 bool dataDidNotMove)
1325 size_t len = partList.size();
1333 partId_t lMax=0, lMin=0, gMax, gMin;
1339 lMin, lMax, gMin, gMax);
1341 nGlobalPartsSolution_ = gMax - gMin + 1;
1343 if (dataDidNotMove) {
1347 if (idMap_->gnosAreGids()){
1348 gids_ = Teuchos::arcp_reinterpret_cast<
const gid_t>(gnoList);
1351 gid_t *gidList =
new gid_t [len];
1352 env_->localMemoryAssertion(__FILE__, __LINE__, len, gidList);
1353 ArrayView<gid_t> gidView(gidList, len);
1355 const gno_t *gnos = gnoList.getRawPtr();
1356 ArrayView<gno_t> gnoView(const_cast<gno_t *>(gnos), len);
1363 gids_ = arcp<const gid_t>(gidList, 0, len);
1374 ArrayView<gid_t> emptyView;
1375 ArrayView<int> procList;
1378 int *tmp =
new int [len];
1379 env_->localMemoryAssertion(__FILE__, __LINE__, len, tmp);
1380 procList = ArrayView<int>(tmp, len);
1383 idMap_->gnoGlobalTranslate (gnoList.view(0,len), emptyView, procList);
1385 int remotelyOwned = 0;
1386 int rank = comm_->getRank();
1387 int nprocs = comm_->getSize();
1389 for (
size_t i=0; !remotelyOwned && i < len; i++){
1390 if (procList[i] != rank)
1394 int anyRemotelyOwned=0;
1397 reduceAll<int, int>(*comm_, Teuchos::REDUCE_MAX, 1,
1398 &remotelyOwned, &anyRemotelyOwned);
1402 if (anyRemotelyOwned){
1406 Array<int> countOutBuf(nprocs, 0);
1407 Array<int> countInBuf(nprocs, 0);
1409 Array<gno_t> outBuf(len*2, 0);
1412 Array<lno_t> offsetBuf(nprocs+1, 0);
1414 for (
size_t i=0; i < len; i++){
1415 countOutBuf[procList[i]]+=2;
1419 for (
int i=0; i < nprocs; i++)
1420 offsetBuf[i+1] = offsetBuf[i] + countOutBuf[i];
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]);
1431 ArrayRCP<gno_t> inBuf;
1434 AlltoAllv<gno_t>(*comm_, *env_,
1435 outBuf(), countOutBuf(), inBuf, countInBuf());
1440 countOutBuf.clear();
1443 for (
int i=0; i < nprocs; i++)
1444 newLen += countInBuf[i];
1450 ArrayRCP<partId_t> parts;
1451 ArrayRCP<const gno_t> myGnos;
1456 env_->localMemoryAssertion(__FILE__, __LINE__, newLen, tmpGno);
1459 env_->localMemoryAssertion(__FILE__, __LINE__, newLen, tmpPart);
1462 for (
lno_t i=0; i < newLen; i++){
1463 tmpGno[i] = inBuf[next++];
1464 tmpPart[i] = inBuf[next++];
1467 parts = arcp(tmpPart, 0, newLen);
1468 myGnos = arcp(tmpGno, 0, newLen);
1476 delete [] procList.getRawPtr();
1478 if (idMap_->gnosAreGids()){
1479 gids_ = Teuchos::arcp_reinterpret_cast<
const gid_t>(gnoList);
1482 gid_t *gidList =
new gid_t [len];
1483 env_->localMemoryAssertion(__FILE__, __LINE__, len, gidList);
1484 ArrayView<gid_t> gidView(gidList, len);
1486 const gno_t *gnos = gnoList.getRawPtr();
1487 ArrayView<gno_t> gnoView(const_cast<gno_t *>(gnos), len);
1494 gids_ = arcp<const gid_t>(gidList, 0, len);
1502 if (!onePartPerProc_){
1504 int *procs =
new int [len];
1505 env_->localMemoryAssertion(__FILE__, __LINE__, len, procs);
1506 procs_ = arcp<int>(procs, 0, len);
1508 partId_t *parts = partList.getRawPtr();
1510 if (procDist_.size() > 0){
1513 for (
size_t i=0; i < len; i++){
1514 partToProcsMap(parts[i], procs[i], procId);
1519 lno_t *partCounter =
new lno_t [nGlobalPartsSolution_];
1520 env_->localMemoryAssertion(__FILE__, __LINE__, nGlobalPartsSolution_,
1523 int numProcs = comm_->getSize();
1527 memset(partCounter, 0,
sizeof(
lno_t) * nGlobalPartsSolution_);
1529 for (ArrayRCP<partId_t>::size_type i=0; i < partList.size(); i++)
1530 partCounter[parts[i]]++;
1533 env_->localMemoryAssertion(__FILE__, __LINE__, numProcs, procCounter);
1536 int proc2 = partDist_[0];
1538 for (
partId_t part=1; part < nGlobalParts_; part++){
1540 proc2 = partDist_[part+1];
1541 int numprocs = proc2 - proc1;
1543 double dNum = partCounter[part];
1544 double dProcs = numprocs;
1547 double each = floor(dNum/dProcs);
1548 double extra = fmod(dNum,dProcs);
1550 for (
int proc=proc1, i=0; proc<proc2; proc++, i++){
1552 procCounter[proc] =
lno_t(each) + 1;
1554 procCounter[proc] =
lno_t(each);
1558 delete [] partCounter;
1560 for (ArrayRCP<partId_t>::size_type i=0; i < partList.size(); i++){
1561 if (partList[i] >= nGlobalParts_){
1564 procs[i] = comm_->getRank();
1568 proc1 = partDist_[partNum];
1569 proc2 = partDist_[partNum + 1];
1572 for (proc=proc1; proc < proc2; proc++){
1573 if (procCounter[proc] > 0){
1575 procCounter[proc]--;
1579 env_->localBugAssertion(__FILE__, __LINE__,
"part to proc",
1583 delete [] procCounter;
1593 const Teuchos::ParameterEntry *pe =
1594 env_->getParameters().getEntryPtr(
"remap_parts");
1595 if (pe) doRemap = pe->getValue(&doRemap);
1596 if (doRemap) RemapParts();
1598 haveSolution_ =
true;
1600 env_->memory(
"After Solution has processed algorithm's answer");
1604 template <
typename Adapter>
1605 template <
typename Extra>
1607 int numExtra, ArrayRCP<Extra> &xtraInfo,
1608 ArrayRCP<typename Adapter::gid_t> &imports,
1609 ArrayRCP<Extra> &newXtraInfo)
const
1611 env_->localInputAssertion(__FILE__, __LINE__,
"no solution yet",
1615 int numProcs = comm_->getSize();
1616 size_t localNumIds = gids_.size();
1619 Array<int> counts(numProcs, 0);
1620 if (onePartPerProc_)
1621 for (
size_t i=0; i < localNumIds; i++)
1622 counts[parts_[i]]++;
1624 for (
size_t i=0; i < localNumIds; i++)
1625 counts[procs_[i]]++;
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];
1632 Array<gid_t> gidList(localNumIds);
1633 Array<Extra> numericInfo;
1636 numericInfo.resize(localNumIds);
1638 if (onePartPerProc_){
1639 for (
size_t i=0; i < localNumIds; i++){
1640 lno_t idx = offsets[parts_[i]];
1641 gidList[idx] = gids_[i];
1643 numericInfo[idx] = xtraInfo[i];
1644 offsets[parts_[i]] = idx + 1;
1648 for (
size_t i=0; i < localNumIds; i++){
1649 lno_t idx = offsets[procs_[i]];
1650 gidList[idx] = gids_[i];
1652 numericInfo[idx] = xtraInfo[i];
1653 offsets[procs_[i]] = idx + 1;
1657 Array<int> recvCounts(numProcs, 0);
1660 AlltoAllv<gid_t>(*comm_, *env_, gidList(),
1661 counts(), imports, recvCounts());
1663 catch (std::exception &e){
1664 throw std::runtime_error(
"alltoallv 1");
1669 AlltoAllv<Extra>(*comm_, *env_, xtraInfo(),
1670 counts(), newXtraInfo, recvCounts());
1672 catch (std::exception &e){
1673 throw std::runtime_error(
"alltoallv 2");
1678 return imports.size();
1681 template <
typename Adapter>
1685 if (onePartPerProc_){
1687 partMin = partMax = procId;
1689 else if (procDist_.size() > 0){
1690 partMin = procDist_[procId];
1691 partMax = procDist_[procId+1] - 1;
1692 numParts = procDist_[procId+1] - partMin;
1697 std::vector<int>::const_iterator entry;
1698 entry = std::upper_bound(partDist_.begin(), partDist_.end(), procId);
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;
1707 template <
typename Adapter>
1708 void PartitioningSolution<Adapter>::partToProcsMap(
partId_t partId,
1709 int &procMin,
int &procMax)
const
1711 if (partId >= nGlobalParts_){
1715 procMin = procMax = comm_->getRank();
1717 else if (onePartPerProc_){
1718 procMin = procMax = int(partId);
1720 else if (procDist_.size() > 0){
1721 if (procDistEquallySpread_) {
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++;
1735 std::vector<partId_t>::const_iterator entry;
1736 entry = std::upper_bound(procDist_.begin(), procDist_.end(), partId);
1738 size_t procIdx = entry - procDist_.begin();
1739 procMin = procMax = int(procIdx) - 1;
1743 procMin = partDist_[partId];
1744 procMax = partDist_[partId+1] - 1;
1748 template <
typename Adapter>
1750 int c1,
int c2)
const
1752 if (c1 < 0 || c1 >= weightDim_ || c2 < 0 || c2 >= weightDim_ )
1753 throw logic_error(
"criteriaHaveSamePartSizes error");
1755 bool theSame =
false;
1760 else if (pSizeUniform_[c1] ==
true && pSizeUniform_[c2] ==
true)
1763 else if (pCompactIndex_[c1].size() == pCompactIndex_[c2].size()){
1765 bool useIndex = pCompactIndex_[c1].size() > 0;
1767 for (
partId_t p=0; theSame && p < nGlobalParts_; p++)
1768 if (pSize_[c1][pCompactIndex_[c1][p]] !=
1769 pSize_[c2][pCompactIndex_[c2][p]])
1773 for (
partId_t p=0; theSame && p < nGlobalParts_; p++)
1774 if (pSize_[c1][p] != pSize_[c2][p])
1797 template <
typename Adapter>
1799 bool doCheck,
bool haveNumLocalParts,
bool haveNumGlobalParts,
1800 int numLocalParts,
int numGlobalParts)
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};
1816 reduceAll<int, ssize_t>(*comm_, Teuchos::REDUCE_SUM, 4,
vals, reducevals);
1820 sumHaveGlobal = reducevals[0];
1821 sumHaveLocal = reducevals[1];
1822 sumGlobal = reducevals[2];
1823 sumLocal = reducevals[3];
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),
1832 if (haveNumLocalParts)
1833 sumLocal = numLocalParts * nprocs;
1834 if (haveNumGlobalParts)
1835 sumGlobal = numGlobalParts * nprocs;
1837 sumHaveGlobal = haveNumGlobalParts ? nprocs : 0;
1838 sumHaveLocal = haveNumLocalParts ? nprocs : 0;
1840 maxLocal = numLocalParts;
1841 maxGlobal = numGlobalParts;
1844 if (!haveNumLocalParts && !haveNumGlobalParts){
1845 onePartPerProc_ =
true;
1849 if (haveNumGlobalParts){
1851 vals[0] = numGlobalParts;
1852 vals[1] = numLocalParts;
1854 reduceAll<int, ssize_t>(
1855 *comm_, Teuchos::REDUCE_MAX, 2,
vals, reducevals);
1859 maxGlobal = reducevals[0];
1860 maxLocal = reducevals[1];
1862 env_->localInputAssertion(__FILE__, __LINE__,
1863 "Value for num_global_parts is different on different processes.",
1868 env_->localInputAssertion(__FILE__, __LINE__,
1869 "Sum of num_local_parts does not equal requested num_global_parts",
1872 if (sumLocal == nprocs && maxLocal == 1){
1873 onePartPerProc_ =
true;
1878 if (maxGlobal == nprocs){
1879 onePartPerProc_ =
true;
1887 if (sumHaveLocal == nprocs){
1893 procDist_.resize(nprocs+1);
1895 catch (std::exception &e){
1896 throw(std::bad_alloc());
1899 int *procArray = &procDist_[0];
1903 gatherAll<int, partId_t>(*comm_, 1, &tmp, nprocs, procArray + 1);
1909 for (
int proc=0; proc < nprocs; proc++)
1910 procArray[proc+1] += procArray[proc];
1916 double fParts = numGlobalParts;
1917 double fProcs = nprocs;
1919 if (fParts < fProcs){
1922 partDist_.resize(
size_t(fParts+1));
1924 catch (std::exception &e){
1925 throw(std::bad_alloc());
1928 int *partArray = &partDist_[0];
1930 double each = floor(fProcs / fParts);
1931 double extra = fmod(fProcs, fParts);
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;
1939 env_->globalBugAssertion(__FILE__, __LINE__,
"#parts != #procs",
1942 else if (fParts > fProcs){
1947 procDistEquallySpread_ =
true;
1950 procDist_.resize(
size_t(fProcs+1));
1952 catch (std::exception &e){
1953 throw(std::bad_alloc());
1956 int *procArray = &procDist_[0];
1958 double each = floor(fParts / fProcs);
1959 double extra = fmod(fParts, fProcs);
1962 for (
int proc=0; proc < nprocs; proc++){
1964 procArray[proc+1] = procArray[proc] + numParts;
1967 env_->globalBugAssertion(__FILE__, __LINE__,
"#parts != #procs",
1971 env_->globalBugAssertion(__FILE__, __LINE__,
1989 template <
typename Adapter>
1992 size_t len = parts_.size();
1995 int np = comm_->getSize();
1997 if (np < nGlobalParts_) {
1999 cout <<
"Remapping not yet supported for "
2000 <<
"num_global_parts " << nGlobalParts_
2001 <<
" > num procs " << np << endl;
2014 std::map<partId_t, long> edges;
2019 for (
size_t i = 0; i < len; i++) {
2021 if (parts_[i] == me) lstaying++;
2024 Teuchos::reduceAll<int, long>(*comm_, Teuchos::REDUCE_SUM, 1,
2025 &lstaying, &gstaying);
2030 int nedges = edges.size();
2033 partId_t tnVtx = np + nGlobalParts_;
2037 idx =
new int[tnVtx+1];
2038 sizes =
new int[np];
2041 Teuchos::gather<int, int>(&nedges, 1, sizes, 1, 0, *comm_);
2048 for (
int i = 0; i < np; i++)
2049 idx[i+1] = idx[i] + sizes[i];
2058 bufw =
new long[nedges];
2060 for (std::map<partId_t, long>::iterator it = edges.begin();
2061 it != edges.end(); it++) {
2062 bufv[cnt] = it->first;
2063 bufw[cnt] = it->second;
2075 wgt =
new long[idx[np]];
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_);
2090 for (
int i = 0; i < idx[np]; i++) {
2095 for (
partId_t i = np; i < tnVtx; i++) {
2101 for (
partId_t i = 0; i <= tnVtx; i++) cout << idx[i] <<
" ";
2105 for (
partId_t i = 0; i < idx[tnVtx]; i++) cout << adj[i] <<
" ";
2109 for (
partId_t i = 0; i < idx[tnVtx]; i++) cout << wgt[i] <<
" ";
2115 for (
partId_t i = 0; i < tnVtx; i++) match[i] = i;
2117 Zoltan2::GreedyMWM<partId_t, long>(idx, adj, wgt, tnVtx, match);
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))
2130 bool nontrivial =
false;
2132 for (
partId_t i = 0; i < np; i++) {
2133 if ((match[i] != i) && (match[i] != (i+np))) {
2142 remap =
new partId_t[nGlobalParts_];
2143 for (
partId_t i = 0; i < nGlobalParts_; i++) remap[i] = -1;
2145 bool *used =
new bool[nGlobalParts_];
2146 for (
partId_t i = 0; i < nGlobalParts_; i++) used[i] =
false;
2149 for (
partId_t i = 0; i < nGlobalParts_; i++) {
2151 if (match[tmp] != tmp) {
2152 remap[i] = match[tmp];
2153 used[match[tmp]] =
true;
2158 for (
partId_t i = 0; i < nGlobalParts_; i++) {
2159 if (remap[i] > -1)
continue;
2167 for (
partId_t i = 0, uidx = 0; i < nGlobalParts_; i++) {
2168 if (remap[i] > -1)
continue;
2169 while (used[uidx]) uidx++;
2178 cout <<
"Remap vector: ";
2179 for (
partId_t i = 0; i < nGlobalParts_; i++) cout << remap[i] <<
" ";
2185 doRemap = (newgstaying > gstaying);
2186 cout <<
"gstaying " << gstaying <<
" measure(input) "
2189 <<
" newgstaying " << newgstaying
2190 <<
" nontrivial " << nontrivial
2191 <<
" doRemap " << doRemap << endl;
2198 Teuchos::broadcast<int, int>(*comm_, 0, 1, &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]];
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
Zoltan2_BoxBoundaries(Ordinal s_)
Constructor.
void RemapParts()
Remap a new partition for maximum overlap with an input partition.
Just a placeholder for now.
more involved, like validate a graph
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'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
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.
Zoltan2_BoxBoundaries()
Default Constructor.
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.
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'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.