50 #ifndef _ZOLTAN2_ALGPQJagged_HPP_
51 #define _ZOLTAN2_ALGPQJagged_HPP_
56 #include <Tpetra_Distributor.hpp>
57 #include <Teuchos_ParameterList.hpp>
66 #ifdef enable_migration2
67 #include "zoltan_comm_cpp.h"
76 #define LEAST_SIGNIFICANCE 0.0001
77 #define SIGNIFICANCE_MUL 1000
78 #define FUTURE_REDUCEALL_CUTOFF 1500000
79 #define MIN_WORK_LAST_DIM 4000
81 #ifdef HAVE_ZOLTAN2_OMP
84 #define ABS(x) ((x) >= 0 ? (x) : -(x))
86 #define LEAF_IMBALANCE_FACTOR 0.1
87 #define BINARYCUTOFF 32
90 #define imbalanceOf(Wachieved, totalW, expectedRatio) \
91 (Wachieved) / ((totalW) * (expectedRatio)) - 1
92 #define imbalanceOf2(Wachieved, wExpected) \
93 (Wachieved) / (wExpected) - 1
94 #define MAXOF (a,b) (((a)>(b))?(a):(b))
97 #define forceMigration 1500000
98 #define Z2_DEFAULT_CON_PART_COUNT 16
103 template <
typename Ordinal,
typename T>
107 Ordinal numSum_0, numMin_1, numMin_2;
108 std::vector <Ordinal> *partVector;
117 numMin_2(0), k(0), partVector(NULL), vectorBegin(0), reductionType(0){}
126 numSum_0(nsum), numMin_1(nmin1), numMin_2(nmin2),
127 partVector(NULL),vectorBegin(0),
134 numSum_0(0), numMin_1(0), numMin_2(0),
135 partVector(pVector), vectorBegin(vBegin),
142 void reduce(
const Ordinal count,
const T inBuffer[], T inoutBuffer[])
const
144 if (reductionType == 0){
146 for(Ordinal ii = 0; ii < k ; ++ii){
147 for (Ordinal i=0; i < numSum_0; i++, next++)
148 inoutBuffer[next] += inBuffer[next];
150 for (Ordinal i=0; i < numMin_1; i++, next++)
151 if (inoutBuffer[next] < inBuffer[next])
152 inoutBuffer[next] = inBuffer[next];
154 for (Ordinal i=0; i < numMin_2; i++, next++)
155 if (inoutBuffer[next] > inBuffer[next])
156 inoutBuffer[next] = inBuffer[next];
161 for(Ordinal ii = 0; ii < k ; ++ii){
162 Ordinal partPartition = (*partVector)[ii + vectorBegin];
163 Ordinal tnumSum_ = 2 * partPartition - 1;
164 Ordinal tnumMin_1 = partPartition - 1;
165 Ordinal tnumMin_2 = tnumMin_1 ;
166 for (Ordinal i=0; i < tnumSum_; i++, next++)
167 inoutBuffer[next] += inBuffer[next];
169 for (Ordinal i=0; i < tnumMin_1; i++, next++)
170 if (inoutBuffer[next] < inBuffer[next])
171 inoutBuffer[next] = inBuffer[next];
173 for (Ordinal i=0; i < tnumMin_2; i++, next++)
174 if (inoutBuffer[next] > inBuffer[next])
175 inoutBuffer[next] = inBuffer[next];
182 template <
typename Ordinal,
typename T>
186 Ordinal numMin, numMax, numTotal;
200 numMin(nmin), numMax(nmax), numTotal(nTotal){}
204 void reduce(
const Ordinal count,
const T inBuffer[], T inoutBuffer[])
const
208 for (Ordinal i=0; i < numMin; i++, next++)
209 if (inoutBuffer[next] > inBuffer[next])
210 inoutBuffer[next] = inBuffer[next];
212 for (Ordinal i=0; i < numMax; i++, next++)
213 if (inoutBuffer[next] < inBuffer[next])
214 inoutBuffer[next] = inBuffer[next];
217 for (Ordinal i=0; i < numTotal; i++, next++)
218 inoutBuffer[next] += inBuffer[next];
227 #ifdef mpi_communication
229 void sumMinMin(
void *in,
void *inout,
int *count, MPI_Datatype *type) {
232 int numCut = (k - 1) / 4;
233 int total_part_count = numCut * 2 + 1;
236 float *inoutBuffer = (
float *) inout;
237 float *inBuffer = (
float *) in;
240 for(
partId_t ii = 0; ii < concurrent ; ++ii){
241 for (
long i=0; i < total_part_count; i++, next++)
242 inoutBuffer[next] += inBuffer[next];
244 for (
long i=0; i < numCut; i++, next++)
245 if (inoutBuffer[next] > inBuffer[next])
246 inoutBuffer[next] = inBuffer[next];
248 for (
long i=0; i < numCut; i++, next++)
249 if (inoutBuffer[next] > inBuffer[next])
250 inoutBuffer[next] = inBuffer[next];
255 void minMaxSum(
void *in,
void *inout,
int *count, MPI_Datatype *type) {
260 float *inoutBuffer = (
float *) inout;
261 float *inBuffer = (
float *) in;
264 for (
long i=0; i < num; i++, next++)
265 if (inoutBuffer[next] > inBuffer[next])
266 inoutBuffer[next] = inBuffer[next];
267 for (
long i=0; i < num; i++, next++)
268 if (inoutBuffer[next] < inBuffer[next])
269 inoutBuffer[next] = inBuffer[next];
270 for (
long i=0; i < num; i++, next++)
271 inoutBuffer[next] += inBuffer[next];
280 template <
typename lno_t,
typename partId_t>
301 linkedList = linkedList_;
302 partBegins = partBegins_;
303 partEnds = partEnds_;
315 switch (partEnds[partNo]){
317 partBegins[partNo] = coordinateIndex;
318 partEnds[partNo] = coordinateIndex;
321 linkedList[coordinateIndex] = partBegins[partNo];
322 partBegins[partNo] = coordinateIndex;
347 #ifdef HAVE_ZOLTAN2_OMP
348 #pragma omp parallel for
350 for(
size_t jj = 0; jj < arraySize; ++jj){
365 template <
typename scalar_t>
368 if(
ABS(cutUpperWeight[currentCutIndex] - cutLowerWeight[currentCutIndex]) < _EPSILON){
369 return cutLowerBounds[currentCutIndex];
372 scalar_t newCut =((cutUpperBounds[currentCutIndex] - cutLowerBounds[currentCutIndex]) /
373 (cutUpperWeight[currentCutIndex] - cutLowerWeight[currentCutIndex])) * (ew - cutLowerWeight[currentCutIndex]) + cutLowerBounds[currentCutIndex];
383 template <
typename T>
385 const Teuchos::ParameterList &pl,
386 T &imbalanceTolerance,
388 std::bitset<NUM_RCB_PARAMS> ¶ms,
390 bool &ignoreWeights){
394 const Teuchos::ParameterEntry *pe = pl.getEntryPtr(
"partitioning_objective");
396 obj = pe->getValue(&obj);
402 else if (obj ==
string(
"balance_object_count")){
405 else if (obj ==
string(
"multicriteria_minimize_total_weight")){
409 else if (obj ==
string(
"multicriteria_minimize_maximum_weight")){
413 else if (obj ==
string(
"multicriteria_balance_total_maximum")){
423 pe = pl.getEntryPtr(
"average_cuts");
425 val = pe->getValue(&val);
430 imbalanceTolerance = .1;
431 pe = pl.getEntryPtr(
"imbalance_tolerance");
434 tol = pe->getValue(&tol);
435 imbalanceTolerance = tol - 1.0;
438 if (imbalanceTolerance <= 0)
439 imbalanceTolerance = 10e-4;
442 pe = pl.getEntryPtr(
"bisection_num_test_cuts");
444 numTestCuts = pe->getValue(&numTestCuts);
469 template <
typename T>
471 const Teuchos::ParameterList &pl,
472 bool &allowNonrectilinear,
474 int &migration_actualMigration_option,
475 int &migration_check_option,
476 int &migration_all2all_option,
477 T &migration_imbalance_cut_off,
478 int &migration_assignment_type,
479 int &keep_part_boxes,
481 int &recursion_depth){
484 const Teuchos::ParameterEntry *pe = pl.getEntryPtr(
"partitioning_objective");
485 migration_imbalance_cut_off = .1;
486 pe = pl.getEntryPtr(
"migration_imbalance_cut_off");
489 tol = pe->getValue(&tol);
490 migration_imbalance_cut_off = tol - 1.0;
493 pe = pl.getEntryPtr(
"migration_all_to_all_type");
495 migration_all2all_option = pe->getValue(&concurrentPartCount);
497 migration_all2all_option = 1;
500 pe = pl.getEntryPtr(
"migration_check_option");
502 migration_check_option = pe->getValue(&concurrentPartCount);
504 migration_check_option = 0;
506 if (migration_check_option > 1) migration_check_option = -1;
508 pe = pl.getEntryPtr(
"migration_processor_assignment_type");
510 migration_assignment_type = pe->getValue(&concurrentPartCount);
512 migration_assignment_type = 1;
514 pe = pl.getEntryPtr(
"migration_doMigration_type");
516 migration_actualMigration_option = pe->getValue(&migration_actualMigration_option);
518 migration_actualMigration_option = 1;
521 pe = pl.getEntryPtr(
"parallel_part_calculation_count");
523 concurrentPartCount = pe->getValue(&concurrentPartCount);
525 concurrentPartCount = 0;
528 pe = pl.getEntryPtr(
"keep_part_boxes");
530 keep_part_boxes = pe->getValue(&keep_part_boxes);
535 if (keep_part_boxes == 0){
536 pe = pl.getEntryPtr(
"mapping_type");
538 int mapping_type = -1;
539 mapping_type = pe->getValue(&mapping_type);
540 if (mapping_type == 0){
546 pe = pl.getEntryPtr(
"mj_enable_rcb");
548 enable_rcb = pe->getValue(&enable_rcb);
553 pe = pl.getEntryPtr(
"recursion_depth");
555 recursion_depth = pe->getValue(&recursion_depth);
557 recursion_depth = -1;
561 pe = pl.getEntryPtr(
"rectilinear_blocks");
563 val = pe->getValue(&val);
566 allowNonrectilinear =
false;
568 allowNonrectilinear =
true;
586 template <
typename Adapter>
588 typename Adapter::base_adapter_t> > &coords,
int &coordDim,
589 int &weightDim,
size_t &numLocalCoords,
global_size_t &numGlobalCoords,
int &criteriaDim,
const bool &ignoreWeights){
591 coordDim = coords->getCoordinateDim();
592 weightDim = coords->getCoordinateWeightDim();
593 numLocalCoords = coords->getLocalNumCoordinates();
594 numGlobalCoords = coords->getGlobalNumCoordinates();
595 criteriaDim = (weightDim ? weightDim : 1);
596 if (criteriaDim > 1 && ignoreWeights)
627 template <
typename Adapter,
typename scalar_t,
typename gno_t>
630 typename Adapter::base_adapter_t> > &coords,
632 std::bitset<NUM_RCB_PARAMS> ¶ms,
634 const int &weightDim,
635 const size_t &numLocalCoords,
636 size_t &numGlobalParts,
637 int &pqJagged_multiVectorDim,
639 const int &criteriaDim,
641 ArrayView<const gno_t> &pqJagged_gnos,
643 bool *pqJagged_uniformWeights,
644 bool *pqJagged_uniformParts,
651 ArrayView<const gno_t> gnos;
652 ArrayView<input_t> xyz;
653 ArrayView<input_t> wgts;
655 coords->getCoordinates(gnos, xyz, wgts);
656 pqJagged_gnos = gnos;
661 for (
int dim=0; dim < coordDim; dim++){
662 ArrayRCP<const scalar_t> ar;
663 xyz[dim].getInputArray(ar);
665 pqJagged_values[dim] = (
scalar_t *)ar.getRawPtr();
668 if (weightDim == 0 || ignoreWeights){
670 pqJagged_uniformWeights[0] =
true;
671 pqJagged_weights[0] = NULL;
674 for (
int wdim = 0; wdim < weightDim; wdim++){
675 if (wgts[wdim].size() == 0){
676 pqJagged_uniformWeights[wdim] =
true;
677 pqJagged_weights[wdim] = NULL;
680 ArrayRCP<const scalar_t> ar;
681 wgts[wdim].getInputArray(ar);
682 pqJagged_uniformWeights[wdim] =
false;
683 pqJagged_weights[wdim] = (
scalar_t *) ar.getRawPtr();
693 numGlobalParts = solution->getTargetGlobalNumberOfParts();
695 for (
int wdim = 0; wdim < criteriaDim; wdim++){
696 if (solution->criteriaHasUniformPartSizes(wdim)){
697 pqJagged_uniformParts[wdim] =
true;
698 pqJagged_partSizes[wdim] = NULL;
703 env->localMemoryAssertion(__FILE__, __LINE__, numGlobalParts, tmp) ;
704 for (
size_t i=0; i < numGlobalParts; i++){
705 tmp[i] = solution->getCriteriaPartSize(wdim, i);
707 pqJagged_partSizes[wdim] = tmp;
715 bool multiplePartSizeSpecs =
false;
717 if (criteriaDim > 1){
718 for (
int wdim1 = 0; wdim1 < criteriaDim; wdim1++)
719 for (
int wdim2 = wdim1+1; wdim2 < criteriaDim; wdim2++)
720 if (!solution->criteriaHaveSamePartSizes(wdim1, wdim2)){
721 multiplePartSizeSpecs =
true;
726 if (multiplePartSizeSpecs)
736 pqJagged_multiVectorDim = coordDim;
737 for (
int wdim = 0; wdim < criteriaDim; wdim++){
738 if (!pqJagged_uniformWeights[wdim]) pqJagged_multiVectorDim++;
747 template <
typename scalar_t,
typename gno_t>
750 bool *pqJagged_uniformParts,
bool *pqJagged_uniformWeights,
gno_t *pqJagged_gnos,
751 bool ignoreWeights,
size_t numGlobalParts,
scalar_t **pqJagged_partSizes){
753 std::cout <<
"numLocalCoords:" << numLocalCoords << std::endl;
754 std::cout <<
"coordDim:" << coordDim << std::endl;
755 for(
size_t i = 0; i < numLocalCoords; ++i){
756 for (
int ii = 0; ii < coordDim; ++ii){
757 std::cout << pqJagged_values[ii][i] <<
" ";
759 std::cout << std::endl;
763 std::cout <<
"criteriaDim:" << criteriaDim << std::endl;
764 std::cout <<
"weightDim:" << weightDim << std::endl;
766 for(
size_t i = 0; i < numLocalCoords; ++i){
767 for (
int ii = 0; ii < weightDim; ++ii){
768 std::cout << pqJagged_weights[ii][i] <<
" ";
770 std::cout << std::endl;
774 std::cout <<
"pqJagged_uniformWeights:" << pqJagged_uniformWeights[0] << std::endl;
775 for(
int i = 0; i < criteriaDim; ++i){
776 std::cout << pqJagged_uniformWeights[i] <<
" ";
778 std::cout << std::endl;
781 std::cout <<
"gnos" << std::endl;
782 for(
size_t i = 0; i < numLocalCoords; ++i){
783 std::cout << pqJagged_gnos[i] <<
" ";
785 std::cout << std::endl;
787 std::cout <<
"ignoreWeights:" << ignoreWeights << std::endl;
789 std::cout <<
"pqJagged_uniformParts:" << pqJagged_uniformParts[0] << std::endl;
790 for(
int i = 0; i < criteriaDim; ++i){
791 std::cout << pqJagged_uniformParts[i] <<
" ";
793 std::cout << std::endl;
795 std::cout <<
"pqJagged_partSizes:" << std::endl;
796 std::cout <<
"numGlobalParts:" << numGlobalParts << std::endl;
797 for(
int i = 0; i < criteriaDim; ++i){
798 if(!pqJagged_uniformParts[i])
799 for(
size_t ii = 0; ii < numGlobalParts; ++ii){
800 std::cout << pqJagged_partSizes[i][ii] <<
" ";
802 std::cout << std::endl;
827 template <
typename scalar_t,
typename lno_t>
829 lno_t *partitionedPointPermutations,
831 bool pqJagged_uniformWeights,
836 lno_t coordinateBegin,
850 if(coordinateBegin >= coordinateEnd)
852 minCoordinate = maxScalar;
853 maxCoordinate = minScalar;
858 #ifdef HAVE_ZOLTAN2_OMP
863 if (pqJagged_uniformWeights) {
864 #ifdef HAVE_ZOLTAN2_OMP
868 mytotalWeight = coordinateEnd - coordinateBegin;
874 #ifdef HAVE_ZOLTAN2_OMP
875 #pragma omp for reduction(+:mytotalWeight)
877 for (
lno_t ii = coordinateBegin; ii < coordinateEnd; ++ii){
878 int i = partitionedPointPermutations[ii];
879 mytotalWeight += pqJagged_weights[i];
884 #ifdef HAVE_ZOLTAN2_OMP
885 myId = omp_get_thread_num();
895 =pqJagged_coordinates[partitionedPointPermutations[coordinateBegin]];
900 #ifdef HAVE_ZOLTAN2_OMP
903 for(
lno_t j = coordinateBegin + 1; j < coordinateEnd; ++j){
904 int i = partitionedPointPermutations[j];
905 if(pqJagged_coordinates[i] > myMax) myMax = pqJagged_coordinates[i];
906 if(pqJagged_coordinates[i] < myMin) myMin = pqJagged_coordinates[i];
908 max_min_array[myId] = myMin;
909 max_min_array[myId + numThreads] = myMax;
916 #ifdef HAVE_ZOLTAN2_OMP
919 #pragma omp single nowait
922 minCoordinate = max_min_array[0];
923 for(
int i = 1; i < numThreads; ++i){
924 if(max_min_array[i] < minCoordinate) minCoordinate = max_min_array[i];
930 #ifdef HAVE_ZOLTAN2_OMP
931 #pragma omp single nowait
934 maxCoordinate = max_min_array[numThreads];
935 for(
int i = numThreads + 1; i < numThreads * 2; ++i){
936 if(max_min_array[i] > maxCoordinate) maxCoordinate = max_min_array[i];
940 totalWeight = mytotalWeight;
945 template <
typename partId_t>
947 float fp = pow(numFuture, root);
949 if (fp - ip < fEpsilon){
967 template <
typename scalar_t>
969 RCP<Comm<int> > &comm,
970 const RCP<const Environment> &env,
978 if(comm->getSize() > 1){
980 #ifndef mpi_communication
984 concurrentPartCount);
987 #ifdef mpi_communication
989 MPI_Op_create(minMaxSum, 0, &myop);
995 #ifdef mpi_communication
997 MPI_Allreduce(localMinMaxTotal, globalMinMaxTotal, 3 * concurrentPartCount, MPI_FLOAT, myop,MPI_COMM_WORLD);
999 #ifndef mpi_communication
1000 reduceAll<int, scalar_t>(*comm, reductionOp,
1001 3 * concurrentPartCount, localMinMaxTotal, globalMinMaxTotal
1009 partId_t s = 3 * concurrentPartCount;
1011 globalMinMaxTotal[i] = localMinMaxTotal[i];
1040 template <
typename scalar_t>
1045 bool pqJagged_uniformParts,
1046 bool pqJagged_uniformWeights,
1056 vector <partId_t> *currentPartitions,
1057 vector <partId_t> *futurePartitions,
1062 scalar_t coordinateRange = maxCoordinate - minCoordinate;
1063 if(pqJagged_uniformParts){
1067 scalar_t unitWeight = globalTotalWeight / totalInnerPartCount;
1068 for(
partId_t i = 0; i < noCuts; ++i){
1069 cumulative += (*futurePartitions)[i + futureArrayIndex];
1071 cutPartRatios[i] = cumulative * unitWeight;
1072 cutCoordinates[i] = minCoordinate + (coordinateRange * cumulative) / totalInnerPartCount;
1074 cutPartRatios[noCuts] = 1;
1076 if (pqJagged_uniformWeights){
1077 for(
partId_t i = 0; i < noCuts + 1; ++i){
1078 cutPartRatios[i] = long(cutPartRatios[i] + 0.5);
1135 template <
typename scalar_t>
1137 const RCP<const Environment> &env,
1138 RCP<Comm<int> > &comm,
1139 const size_t &total_part_count,
1144 const scalar_t &imbalanceTolerance,
1150 const scalar_t *targetPartWeightRatios,
1162 bool allowNonRectelinearPart,
1163 float *nonRectelinearPartRatios,
1175 scalar_t leftImbalance = 0, rightImbalance = 0;
1176 scalar_t _EPSILON = numeric_limits<scalar_t>::epsilon();
1179 #ifdef HAVE_ZOLTAN2_OMP
1182 for (
partId_t i = 0; i < noCuts; i++){
1185 if(minCoordinate - leftClosestDistance[i] > _EPSILON)
1186 leftClosestDistance[i] = cutCoordinates[i];
1187 if(rightClosestDistance[i] - maxCoordinate > _EPSILON)
1188 rightClosestDistance[i] = cutCoordinates[i];
1192 #ifdef HAVE_ZOLTAN2_OMP
1195 for (
partId_t i = 0; i < noCuts; i++){
1196 globalCutWeights[i] = 0;
1197 localCutWeights[i] = 0;
1200 newCutCoordinates[i] = cutCoordinates[i];
1204 seenW = globalPartWeights[i * 2];
1208 expected = targetPartWeightRatios[i];
1212 rightImbalance =
imbalanceOf2(globalTotalWeight - seenW, globalTotalWeight - expected);
1214 bool isLeftValid =
ABS(leftImbalance) - imbalanceTolerance < _EPSILON ;
1215 bool isRightValid =
ABS(rightImbalance) - imbalanceTolerance < _EPSILON;
1232 if(isLeftValid && isRightValid){
1235 #ifdef HAVE_ZOLTAN2_OMP
1238 myNoneDoneCount -= 1;
1239 newCutCoordinates [i] = cutCoordinates[i];
1242 else if(leftImbalance < 0){
1245 if(allowNonRectelinearPart){
1247 if (globalPartWeights[i * 2 + 1] == ew){
1250 #ifdef HAVE_ZOLTAN2_OMP
1253 myNoneDoneCount -= 1;
1254 newCutCoordinates [i] = cutCoordinates[i];
1257 nonRectelinearPartRatios[i] = localPartWeights[i * 2 + 1] - localPartWeights[i * 2];
1267 else if (globalPartWeights[i * 2 + 1] > ew){
1269 #ifdef HAVE_ZOLTAN2_OMP
1272 *rectilinearCutCount += 1;
1274 #ifdef HAVE_ZOLTAN2_OMP
1277 myNoneDoneCount -= 1;
1278 newCutCoordinates [i] = cutCoordinates[i];
1279 scalar_t myWeightOnLine = localPartWeights[i * 2 + 1] - localPartWeights[i * 2];
1280 localCutWeights[i] = myWeightOnLine;
1285 cutLowerBounds[i] = rightClosestDistance[i];
1286 cutLowerWeight[i] = seenW;
1289 for (
partId_t ii = i + 1; ii < noCuts ; ++ii){
1290 scalar_t pw = globalPartWeights[ii * 2];
1291 scalar_t lw = globalPartWeights[ii * 2 + 1];
1295 cutUpperBounds[i] = cutCoordinates[ii];
1296 cutUpperWeight[i] = pw;
1297 cutLowerBounds[i] = cutCoordinates[ii];
1298 cutLowerWeight[i] = pw;
1299 }
else if (pw < cutUpperWeight[i]){
1302 cutUpperBounds[i] = leftClosestDistance[ii];
1303 cutUpperWeight[i] = pw;
1309 cutUpperBounds[i] = cutCoordinates[ii];
1310 cutUpperWeight[i] = lw;
1311 cutLowerBounds[i] = cutCoordinates[ii];
1312 cutLowerWeight[i] = pw;
1317 if (pw <= ew && pw >= cutLowerWeight[i]){
1318 cutLowerBounds[i] = rightClosestDistance[ii] ;
1319 cutLowerWeight[i] = pw;
1324 scalar_t newPivot = pivotPos<scalar_t> (cutUpperBounds, cutLowerBounds,i, cutUpperWeight, cutLowerWeight, ew, _EPSILON);
1327 if (
ABS(cutCoordinates[i] - newPivot) < _EPSILON *
EPS_SCALE || cutLowerBounds[i] - cutUpperBounds[i] > _EPSILON){
1329 #ifdef HAVE_ZOLTAN2_OMP
1332 myNoneDoneCount -= 1;
1333 newCutCoordinates [i] = cutCoordinates[i];
1335 newCutCoordinates [i] = newPivot;
1343 cutUpperBounds[i] = leftClosestDistance[i];
1344 cutUpperWeight[i] = seenW;
1347 for (
int ii = i - 1; ii >= 0; --ii){
1348 scalar_t pw = globalPartWeights[ii * 2];
1349 scalar_t lw = globalPartWeights[ii * 2 + 1];
1352 cutUpperBounds[i] = cutCoordinates[ii];
1353 cutUpperWeight[i] = pw;
1354 cutLowerBounds[i] = cutCoordinates[ii];
1355 cutLowerWeight[i] = pw;
1357 else if (pw > cutLowerWeight[i]){
1358 cutLowerBounds[i] = rightClosestDistance[ii];
1359 cutLowerWeight[i] = pw;
1361 cutUpperBounds[i] = rightClosestDistance[ii];
1363 cutUpperWeight[i] = lw;
1368 if (pw >= ew && (pw < cutUpperWeight[i] || (pw == cutUpperWeight[i] && cutUpperBounds[i] > leftClosestDistance[ii]))){
1369 cutUpperBounds[i] = leftClosestDistance[ii] ;
1371 cutUpperWeight[i] = pw;
1375 scalar_t newPivot = pivotPos<scalar_t> (cutUpperBounds, cutLowerBounds,i, cutUpperWeight, cutLowerWeight, ew, _EPSILON);
1377 if (
ABS(cutCoordinates[i] - newPivot) < _EPSILON *
EPS_SCALE || cutLowerBounds[i] - cutUpperBounds[i] > _EPSILON ){
1380 #ifdef HAVE_ZOLTAN2_OMP
1383 myNoneDoneCount -= 1;
1384 newCutCoordinates [ i] = cutCoordinates[i];
1386 newCutCoordinates [ i] = newPivot;
1394 #ifdef HAVE_ZOLTAN2_OMP
1399 if(*rectilinearCutCount > 0){
1401 Teuchos::scan<int,scalar_t>(
1402 *comm, Teuchos::REDUCE_SUM,
1403 noCuts,localCutWeights, globalCutWeights
1409 for (
partId_t i = 0; i < noCuts; ++i){
1411 if(globalCutWeights[i] > 0) {
1413 scalar_t ew = targetPartWeightRatios[i];
1416 scalar_t expectedWeightOnLine = ew - globalPartWeights[i * 2];
1417 scalar_t myWeightOnLine = localCutWeights[i];
1418 scalar_t weightOnLineBefore = globalCutWeights[i];
1419 scalar_t incMe = expectedWeightOnLine - weightOnLineBefore;
1420 scalar_t mine = incMe + myWeightOnLine;
1422 nonRectelinearPartRatios[i] = 0;
1429 else if(mine >= myWeightOnLine){
1431 nonRectelinearPartRatios[i] = myWeightOnLine;
1441 nonRectelinearPartRatios[i] = mine ;
1458 *rectilinearCutCount = 0;
1492 template <
typename scalar_t,
typename lno_t>
1494 size_t total_part_count,
1500 lno_t coordinateBegin,
1501 lno_t coordinateEnd,
1502 lno_t *partitionedPointPermutations,
1504 bool pqJagged_uniformWeights,
1509 double *myPartWeights,
1512 bool useBinarySearch,
1517 for (
size_t i = 0; i < total_part_count; ++i){
1518 myPartWeights[i] = 0;
1522 for(
partId_t i = 0; i < noCuts; ++i){
1525 myLeftClosest[i] = minCoordinate - 1;
1526 myRightClosest[i] = maxCoordinate + 1;
1528 if(useBinarySearch){
1531 scalar_t minus_EPSILON = -_EPSILON;
1532 #ifdef HAVE_ZOLTAN2_OMP
1536 for (
lno_t ii = coordinateBegin; ii < coordinateEnd; ++ii){
1537 int i = partitionedPointPermutations[ii];
1548 scalar_t w = pqJagged_uniformWeights? 1:pqJagged_weights[i];
1549 bool isInserted =
false;
1550 bool onLeft =
false;
1551 bool onRight =
false;
1554 scalar_t coord = pqJagged_coordinates[i];
1562 scalar_t cut = cutCoordinates_tmp[j];
1566 if(absdistance < _EPSILON){
1568 myPartWeights[j * 2 + 1] += w;
1569 partIds[i] = j * 2 + 1;
1572 myLeftClosest[j] = coord;
1573 myRightClosest[j] = coord;
1579 distance =
ABS(cutCoordinates_tmp[kk] - cut);
1580 if(distance < _EPSILON){
1581 myPartWeights[2 * kk + 1] += w;
1583 myLeftClosest[kk] = coord;
1584 myRightClosest[kk] = coord;
1588 if(coord - myLeftClosest[kk] > _EPSILON){
1589 myLeftClosest[kk] = coord;
1597 distance =
ABS(cutCoordinates_tmp[kk] - cut);
1598 if(distance < _EPSILON){
1599 myPartWeights[2 * kk + 1] += w;
1602 partIds[i] = kk * 2 + 1;
1603 myLeftClosest[kk] = coord;
1604 myRightClosest[kk] = coord;
1608 if(myRightClosest[kk] - coord > _EPSILON){
1609 myRightClosest[kk] = coord;
1627 bool _break =
false;
1629 distance = coord - cutCoordinates_tmp[j - 1];
1630 if(distance > _EPSILON){
1661 bool _break =
false;
1663 scalar_t distance_ = coord - cutCoordinates_tmp[j + 1];
1669 if(distance_ < minus_EPSILON){
1698 myPartWeights[2 * lastPart + 2] += w;
1699 partIds[i] = 2 * lastPart + 2;
1701 if(myRightClosest[lastPart] - coord > _EPSILON){
1702 myRightClosest[lastPart] = coord;
1704 if(lastPart+1 < noCuts){
1706 if(coord - myLeftClosest[lastPart + 1] > _EPSILON){
1707 myLeftClosest[lastPart + 1] = coord;
1714 myPartWeights[2 * lastPart] += w;
1715 partIds[i] = 2 * lastPart;
1717 if(coord - myLeftClosest[lastPart] > _EPSILON){
1718 myLeftClosest[lastPart] = coord;
1721 if(lastPart-1 >= 0){
1723 if(myRightClosest[lastPart -1] - coord > _EPSILON){
1724 myRightClosest[lastPart -1] = coord;
1733 #ifdef HAVE_ZOLTAN2_OMP
1736 for (
lno_t ii = coordinateBegin; ii < coordinateEnd; ++ii){
1737 int i = partitionedPointPermutations[ii];
1739 scalar_t w = pqJagged_uniformWeights ? 1 : pqJagged_weights[i];
1741 scalar_t coord = pqJagged_coordinates[i];
1748 scalar_t cut = cutCoordinates_tmp[j];
1753 if(absdistance < _EPSILON){
1754 myPartWeights[j * 2 + 1] += w;
1756 myLeftClosest[j] = 0;
1757 myRightClosest[j] = 0;
1758 partIds[i] = j * 2 + 1;
1765 distance =
ABS(cutCoordinates_tmp[kk] - cut);
1766 if(distance < _EPSILON){
1768 myPartWeights[2 * kk + 1] += w;
1771 myLeftClosest[kk] = 0;
1772 myRightClosest[kk] = 0;
1776 if(myLeftClosest[kk] > distance){
1777 myLeftClosest[kk] = distance;
1785 distance =
ABS(cutCoordinates_tmp[kk] - cut);
1786 if(distance < _EPSILON){
1787 myPartWeights[2 * kk + 1] += w;
1790 myLeftClosest[kk] = 0;
1791 myRightClosest[kk] = 0;
1795 if(myRightClosest[kk] > distance){
1796 myRightClosest[kk] = distance;
1802 else if (distance < 0) {
1803 while((absdistance > _EPSILON) && distance < 0){
1805 if (myLeftClosest[j] > absdistance){
1806 myLeftClosest[j] = absdistance;
1810 distance = coord - cutCoordinates_tmp[j];
1812 absdistance =
ABS(distance);
1814 if(absdistance < _EPSILON)
1816 myPartWeights[j * 2 + 1] += w;
1817 myLeftClosest[j] = 0;
1818 myRightClosest[j] = 0;
1819 cut = cutCoordinates_tmp[j];
1822 partIds[i] = j * 2 + 1;
1828 distance =
ABS(cutCoordinates_tmp[kk] - cut);
1830 if(distance < _EPSILON){
1831 myPartWeights[2 * kk + 1] += w;
1834 myLeftClosest[kk] = 0;
1835 myRightClosest[kk] = 0;
1839 if(myLeftClosest[kk] > distance){
1840 myLeftClosest[kk] = distance;
1848 distance =
ABS(cutCoordinates_tmp[kk] - cut);
1849 if(distance < _EPSILON){
1850 myPartWeights[2 * kk + 1] += w;
1854 myLeftClosest[kk] = 0;
1855 myRightClosest[kk] = 0;
1859 if(myRightClosest[kk] > distance){
1860 myRightClosest[kk] = distance;
1867 myPartWeights[j * 2 + 2] += w;
1868 if (j >= 0 && myRightClosest[j] > absdistance){
1869 myRightClosest[j] = absdistance;
1871 partIds[i] = j * 2 + 2;
1878 while((absdistance > _EPSILON) && distance > 0){
1880 if ( myRightClosest[j] > absdistance){
1881 myRightClosest[j] = absdistance;
1884 if(j >= noCuts)
break;
1885 distance = coord - cutCoordinates_tmp[j];
1886 absdistance =
ABS(distance);
1891 if(absdistance < _EPSILON)
1893 myPartWeights[j * 2 + 1] += w;
1894 myLeftClosest[j] = 0;
1895 myRightClosest[j] = 0;
1896 partIds[i] = j * 2 + 1;
1897 cut = cutCoordinates_tmp[j];
1902 distance =
ABS(cutCoordinates_tmp[kk] - cut);
1903 if(distance < _EPSILON){
1904 myPartWeights[2 * kk + 1] += w;
1905 myLeftClosest[kk] = 0;
1906 myRightClosest[kk] = 0;
1910 if(myLeftClosest[kk] > distance){
1911 myLeftClosest[kk] = distance;
1919 distance =
ABS(cutCoordinates_tmp[kk] - cut);
1920 if(distance < _EPSILON){
1921 myPartWeights[2 * kk + 1] += w;
1923 myLeftClosest[kk] = 0;
1924 myRightClosest[kk] = 0;
1928 if(myRightClosest[kk] > distance){
1929 myRightClosest[kk] = distance;
1936 myPartWeights[j * 2] += w;
1937 if (j < noCuts && myLeftClosest[j] > absdistance){
1938 myLeftClosest[j] = absdistance;
1940 partIds[i] = j * 2 ;
1993 for (
size_t i = 1; i < total_part_count; ++i){
1997 if(i % 2 == 0 && i > 1 && i < total_part_count - 1 &&
1998 ABS(cutCoordinates_tmp[i / 2] - cutCoordinates_tmp[i /2 - 1]) < _EPSILON){
1999 myPartWeights[i] = myPartWeights[i-2];
2002 myPartWeights[i] += myPartWeights[i-1];
2021 template <
typename scalar_t>
2023 const vector <partId_t> &pVector,
2031 double **partWeights,
2033 scalar_t *totalPartWeights_leftClosest_rightCloset
2036 #ifdef HAVE_ZOLTAN2_OMP
2042 size_t tlr_shift = 0;
2044 for(
partId_t i = 0; i < concurrentPartCount; ++i){
2046 partId_t partNo = pVector[vBegin + i];
2048 size_t total_part_count = partNo + size_t (noCuts) ;
2050 for(
partId_t ii = 0; ii < noCuts ; ++ii){
2053 if(isDone[nCut])
continue;
2054 scalar_t minl = leftClosestPoints[0][nCut], minr = rightClosestPoints[0][nCut];
2056 for (
int j = 1; j < numThreads; ++j){
2057 if (rightClosestPoints[j][nCut] < minr ){
2058 minr = rightClosestPoints[j][nCut];
2060 if (leftClosestPoints[j][nCut] > minl ){
2061 minl = leftClosestPoints[j][nCut];
2064 totalPartWeights_leftClosest_rightCloset[total_part_count + next] = minl;
2065 totalPartWeights_leftClosest_rightCloset[total_part_count + noCuts + next] = minr;
2067 tlr_shift += (total_part_count + 2 * noCuts);
2068 cut_shift += noCuts;
2073 size_t totalPartShift = 0;
2075 for(
partId_t i = 0; i < concurrentPartCount; ++i){
2077 partId_t partNo = pVector[vBegin + i];
2079 size_t total_part_count = partNo + size_t (noCuts) ;
2081 for(
size_t j = 0; j < total_part_count; ++j){
2083 partId_t cutInd = j / 2 + cut_shift;
2085 if(j != total_part_count - 1 && isDone[cutInd])
continue;
2087 for (
int k = 0; k < numThreads; ++k){
2088 pwj += partWeights[k][totalPartShift + j];
2091 totalPartWeights_leftClosest_rightCloset[tlr_shift + j] = pwj;
2093 cut_shift += noCuts;
2094 tlr_shift += total_part_count + 2 * noCuts;
2095 totalPartShift += total_part_count;
2147 template <
typename scalar_t,
typename lno_t>
2149 const RCP<const Environment> &env,
2150 RCP<Comm<int> > &comm,
2152 lno_t *partitionedPointPermutations,
2154 bool pqJagged_uniformWeights,
2167 lno_t *inTotalCounts,
2178 double **partWeights,
2179 scalar_t *local_totalPartWeights_leftClosest_rightCloset,
2180 scalar_t *global_totalPartWeights_leftClosest_rightCloset,
2181 bool allowNonRectelinearPart,
2182 float *nonRectelinearPartRatios,
2188 bool useBinarySearch,
2191 vector <partId_t> &pVector
2195 scalar_t *cutCoordinates_tmp = cutCoordinates;
2198 #ifdef mpi_communication
2200 MPI_Op_create(sumMinMin, 0, &myop);
2204 scalar_t _EPSILON = numeric_limits<scalar_t>::epsilon();
2210 size_t totalReductionSize = 0;
2211 #ifdef HAVE_ZOLTAN2_OMP
2212 #pragma omp parallel shared(allDone, recteLinearCutCount)
2216 #ifdef HAVE_ZOLTAN2_OMP
2217 me = omp_get_thread_num();
2219 double *myPartWeights = partWeights[me];
2220 scalar_t *myLeftClosest = leftClosestDistance[me];
2221 scalar_t *myRightClosest = rightClosestDistance[me];
2223 #ifdef HAVE_ZOLTAN2_OMP
2229 for(
partId_t i = 0; i < concurrentPartCount; ++i){
2231 partId_t partNo = pVector[currentPartBeginIndex + i];
2233 totalReductionSize += (4 * noCuts + 1);
2235 for(
partId_t ii = 0; ii < noCuts; ++ii){
2236 isDone[next] =
false;
2237 cutLowerBounds[next] = globalMinMaxTotal[i];
2238 cutUpperBounds[next] = globalMinMaxTotal[i + concurrentPartCount];
2240 cutUpperWeight[next] = globalMinMaxTotal[i + 2 * concurrentPartCount];
2241 cutLowerWeight[next] = 0;
2243 if(allowNonRectelinearPart){
2244 nonRectelinearPartRatios[next] = 0;
2255 while (allDone != 0){
2258 size_t totalPartShift = 0;
2261 for (
partId_t kk = 0; kk < concurrentPartCount; ++kk){
2263 partNo = pVector[currentPartBeginIndex + kk];
2269 size_t total_part_count = partNo + size_t (noCuts) ;
2270 if (myNonDoneCounts[kk] > 0){
2273 bool *currentDone = isDone + cutShifts;
2274 double *myCurrentPartWeights = myPartWeights + totalPartShift;
2275 scalar_t *myCurrentLeftClosest = myLeftClosest + cutShifts;
2276 scalar_t *myCurrentRightClosest = myRightClosest + cutShifts;
2278 partId_t current = currentPartBeginIndex + kk;
2279 lno_t coordinateBegin = current ==0 ? 0: inTotalCounts[current -1];
2280 lno_t coordinateEnd = inTotalCounts[current];
2281 scalar_t *cutCoordinates_ = cutCoordinates_tmp + cutShifts;
2283 scalar_t minCoordinate = globalMinMaxTotal[kk];
2284 scalar_t maxCoordinate = globalMinMaxTotal[kk + concurrentPartCount];
2287 pqJagged_1DPart_getPartWeights<scalar_t, lno_t>(
2296 partitionedPointPermutations,
2297 pqJagged_coordinates,
2298 pqJagged_uniformWeights,
2302 myCurrentPartWeights,
2303 myCurrentLeftClosest,
2304 myCurrentRightClosest,
2311 cutShifts += noCuts;
2312 totalPartShift += total_part_count;
2316 accumulateThreadResults<scalar_t>(
2318 currentPartBeginIndex,
2319 concurrentPartCount,
2321 leftClosestDistance, rightClosestDistance, partWeights,
2323 local_totalPartWeights_leftClosest_rightCloset
2356 #ifdef HAVE_ZOLTAN2_OMP
2360 if(comm->getSize() > 1){
2362 #ifdef mpi_communication
2364 MPI_Allreduce(local_totalPartWeights_leftClosest_rightCloset, global_totalPartWeights_leftClosest_rightCloset,
2366 (total_part_count + 2 * noCuts) * concurrentPartCount, MPI_FLOAT, myop,MPI_COMM_WORLD);
2368 #ifndef mpi_communication
2370 reduceAll<int, scalar_t>(
2374 local_totalPartWeights_leftClosest_rightCloset,
2375 global_totalPartWeights_leftClosest_rightCloset
2382 memcpy(global_totalPartWeights_leftClosest_rightCloset,
2383 local_totalPartWeights_leftClosest_rightCloset,
2384 totalReductionSize *
sizeof(
scalar_t)
2389 size_t tlrShift = 0;
2390 for (
partId_t kk = 0; kk < concurrentPartCount; ++kk){
2392 partNo = pVector[currentPartBeginIndex + kk];
2395 size_t total_part_count = partNo + size_t (noCuts) ;
2397 if (myNonDoneCounts[kk] == 0) {
2399 tlrShift += (total_part_count + 2 * noCuts);
2403 scalar_t *localPartWeights = local_totalPartWeights_leftClosest_rightCloset + tlrShift ;
2404 scalar_t *gtlr = global_totalPartWeights_leftClosest_rightCloset + tlrShift;
2405 scalar_t *glc = gtlr + total_part_count;
2406 scalar_t *grc = gtlr + total_part_count + noCuts;
2407 scalar_t *globalPartWeights = gtlr;
2408 bool *currentDone = isDone + cutShift;
2410 scalar_t *currentTargetPartWeightRatios = targetPartWeightRatios + cutShift + kk;
2411 float *currentnonRectelinearPartRatios = nonRectelinearPartRatios + cutShift;
2413 scalar_t minCoordinate = globalMinMaxTotal[kk];
2414 scalar_t maxCoordinate = globalMinMaxTotal[kk + concurrentPartCount];
2415 scalar_t globalTotalWeight = globalMinMaxTotal[kk + concurrentPartCount * 2];
2416 scalar_t *currentcutLowerWeight = cutLowerWeight + cutShift;
2417 scalar_t *currentcutUpperWeight = cutUpperWeight + cutShift;
2418 scalar_t *currentcutUpperBounds = cutUpperBounds + cutShift;
2419 scalar_t *currentcutLowerBounds = cutLowerBounds + cutShift;
2421 partId_t prevDoneCount = myNonDoneCounts[kk];
2424 getNewCoordinates<scalar_t>(
2437 currentTargetPartWeightRatios,
2440 cutCoordinates_tmp + cutShift,
2441 currentcutUpperBounds,
2442 currentcutLowerBounds,
2445 currentcutLowerWeight,
2446 currentcutUpperWeight,
2447 cutCoordinatesWork +cutShift,
2449 allowNonRectelinearPart,
2450 currentnonRectelinearPartRatios,
2451 &recteLinearCutCount,
2458 tlrShift += (total_part_count + 2 * noCuts);
2459 partId_t reduction = prevDoneCount - myNonDoneCounts[kk];
2460 #ifdef HAVE_ZOLTAN2_OMP
2466 allDone -= reduction;
2470 #ifdef HAVE_ZOLTAN2_OMP
2476 cutCoordinates_tmp = cutCoordinatesWork;
2477 cutCoordinatesWork = t;
2488 if (cutCoordinates != cutCoordinates_tmp){
2489 #ifdef HAVE_ZOLTAN2_OMP
2494 for(
partId_t i = 0; i < concurrentPartCount; ++i){
2496 partNo = pVector[currentPartBeginIndex + i];
2499 for(
partId_t ii = 0; ii < noCuts; ++ii){
2500 cutCoordinates[next + ii] = cutCoordinates_tmp[next + ii];
2506 #ifdef HAVE_ZOLTAN2_OMP
2510 cutCoordinatesWork = cutCoordinates_tmp;
2519 template <
typename T>
2522 T * a =
new T[size];
2524 throw "cannot allocate memory";
2533 template <
typename T>
2543 template <
typename tt>
2545 std::stringstream ss (std::stringstream::in |std::stringstream::out);
2547 std::string tmp =
"";
2556 template <
typename IT,
typename CT,
typename WT>
2570 this->
_EPSILON = numeric_limits<WT>::epsilon();
2575 this->
index = index_;
2576 this->count = count_;
2578 this->
_EPSILON = numeric_limits<WT>::epsilon();
2583 this->count = other.
count;
2592 void set(IT index_ ,CT count_, WT *vals_){
2593 this->
index = index_;
2594 this->count = count_;
2601 this->count = other.
count;
2606 bool operator<(const uMultiSortItem<IT,CT,WT>& other)
const{
2607 assert (this->count == other.count);
2608 for(CT i = 0; i < this->
count; ++i){
2610 if (
ABS(this->
val[i] - other.val[i]) < this->_EPSILON){
2614 if(this->
val[i] < other.val[i]){
2626 assert (this->count == other.
count);
2627 for(CT i = 0; i < this->
count; ++i){
2629 if (
ABS(this->
val[i] - other.
val[i]) < this->_EPSILON){
2633 if(this->
val[i] > other.
val[i]){
2648 template <
class IT,
class WT>
2659 template <
class IT,
class WT>
2662 #define SWAP(a,b,temp) temp=(a);(a)=(b);(b)=temp;
2665 IT i, ir=n, j, k, l=1;
2666 IT jstack=0, istack[50];
2675 for (j=l+1;j<=ir;j++)
2679 for (i=j-1;i>=1;i--)
2681 if (arr[i].val <= aval)
2689 ir=istack[jstack--];
2695 SWAP(arr[k],arr[l+1], temp)
2696 if (arr[l+1].val > arr[ir].val)
2698 SWAP(arr[l+1],arr[ir],temp)
2700 if (arr[l].val > arr[ir].val)
2702 SWAP(arr[l],arr[ir],temp)
2704 if (arr[l+1].val > arr[l].val)
2706 SWAP(arr[l+1],arr[l],temp)
2714 do i++;
while (arr[i].val < aval);
2715 do j--;
while (arr[j].val > aval);
2717 SWAP(arr[i],arr[j],temp);
2722 if (jstack > NSTACK){
2723 cout <<
"uqsort: NSTACK too small in sort." << endl;
2742 #ifdef enable_migration2
2743 template <
typename gno_t,
typename lno_t,
typename scalar_t,
typename node_t>
2744 RCP<const Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> > create_initial_multi_vector(
2745 const RCP<const Environment> &env,
2746 RCP<Comm<int> > &comm,
2747 gno_t numGlobalPoints,
2748 lno_t numLocalPoints,
2754 int pqJagged_multiVectorDim
2756 typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> tMVector_t;
2758 sleep(1); env->memory(
"initial multivector before map");
2760 RCP<Tpetra::Map<lno_t, gno_t, node_t> > mp = rcp(
2761 new Tpetra::Map<lno_t, gno_t, node_t> (numGlobalPoints, numLocalPoints, 0, comm));
2765 if(comm->getRank() == 0) cout <<
"nn:"<< numLocalPoints<<
"created map:" << mp.getRawPtr() << endl;
2766 sleep(1); env->memory(
"initial multivector after map");
2768 Teuchos::Array<Teuchos::ArrayView<const scalar_t> > coordView(pqJagged_multiVectorDim);
2769 for (
int i=0; i < coord_dim; i++){
2771 if(numLocalPoints > 0){
2772 Teuchos::ArrayView<const scalar_t> a(coords[i], numLocalPoints);
2775 Teuchos::ArrayView<const scalar_t> a;
2779 sleep(1); env->memory(
"initial multivector after coordinates -" + toString<int>(i));
2783 sleep(1); env->memory(
"initial multivector after coordinates");
2785 for (
int i=0; i < weight_dim; ++i){
2786 int j = i + coord_dim;
2789 if(numLocalPoints > 0){
2790 Teuchos::ArrayView<const scalar_t> a(weight[i], numLocalPoints);
2793 Teuchos::ArrayView<const scalar_t> a;
2799 sleep(1); env->memory(
"initial multivector after weights");
2802 if(numLocalPoints > 0){
2804 Teuchos::ArrayView<const scalar_t> a(mappedGnos, numLocalPoints);
2805 coordView[pqJagged_multiVectorDim - 1] = a;
2807 Teuchos::ArrayView<const scalar_t> a;
2808 coordView[pqJagged_multiVectorDim - 1] = a;
2811 RCP< Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> >tmVector =
2812 RCP< Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> >(
2813 new Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t>( mp, coordView.view(0, pqJagged_multiVectorDim), pqJagged_multiVectorDim));
2816 sleep(1); env->memory(
"initial multivector after create");
2818 RCP<const tMVector_t> coordsConst = Teuchos::rcp_const_cast<
const tMVector_t>(tmVector);
2821 sleep(1); env->memory(
"initial multivector after cast");
2829 template <
typename gno_t,
2832 void getProcessorCoordinatePartCounts(
2834 RCP<Comm<int> > &pcomm,
2835 const RCP<const Environment> &env,
2836 RCP<Comm<int> > &comm,
2838 int migration_proc_assignment_type,
2842 lno_t *partBeginArray,
2843 gno_t *&p_gno_np_global_num_coord_each_part_actual
2847 if (nprocs <= num_parts) migration_proc_assignment_type = 1;
2850 size_t allocation_size = num_parts;
2851 if (migration_proc_assignment_type == 1){
2854 allocation_size = num_parts * (nprocs + 1);
2859 p_gno_np_global_num_coord_each_part_actual = allocMemory<gno_t>(allocation_size);
2862 gno_t *p_gno_np_local_num_coord_each_part_actual = allocMemory<gno_t>(allocation_size);
2865 gno_t *p_gno_np_local_num_coord_each_part = p_gno_np_local_num_coord_each_part_actual;
2866 gno_t *p_gno_np_local_num_coord_each_part_mypart = p_gno_np_local_num_coord_each_part_actual;
2872 if (migration_proc_assignment_type == 1){
2873 partId_t shift_amount = nprocs * num_parts;
2874 partId_t my_part_shift = myRank * num_parts;
2875 p_gno_np_local_num_coord_each_part += shift_amount;
2876 p_gno_np_local_num_coord_each_part_mypart += my_part_shift;
2879 memset(p_gno_np_local_num_coord_each_part_actual, 0,
sizeof(
gno_t)*allocation_size);
2882 for (
partId_t i = 0; i < num_parts; ++i){
2885 pBegin = partBeginArray[i - 1];
2887 lno_t pEnd = partBeginArray[i];
2888 p_gno_np_local_num_coord_each_part[i] = pEnd - pBegin;
2893 if (migration_proc_assignment_type == 1){
2894 memcpy (p_gno_np_local_num_coord_each_part_mypart,
2895 p_gno_np_local_num_coord_each_part,
2896 sizeof(
gno_t) * (num_parts) );
2907 reduceAll<int, gno_t>(
2909 Teuchos::REDUCE_SUM,
2911 p_gno_np_local_num_coord_each_part_actual,
2912 p_gno_np_global_num_coord_each_part_actual);
2929 bool checkMigration(
2931 RCP<Comm<
int> > &pcomm,
2932 const RCP<const Environment> &env,
2933 RCP<Comm<
int> > &comm,
2935 int migration_check_option,
2936 size_t futureReduceAll,
2937 lno_t numCoordinatesForLastDimPartitioning,
2938 float migration_imbalance_cut_off,
2940 int migration_proc_assignment_type,
2945 gno_t *p_gno_np_global_num_coord_each_part_actual,
2946 lno_t *partBeginArray
2952 if (migration_check_option == 0){
2953 double diff = 0, global_diff = 0;
2954 if (migration_proc_assignment_type == 0 )
2957 for (
partId_t i = 0; i < num_parts; ++i){
2958 double ideal_num = p_gno_np_global_num_coord_each_part_actual[i] / double(nprocs);
2962 pBegin = partBeginArray[i - 1];
2964 lno_t pEnd = partBeginArray[i];
2965 diff +=
ABS(ideal_num -
2966 (pEnd - pBegin)) / (ideal_num);
2969 reduceAll<int, double>(
2971 Teuchos::REDUCE_SUM,
2977 size_t global_shift = nprocs * num_parts;
2978 for (
partId_t ii = 0; ii < nprocs; ++ii){
2979 for (
partId_t i = 0; i < num_parts; ++i){
2980 double ideal_num = p_gno_np_global_num_coord_each_part_actual[global_shift + i] / double(nprocs);
2981 global_diff +=
ABS(ideal_num -
2982 p_gno_np_global_num_coord_each_part_actual[ii * num_parts + i]) / (ideal_num);
2985 global_diff /= num_parts;
2988 global_diff /= nprocs;
2990 cout <<
"imbalance for next iteration:" << global_diff << endl;
2995 if(global_diff <= migration_imbalance_cut_off){
3012 template <
typename gno_t,
typename lno_t,
typename scalar_t,
typename node_t>
3013 RCP< Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> > createNewMultivector(
3014 RCP<Comm<int> > &comm,
3015 gno_t numGlobalPoints,
3016 lno_t numLocalPoints,
3026 lno_t *permutationArray,
3028 int &multiVectorDim,
3029 RCP<
const Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> > old_mvector
3033 typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> tMVector_t;
3034 RCP<const Tpetra::Map<lno_t, gno_t, node_t> > mp = old_mvector->getMap();
3036 Teuchos::Array<Teuchos::ArrayView<const scalar_t> > coordView(multiVectorDim + 1);
3038 for (
int i=0; i < coord_dim; i++){
3039 if(numLocalPoints > 0){
3040 Teuchos::ArrayView<const scalar_t> a(coords[i], numLocalPoints);
3043 Teuchos::ArrayView<const scalar_t> a;
3048 for (
int i=0; i < weight_dim; i++){
3049 int j = i + coord_dim;
3050 if(numLocalPoints > 0){
3051 Teuchos::ArrayView<const scalar_t> a(weight[i], numLocalPoints);
3054 Teuchos::ArrayView<const scalar_t> a;
3058 scalar_t *assigned_parts = allocMemory<scalar_t>(numLocalPoints);
3061 for (
partId_t i = 0; i < num_parts; ++i){
3063 if (i > 0) pBegin = partBeginArray[i - 1];
3064 lno_t pEnd = partBeginArray[i];
3068 for (lno_t j = pBegin; j < pEnd; ++j){
3069 lno_t ind = permutationArray[j];
3070 assigned_parts[ind] = p;
3073 if(numLocalPoints > 0){
3074 Teuchos::ArrayView<const scalar_t> a(assigned_parts, numLocalPoints);
3075 coordView[multiVectorDim] = a;
3077 Teuchos::ArrayView<const scalar_t> a;
3078 coordView[multiVectorDim] = a;
3080 multiVectorDim += 1;
3082 RCP< Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> >tmVector = RCP< Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> >(
3083 new Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t>( mp, coordView.view(0, multiVectorDim), multiVectorDim));
3085 RCP<const tMVector_t> coordsConst = Teuchos::rcp_const_cast<
const tMVector_t>(tmVector);
3091 template <
typename partId_t,
typename lno_t,
typename gno_t>
3092 void fillContinousSendBuffer2(
partId_t num_parts,
3095 const gno_t *gno_list,
3096 uSortItem<partId_t, partId_t> * part_assignment,
3099 vector<partId_t> *newFuturePartitions,
3104 partId_t partShift = partBeginIndex;
3106 for(
partId_t i = 0; i < num_parts; ++i){
3107 partId_t p = part_assignment[i].id;
3112 if (p > 0) pBegin = partBegins[p - 1];
3113 lno_t pEnd = partBegins[p];
3115 partId_t assigned_proc = part_assignment[i].val;
3116 if (myRank == assigned_proc && previous_processor != assigned_proc){
3117 partBeginIndex = partShift;
3119 previous_processor = assigned_proc;
3120 partShift += (*newFuturePartitions)[p];
3122 for (lno_t j=pBegin; j < pEnd; j++){
3123 lno_t localInd = permutation[j];
3124 gno_t to_send = gno_list[localInd];
3125 sendBuf[nextInd++] = to_send;
3132 template <
typename partId_t,
typename lno_t>
3133 void Z1fillSendBuffer2(
partId_t num_parts,
3136 uSortItem<partId_t, partId_t> * part_assignment,
3137 int *coordinate_destionations,
3139 vector<partId_t> *newFuturePartitions,
3144 partId_t partShift = partBeginIndex;
3146 for(
partId_t i = 0; i < num_parts; ++i){
3147 partId_t p = part_assignment[i].id;
3152 if (p > 0) pBegin = partBegins[p - 1];
3153 lno_t pEnd = partBegins[p];
3155 partId_t assigned_proc = part_assignment[i].val;
3156 if (myRank == assigned_proc && previous_processor != assigned_proc){
3157 partBeginIndex = partShift;
3159 previous_processor = assigned_proc;
3160 partShift += (*newFuturePartitions)[p];
3162 for (lno_t j=pBegin; j < pEnd; j++){
3163 lno_t localInd = permutation[j];
3164 coordinate_destionations[localInd] = assigned_proc;
3175 template <
typename partId_t,
typename lno_t,
typename gno_t>
3176 void fillContinousSendBuffer1(
3178 RCP<Comm<int> > &pcomm,
3183 const gno_t *gno_list,
3196 lno_t *_sendCount_psum = allocMemory<lno_t>(nprocs);
3197 lno_t prefixsum = 0;
3198 for (
int i = 0; i < nprocs; ++i ){
3199 _sendCount_psum[i] = prefixsum;
3200 prefixsum += sendCount[i];
3211 for (
partId_t p = 0; p < numParts; ++p){
3213 if (p > 0) pBegin = partBegins[p - 1];
3214 lno_t pEnd = partBegins[p];
3217 partId_t proc_to_sent = part_assign_begins[p];
3219 lno_t totalSend = 0;
3220 for (lno_t j=pBegin; j < pEnd; j++){
3221 lno_t localInd = permutation[j];
3222 gno_t to_send = gno_list[localInd];
3225 while (totalSend >= sendCount[proc_to_sent]){
3228 part_assign_begins[p] = proc_chains[proc_to_sent];
3230 proc_chains[proc_to_sent] = -1;
3232 proc_to_sent = part_assign_begins[p];
3235 sendBuf[_sendCount_psum[proc_to_sent] + totalSend++] = to_send;
3239 freeArray<lno_t>(_sendCount_psum);
3244 template <
typename partId_t,
typename lno_t>
3245 void Z1fillSendBuffer1(
3247 RCP<Comm<int> > &pcomm,
3259 int *coordinate_destionations
3261 for (
partId_t p = 0; p < numParts; ++p){
3263 if (p > 0) pBegin = partBegins[p - 1];
3264 lno_t pEnd = partBegins[p];
3267 partId_t proc_to_sent = part_assign_begins[p];
3269 lno_t totalSend = 0;
3270 for (lno_t j=pBegin; j < pEnd; j++){
3271 lno_t localInd = permutation[j];
3272 while (totalSend >= sendCount[proc_to_sent]){
3275 part_assign_begins[p] = proc_chains[proc_to_sent];
3277 proc_chains[proc_to_sent] = -1;
3279 proc_to_sent = part_assign_begins[p];
3282 coordinate_destionations[localInd] = proc_to_sent;
3290 template <
typename partId_t,
typename lno_t,
typename gno_t>
3291 void procAssignment2(
3292 int assignment_type,
3293 gno_t * p_gno_np_global_num_coord_each_part_actual,
3303 const gno_t *gnoList,
3309 vector<partId_t> *newFuturePartitions,
3311 vector<partId_t> &out_part_index,
3313 int doMigrationType,
3314 int *coordinate_destionations
3320 gno_t *p_gno_np_global_num_coord_each_part = p_gno_np_global_num_coord_each_part_actual + nprocs * num_parts;
3323 out_part_index.clear();
3327 uSortItem<partId_t, partId_t> * part_assignment = allocMemory <uSortItem<partId_t, partId_t> >(num_parts);
3328 uSortItem<partId_t, gno_t> * proc_load_sort = allocMemory <uSortItem<partId_t, gno_t> >(nprocs);
3333 lno_t work_each = nGlobalObj / (float (nprocs)) + 0.5f;
3335 lno_t *space_in_each_processor = allocMemory <lno_t>(nprocs);
3337 for (
partId_t i = 0; i < nprocs; ++i){
3338 space_in_each_processor[i] = work_each;
3344 uSortItem<partId_t, gno_t> * part_loads = allocMemory <uSortItem<partId_t, gno_t> >(num_parts);
3348 for (
partId_t i = 0; i < num_parts; ++i){
3349 part_loads[i].id = i;
3350 part_loads[i].val = p_gno_np_global_num_coord_each_part[i];
3353 uqsort<partId_t, gno_t>(num_parts, part_loads);
3359 for (
partId_t j = 0; j < num_parts; ++j){
3361 partId_t i = part_loads[num_parts - 1 - j].id;
3363 gno_t load = p_gno_np_global_num_coord_each_part[i];
3372 for (
partId_t ii = 0; ii < nprocs; ++ii){
3373 proc_load_sort[ii].id = ii;
3375 proc_load_sort[ii].val = p_gno_np_global_num_coord_each_part_actual[ii * num_parts + i];
3377 uqsort<partId_t, gno_t>(nprocs, proc_load_sort);
3384 for (
partId_t iii = nprocs - 1; iii >= 0; --iii){
3386 partId_t ii = proc_load_sort[iii].id;
3387 lno_t left_space = space_in_each_processor[ii] - load;
3389 if(left_space >= 0 ){
3394 if (space_in_each_processor[best_proc_to_assign] < space_in_each_processor[ii]){
3395 best_proc_to_assign = ii;
3398 if (assigned_proc == -1){
3399 assigned_proc = best_proc_to_assign;
3401 space_in_each_processor[assigned_proc] -= load;
3404 part_assignment[j].id = i;
3405 part_assignment[j].val = assigned_proc;
3409 if (assigned_proc == myRank){
3411 out_part_index.push_back(i);
3414 sendCount[assigned_proc] += p_gno_np_global_num_coord_each_part_actual[myRank * num_parts + i];
3416 freeArray< uSortItem<partId_t, gno_t> > (proc_load_sort);
3417 freeArray<uSortItem<partId_t, gno_t> >(part_loads);
3418 freeArray<lno_t >(space_in_each_processor);
3422 uqsort<partId_t, partId_t>(num_parts, part_assignment);
3425 if (doMigrationType == 0){
3426 fillContinousSendBuffer2< partId_t, lno_t, gno_t>(
3434 newFuturePartitions,
3439 Z1fillSendBuffer2< partId_t, lno_t>(
3444 coordinate_destionations,
3446 newFuturePartitions,
3452 freeArray<uSortItem<partId_t, partId_t> >(part_assignment);
3455 template <
typename partId_t,
typename lno_t,
typename gno_t>
3456 void procAssignment1(
3457 RCP<Comm<int> > &pcomm,
3458 int assignment_type,
3459 gno_t * p_gno_np_global_num_coord_each_part_actual,
3469 const gno_t *gnoList,
3473 vector<partId_t> &ids,
3477 vector<partId_t> *newFuturePartitions,
3481 int doMigrationType,
3482 int *coordinate_destionations
3488 gno_t *p_gno_np_global_num_coord_each_part = p_gno_np_global_num_coord_each_part_actual + nprocs * num_parts;
3492 partId_t *p_pid_np_num_procs_each_part = allocMemory<partId_t>(num_parts);
3494 bool did_i_find_my_group =
false;
3499 partId_t min_required_for_rest = num_parts - 1;
3501 float max_difference = 0;
3505 for (
partId_t i=0; i < num_parts; i++){
3508 float scalar_required_proc = nprocs *
3509 (float (p_gno_np_global_num_coord_each_part[i])
3515 floor (0.5f + scalar_required_proc));
3519 if (left_proc - required_proc < min_required_for_rest){
3520 required_proc = left_proc - (min_required_for_rest);
3522 left_proc -= required_proc;
3523 --min_required_for_rest;
3524 p_pid_np_num_procs_each_part[i] = required_proc;
3528 float imbalance_wrt_ideal = (scalar_required_proc - required_proc) / required_proc;
3529 if (imbalance_wrt_ideal > max_difference){
3530 max_difference = imbalance_wrt_ideal;
3531 max_differ_part = i;
3541 p_pid_np_num_procs_each_part[max_differ_part] += left_proc;
3547 partId_t *part_assign_begins = allocMemory<partId_t>(num_parts);
3548 partId_t *proc_chains = allocMemory<partId_t>(nprocs);
3549 partId_t *proc_part_assignments = allocMemory<partId_t>(nprocs);
3558 for (
int i = 0; i < nprocs; ++i ){
3559 proc_part_assignments[i] = -1;
3560 proc_chains[i] = -1;
3562 for (
int i = 0; i < num_parts; ++i ){
3563 part_assign_begins[i] = -1;
3573 uSortItem<partId_t, lno_t> * proc_points_in_part = allocMemory <uSortItem<partId_t, partId_t> > (nprocs);
3574 for(
partId_t i = 0; i < num_parts; ++i){
3578 if(assignment_type == 0){
3579 for(
partId_t ii = 0; ii < nprocs; ++ii){
3581 proc_points_in_part[ii].id = ii;
3583 if (proc_part_assignments[ii] == -1){
3584 proc_points_in_part[ii].val = p_gno_np_global_num_coord_each_part_actual[ii * num_parts + i];
3588 proc_points_in_part[ii].val = -p_gno_np_global_num_coord_each_part_actual[ii * num_parts + i] - 1;
3596 partId_t required_proc = p_pid_np_num_procs_each_part[i];
3597 partId_t last_proc_to_assign = next_proc_to_assign + required_proc;
3598 for(
partId_t ii = 0; ii < nprocs; ++ii){
3599 proc_points_in_part[ii].id = ii;
3600 if (ii >= next_proc_to_assign && ii < last_proc_to_assign){
3601 proc_points_in_part[ii].val = p_gno_np_global_num_coord_each_part_actual[ii * num_parts + i];
3604 proc_points_in_part[ii].val = -p_gno_np_global_num_coord_each_part_actual[ii * num_parts + i] - 1;
3607 next_proc_to_assign = last_proc_to_assign;
3611 uqsort<partId_t, partId_t>(nprocs, proc_points_in_part);
3613 partId_t required_proc_count = p_pid_np_num_procs_each_part[i];
3614 gno_t total_num_points_in_part = p_gno_np_global_num_coord_each_part[i];
3615 gno_t ideal_num_points_in_procs =
3616 ceil (total_num_points_in_part /
float (required_proc_count));
3619 partId_t next_part_to_send = nprocs - required_proc_count;
3620 partId_t next_part_to_send_id = proc_points_in_part[next_part_to_send].id;
3621 lno_t space_left = ideal_num_points_in_procs - proc_points_in_part[next_part_to_send].val;
3624 for(
partId_t ii = nprocs - 1; ii >= nprocs - required_proc_count; --ii){
3626 partId_t partid = proc_points_in_part[ii].id;
3627 proc_part_assignments[partid] = i;
3640 bool did_change_anything =
false;
3641 for(
partId_t ii = 0; ii < nprocs; ++ii){
3642 if (proc_points_in_part[ii].val < 0){
3643 did_change_anything =
true;
3644 proc_points_in_part[ii].val = -proc_points_in_part[ii].val - 1;
3651 if(did_change_anything){
3653 uqsort<partId_t, partId_t>(nprocs - required_proc_count, proc_points_in_part);
3672 if (!did_i_find_my_group){
3673 for(
partId_t ii = nprocs - 1; ii >= nprocs - required_proc_count; --ii){
3674 partId_t partid = proc_points_in_part[ii].id;
3676 ids.push_back(partid);
3679 if(partid == myRank){
3680 did_i_find_my_group =
true;
3681 part_assign_begins[i] = myRank;
3682 proc_chains[myRank] = -1;
3683 sendCount[myRank] = proc_points_in_part[ii].val;
3686 for (
partId_t in = 0; in < i; ++in){
3687 partIndexBegin += (*newFuturePartitions)[in];
3698 if (!did_i_find_my_group){
3706 for(
partId_t ii = nprocs - required_proc_count - 1; ii >= 0; --ii){
3707 partId_t partid = proc_points_in_part[ii].id;
3708 lno_t to_sent = proc_points_in_part[ii].val;
3712 if (to_sent < 0) to_sent = -to_sent - 1;
3715 while (to_sent > 0){
3718 if (to_sent <= space_left){
3729 space_left -= to_sent;
3731 if (myRank == partid){
3733 sendCount[next_part_to_send_id] = to_sent;
3737 partId_t prev_begin = part_assign_begins[i];
3738 part_assign_begins[i] = next_part_to_send_id;
3739 proc_chains[next_part_to_send_id] = prev_begin;
3755 to_sent -= space_left;
3758 if (myRank == partid){
3759 sendCount[next_part_to_send_id] = space_left;
3760 partId_t prev_begin = part_assign_begins[i];
3761 part_assign_begins[i] = next_part_to_send_id;
3762 proc_chains[next_part_to_send_id] = prev_begin;
3767 ++next_part_to_send;
3771 if(next_part_to_send < nprocs - required_proc_count ){
3772 cout <<
"this should not happen next part to send:" << next_part_to_send << endl;
3776 next_part_to_send_id = proc_points_in_part[next_part_to_send].id;
3777 space_left = ideal_num_points_in_procs - proc_points_in_part[next_part_to_send].val;
3788 if (doMigrationType == 0){
3789 fillContinousSendBuffer1 <partId_t, lno_t, gno_t> (
3805 Z1fillSendBuffer1 <partId_t, lno_t> (
3816 coordinate_destionations
3824 freeArray<partId_t>(part_assign_begins);
3825 freeArray<partId_t>(proc_chains);
3826 freeArray<partId_t>(proc_part_assignments);
3827 freeArray<uSortItem<partId_t, partId_t> > (proc_points_in_part);
3828 freeArray<partId_t > (p_pid_np_num_procs_each_part);
3832 template <
typename partId_t,
typename lno_t,
typename gno_t>
3833 void getProcGroups_SendCounts_SendBuff(
3835 RCP<Comm<int> > &pcomm,
3836 int migration_proc_assignment_type,
3837 int assignment_type,
3839 gno_t * p_gno_np_global_num_coord_each_part_actual,
3849 const gno_t *gnoList,
3853 vector<partId_t> &ids,
3858 vector<partId_t> *newFuturePartitions,
3860 vector<partId_t> &out_part_indices,
3862 int doMigrationType,
3863 int *coordinate_destionations
3882 if (1 || nLocal > 0){
3883 if (nprocs > num_parts){
3890 procAssignment1<partId_t, lno_t, gno_t>(
3893 p_gno_np_global_num_coord_each_part_actual,
3911 newFuturePartitions,
3916 coordinate_destionations
3924 out_part_indices.clear();
3925 out_part_indices.push_back(out_part_index);
3958 ids.push_back(myRank);
3960 procAssignment2<partId_t, lno_t, gno_t>(
3962 p_gno_np_global_num_coord_each_part_actual,
3978 newFuturePartitions,
3983 coordinate_destionations
4018 template <
typename partId_t,
typename lno_t,
typename gno_t,
typename scalar_t>
4020 const RCP<const Environment> &env,
4021 RCP<Comm<int> > &comm,
4022 int doMigrationType,
4028 ArrayRCP<gno_t> &recvBuf,
4029 gno_t &numMyNewGnos,
4036 gno_t *&coordinate_gnos,
4037 int *&coordinate_owners,
4038 int *coordinate_destionations,
4045 if (doMigrationType == 0){
4046 if (all2alloption == 2) {
4049 partId_t *partIds = allocMemory< partId_t>(nLocal);
4053 for (
int i = 0; i < nprocs; ++i){
4054 lno_t sendC = sendCount[i];
4055 for (
int ii = 0; ii < sendC; ++ii){
4060 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration DistPlanCreating-" + iteration);
4061 Tpetra::Distributor distributor(comm);
4063 ArrayView<const partId_t> pIds( partIds, nLocal);
4064 numMyNewGnos = distributor.createFromSends(pIds);
4065 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration DistPlanCreating-" + iteration);
4067 ArrayRCP<gno_t> recvBuf2(distributor.getTotalReceiveLength());
4069 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration DistPlanCom-" + iteration);
4070 ArrayView<gno_t> s(sendBuf, nLocal);
4071 distributor.doPostsAndWaits<gno_t>(s, 1, recvBuf2());
4072 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration DistPlanCom-" + iteration);
4074 freeArray<partId_t>(partIds);
4076 }
else if (all2alloption == 1){
4079 partId_t *partIds = allocMemory< partId_t>(nLocal);
4084 for (
int i = 0; i < nprocs; ++i){
4085 lno_t sendC = sendCount[i];
4087 for (
int ii = 0; ii < sendC; ++ii){
4093 ZOLTAN_COMM_OBJ *plan = NULL;
4096 MPI_Comm mpi_comm = Teuchos2MPI (comm);
4098 int message_tag = 7859;
4100 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration Z1PlanCreating-" + iteration);
4101 int ierr = Zoltan_Comm_Create(&plan, nLocal, partIds, mpi_comm, message_tag,
4103 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration Z1PlanCreating-" + iteration);
4106 ArrayRCP<gno_t> recvBuf2(incoming);
4107 gno_t *recieves = recvBuf2.getRawPtr();
4111 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration Z1PlanComm-" + iteration);
4112 ierr = Zoltan_Comm_Do(plan, message_tag, (
char *) sendBuf,
4115 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration Z1PlanComm-" + iteration);
4117 ierr = Zoltan_Comm_Destroy(&plan);
4118 numMyNewGnos = incoming;
4120 freeArray<partId_t>(partIds);
4123 ArrayView<gno_t> s (sendBuf, nLocal);
4124 ArrayView<int> sc (sendCount, nprocs);
4125 Array<lno_t> recvCount(nprocs, 0);
4127 AlltoAllv<gno_t>(*comm, *env,
4129 recvBuf, recvCount());
4133 for (
int i=0; i < nprocs; i++){
4134 numMyNewGnos += recvCount[i];
4151 ZOLTAN_COMM_OBJ *plan = NULL;
4154 MPI_Comm mpi_comm = Teuchos2MPI (comm);
4156 int message_tag = 7859;
4168 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration Z1PlanCreating-" + iteration);
4169 int ierr = Zoltan_Comm_Create(
4171 coordinate_destionations, mpi_comm,
4172 message_tag, &incoming);
4173 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration Z1PlanCreating-" + iteration);
4178 for (
int i = 0; i < nLocal; ++i){
4179 if (coordinate_destionations[i] == comm->getRank()){
4193 gno_t *incoming_gnos = allocMemory< gno_t>(incoming);
4196 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration Z1PlanComm-" + iteration);
4197 ierr = Zoltan_Comm_Do(
4199 (
char *) coordinate_gnos,
4200 sizeof(gno_t), (
char *) incoming_gnos);
4201 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration Z1PlanComm-" + iteration);
4202 freeArray<gno_t>(coordinate_gnos);
4203 coordinate_gnos = incoming_gnos;
4207 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration Z1Migration-" + iteration);
4208 for (
int i = 0; i < coord_dim; ++i){
4212 coords[i] = allocMemory<scalar_t>(incoming);
4213 ierr = Zoltan_Comm_Do(
4216 sizeof(
scalar_t), (
char *) coords[i]);
4217 freeArray<scalar_t>(coord);
4220 for (
int i = 0; i < weight_dim; ++i){
4224 weights[i] = allocMemory<scalar_t>(incoming);
4225 ierr = Zoltan_Comm_Do(
4228 sizeof(
scalar_t), (
char *) weights[i]);
4229 freeArray<scalar_t>(weight);
4233 int *coord_own = allocMemory<int>(incoming);
4235 ierr = Zoltan_Comm_Do(
4237 (
char *) coordinate_owners,
4238 sizeof(
int), (
char *) coord_own);
4239 freeArray<int>(coordinate_owners);
4240 coordinate_owners = coord_own;
4254 partId_t *new_parts = allocMemory<int>(incoming);
4257 if(nprocs < num_parts){
4259 ierr = Zoltan_Comm_Do(
4261 (
char *) assigned_parts,
4262 sizeof(
partId_t), (
char *) new_parts);
4265 freeArray<partId_t>(assigned_parts);
4266 assigned_parts = new_parts;
4267 ierr = Zoltan_Comm_Destroy(&plan);
4268 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration Z1Migration-" + iteration);
4269 numMyNewGnos = incoming;
4273 template <
typename mvector_t,
typename gno_t>
4274 void doActualMigration(
4275 const RCP<const Environment> &env,
4276 RCP<const mvector_t> &vectors,
4278 ArrayRCP<gno_t> recvBuf){
4281 vectors, numMyNewGnos, recvBuf.getRawPtr());
4286 template <
typename partId_t>
4287 void createSubCommunicator(
4288 RCP<Comm<int> > &comm,
4289 vector<partId_t> &proc_ids
4292 partId_t groupSize = proc_ids.size();
4293 partId_t *ids = allocMemory<partId_t>(groupSize);
4294 for(
partId_t i = 0; i < groupSize; ++i) {
4296 ids[i] = proc_ids[i];
4299 ArrayView<const partId_t> idView(ids, groupSize);
4300 comm = comm->createSubcommunicator(idView);
4301 freeArray<partId_t>(ids);
4305 template <
typename mvector_t,
typename lno_t,
typename gno_t,
typename scalar_t,
typename node_t>
4306 void create_new_multi_vector(
4307 const RCP<const Environment> &env,
4308 RCP<Comm<int> > &comm,
4309 RCP <const mvector_t> &mvector,
4313 typedef ArrayView<const scalar_t> coordList_t;
4314 typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
4316 ArrayView<const gno_t> gnoList = mvector->getMap()->getNodeElementList();
4317 size_t localSize = mvector->getLocalLength();
4320 size_t globalSize = Teuchos::OrdinalTraits<size_t>::invalid();
4324 subMap= rcp(
new map_t(globalSize, gnoList, 0, comm));
4328 coordList_t *avSubList =
allocMemory<coordList_t>(multiVectorDim);
4330 for (
int dim=0; dim < multiVectorDim; dim++)
4331 avSubList[dim] = mvector->getData(dim).view(0, localSize);
4333 ArrayRCP<const ArrayView<const
scalar_t> > subVectors =
4334 arcp(avSubList, 0, multiVectorDim);
4338 mvector = rcp(
new mvector_t(
4339 subMap, subVectors.view(0, multiVectorDim), multiVectorDim));
4344 template <
typename lno_t,
typename partId_t>
4346 lno_t numLocalPoints,
4347 lno_t prev_num_local,
4354 if (prev_num_local != numLocalPoints){
4355 freeArray<partId_t>(partArray);
4356 partArray = allocMemory<partId_t>(numLocalPoints);
4358 freeArray<lno_t>(permutation);
4359 freeArray<lno_t>(oldpermutation);
4360 oldpermutation = allocMemory<lno_t>(numLocalPoints);
4361 permutation = allocMemory<lno_t>(numLocalPoints);
4367 template <
typename gno_t,
typename lno_t,
typename scalar_t,
typename node_t>
4368 void getNewMultivectorArrays(
4369 RCP<Comm<int> > &comm,
4370 lno_t &numLocalPoints,
4376 ,RCP<
const Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> > coordsConst
4378 lno_t *&permutations,
4381 int pqJagged_multiVectorDim
4385 for (
int i=0; i < coord_dim; i++){
4386 ArrayRCP< const scalar_t > coord = coordsConst->getData(i);
4387 coords[i] =(
scalar_t *) coord.getRawPtr();
4390 for (
int i=0; i < weight_dim; i++){
4391 if (i + coord_dim >= pqJagged_multiVectorDim)
break;
4392 ArrayRCP< const scalar_t > wgts = coordsConst->getData(i+ coord_dim);
4393 weight[i] = (
scalar_t *) wgts.getRawPtr();
4397 if (out_num_parts == 1){
4399 for(lno_t i = 0; i < numLocalPoints; ++i){
4400 permutations[i] = i;
4402 part_begins[0] = numLocalPoints;
4407 scalar_t *assigned_parts = (
scalar_t *)coordsConst->getData (pqJagged_multiVectorDim).getRawPtr();
4408 lno_t *counts = allocMemory<lno_t>(num_parts);
4410 partId_t *part_shifts = allocMemory<partId_t>(num_parts);
4412 memset(counts, 0,
sizeof(lno_t) * num_parts);
4414 for(lno_t i = 0; i < numLocalPoints; ++i){
4419 lno_t prev_index = 0;
4420 for(
partId_t i = 0; i < num_parts; ++i){
4423 part_begins[p] = prev_index + counts[i];
4424 prev_index += counts[i];
4425 part_shifts[i] = p++;
4430 for (;p < num_parts; ++p){
4431 part_begins[p] = part_begins[assigned_count];
4433 for(
partId_t i = 0; i < out_num_parts; ++i){
4434 counts[i] = part_begins[i];
4436 for(lno_t i = numLocalPoints - 1; i >= 0; --i){
4438 permutations[--counts[part]] = i;
4441 freeArray<lno_t>(counts);
4442 freeArray<partId_t>(part_shifts);
4448 template <
typename lno_t,
typename partId_t>
4449 void fillPermutationArrays(
4450 RCP<Comm<int> > &comm,
4451 lno_t &numLocalPoints,
4453 lno_t *permutations,
4458 if (out_num_parts == 1){
4460 for(lno_t i = 0; i < numLocalPoints; ++i){
4461 permutations[i] = i;
4463 part_begins[0] = numLocalPoints;
4466 lno_t *counts = allocMemory<lno_t>(num_parts);
4468 partId_t *part_shifts = allocMemory<partId_t>(num_parts);
4470 memset(counts, 0,
sizeof(lno_t) * num_parts);
4472 for(lno_t i = 0; i < numLocalPoints; ++i){
4482 lno_t prev_index = 0;
4483 for(
partId_t i = 0; i < num_parts; ++i){
4486 part_begins[p] = prev_index + counts[i];
4487 prev_index += counts[i];
4488 part_shifts[i] = p++;
4493 for (;p < num_parts; ++p){
4494 part_begins[p] = part_begins[assigned_count];
4496 for(
partId_t i = 0; i < out_num_parts; ++i){
4497 counts[i] = part_begins[i];
4499 for(lno_t i = numLocalPoints - 1; i >= 0; --i){
4501 permutations[--counts[part]] = i;
4504 freeArray<lno_t>(counts);
4505 freeArray<partId_t>(part_shifts);
4510 template <
typename gno_t,
4515 bool migration_refactored(
4516 RCP<Comm<int> > &pcomm,
4517 const RCP<const Environment> &env,
4518 RCP<Comm<int> > &comm,
4520 RCP<
const Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> > &vectors,
4521 int pqJagged_multiVectorDim,
4523 gno_t &numGlobalPoints,
4524 lno_t &numLocalPoints,
4535 vector<partId_t> *newFuturePartitions,
4537 lno_t *&permutation,
4538 lno_t *&old_permutation,
4540 lno_t *partBeginArray,
4545 int assignment_type,
4546 int doMigrationType,
4547 int migration_check_option,
4548 scalar_t migration_imbalance_cut_off,
4549 size_t futureReduceAll,
4550 lno_t numCoordinatesForLastDimPartitioning,
4552 gno_t *&coordinate_gnos,
4553 int *&actual_gno_owner,
4555 int keep_part_boxes,
4556 RCP < vector <coordinateModelPartBox <scalar_t, partId_t> > > &inPartBoxes,
4557 RCP < vector <coordinateModelPartBox <scalar_t, partId_t> > > &outPartBoxes
4568 int migration_proc_assignment_type = 1;
4571 int multiVectorDim = pqJagged_multiVectorDim;
4574 gno_t *p_gno_np_global_num_coord_each_part_actual = NULL;
4580 getProcessorCoordinatePartCounts <gno_t, lno_t ,partId_t>(
4585 migration_proc_assignment_type,
4590 p_gno_np_global_num_coord_each_part_actual);
4594 if (!checkMigration <gno_t, lno_t ,partId_t>(
4599 migration_check_option,
4601 numCoordinatesForLastDimPartitioning,
4602 migration_imbalance_cut_off,
4604 migration_proc_assignment_type,
4609 p_gno_np_global_num_coord_each_part_actual,
4612 freeArray<gno_t>(p_gno_np_global_num_coord_each_part_actual);
4617 if (nprocs < num_parts) {
4618 if (doMigrationType == 0){
4619 vectors = createNewMultivector(
4639 for (
partId_t i = 0; i < num_parts; ++i){
4641 if (i > 0) pBegin = partBeginArray[i - 1];
4642 lno_t pEnd = partBeginArray[i];
4646 for (lno_t j = pBegin; j < pEnd; ++j){
4647 lno_t ind = permutation[j];
4648 assigned_parts_[ind] = i;
4654 const gno_t *gnoList = NULL;
4657 lno_t *sendCount = NULL;
4658 gno_t *sendBuf = NULL;
4659 int *coordinate_destionations = NULL;
4660 sendCount = allocMemory<lno_t>(nprocs);
4661 for (
int i = 0; i < nprocs; ++i) sendCount[i] = 0;
4663 if (doMigrationType == 0){
4665 sendBuf = allocMemory<gno_t>(numLocalPoints);
4667 ArrayView<const gno_t> gno_list = vectors->getMap()->getNodeElementList();
4668 gnoList = gno_list.getRawPtr();
4672 coordinate_destionations = allocMemory<int>(numLocalPoints);
4675 vector<partId_t> ids;
4676 vector<partId_t> out_part_indices;
4679 getProcGroups_SendCounts_SendBuff<partId_t, lno_t, gno_t>(
4681 migration_proc_assignment_type,
4684 p_gno_np_global_num_coord_each_part_actual,
4702 newFuturePartitions,
4707 coordinate_destionations
4709 vector <partId_t> tmpv;
4710 std::sort (out_part_indices.begin(), out_part_indices.end());
4711 partId_t outP = out_part_indices.size();
4713 gno_t newGlobalCount = 0;
4714 gno_t *p_gno_np_global_num_coord_each_part = p_gno_np_global_num_coord_each_part_actual + nprocs * num_parts;
4717 if (keep_part_boxes){
4718 inPartBoxes->clear();
4722 for (
partId_t i = 0; i < outP; ++i){
4723 partId_t ind = out_part_indices[i];
4725 newGlobalCount += p_gno_np_global_num_coord_each_part[ind];
4727 tmpv.push_back((*newFuturePartitions)[ind]);
4729 if (keep_part_boxes){
4732 inPartBoxes->push_back((*outPartBoxes)[ind]);
4737 if (keep_part_boxes){
4738 RCP < vector <coordinateModelPartBox <scalar_t, partId_t> > > tmpPartBoxes = inPartBoxes;
4739 inPartBoxes = outPartBoxes;
4740 outPartBoxes = tmpPartBoxes;
4743 newFuturePartitions->clear();
4745 for (
partId_t i = 0; i < outP; ++i){
4747 newFuturePartitions->push_back(p);
4750 ArrayRCP<gno_t> recvBuf;
4751 gno_t numMyNewGnos = 0;
4758 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration AlltoAll-" + iteration);
4759 doAll2All<partId_t, lno_t, gno_t, scalar_t>(
4778 coordinate_destionations,
4787 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration AlltoAll-" + iteration);
4790 if (doMigrationType == 0){
4791 freeArray<gno_t>(sendBuf);
4794 freeArray<int>(coordinate_destionations);
4796 if(numLocalPoints != numMyNewGnos){
4797 freeArray<lno_t>(permutation);
4798 freeArray<lno_t>(old_permutation);
4800 permutation = allocMemory<lno_t>(numMyNewGnos);
4801 old_permutation = allocMemory<lno_t>(numMyNewGnos);
4803 numLocalPoints = numMyNewGnos;
4804 numGlobalPoints = newGlobalCount;
4806 freeArray<lno_t>(sendCount);
4807 freeArray<gno_t>(p_gno_np_global_num_coord_each_part_actual);
4809 typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> mvector_t;
4810 if(doMigrationType == 0){
4811 env->timerStart(
MACRO_TIMERS,
"PQJagged - Migration doActualMigration-" + iteration);
4812 doActualMigration<mvector_t, gno_t>(
4818 env->timerStop(
MACRO_TIMERS,
"PQJagged - Migration doActualMigration-" + iteration);
4820 createSubCommunicator <partId_t>(comm, ids);
4823 if(doMigrationType == 0){
4824 create_new_multi_vector<mvector_t, lno_t, gno_t,scalar_t, node_t>(
4833 lno_t prev_num_local = numLocalPoints;
4834 numLocalPoints = vectors->getLocalLength();
4835 numGlobalPoints = vectors->getGlobalLength();
4839 resizeArrays<lno_t, partId_t>(
4848 getNewMultivectorArrays<gno_t, lno_t,scalar_t, node_t>(
4860 pqJagged_multiVectorDim
4865 fillPermutationArrays<lno_t,partId_t>(
4881 template <
typename scalar_t,
typename partId_t>
4884 vector <partId_t> &pAlongI,
4885 vector<partId_t> *currentPartitions,
4886 vector<partId_t> *newFuturePartitions,
4893 int keep_part_boxes,
4905 cout <<
"i:" << i <<
" p is given as:" << pAlongI[0] << endl;
4909 return currentPartitionCount;
4912 for (
partId_t ii = 0; ii < currentPartitionCount; ++ii){
4913 pAlongI.push_back(p);
4918 futurePartNumbers /= pAlongI[0];
4919 outPartCount = currentPartitionCount * pAlongI[0];
4921 if (keep_part_boxes){
4922 for (
partId_t k = 0; k < currentPartitionCount; ++k){
4923 for (
partId_t j = 0; j < pAlongI[0]; ++j){
4924 outPartBoxes->push_back((*inPartBoxes)[k]);
4932 for (
partId_t ii = 0; ii < outPartCount; ++ii){
4933 newFuturePartitions->push_back(futurePartNumbers);
4943 futurePartNumbers = 1;
4946 float fEpsilon = numeric_limits<float>::epsilon();
4947 for (
partId_t ii = 0; ii < currentPartitionCount; ++ii){
4949 partId_t numFuture = (*currentPartitions)[ii];
4953 partId_t numParts = getPartCount<partId_t>( numFuture, 1.0f / (partArraySize - i), fEpsilon);
4958 if (numParts > maxPartNo){
4959 cerr <<
"ERROR: maxPartNo calculation is wrong." << endl;
4963 pAlongI.push_back(numParts);
4967 outPartCount += numParts;
4970 partId_t idealNumFuture = numFuture / numParts;
4971 for (
partId_t iii = 0; iii < numParts; ++iii){
4972 partId_t fNofCuts = idealNumFuture;
4974 if (iii < numFuture % numParts){
4978 newFuturePartitions->push_back(fNofCuts);
4980 if (keep_part_boxes){
4982 outPartBoxes->push_back((*inPartBoxes)[ii]);
4988 if (fNofCuts > futurePartNumbers) futurePartNumbers = fNofCuts;
4993 return outPartCount;
5010 template <
typename scalar_t,
typename lno_t,
typename partId_t>
5012 scalar_t &maxCoordinate,
5013 scalar_t &minCoordinate,
5016 lno_t *inTotalCounts,
5017 lno_t *partitionedPointCoordinates,
5024 scalar_t coordinate_range = maxCoordinate - minCoordinate;
5026 lno_t coordinateEnd= inTotalCounts[currentPart];
5027 lno_t coordinateBegin = currentPart==0 ? 0: inTotalCounts[currentPart -1];
5031 if(
ABS(coordinate_range) < _EPSILON ){
5032 #ifdef HAVE_ZOLTAN2_OMP
5033 #pragma omp parallel for
5035 for(lno_t ii = coordinateBegin; ii < coordinateEnd; ++ii){
5036 partIds[partitionedPointCoordinates[ii]] = 0;
5043 scalar_t slice = coordinate_range / partition;
5045 #ifdef HAVE_ZOLTAN2_OMP
5046 #pragma omp parallel for
5048 for(lno_t ii = coordinateBegin; ii < coordinateEnd; ++ii){
5050 lno_t iii = partitionedPointCoordinates[ii];
5052 partIds[iii] = 2 * pp;
5059 template <
typename partId_t>
5062 size_t &numGlobalParts,
5067 partId_t &maxTotalCumulativePartCount,
5070 size_t &maxTotalPartCount
5073 for (
int i = 0; i < partArraySize; ++i){
5074 reduceAllCount += totalPartCount;
5075 totalPartCount *= partNo[i];
5076 if(partNo[i] > maxPartNo) maxPartNo = partNo[i];
5078 maxTotalCumulativePartCount = totalPartCount / partNo[partArraySize - 1];
5079 numGlobalParts = totalPartCount;
5081 float fEpsilon = numeric_limits<float>::epsilon();
5082 partId_t futureNumParts = numGlobalParts;
5084 for (
int i = 0; i < partArraySize; ++i){
5086 partId_t maxNoPartAlongI = getPartCount<partId_t>( futureNumParts, 1.0f / (partArraySize - i), fEpsilon);
5089 if (maxNoPartAlongI > maxPartNo){
5090 maxPartNo = maxNoPartAlongI;
5094 partId_t nfutureNumParts = futureNumParts / maxNoPartAlongI;
5095 if (futureNumParts % maxNoPartAlongI){
5098 futureNumParts = nfutureNumParts;
5101 totalPartCount = numGlobalParts;
5105 for (
int i = 0; i < partArraySize; ++i){
5106 reduceAllCount += p;
5111 maxTotalCumulativePartCount = p / maxPartNo;
5114 totalDimensionCut = totalPartCount - 1;
5115 maxCutNo = maxPartNo - 1;
5116 maxTotalPartCount = maxPartNo + size_t(maxCutNo);
5152 template <
typename lno_t,
typename scalar_t>
5157 lno_t *partitionedPointPermutations,
5158 scalar_t *pqJagged_coordinates,
5159 bool pqJagged_uniformWeights,
5160 scalar_t *coordWeights,
5161 scalar_t *cutCoordinates,
5162 lno_t coordinateBegin,
5163 lno_t coordinateEnd,
5165 bool allowNonRectelinearPart,
5166 float *actual_ratios,
5167 scalar_t *localPartWeights,
5168 double **partWeights,
5169 float **nonRectelinearRatios,
5171 lno_t ** partPointCounts,
5173 lno_t *newpartitionedPointPermutations,
5177 bool isSequentialAndPricise,
5178 scalar_t **pqJaggedAllCoordinates,
5186 scalar_t _EPSILON = numeric_limits<scalar_t>::epsilon();
5189 #ifdef HAVE_ZOLTAN2_OMP
5190 #pragma omp parallel
5194 #ifdef HAVE_ZOLTAN2_OMP
5195 me = omp_get_thread_num();
5198 lno_t *myPartPointCounts = partPointCounts[me];
5199 float *myRatios = NULL;
5200 if (allowNonRectelinearPart){
5203 myRatios = nonRectelinearRatios[me];
5204 #ifdef HAVE_ZOLTAN2_OMP
5207 for (
partId_t i = 0; i < noCuts; ++i){
5210 scalar_t leftWeight = actual_ratios[i];
5211 for(
int ii = 0; ii < noThreads; ++ii){
5212 if(leftWeight > _EPSILON){
5214 scalar_t ithWeight = partWeights[ii][i * 2 + 1] - partWeights[ii][i * 2 ];
5215 if(ithWeight < leftWeight){
5216 nonRectelinearRatios[ii][i] = ithWeight;
5219 nonRectelinearRatios[ii][i] = leftWeight ;
5221 leftWeight -= ithWeight;
5224 nonRectelinearRatios[ii][i] = 0;
5235 for (
partId_t i = noCuts - 1; i > 0 ; --i){
5236 if(
ABS(cutCoordinates[i] - cutCoordinates[i -1]) < _EPSILON){
5237 myRatios[i] -= myRatios[i - 1] ;
5251 for(
partId_t ii = 0; ii < partNo; ++ii){
5252 myPartPointCounts[ii] = 0;
5255 if(!allowNonRectelinearPart || !isSequentialAndPricise) {
5257 #ifdef HAVE_ZOLTAN2_OMP
5260 for (lno_t ii = coordinateBegin; ii < coordinateEnd; ++ii){
5262 lno_t i = partitionedPointPermutations[ii];
5263 scalar_t w = pqJagged_uniformWeights? 1:coordWeights[i];
5267 if(allowNonRectelinearPart && myRatios[p] > _EPSILON *
EPS_SCALE){
5270 if(myRatios[p] < 0 && p < noCuts - 1 &&
ABS(cutCoordinates[p+1] - cutCoordinates[p]) < _EPSILON){
5271 myRatios[p + 1] += myRatios[p];
5273 ++myPartPointCounts[p];
5281 while(allowNonRectelinearPart &&
5284 if(
ABS(cutCoordinates[p] - cutCoordinates[p - 1]) < _EPSILON){
5286 if(myRatios[p] > _EPSILON * EPS_SCALE && myRatios[p] >=
ABS(myRatios[p] - w)){
5289 if(myRatios[p] < 0 && p < noCuts - 1 &&
ABS(cutCoordinates[p+1] - cutCoordinates[p]) < _EPSILON){
5290 myRatios[p + 1] += myRatios[p];
5308 ++myPartPointCounts[p];
5313 ++myPartPointCounts[p];
5331 partId_t *cutMap = allocMemory<partId_t> (noCuts);
5334 typedef std::vector< multiSItem > multiSVector;
5335 typedef std::vector<multiSVector> multiS2Vector;
5337 std::vector<scalar_t *>allocated_memory;
5341 multiS2Vector cutPointSortArrays;
5346 multiSVector tmpMultiSVector;
5347 cutPointSortArrays.push_back(tmpMultiSVector);
5349 for (
partId_t i = 1; i < noCuts ; ++i){
5350 if(
ABS(cutCoordinates[i] - cutCoordinates[i -1]) < _EPSILON){
5351 cutMap[i] = cutMap[i-1];
5354 cutMap[i] = differentCutCount++;
5355 multiSVector tmp2MultiSVector;
5356 cutPointSortArrays.push_back(tmp2MultiSVector);
5361 #ifdef HAVE_ZOLTAN2_OMP
5364 for (lno_t ii = coordinateBegin; ii < coordinateEnd; ++ii){
5366 lno_t i = partitionedPointPermutations[ii];
5373 scalar_t *
vals = allocMemory<scalar_t>(pqCoordDim -1);
5374 allocated_memory.push_back(vals);
5377 for(
int dim = currentCoord + 1; dim < pqCoordDim; ++dim){
5378 vals[val_ind++] = pqJaggedAllCoordinates[dim][i];
5380 for(
int dim = 0; dim < currentCoord; ++dim){
5381 vals[val_ind++] = pqJaggedAllCoordinates[dim][i];
5383 multiSItem tempSortItem(i, pqCoordDim -1, vals);
5386 cutPointSortArrays[cmap].push_back(tempSortItem);
5390 ++myPartPointCounts[p];
5396 for (
partId_t i = 0; i < differentCutCount; ++i){
5397 std::sort (cutPointSortArrays[i].begin(), cutPointSortArrays[i].end());
5421 scalar_t leftOver = 0;
5422 for (
partId_t p = 0; p < noCuts; ++p){
5441 if (prevMap != mappedCut){
5443 lno_t vEnd = (
lno_t)cutPointSortArrays[prevMap].size() - 1;
5444 for (; vEnd >= 0; --vEnd){
5445 multiSItem t = cutPointSortArrays[prevMap][vEnd];
5450 ++myPartPointCounts[p];
5453 cutPointSortArrays[prevMap].clear();
5456 lno_t vEnd = (
lno_t)cutPointSortArrays[mappedCut].size() - 1;
5461 for (; vEnd >= 0; --vEnd){
5462 multiSItem t = cutPointSortArrays[mappedCut][vEnd];
5464 scalar_t w = pqJagged_uniformWeights? 1:coordWeights[i];
5466 if(myRatios[p] + leftOver> _EPSILON *
EPS_SCALE &&
5467 myRatios[p] + leftOver -
ABS(myRatios[p] + leftOver - w) > _EPSILON){
5481 cutPointSortArrays[mappedCut].pop_back();
5482 ++myPartPointCounts[p];
5485 if(p < noCuts - 1 && myRatios[p] < _EPSILON){
5486 if(mappedCut == cutMap[p + 1] ){
5487 if (prevMap != mappedCut){
5488 leftOver = myRatios[p];
5491 leftOver += myRatios[p];
5495 leftOver = -myRatios[p];
5500 if(p < noCuts - 1 && mappedCut == cutMap[p + 1]){
5501 if (prevMap != mappedCut){
5502 leftOver = myRatios[p];
5505 leftOver += myRatios[p];
5509 leftOver = -myRatios[p];
5514 prevMap = mappedCut;
5517 lno_t vEnd = (
lno_t)cutPointSortArrays[prevMap].size() - 1;
5518 for (; vEnd >= 0; --vEnd){
5519 multiSItem t = cutPointSortArrays[prevMap][vEnd];
5524 ++myPartPointCounts[noCuts];
5525 partIds[i] = noCuts;
5527 cutPointSortArrays[prevMap].clear();
5530 freeArray<partId_t> (cutMap);
5533 lno_t vSize = (
lno_t) allocated_memory.size();
5534 for(lno_t i = 0; i < vSize; ++i){
5535 freeArray<scalar_t> (allocated_memory[i]);
5540 #ifdef HAVE_ZOLTAN2_OMP
5543 for(
partId_t j = 0; j < partNo; ++j){
5545 for (
int i = 0; i < noThreads; ++i){
5546 lno_t threadPartPointCount = partPointCounts[i][j];
5547 partPointCounts[i][j] = pwj;
5548 pwj += threadPartPointCount;
5551 totalCounts[j] = pwj;
5554 #ifdef HAVE_ZOLTAN2_OMP
5558 for(
partId_t j = 1; j < partNo; ++j){
5559 totalCounts[j] += totalCounts[j - 1];
5563 for(
partId_t j = 1; j < partNo; ++j){
5564 myPartPointCounts[j] += totalCounts[j - 1] ;
5568 #ifdef HAVE_ZOLTAN2_OMP
5571 for (lno_t ii = coordinateBegin; ii < coordinateEnd; ++ii){
5572 lno_t i = partitionedPointPermutations[ii];
5574 newpartitionedPointPermutations[coordinateBegin + myPartPointCounts[p]++] = i;
5582 template <
typename scalar_t,
typename lno_t,
typename partId_t>
5584 const RCP<const Environment> &env,
5585 lno_t numLocalCoords,
5586 lno_t actualNumCoords,
5587 size_t numGlobalParts,
5589 scalar_t **pqJagged_coordinates,
5590 lno_t *output_permutation,
5591 lno_t *output_partIndices
5596 const RCP<Comm<int> > commN;
5597 RCP<Comm<int> >comm = Teuchos::rcp_const_cast<Comm<int> >
5598 (Teuchos::DefaultComm<int>::getDefaultSerialComm(commN));
5602 int partArraySize = 0;
5605 bool pqJagged_uniformWeights[1];
5606 pqJagged_uniformWeights[0] =
true;
5610 bool pqJagged_uniformParts[1];
5611 pqJagged_uniformParts[0] =
true;
5612 scalar_t *pqJagged_partSizes[1];
5613 pqJagged_partSizes[0] = NULL;
5616 scalar_t pqJagged_weights[1][1];
5620 lno_t *partitionedPointCoordinates = allocMemory< lno_t>(numLocalCoords);
5623 #ifdef HAVE_ZOLTAN2_OMP
5624 #pragma omp parallel for
5626 for(
size_t i = 0; i < static_cast<size_t>(numLocalCoords); ++i){
5627 partitionedPointCoordinates[i] = output_permutation[i];
5631 lno_t *newpartitionedPointCoordinates = allocMemory< lno_t>(numLocalCoords);
5632 #ifdef HAVE_ZOLTAN2_OMP
5634 firstTouch<lno_t>(newpartitionedPointCoordinates, numLocalCoords);
5640 ArrayRCP<partId_t> partId;
5641 if(numLocalCoords > 0){
5642 partIds = allocMemory<partId_t>(numLocalCoords);
5646 partId_t currentPartitionCount = 1;
5650 lno_t *inTotalCounts = allocMemory<lno_t>(1);
5651 inTotalCounts[0] =
static_cast<lno_t
>(actualNumCoords);
5653 lno_t *outTotalCounts = NULL;
5658 bool allowNonRectelinearPart =
true;
5659 int concurrentPartCount = 1;
5663 #ifdef HAVE_ZOLTAN2_OMP
5664 #pragma omp parallel shared(numThreads)
5666 numThreads = omp_get_num_threads();
5676 partId_t maxTotalCumulativePartCount = 1;
5679 size_t maxTotalPartCount = 0;
5681 partArraySize = coordDim;
5682 getPartSpecifications <partId_t>(
5691 maxTotalCumulativePartCount,
5702 scalar_t *allCutCoordinates = allocMemory< scalar_t>(totalDimensionCut);
5705 scalar_t *max_min_array = allocMemory< scalar_t>(numThreads * 2);
5707 float *nonRectelinearPart = NULL;
5708 float **nonRectRatios = NULL;
5709 if(allowNonRectelinearPart){
5710 nonRectelinearPart = allocMemory<float>(maxCutNo * concurrentPartCount);
5711 #ifdef HAVE_ZOLTAN2_OMP
5713 firstTouch<float>(nonRectelinearPart, maxCutNo);
5716 nonRectRatios = allocMemory<float *>(numThreads);
5717 for(
int i = 0; i < numThreads; ++i){
5718 nonRectRatios[i] = allocMemory<float>(maxCutNo);
5720 #ifdef HAVE_ZOLTAN2_OMP
5722 #pragma omp parallel
5724 int me = omp_get_thread_num();
5725 for(
partId_t ii = 0; ii < maxCutNo; ++ii){
5726 nonRectRatios[me][ii] = 0;
5736 scalar_t *cutCoordinatesWork = allocMemory<scalar_t>(maxCutNo * concurrentPartCount);
5738 #ifdef HAVE_ZOLTAN2_OMP
5740 firstTouch<scalar_t>(cutCoordinatesWork, maxCutNo);
5745 scalar_t *targetPartWeightRatios = allocMemory<scalar_t>(maxPartNo * concurrentPartCount);
5746 #ifdef HAVE_ZOLTAN2_OMP
5748 firstTouch<scalar_t>(cutPartRatios, maxCutNo);
5753 scalar_t *cutUpperBounds = allocMemory<scalar_t>(maxCutNo * concurrentPartCount);
5754 scalar_t *cutLowerBounds = allocMemory<scalar_t>(maxCutNo* concurrentPartCount);
5755 scalar_t *cutLowerWeight = allocMemory<scalar_t>(maxCutNo* concurrentPartCount);
5756 scalar_t *cutUpperWeight = allocMemory<scalar_t>(maxCutNo* concurrentPartCount);
5758 scalar_t *localMinMaxTotal = allocMemory<scalar_t>(3 * concurrentPartCount);
5759 scalar_t *globalMinMaxTotal = allocMemory<scalar_t>(3 * concurrentPartCount);
5763 bool *isDone = allocMemory<bool>(maxCutNo * concurrentPartCount);
5766 partId_t *myNonDoneCount = allocMemory<partId_t>(concurrentPartCount);
5768 double **partWeights = allocMemory<double *>(numThreads);
5770 double **pws = allocMemory<double *>(numThreads);
5773 scalar_t **leftClosestDistance = allocMemory<scalar_t *>(numThreads);
5775 scalar_t **rightClosestDistance = allocMemory<scalar_t *>(numThreads);
5778 lno_t **partPointCounts = allocMemory<lno_t *>(numThreads);
5780 for(
int i = 0; i < numThreads; ++i){
5782 partWeights[i] = allocMemory < double >(maxTotalPartCount * concurrentPartCount);
5783 rightClosestDistance[i] = allocMemory<scalar_t>(maxCutNo * concurrentPartCount);
5784 leftClosestDistance[i] = allocMemory<scalar_t>(maxCutNo * concurrentPartCount);
5785 partPointCounts[i] = allocMemory<lno_t>(maxPartNo);
5787 #ifdef HAVE_ZOLTAN2_OMP
5789 #pragma omp parallel
5791 int me = omp_get_thread_num();
5792 for(
partId_t ii = 0; ii < maxPartNo; ++ii){
5793 partPointCounts[me][ii] = 0;
5795 for(
size_t ii = 0; ii < maxTotalPartCount; ++ii){
5796 partWeights[me][ii] = 0;
5798 for(
partId_t ii = 0; ii < maxCutNo; ++ii){
5799 rightClosestDistance[me][ii] = 0;
5800 leftClosestDistance[me][ii] = 0;
5807 scalar_t *cutWeights = allocMemory<scalar_t>(maxCutNo);
5808 scalar_t *globalCutWeights = allocMemory<scalar_t>(maxCutNo);
5814 scalar_t *totalPartWeights_leftClosests_rightClosests = allocMemory<scalar_t>((maxTotalPartCount + maxCutNo * 2) * concurrentPartCount);
5815 scalar_t *global_totalPartWeights_leftClosests_rightClosests = allocMemory<scalar_t>((maxTotalPartCount + maxCutNo * 2) * concurrentPartCount);
5818 scalar_t *cutCoordinates = allCutCoordinates;
5821 scalar_t maxScalar_t = numeric_limits<scalar_t>::max();
5822 scalar_t minScalar_t = -numeric_limits<scalar_t>::max();
5826 scalar_t _EPSILON = numeric_limits<scalar_t>::epsilon();
5828 partId_t futurePartNumbers = totalPartCount;
5830 vector<partId_t> *currentPartitions =
new vector<partId_t> ();
5831 vector<partId_t> *newFuturePartitions =
new vector<partId_t> ();
5832 newFuturePartitions->push_back(numGlobalParts);
5833 RCP < vector <coordinateModelPartBox <scalar_t, partId_t> > > t1;
5834 RCP < vector <coordinateModelPartBox <scalar_t, partId_t> > > t2;
5837 for (
int i = 0; i < partArraySize; ++i){
5843 vector <partId_t> pAlongI;
5861 vector<partId_t> *tmpPartVect= currentPartitions;
5862 currentPartitions = newFuturePartitions;
5863 newFuturePartitions = tmpPartVect;
5870 newFuturePartitions->clear();
5873 partId_t outPartCount = getPartitionArrays<scalar_t, partId_t>(
5877 newFuturePartitions,
5879 currentPartitionCount,
5888 if(outPartCount == currentPartitionCount) {
5889 tmpPartVect= currentPartitions;
5890 currentPartitions = newFuturePartitions;
5891 newFuturePartitions = tmpPartVect;
5896 int coordInd = i % coordDim;
5897 scalar_t * pqCoord = pqJagged_coordinates[coordInd];
5900 string istring = toString<int>(i);
5905 outTotalCounts = allocMemory<lno_t>(outPartCount);
5915 partId_t concurrentPart = min(currentPartitionCount - currentWorkPart, concurrentPartCount);
5918 bool useBinarySearch =
true;
5922 for (; currentWorkPart < currentPartitionCount; currentWorkPart += concurrentPart){
5924 concurrentPart = min(currentPartitionCount - currentWorkPart, concurrentPartCount);
5929 for(
int kk = 0; kk < concurrentPart; ++kk){
5930 partId_t currentPart = currentWorkPart + kk;
5934 if (pAlongI[currentPart] == 1){
5938 lno_t coordinateEnd= inTotalCounts[currentPart];
5939 lno_t coordinateBegin = currentPart==0 ? 0: inTotalCounts[currentPart -1];
5941 pqJagged_getLocalMinMaxTotalCoord<scalar_t, lno_t>(
5942 partitionedPointCoordinates,
5944 pqJagged_uniformWeights[0],
5945 pqJagged_weights[0],
5952 localMinMaxTotal[kk],
5953 localMinMaxTotal[kk + concurrentPart],
5954 localMinMaxTotal[kk + 2*concurrentPart]
5961 if (workPartCount > 0){
5963 pqJagged_getGlobalMinMaxTotalCoord<scalar_t>(
5980 for(
int kk = 0; kk < concurrentPart; ++kk){
5981 scalar_t minCoordinate = globalMinMaxTotal[kk];
5982 scalar_t maxCoordinate = globalMinMaxTotal[kk + concurrentPart];
5983 scalar_t globalTotalWeight = globalMinMaxTotal[kk + 2 * concurrentPart];
5985 partId_t currentPart = currentWorkPart + kk;
5987 partId_t partition = pAlongI[currentPart];
5989 scalar_t *usedCutCoordinate = cutCoordinates + cutShifts;
5990 scalar_t *usedCutPartRatios = targetPartWeightRatios + partShift;
5992 cutShifts += partition - 1;
5994 partShift += partition;
5999 if(partition > 1 && minCoordinate <= maxCoordinate){
6002 allDone += partition - 1;
6004 myNonDoneCount[kk] = partition - 1;
6007 pqJagged_getCutCoord_Weights<scalar_t>(
6010 pqJagged_uniformParts[0],
6011 pqJagged_uniformWeights[0],
6012 pqJagged_partSizes[0],
6019 newFuturePartitions,
6025 getInitialPartAssignments<scalar_t, lno_t, partId_t>(
6030 partitionedPointCoordinates,
6039 myNonDoneCount[kk] = 0;
6041 obtainedPartCount += partition;
6045 scalar_t used_imbalance = 0;
6048 pqJagged_1D_Partition<scalar_t, lno_t>(
6051 partitionedPointCoordinates,
6053 pqJagged_uniformWeights[0],
6054 pqJagged_weights[0],
6055 targetPartWeightRatios,
6068 leftClosestDistance,
6069 rightClosestDistance,
6076 totalPartWeights_leftClosests_rightClosests,
6077 global_totalPartWeights_leftClosests_rightClosests,
6078 allowNonRectelinearPart,
6095 size_t tlrShift = 0;
6098 for(
int kk = 0; kk < concurrentPart; ++kk){
6099 partId_t curr = currentWorkPart + kk;
6103 if((noParts != 1 )&& globalMinMaxTotal[kk] > globalMinMaxTotal[kk + concurrentPart]) {
6105 for(
partId_t jj = 0; jj < noParts; ++jj){
6106 outTotalCounts[currentOut + outShift + jj] = 0;
6108 cutShift += noParts - 1;
6109 tlrShift += (4 *(noParts - 1) + 1);
6110 outShift += noParts;
6111 pwShift += (2 * (noParts - 1) + 1);
6115 lno_t coordinateEnd= inTotalCounts[curr];
6116 lno_t coordinateBegin = curr==0 ? 0: inTotalCounts[curr -1];
6117 scalar_t *usedCutCoordinate = cutCoordinates + cutShift;
6118 float *usednonRectelinearPart = nonRectelinearPart + cutShift;
6120 scalar_t *tlr = totalPartWeights_leftClosests_rightClosests + tlrShift;
6122 for(
int ii = 0; ii < numThreads; ++ii){
6123 pws[ii] = partWeights[ii] + pwShift;
6130 getChunksFromCoordinates<lno_t,scalar_t>(
6133 partitionedPointCoordinates,
6135 pqJagged_uniformWeights[0],
6136 pqJagged_weights[0],
6142 allowNonRectelinearPart,
6143 usednonRectelinearPart,
6152 newpartitionedPointCoordinates,
6153 outTotalCounts + currentOut + outShift,
6157 pqJagged_coordinates,
6167 lno_t partSize = coordinateEnd - coordinateBegin;
6168 *(outTotalCounts + currentOut + outShift) = partSize;
6169 memcpy(newpartitionedPointCoordinates + coordinateBegin, partitionedPointCoordinates + coordinateBegin, partSize *
sizeof(lno_t));
6171 cutShift += noParts - 1;
6172 tlrShift += (4 *(noParts - 1) + 1);
6173 outShift += noParts;
6174 pwShift += (2 * (noParts - 1) + 1);
6178 cutCoordinates += cutShift;
6183 for(
partId_t kk = 0; kk < concurrentPart; ++kk){
6184 partId_t noParts = pAlongI[ currentWorkPart + kk];
6185 for (
partId_t ii = 0;ii < noParts ; ++ii){
6187 outTotalCounts[currentOut+ii] += previousEnd;
6190 previousEnd = outTotalCounts[currentOut + noParts - 1];
6192 currentOut += noParts ;
6198 currentPartitionCount = outPartCount;
6199 lno_t * tmp = partitionedPointCoordinates;
6200 partitionedPointCoordinates = newpartitionedPointCoordinates;
6201 newpartitionedPointCoordinates = tmp;
6202 freeArray<lno_t>(inTotalCounts);
6203 inTotalCounts = outTotalCounts;
6208 #ifdef HAVE_ZOLTAN2_OMP
6209 #pragma omp parallel for
6211 for(lno_t i = 0; i < numLocalCoords; ++i){
6212 output_permutation[i] = partitionedPointCoordinates[i];
6215 for(
size_t i = 0; i < numGlobalParts ; ++i){
6217 output_partIndices[i] = inTotalCounts[i];
6221 delete currentPartitions;
6222 delete newFuturePartitions;
6224 freeArray<lno_t>(partitionedPointCoordinates);
6225 freeArray<lno_t>(newpartitionedPointCoordinates);
6226 freeArray<lno_t>(inTotalCounts);
6228 freeArray<scalar_t>(allCutCoordinates);
6229 freeArray<scalar_t>(cutCoordinatesWork);
6230 freeArray<scalar_t>(max_min_array);
6232 if(allowNonRectelinearPart){
6233 freeArray<float>(nonRectelinearPart);
6234 for(
int i = 0; i < numThreads; ++i){
6235 freeArray<float>(nonRectRatios[i]);
6237 freeArray<float *>(nonRectRatios);
6239 freeArray<scalar_t>(targetPartWeightRatios);
6241 freeArray<scalar_t>(cutUpperBounds);
6242 freeArray<scalar_t>(cutLowerBounds);
6243 freeArray<scalar_t>(cutLowerWeight);
6244 freeArray<scalar_t>(cutUpperWeight);
6245 freeArray<scalar_t> (localMinMaxTotal);
6246 freeArray<scalar_t> (globalMinMaxTotal);
6247 freeArray<bool>(isDone);
6248 freeArray<partId_t>(myNonDoneCount);
6250 for(
int i = 0; i < numThreads; ++i){
6251 freeArray<double>(partWeights[i]);
6252 freeArray<scalar_t>(rightClosestDistance[i]);
6253 freeArray<scalar_t>(leftClosestDistance[i]);
6255 freeArray<double *>(partWeights);
6256 freeArray<scalar_t *>(leftClosestDistance);
6257 freeArray<scalar_t *>(rightClosestDistance);
6259 freeArray<double *> (pws);
6261 for(
int i = 0; i < numThreads; ++i){
6262 freeArray<lno_t>(partPointCounts[i]);
6264 freeArray<lno_t *>(partPointCounts);
6265 freeArray<scalar_t>(cutWeights);
6267 freeArray<scalar_t>(globalCutWeights);
6268 freeArray<scalar_t>(totalPartWeights_leftClosests_rightClosests);
6269 freeArray<scalar_t>(global_totalPartWeights_leftClosests_rightClosests);
6282 template <
typename Adapter>
6284 const RCP<const Environment> &env,
6285 RCP<Comm<int> > &problemComm,
6287 typename Adapter::base_adapter_t> > &coords,
6291 #ifndef INCLUDE_ZOLTAN2_EXPERIMENTAL
6294 "while it is being developed and tested.")
6318 const Teuchos::ParameterList &pl = env->getParameters();
6320 std::bitset<NUM_RCB_PARAMS> params;
6321 int numTestCuts = 5;
6324 scalar_t imbalanceTolerance;
6327 bool ignoreWeights=
false;
6330 get_partitioning_params<scalar_t>(pl,
6338 int partArraySize = 0;
6340 if (pl.getPtr<Array <partId_t> >(
"pqParts")){
6341 partNo = pl.getPtr<Array <partId_t> >(
"pqParts")->getRawPtr();
6342 partArraySize = pl.getPtr<Array <partId_t> >(
"pqParts")->size() - 1;
6346 int coordDim, weightDim;
6349 pqJagged_getCoordinateValues<Adapter>(
6360 lno_t numLocalCoords = nlc;
6361 #ifdef enable_migration2
6362 gno_t numGlobalCoords = gnc;
6367 scalar_t **pqJagged_coordinates = allocMemory<scalar_t *>(coordDim);
6368 scalar_t **pqJagged_weights = allocMemory<scalar_t *>(criteriaDim);
6369 bool *pqJagged_uniformParts = allocMemory< bool >(criteriaDim);
6370 scalar_t **pqJagged_partSizes = allocMemory<scalar_t *>(criteriaDim);
6371 bool *pqJagged_uniformWeights = allocMemory< bool >(criteriaDim);
6373 ArrayView<const gno_t> pqJagged_gnos;
6374 size_t numGlobalParts;
6375 int pqJagged_multiVectorDim;
6377 pqJagged_getInputValues<Adapter, scalar_t, gno_t>(
6386 pqJagged_multiVectorDim,
6387 pqJagged_coordinates,
6392 pqJagged_uniformWeights,
6393 pqJagged_uniformParts,
6400 #ifdef enable_migration2
6401 const gno_t *actual_pqgnos = pqJagged_gnos.getRawPtr();
6402 gno_t *pq_gnos = (gno_t *)actual_pqgnos;
6403 int *actual_owner_of_coordinate = NULL;
6407 lno_t *partitionedPointCoordinates = allocMemory< lno_t>(numLocalCoords);
6410 #ifdef HAVE_ZOLTAN2_OMP
6411 #pragma omp parallel for
6413 for(
size_t i = 0; i < static_cast<size_t>(numLocalCoords); ++i){
6414 partitionedPointCoordinates[i] = i;
6418 lno_t *newpartitionedPointCoordinates = allocMemory< lno_t>(numLocalCoords);
6419 #ifdef HAVE_ZOLTAN2_OMP
6421 firstTouch<lno_t>(newpartitionedPointCoordinates, numLocalCoords);
6427 ArrayRCP<partId_t> partId;
6428 if(numLocalCoords > 0){
6429 partIds = allocMemory<partId_t>(numLocalCoords);
6433 partId_t currentPartitionCount = 1;
6437 lno_t *inTotalCounts = allocMemory<lno_t>(1);
6438 inTotalCounts[0] =
static_cast<lno_t
>(numLocalCoords);
6440 lno_t *outTotalCounts = NULL;
6445 bool allowNonRectelinearPart =
false;
6446 int concurrentPartCount = 0;
6447 int migration_actualMigration_option = 1;
6448 int migration_check_option = 0;
6449 int migration_all2all_option = 1;
6450 scalar_t migration_imbalance_cut_off = 0.03;
6451 int migration_assignment_type = 0;
6452 int keep_part_boxes = 0;
6455 int recursion_depth = -1;
6457 pqJagged_getParameters<scalar_t>(pl,
6458 allowNonRectelinearPart,
6459 concurrentPartCount,
6460 migration_actualMigration_option,
6461 migration_check_option,
6462 migration_all2all_option,
6463 migration_imbalance_cut_off,
6464 migration_assignment_type,
6472 recursion_depth = ceil(log ((numGlobalParts)) / log (2.0));
6474 if (partArraySize < 1){
6475 if (recursion_depth > 0){
6476 partArraySize = recursion_depth;
6479 partArraySize = coordDim;
6486 #ifdef HAVE_ZOLTAN2_OMP
6487 #pragma omp parallel shared(numThreads)
6489 numThreads = omp_get_num_threads();
6499 partId_t maxTotalCumulativePartCount = 1;
6502 size_t maxTotalPartCount = 0;
6504 getPartSpecifications <partId_t>(
6513 maxTotalCumulativePartCount,
6519 if (concurrentPartCount == 0)
6525 if (coordDim == partArraySize)
6538 if(concurrentPartCount > maxTotalCumulativePartCount){
6539 if(problemComm->getRank() == 0){
6540 cerr <<
"Warning: Concurrent part calculation count ("<< concurrentPartCount <<
") has been set bigger than maximum amount that can be used." <<
" Setting to:" << maxTotalCumulativePartCount <<
"." << endl;
6542 concurrentPartCount = maxTotalCumulativePartCount;
6544 RCP<Comm<int> > comm = problemComm->duplicate();
6550 scalar_t *allCutCoordinates = allocMemory< scalar_t>(totalDimensionCut);
6553 scalar_t *max_min_array = allocMemory< scalar_t>(numThreads * 2);
6555 float *nonRectelinearPart = NULL;
6556 float **nonRectRatios = NULL;
6557 if(allowNonRectelinearPart){
6558 nonRectelinearPart = allocMemory<float>(maxCutNo * concurrentPartCount);
6559 #ifdef HAVE_ZOLTAN2_OMP
6561 firstTouch<float>(nonRectelinearPart, maxCutNo);
6564 nonRectRatios = allocMemory<float *>(numThreads);
6565 for(
int i = 0; i < numThreads; ++i){
6566 nonRectRatios[i] = allocMemory<float>(maxCutNo);
6568 #ifdef HAVE_ZOLTAN2_OMP
6570 #pragma omp parallel
6572 int me = omp_get_thread_num();
6573 for(
partId_t ii = 0; ii < maxCutNo; ++ii){
6574 nonRectRatios[me][ii] = 0;
6584 scalar_t *cutCoordinatesWork = allocMemory<scalar_t>(maxCutNo * concurrentPartCount);
6586 #ifdef HAVE_ZOLTAN2_OMP
6588 firstTouch<scalar_t>(cutCoordinatesWork, maxCutNo);
6593 scalar_t *targetPartWeightRatios = allocMemory<scalar_t>(maxPartNo * concurrentPartCount);
6594 #ifdef HAVE_ZOLTAN2_OMP
6596 firstTouch<scalar_t>(cutPartRatios, maxCutNo);
6601 scalar_t *cutUpperBounds = allocMemory<scalar_t>(maxCutNo * concurrentPartCount);
6602 scalar_t *cutLowerBounds = allocMemory<scalar_t>(maxCutNo* concurrentPartCount);
6603 scalar_t *cutLowerWeight = allocMemory<scalar_t>(maxCutNo* concurrentPartCount);
6604 scalar_t *cutUpperWeight = allocMemory<scalar_t>(maxCutNo* concurrentPartCount);
6606 scalar_t *localMinMaxTotal = allocMemory<scalar_t>(3 * concurrentPartCount);
6607 scalar_t *globalMinMaxTotal = allocMemory<scalar_t>(3 * concurrentPartCount);
6611 bool *isDone = allocMemory<bool>(maxCutNo * concurrentPartCount);
6614 partId_t *myNonDoneCount = allocMemory<partId_t>(concurrentPartCount);
6616 double **partWeights = allocMemory<double *>(numThreads);
6618 double **pws = allocMemory<double *>(numThreads);
6621 scalar_t **leftClosestDistance = allocMemory<scalar_t *>(numThreads);
6623 scalar_t **rightClosestDistance = allocMemory<scalar_t *>(numThreads);
6626 lno_t **partPointCounts = allocMemory<lno_t *>(numThreads);
6628 for(
int i = 0; i < numThreads; ++i){
6630 partWeights[i] = allocMemory < double >(maxTotalPartCount * concurrentPartCount);
6631 rightClosestDistance[i] = allocMemory<scalar_t>(maxCutNo * concurrentPartCount);
6632 leftClosestDistance[i] = allocMemory<scalar_t>(maxCutNo * concurrentPartCount);
6633 partPointCounts[i] = allocMemory<lno_t>(maxPartNo);
6635 #ifdef HAVE_ZOLTAN2_OMP
6637 #pragma omp parallel
6639 int me = omp_get_thread_num();
6640 for(
partId_t ii = 0; ii < maxPartNo; ++ii){
6641 partPointCounts[me][ii] = 0;
6643 for(
size_t ii = 0; ii < maxTotalPartCount; ++ii){
6644 partWeights[me][ii] = 0;
6646 for(
partId_t ii = 0; ii < maxCutNo; ++ii){
6647 rightClosestDistance[me][ii] = 0;
6648 leftClosestDistance[me][ii] = 0;
6655 scalar_t *cutWeights = allocMemory<scalar_t>(maxCutNo);
6656 scalar_t *globalCutWeights = allocMemory<scalar_t>(maxCutNo);
6662 scalar_t *totalPartWeights_leftClosests_rightClosests = allocMemory<scalar_t>((maxTotalPartCount + maxCutNo * 2) * concurrentPartCount);
6663 scalar_t *global_totalPartWeights_leftClosests_rightClosests = allocMemory<scalar_t>((maxTotalPartCount + maxCutNo * 2) * concurrentPartCount);
6666 scalar_t *cutCoordinates = allCutCoordinates;
6669 scalar_t maxScalar_t = numeric_limits<scalar_t>::max();
6670 scalar_t minScalar_t = -numeric_limits<scalar_t>::max();
6673 env->timerStart(
MACRO_TIMERS,
"PQJagged - Problem_Partitioning");
6676 #ifdef enable_migration2
6677 typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> mvector_t;
6678 RCP<const mvector_t> mvector;
6679 if(migration_actualMigration_option == 0){
6681 mvector = create_initial_multi_vector <gno_t,lno_t,scalar_t,node_t>(
6687 pqJagged_coordinates,
6690 pqJagged_multiVectorDim
6695 for (
int i=0; i < coordDim; i++){
6696 scalar_t *coord = allocMemory<scalar_t>(numLocalCoords);
6697 #ifdef HAVE_ZOLTAN2_OMP
6698 #pragma omp parallel for
6700 for (lno_t j=0; j < numLocalCoords; j++) coord[j] = pqJagged_coordinates[i][j];
6702 pqJagged_coordinates[i] = coord;
6704 for (
int i=0; i < weightDim; i++){
6705 scalar_t *w = allocMemory<scalar_t>(numLocalCoords);
6706 #ifdef HAVE_ZOLTAN2_OMP
6707 #pragma omp parallel for
6709 for (lno_t j=0; j < numLocalCoords; j++) w[j] = pqJagged_weights[i][j];
6711 pqJagged_weights[i] = w;
6713 pq_gnos = allocMemory<gno_t>(numLocalCoords);
6714 #ifdef HAVE_ZOLTAN2_OMP
6715 #pragma omp parallel for
6717 for (lno_t j=0; j < numLocalCoords; j++) pq_gnos[j] = actual_pqgnos[j];
6719 actual_owner_of_coordinate = allocMemory<int>(numLocalCoords);
6720 int me = comm->getRank();
6721 #ifdef HAVE_ZOLTAN2_OMP
6722 #pragma omp parallel for
6724 for (lno_t j=0; j < numLocalCoords; j++) actual_owner_of_coordinate[j] = me;
6730 scalar_t _EPSILON = numeric_limits<scalar_t>::epsilon();
6732 partId_t futurePartNumbers = totalPartCount;
6733 bool is_data_ever_migrated =
false;
6735 vector<partId_t> *currentPartitions =
new vector<partId_t> ();
6736 vector<partId_t> *newFuturePartitions =
new vector<partId_t> ();
6737 newFuturePartitions->push_back(numGlobalParts);
6743 if(keep_part_boxes){
6745 scalar_t *mins = allocMemory<scalar_t>(coordDim);
6746 scalar_t *gmins = allocMemory<scalar_t>(coordDim);
6747 scalar_t *maxs = allocMemory<scalar_t>(coordDim);
6748 scalar_t *gmaxs = allocMemory<scalar_t>(coordDim);
6749 for (
int i = 0; i < coordDim; ++i){
6751 scalar_t localMin = pqJagged_coordinates[i][0];
6752 scalar_t localMax = pqJagged_coordinates[i][0];
6753 for (lno_t j = 1; j < numLocalCoords; ++j){
6755 if (pqJagged_coordinates[i][j] < localMin){
6756 localMin = pqJagged_coordinates[i][j];
6758 if (pqJagged_coordinates[i][j] > localMax){
6759 localMax = pqJagged_coordinates[i][j];
6767 reduceAll<int, scalar_t>(*comm, Teuchos::REDUCE_MIN,
6768 coordDim, mins, gmins
6771 reduceAll<int, scalar_t>(*comm, Teuchos::REDUCE_MAX,
6772 coordDim, maxs, gmaxs
6776 freeArray<scalar_t>(mins);
6777 freeArray<scalar_t>(gmins);
6778 freeArray<scalar_t>(maxs);
6779 freeArray<scalar_t>(gmaxs);
6780 outPartBoxes->push_back(tmpBox);
6784 for (
int i = 0; i < partArraySize; ++i){
6790 vector <partId_t> pAlongI;
6808 vector<partId_t> *tmpPartVect= currentPartitions;
6809 currentPartitions = newFuturePartitions;
6810 newFuturePartitions = tmpPartVect;
6815 newFuturePartitions->clear();
6817 if(keep_part_boxes){
6818 RCP < vector <coordinateModelPartBox <scalar_t, partId_t> > > tmpPartBoxes = inPartBoxes;
6819 inPartBoxes = outPartBoxes;
6820 outPartBoxes = tmpPartBoxes;
6821 outPartBoxes->clear();
6834 partId_t outPartCount = getPartitionArrays<scalar_t, partId_t>(
6838 newFuturePartitions,
6840 currentPartitionCount,
6851 if(outPartCount == currentPartitionCount) {
6852 tmpPartVect= currentPartitions;
6853 currentPartitions = newFuturePartitions;
6854 newFuturePartitions = tmpPartVect;
6856 if(keep_part_boxes){
6857 RCP < vector <coordinateModelPartBox <scalar_t, partId_t> > > tmpPartBoxes = inPartBoxes;
6858 inPartBoxes = outPartBoxes;
6859 outPartBoxes = tmpPartBoxes;
6864 #ifdef enable_migration2
6865 int worldSize = comm->getSize();
6866 long migration_reduceAllPop = reduceAllCount * worldSize;
6871 int coordInd = i % coordDim;
6872 scalar_t * pqCoord = pqJagged_coordinates[coordInd];
6874 string istring = toString<int>(i);
6877 env->timerStart(
MACRO_TIMERS,
"PQJagged - Problem_Partitioning_" + istring);
6881 outTotalCounts = allocMemory<lno_t>(outPartCount);
6889 partId_t concurrentPart = min(currentPartitionCount - currentWorkPart, concurrentPartCount);
6892 bool useBinarySearch =
true;
6896 for (; currentWorkPart < currentPartitionCount; currentWorkPart += concurrentPart){
6898 concurrentPart = min(currentPartitionCount - currentWorkPart, concurrentPartCount);
6899 #ifdef mpi_communication
6900 concurrent = concurrentPart;
6907 for(
int kk = 0; kk < concurrentPart; ++kk){
6908 partId_t currentPart = currentWorkPart + kk;
6912 if (pAlongI[currentPart] == 1){
6916 lno_t coordinateEnd= inTotalCounts[currentPart];
6917 lno_t coordinateBegin = currentPart==0 ? 0: inTotalCounts[currentPart -1];
6919 pqJagged_getLocalMinMaxTotalCoord<scalar_t, lno_t>(
6920 partitionedPointCoordinates,
6922 pqJagged_uniformWeights[0],
6923 pqJagged_weights[0],
6930 localMinMaxTotal[kk],
6931 localMinMaxTotal[kk + concurrentPart],
6932 localMinMaxTotal[kk + 2*concurrentPart]
6939 if (workPartCount > 0){
6941 pqJagged_getGlobalMinMaxTotalCoord<scalar_t>(
6958 for(
int kk = 0; kk < concurrentPart; ++kk){
6959 scalar_t minCoordinate = globalMinMaxTotal[kk];
6960 scalar_t maxCoordinate = globalMinMaxTotal[kk + concurrentPart];
6962 scalar_t globalTotalWeight = globalMinMaxTotal[kk + 2 * concurrentPart];
6964 partId_t currentPart = currentWorkPart + kk;
6966 partId_t partition = pAlongI[currentPart];
6968 scalar_t *usedCutCoordinate = cutCoordinates + cutShifts;
6969 scalar_t *usedCutPartRatios = targetPartWeightRatios + partShift;
6971 cutShifts += partition - 1;
6973 partShift += partition;
6978 if(partition > 1 && minCoordinate <= maxCoordinate){
6981 allDone += partition - 1;
6983 myNonDoneCount[kk] = partition - 1;
6986 pqJagged_getCutCoord_Weights<scalar_t>(
6989 pqJagged_uniformParts[0],
6990 pqJagged_uniformWeights[0],
6991 pqJagged_partSizes[0],
6998 newFuturePartitions,
7004 getInitialPartAssignments<scalar_t, lno_t, partId_t>(
7009 partitionedPointCoordinates,
7018 myNonDoneCount[kk] = 0;
7020 obtainedPartCount += partition;
7026 scalar_t used_imbalance = 0;
7030 pqJagged_1D_Partition<scalar_t, lno_t>(
7033 partitionedPointCoordinates,
7035 pqJagged_uniformWeights[0],
7036 pqJagged_weights[0],
7037 targetPartWeightRatios,
7050 leftClosestDistance,
7051 rightClosestDistance,
7058 totalPartWeights_leftClosests_rightClosests,
7059 global_totalPartWeights_leftClosests_rightClosests,
7060 allowNonRectelinearPart,
7081 size_t tlrShift = 0;
7084 for(
int kk = 0; kk < concurrentPart; ++kk){
7085 partId_t curr = currentWorkPart + kk;
7089 if((noParts != 1 )&& globalMinMaxTotal[kk] > globalMinMaxTotal[kk + concurrentPart]) {
7091 for(
partId_t jj = 0; jj < noParts; ++jj){
7092 outTotalCounts[currentOut + outShift + jj] = 0;
7094 cutShift += noParts - 1;
7095 tlrShift += (4 *(noParts - 1) + 1);
7096 outShift += noParts;
7097 pwShift += (2 * (noParts - 1) + 1);
7101 lno_t coordinateEnd= inTotalCounts[curr];
7102 lno_t coordinateBegin = curr==0 ? 0: inTotalCounts[curr -1];
7103 scalar_t *usedCutCoordinate = cutCoordinates + cutShift;
7104 float *usednonRectelinearPart = nonRectelinearPart + cutShift;
7106 scalar_t *tlr = totalPartWeights_leftClosests_rightClosests + tlrShift;
7108 for(
int ii = 0; ii < numThreads; ++ii){
7109 pws[ii] = partWeights[ii] + pwShift;
7127 if(keep_part_boxes){
7128 for (
partId_t j = 0; j < noParts - 1; ++j){
7137 (*outPartBoxes)[outShift + currentOut + j].updateMinMax(usedCutCoordinate[j], 1 , coordInd);
7146 (*outPartBoxes)[outShift + currentOut + j + 1].updateMinMax(usedCutCoordinate[j], 0 , coordInd);
7156 getChunksFromCoordinates<lno_t,scalar_t>(
7159 partitionedPointCoordinates,
7161 pqJagged_uniformWeights[0],
7162 pqJagged_weights[0],
7168 allowNonRectelinearPart,
7169 usednonRectelinearPart,
7178 newpartitionedPointCoordinates,
7179 outTotalCounts + currentOut + outShift,
7183 pqJagged_coordinates,
7204 lno_t partSize = coordinateEnd - coordinateBegin;
7205 *(outTotalCounts + currentOut + outShift) = partSize;
7206 memcpy(newpartitionedPointCoordinates + coordinateBegin, partitionedPointCoordinates + coordinateBegin, partSize *
sizeof(lno_t));
7208 cutShift += noParts - 1;
7209 tlrShift += (4 *(noParts - 1) + 1);
7210 outShift += noParts;
7211 pwShift += (2 * (noParts - 1) + 1);
7215 cutCoordinates += cutShift;
7220 for(
partId_t kk = 0; kk < concurrentPart; ++kk){
7221 partId_t noParts = pAlongI[ currentWorkPart + kk];
7222 for (
partId_t ii = 0;ii < noParts ; ++ii){
7224 outTotalCounts[currentOut+ii] += previousEnd;
7227 previousEnd = outTotalCounts[currentOut + noParts - 1];
7229 currentOut += noParts ;
7236 bool is_migrated_in_current =
false;
7245 #ifdef enable_migration2
7247 futurePartNumbers > 1 &&
7248 migration_check_option >= 0 &&
7250 env->timerStart(
MACRO_TIMERS,
"PQJagged - Problem_Migration-" + istring);
7255 migration_refactored<gno_t, lno_t, scalar_t, node_t, partId_t>(
7259 pqJagged_multiVectorDim,
7263 pqJagged_coordinates,
7269 currentPartitionCount,
7270 newFuturePartitions,
7271 newpartitionedPointCoordinates,
7272 partitionedPointCoordinates,
7277 migration_all2all_option,
7278 migration_assignment_type,
7279 migration_actualMigration_option,
7280 migration_check_option,
7281 migration_imbalance_cut_off,
7282 migration_reduceAllPop,
7283 numLocalCoords / (futurePartNumbers * currentPartitionCount) ,
7287 actual_owner_of_coordinate,
7296 is_migrated_in_current =
true;
7297 is_data_ever_migrated =
true;
7298 env->timerStop(
MACRO_TIMERS,
"PQJagged - Problem_Migration-" + istring);
7299 reduceAllCount /= num_parts;
7305 lno_t * tmp = partitionedPointCoordinates;
7306 partitionedPointCoordinates = newpartitionedPointCoordinates;
7307 newpartitionedPointCoordinates = tmp;
7309 if(!is_migrated_in_current){
7310 reduceAllCount -= currentPartitionCount;
7311 currentPartitionCount = outPartCount;
7313 freeArray<lno_t>(inTotalCounts);
7314 inTotalCounts = outTotalCounts;
7316 env->timerStop(
MACRO_TIMERS,
"PQJagged - Problem_Partitioning_" + istring);
7322 delete currentPartitions;
7323 delete newFuturePartitions;
7325 env->timerStop(
MACRO_TIMERS,
"PQJagged - Problem_Partitioning");
7328 env->timerStart(
MACRO_TIMERS,
"PQJagged - Part_Assignment");
7330 #ifdef debug_setparts
7331 string fname = toString<int>(problemComm->getRank());
7332 fname = fname +
".print";
7333 FILE *f = fopen(fname.c_str(),
"w");
7347 #ifdef HAVE_ZOLTAN2_OMP
7348 #pragma omp parallel for
7350 for(
partId_t i = 0; i < currentPartitionCount;++i){
7353 lno_t end = inTotalCounts[i];
7355 if(i > 0) begin = inTotalCounts[i -1];
7356 partId_t pToSet = i + partIndexBegin;
7357 if (keep_part_boxes){
7358 (*outPartBoxes)[i].setpId(pToSet);
7366 for (lno_t ii = begin; ii < end; ++ii){
7367 lno_t k = partitionedPointCoordinates[ii];
7368 partIds[k] = pToSet;
7370 #ifdef debug_setparts
7371 fprintf(f,
"setting %d with coords: %lf %lf %lf to part %d\n", k, pqJagged_coordinates[0][k], pqJagged_coordinates[1][k], pqJagged_coordinates[
7379 #ifdef debug_setparts
7382 env->timerStop(
MACRO_TIMERS,
"PQJagged - Part_Assignment");
7383 ArrayRCP<const gno_t> gnoList;
7384 if(!is_data_ever_migrated){
7386 if(numLocalCoords > 0){
7387 gnoList = arcpFromArrayView(pqJagged_gnos);
7390 #ifdef enable_migration2
7392 if(migration_actualMigration_option == 0){
7393 gnoList = arcpFromArrayView(mvector->getMap()->getNodeElementList());
7397 ZOLTAN_COMM_OBJ *plan = NULL;
7398 MPI_Comm mpi_comm = Teuchos2MPI (problemComm);
7401 int message_tag = 7856;
7404 env->timerStart(
MACRO_TIMERS,
"PQJagged - Final Z1PlanCreating");
7405 int ierr = Zoltan_Comm_Create(
7406 &plan, numLocalCoords,
7407 actual_owner_of_coordinate, mpi_comm,
7408 message_tag, &incoming);
7409 env->timerStop(
MACRO_TIMERS,
"PQJagged - Final Z1PlanCreating" );
7412 gno_t *incoming_gnos = allocMemory< gno_t>(incoming);
7415 env->timerStart(
MACRO_TIMERS,
"PQJagged - Final Z1PlanComm");
7416 ierr = Zoltan_Comm_Do(
7420 (
char *) incoming_gnos);
7422 freeArray<gno_t>(pq_gnos);
7423 pq_gnos = incoming_gnos;
7425 partId_t *incoming_partIds = allocMemory< partId_t>(incoming);
7428 ierr = Zoltan_Comm_Do(
7432 (
char *) incoming_partIds);
7433 freeArray<partId_t>(partIds);
7434 partIds = incoming_partIds;
7436 env->timerStop(
MACRO_TIMERS,
"PQJagged - Final Z1PlanComm");
7437 ierr = Zoltan_Comm_Destroy(&plan);
7439 numLocalCoords = incoming;
7440 is_data_ever_migrated =
false;
7443 ArrayView<const gno_t> avpqgnos(pq_gnos, numLocalCoords);
7444 gnoList = arcpFromArrayView(avpqgnos);
7452 env->timerStart(
MACRO_TIMERS,
"PQJagged - Solution_Part_Assignment");
7453 partId = arcp(partIds, 0, numLocalCoords,
true);
7456 solution->setParts(gnoList, partId,
true);
7459 if (keep_part_boxes){
7460 solution->setPartBoxes(outPartBoxes);
7463 solution->setParts(gnoList, partId,!is_data_ever_migrated);
7465 env->timerStop(
MACRO_TIMERS,
"PQJagged - Solution_Part_Assignment");
7467 env->timerStart(
MACRO_TIMERS,
"PQJagged - Problem_Free");
7477 #ifdef enable_migration2
7478 if (migration_actualMigration_option != 0){
7480 for (
int i=0; i < coordDim; i++){
7482 freeArray<scalar_t>(pqJagged_coordinates[i]);
7484 for (
int i=0; i < weightDim; i++){
7486 freeArray<scalar_t>(pqJagged_weights[i]);
7490 freeArray<int>(actual_owner_of_coordinate);
7494 for(
int i = 0; i < numThreads; ++i){
7495 freeArray<lno_t>(partPointCounts[i]);
7498 freeArray<lno_t *>(partPointCounts);
7499 freeArray<double *> (pws);
7501 if(allowNonRectelinearPart){
7502 freeArray<float>(nonRectelinearPart);
7503 for(
int i = 0; i < numThreads; ++i){
7504 freeArray<float>(nonRectRatios[i]);
7506 freeArray<float *>(nonRectRatios);
7509 freeArray<partId_t>(myNonDoneCount);
7511 freeArray<scalar_t>(cutWeights);
7513 freeArray<scalar_t>(globalCutWeights);
7515 freeArray<scalar_t>(max_min_array);
7517 freeArray<lno_t>(outTotalCounts);
7519 freeArray<lno_t>(partitionedPointCoordinates);
7521 freeArray<lno_t>(newpartitionedPointCoordinates);
7523 freeArray<scalar_t>(allCutCoordinates);
7525 freeArray<scalar_t *>(pqJagged_coordinates);
7527 freeArray<scalar_t *>(pqJagged_weights);
7529 freeArray<bool>(pqJagged_uniformParts);
7531 freeArray<scalar_t> (localMinMaxTotal);
7533 freeArray<scalar_t> (globalMinMaxTotal);
7535 freeArray<scalar_t *>(pqJagged_partSizes);
7537 freeArray<bool>(pqJagged_uniformWeights);
7539 freeArray<scalar_t>(cutCoordinatesWork);
7541 freeArray<scalar_t>(targetPartWeightRatios);
7543 freeArray<scalar_t>(cutUpperBounds);
7545 freeArray<scalar_t>(cutLowerBounds);
7548 freeArray<scalar_t>(cutLowerWeight);
7549 freeArray<scalar_t>(cutUpperWeight);
7550 freeArray<bool>(isDone);
7551 freeArray<scalar_t>(totalPartWeights_leftClosests_rightClosests);
7552 freeArray<scalar_t>(global_totalPartWeights_leftClosests_rightClosests);
7554 for(
int i = 0; i < numThreads; ++i){
7555 freeArray<double>(partWeights[i]);
7556 freeArray<scalar_t>(rightClosestDistance[i]);
7557 freeArray<scalar_t>(leftClosestDistance[i]);
7560 freeArray<double *>(partWeights);
7561 freeArray<scalar_t *>(leftClosestDistance);
7562 freeArray<scalar_t *>(rightClosestDistance);
7565 env->timerStop(
MACRO_TIMERS,
"PQJagged - Problem_Free");
7570 #endif // INCLUDE_ZOLTAN2_EXPERIMENTAL
void AlgPQJagged(const RCP< const Environment > &env, RCP< Comm< int > > &problemComm, const RCP< const CoordinateModel< typename Adapter::base_adapter_t > > &coords, RCP< PartitioningSolution< Adapter > > &solution)
PQJagged coordinate partitioning algorithm.
Time an algorithm (or other entity) as a whole.
void pqJagged_getLocalMinMaxTotalCoord(lno_t *partitionedPointPermutations, scalar_t *pqJagged_coordinates, bool pqJagged_uniformWeights, scalar_t *pqJagged_weights, int numThreads, lno_t coordinateBegin, lno_t coordinateEnd, scalar_t *max_min_array, scalar_t maxScalar, scalar_t minScalar, scalar_t &minCoordinate, scalar_t &maxCoordinate, scalar_t &totalWeight)
Function to determine the local minimum and maximum coordinate, and local total weight in the given s...
void pqJagged_getCutCoord_Weights(scalar_t minCoordinate, scalar_t maxCoordinate, bool pqJagged_uniformParts, bool pqJagged_uniformWeights, scalar_t *pqJagged_partSizes, partId_t noCuts, scalar_t globalTotalWeight, scalar_t *cutCoordinates, scalar_t *cutPartRatios, int numThreads, vector< partId_t > *currentPartitions, vector< partId_t > *futurePartitions, partId_t partIndex, partId_t futureArrayIndex)
Function that calculates the new coordinates for the cut lines. Function is called inside the paralle...
void pqJagged_getGlobalMinMaxTotalCoord(RCP< Comm< int > > &comm, const RCP< const Environment > &env, partId_t concurrentPartCount, scalar_t *localMinMaxTotal, scalar_t *globalMinMaxTotal)
Function that reduces global minimum and maximum coordinates with global total weight from given loca...
#define MIN_WORK_LAST_DIM
void set(IT index_, CT count_, WT *vals_)
#define FUTURE_REDUCEALL_CUTOFF
PQJaggedCombinedReductionOp(std::vector< Ordinal > *pVector, Ordinal vBegin, Ordinal k_)
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Metric class and namespace methods to compute quality metrics.
Zoltan2::StridedData< lno_t, scalar_t > input_t
scalar_t pivotPos(scalar_t *cutUpperBounds, scalar_t *cutLowerBounds, size_t currentCutIndex, scalar_t *cutUpperWeight, scalar_t *cutLowerWeight, scalar_t ew, scalar_t _EPSILON)
Function that calculates the next pivot position, according to given coordinates of upper bound and l...
void getChunksFromCoordinates(partId_t partNo, int noThreads, lno_t *partitionedPointPermutations, scalar_t *pqJagged_coordinates, bool pqJagged_uniformWeights, scalar_t *coordWeights, scalar_t *cutCoordinates, lno_t coordinateBegin, lno_t coordinateEnd, bool allowNonRectelinearPart, float *actual_ratios, scalar_t *localPartWeights, double **partWeights, float **nonRectelinearRatios, lno_t **partPointCounts, lno_t *newpartitionedPointPermutations, lno_t *totalCounts, partId_t *partIds, bool isSequentialAndPricise, scalar_t **pqJaggedAllCoordinates, int pqCoordDim, int currentCoord)
Function that determines the permutation indices of the coordinates.
partId_t getPartCount(partId_t numFuture, float root, float fEpsilon)
void freeArray(T *&array)
void getNewCoordinates(const RCP< const Environment > &env, RCP< Comm< int > > &comm, const size_t &total_part_count, const partId_t &noCuts, const scalar_t &maxCoordinate, const scalar_t &minCoordinate, const scalar_t &globalTotalWeight, const scalar_t &imbalanceTolerance, scalar_t *globalPartWeights, const scalar_t *localPartWeights, const scalar_t *targetPartWeightRatios, bool *isDone, scalar_t *cutCoordinates, scalar_t *cutUpperBounds, scalar_t *cutLowerBounds, scalar_t *leftClosestDistance, scalar_t *rightClosestDistance, scalar_t *cutLowerWeight, scalar_t *cutUpperWeight, scalar_t *newCutCoordinates, bool allowNonRectelinearPart, float *nonRectelinearPartRatios, partId_t *rectilinearCutCount, scalar_t *localCutWeights, scalar_t *globalCutWeights, partId_t &myNoneDoneCount)
Function that calculates the new coordinates for the cut lines. Function is called inside the paralle...
zoltan2_partId_t partId_t
zoltan2_partId_t partId_t
void get_partitioning_params(const Teuchos::ParameterList &pl, T &imbalanceTolerance, multiCriteriaNorm &mcnorm, std::bitset< NUM_RCB_PARAMS > ¶ms, int &numTestCuts, bool &ignoreWeights)
coordinateModelPartBox Class, represents the boundaries of the box which is a result of a geometric p...
void getInitialPartAssignments(scalar_t &maxCoordinate, scalar_t &minCoordinate, partId_t ¤tPart, lno_t *inTotalCounts, lno_t *partitionedPointCoordinates, scalar_t *pqCoord, partId_t *partIds, scalar_t _EPSILON, partId_t &partition)
Function that calculates the new coordinates for the cut lines. Function is called inside the paralle...
void pqJagged_printInput(int coordDim, int weightDim, size_t numLocalCoords, global_size_t numGlobalCoords, int criteriaDim, scalar_t **pqJagged_values, scalar_t **pqJagged_weights, bool *pqJagged_uniformParts, bool *pqJagged_uniformWeights, gno_t *pqJagged_gnos, bool ignoreWeights, size_t numGlobalParts, scalar_t **pqJagged_partSizes)
Printing the input values, to check configuration.
This class provides geometric coordinates with optional weights to the Zoltan2 algorithm.
void reduce(const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
Implement Teuchos::ValueTypeReductionOp interface.
T * allocMemory(size_t size)
void pqJagged_getInputValues(const RCP< const Environment > &env, const RCP< const CoordinateModel< typename Adapter::base_adapter_t > > &coords, RCP< PartitioningSolution< Adapter > > &solution, std::bitset< NUM_RCB_PARAMS > ¶ms, const int &coordDim, const int &weightDim, const size_t &numLocalCoords, size_t &numGlobalParts, int &pqJagged_multiVectorDim, scalar_t **pqJagged_values, const int &criteriaDim, scalar_t **pqJagged_weights, ArrayView< const gno_t > &pqJagged_gnos, bool &ignoreWeights, bool *pqJagged_uniformWeights, bool *pqJagged_uniformParts, scalar_t **pqJagged_partSizes)
Function returning the input values for the problem such as the coordinates, weights and desiredPartS...
void inserToPart(partId_t partNo, lno_t coordinateIndex)
Inserting a coordinate to a particular part. Since, class does not have the size information, it is user's responsibility to provide indices for partNo and coordinateIndex that are in the range.
void firstTouch(T *arrayName, size_t arraySize)
lno_t * getPartBegins()
partBegins getter function.
uMultiSortItem< IT, CT, WT > operator=(const uMultiSortItem< IT, CT, WT > &other)
void pqJagged_getCoordinateValues(const RCP< const CoordinateModel< typename Adapter::base_adapter_t > > &coords, int &coordDim, int &weightDim, size_t &numLocalCoords, global_size_t &numGlobalCoords, int &criteriaDim, const bool &ignoreWeights)
Returns the input coordinate value parameters.
A PartitioningSolution is a solution to a partitioning problem.
uMultiSortItem(const uMultiSortItem< IT, CT, WT > &other)
void getPartSpecifications(const partId_t *partNo, int partArraySize, size_t &numGlobalParts, partId_t &totalDimensionCut, partId_t &totalPartCount, partId_t &maxPartNo, partId_t &reduceAllCount, partId_t &maxTotalCumulativePartCount, partId_t &maxCutNo, size_t &maxTotalPartCount)
lno_t * getLinkedList()
linkedList getter function.
PQJaggedCombinedReductionOp()
Default Constructor.
The StridedData class manages lists of weights or coordinates.
PQJaggedCombinedMinMaxTotalReductionOp(Ordinal nmin, Ordinal nmax, Ordinal nTotal)
Constructor.
static RCP< const User > doMigration(const RCP< const User > &from, lno_t numLocalRows, const gno_t *myNewRows)
Migrate the object Given a user object and a new row distribution, create and return a new user objec...
void pqJagged_1DPart_getPartWeights(size_t total_part_count, partId_t noCuts, scalar_t maxCoordinate, scalar_t minCoordinate, scalar_t _EPSILON, int numThreads, lno_t coordinateBegin, lno_t coordinateEnd, lno_t *partitionedPointPermutations, scalar_t *pqJagged_coordinates, bool pqJagged_uniformWeights, scalar_t *pqJagged_weights, scalar_t *cutCoordinates_tmp, bool *isDone, double *myPartWeights, scalar_t *myLeftClosest, scalar_t *myRightClosest, bool useBinarySearch, partId_t *partIds)
Function that calculates the new coordinates for the cut lines. Function is called inside the paralle...
void pqJagged_1D_Partition(const RCP< const Environment > &env, RCP< Comm< int > > &comm, lno_t *partitionedPointPermutations, scalar_t *pqJagged_coordinates, bool pqJagged_uniformWeights, scalar_t *pqJagged_weights, scalar_t *targetPartWeightRatios, scalar_t *globalMinMaxTotal, scalar_t *localMinMaxTotal, int numThreads, scalar_t imbalanceTolerance, partId_t currentPartBeginIndex, partId_t concurrentPartCount, lno_t *inTotalCounts, scalar_t *cutCoordinates, scalar_t *cutCoordinatesWork, scalar_t **leftClosestDistance, scalar_t **rightClosestDistance, scalar_t *cutUpperBounds, scalar_t *cutLowerBounds, scalar_t *cutUpperWeight, scalar_t *cutLowerWeight, bool *isDone, double **partWeights, scalar_t *local_totalPartWeights_leftClosest_rightCloset, scalar_t *global_totalPartWeights_leftClosest_rightCloset, bool allowNonRectelinearPart, float *nonRectelinearPartRatios, scalar_t *localCutWeights, scalar_t *globalCutWeights, partId_t allDone, partId_t *myNonDoneCounts, bool useBinarySearch, partId_t *partIds, vector< partId_t > &pVector)
Function that is responsible from 1D partitioning of the given range of coordinates.
partId_t getPartitionArrays(const partId_t *partNo, vector< partId_t > &pAlongI, vector< partId_t > *currentPartitions, vector< partId_t > *newFuturePartitions, partId_t &futurePartNumbers, partId_t currentPartitionCount, int partArraySize, int i, partId_t maxPartNo, int keep_part_boxes, RCP< vector< coordinateModelPartBox< scalar_t, partId_t > > > inPartBoxes, RCP< vector< coordinateModelPartBox< scalar_t, partId_t > > > outPartBoxes)
uMultiSortItem(IT index_, CT count_, WT *vals_)
std::string toString(tt obj)
PQJaggedCombinedReductionOp(Ordinal nsum, Ordinal nmin1, Ordinal nmin2, Ordinal k_)
Constructor.
void set(lno_t *linkedList_, lno_t *partBegins_, lno_t *partEnds_)
The memory is provided to class via set function.
#define imbalanceOf2(Wachieved, wExpected)
#define Z2_THROW_EXPERIMENTAL(mystr)
Throw an error when experimental code is requested but not compiled.
void sequentialTaskPartitioning(const RCP< const Environment > &env, lno_t numLocalCoords, lno_t actualNumCoords, size_t numGlobalParts, int coordDim, scalar_t **pqJagged_coordinates, lno_t *output_permutation, lno_t *output_partIndices)
Defines the CoordinateModel classes.
PQJaggedCombinedMinMaxTotalReductionOp()
Default Constructor.
lno_t * getPartEnds()
partEnds getter function.
#define LEAST_SIGNIFICANCE
multiCriteriaNorm
Enumerator used in code for multicriteria norm choice.
void accumulateThreadResults(const vector< partId_t > &pVector, partId_t vBegin, partId_t concurrentPartCount, int numThreads, bool *isDone, scalar_t **leftClosestPoints, scalar_t **rightClosestPoints, double **partWeights, scalar_t *totalPartWeights_leftClosest_rightCloset)
Function that reduces the result of multiple threads for left and right closest points and part weigh...
bool operator>(const uMultiSortItem< IT, CT, WT > &other) const
#define Z2_THROW_OUTSIDE_ERROR(env)
Throw an error returned from outside the Zoltan2 library.
void uqsort(IT n, uSortItem< IT, WT > *arr)
#define Z2_DEFAULT_CON_PART_COUNT
void reduce(const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
Implement Teuchos::ValueTypeReductionOp interface.
void pqJagged_getParameters(const Teuchos::ParameterList &pl, bool &allowNonrectilinear, partId_t &concurrentPartCount, int &migration_actualMigration_option, int &migration_check_option, int &migration_all2all_option, T &migration_imbalance_cut_off, int &migration_assignment_type, int &keep_part_boxes, int &enable_rcb, int &recursion_depth)
Returns the parameters such as: Partitioning problem parameters of interest: Partitioning objective i...
Methods written for the RCB algorithm.
A helper class containing array representation of coordinate linked lists.
Kokkos::DefaultNode::DefaultNodeType node_t