391 auto rowPtr =
localP.graph.row_map;
392 auto values =
localP.values;
394 auto nnz = rowPtr(rowIdx+1) - rowPtr(rowIdx);
399 for (
auto entryIdx = rowPtr(rowIdx); entryIdx < rowPtr(rowIdx + 1); entryIdx++) rsumTarget += values(entryIdx);
403 SC rowSumDeviation, temp, delta;
404 SC closestToLeftBoundDist, closestToRghtBoundDist;
405 LO closestToLeftBound, closestToRghtBound;
410 if ((KAT::real(rsumTarget) >= KAT::real(leftBound*(
static_cast<SC>(nnz)))) &&
411 (KAT::real(rsumTarget) <= KAT::real(rghtBound*(
static_cast<SC>(nnz))))){
416 aBigNumber = KAT::zero();
417 for (
auto entryIdx = rowPtr(rowIdx); entryIdx < rowPtr(rowIdx + 1); entryIdx++){
418 if ( KAT::magnitude( values(entryIdx) ) > KAT::magnitude(aBigNumber))
419 aBigNumber = KAT::magnitude( values(entryIdx) );
421 aBigNumber = aBigNumber+ (KAT::magnitude(leftBound) + KAT::magnitude(rghtBound))*(100*
one);
424 for (
auto entryIdx = rowPtr(rowIdx); entryIdx < rowPtr(rowIdx + 1); entryIdx++){
426 inds(rowIdx, ind) = ind;
430 auto sortVals = Kokkos::subview(
origSorted, rowIdx, Kokkos::ALL() );
431 auto sortInds = Kokkos::subview(
inds, rowIdx, Kokkos::make_pair(0,ind));
435 for (
LO i = 1; i < static_cast<LO>(nnz); ++i){
439 if ( KAT::real(sortVals(sortInds(i))) < KAT::real(sortVals(sortInds(0))) ){
440 for ( ; j > 0; --j) sortInds(j) = sortInds(j - 1);
444 for ( ; KAT::real(sortVals(ind)) < KAT::real(sortVals(sortInds(j-1))); --j) sortInds(j) = sortInds(j-1);
451 for (
LO i = 0; i < static_cast<LO>(nnz); i++)
origSorted(rowIdx, i) = values(rowPtr(rowIdx) +
inds(rowIdx, i));
453 closestToLeftBound = 0;
454 while ((closestToLeftBound <
static_cast<LO>(nnz)) && (KAT::real(
origSorted(rowIdx, closestToLeftBound)) <= KAT::real(leftBound))) closestToLeftBound++;
457 closestToRghtBound = closestToLeftBound;
458 while ((closestToRghtBound <
static_cast<LO>(nnz)) && (KAT::real(
origSorted(rowIdx, closestToRghtBound)) <= KAT::real(rghtBound))) closestToRghtBound++;
463 closestToLeftBoundDist =
origSorted(rowIdx, closestToLeftBound) - leftBound;
464 if (closestToRghtBound ==
static_cast<LO>(nnz)) closestToRghtBoundDist= aBigNumber;
465 else closestToRghtBoundDist=
origSorted(rowIdx, closestToRghtBound) - rghtBound;
470 rowSumDeviation = leftBound*(
static_cast<SC>(closestToLeftBound)) + (
static_cast<SC>(nnz-closestToRghtBound))*rghtBound - rsumTarget;
471 for (
LO i=closestToLeftBound; i < closestToRghtBound; i++) rowSumDeviation +=
origSorted(rowIdx, i);
476 if (KAT::real(rowSumDeviation) < KAT::real(KAT::zero())) {
478 temp = leftBound; leftBound = -rghtBound; rghtBound = temp;
484 for (
LO i=0; i < static_cast<LO>(nnz/2); i++) {
492 LO itemp = closestToLeftBound;
493 closestToLeftBound = nnz-closestToRghtBound;
494 closestToRghtBound = nnz-itemp;
495 closestToLeftBoundDist =
origSorted(rowIdx, closestToLeftBound) - leftBound;
496 if (closestToRghtBound ==
static_cast<LO>(nnz)) closestToRghtBoundDist= aBigNumber;
497 else closestToRghtBoundDist=
origSorted(rowIdx, closestToRghtBound) - rghtBound;
499 rowSumDeviation = -rowSumDeviation;
504 for (
LO i = 0; i < closestToLeftBound; i++)
fixedSorted(rowIdx, i) = leftBound;
506 for (
LO i = closestToRghtBound; i < static_cast<LO>(nnz); i++)
fixedSorted(rowIdx, i) = rghtBound;
508 while ((KAT::magnitude(rowSumDeviation) > KAT::magnitude((
one*1.e-10)*rsumTarget))){
509 if (closestToRghtBound != closestToLeftBound)
510 delta = rowSumDeviation/
static_cast<SC>(closestToRghtBound - closestToLeftBound);
511 else delta = aBigNumber;
513 if (KAT::magnitude(closestToLeftBoundDist) <= KAT::magnitude(closestToRghtBoundDist)) {
514 if (KAT::magnitude(delta) <= KAT::magnitude(closestToLeftBoundDist)) {
515 rowSumDeviation =
zero;
516 for (
LO i = closestToLeftBound; i < closestToRghtBound ; i++)
fixedSorted(rowIdx, i) =
origSorted(rowIdx, i) - delta;
519 rowSumDeviation = rowSumDeviation - closestToLeftBoundDist;
520 fixedSorted(rowIdx, closestToLeftBound) = leftBound;
521 closestToLeftBound++;
522 if (closestToLeftBound <
static_cast<LO>(nnz)) closestToLeftBoundDist =
origSorted(rowIdx, closestToLeftBound) - leftBound;
523 else closestToLeftBoundDist = aBigNumber;
527 if (KAT::magnitude(delta) <= KAT::magnitude(closestToRghtBoundDist)) {
529 for (
LO i = closestToLeftBound; i < closestToRghtBound ; i++)
fixedSorted(rowIdx, i) =
origSorted(rowIdx, i) - delta;
532 rowSumDeviation = rowSumDeviation + closestToRghtBoundDist;
535 closestToRghtBound++;
537 if (closestToRghtBound >=
static_cast<LO>(nnz)) closestToRghtBoundDist = aBigNumber;
538 else closestToRghtBoundDist =
origSorted(rowIdx, closestToRghtBound) - rghtBound;
543 auto rowStart = rowPtr(rowIdx);
548 for (
LO i=0; i < static_cast<LO>(nnz/2); i++) {
555 for (
LO i = 0; i < static_cast<LO>(nnz); i++) values(rowStart +
inds(rowIdx, i)) =
fixedSorted(rowIdx, i);
559 for (
auto entryIdx = rowPtr(rowIdx); entryIdx < rowPtr(rowIdx + 1); entryIdx++) values(entryIdx) =
one/(
static_cast<SC>(nnz) );