54 #ifndef vtk_m_worklet_contourtree_augmented_process_contourtree_h 
   55 #define vtk_m_worklet_contourtree_augmented_process_contourtree_h 
   95 namespace contourtree_augmented
 
  115     std::vector<EdgePair> arcSorter;
 
  119     auto sortOrderPortal = sortOrder.
ReadPortal();
 
  124       vtkm::Id arcTo = arcsPortal.Get(node);
 
  135       vtkm::Id regularID = sortOrderPortal.Get(node);
 
  138       vtkm::Id regularTo = sortOrderPortal.Get(arcTo);
 
  141       if (regularID < regularTo)
 
  142         arcSorter.push_back(
EdgePair(regularID, regularTo));
 
  144         arcSorter.push_back(
EdgePair(regularTo, regularID));
 
  159     std::vector<EdgePair> superarcSorter;
 
  164     auto sortOrderPortal = sortOrder.
ReadPortal();
 
  170       vtkm::Id sortID = supernodesPortal.Get(supernode);
 
  173       vtkm::Id superTo = superarcsPortal.Get(supernode);
 
  189       if (regularID < regularTo)
 
  192         if (superarcsPortal.Get(superTo) != supernode)
 
  194           superarcSorter.push_back(
EdgePair(regularID, regularTo));
 
  199         superarcSorter.push_back(
EdgePair(regularTo, regularID));
 
  222     auto superarcIntrinsicWeightPortal = superarcIntrinsicWeight.
WritePortal();
 
  223     auto firstVertexForSuperparentPortal = firstVertexForSuperparent.
WritePortal();
 
  233       vtkm::Id sortID = nodesPortal.Get(sortedNode);
 
  234       vtkm::Id superparent = superparentsPortal.Get(sortID);
 
  236         firstVertexForSuperparentPortal.Set(superparent, sortedNode);
 
  237       else if (superparent != superparentsPortal.Get(nodesPortal.Get(sortedNode - 1)))
 
  238         firstVertexForSuperparentPortal.Set(superparent, sortedNode);
 
  243         superarcIntrinsicWeightPortal.Set(superarc,
 
  245                                             firstVertexForSuperparentPortal.Get(superarc));
 
  247         superarcIntrinsicWeightPortal.Set(superarc,
 
  248                                           firstVertexForSuperparentPortal.Get(superarc + 1) -
 
  249                                             firstVertexForSuperparentPortal.Get(superarc));
 
  254       superarcDependentWeight);
 
  257       supernodeTransferWeight);
 
  260       hyperarcDependentWeight);
 
  265     auto supernodeTransferWeightPortal = supernodeTransferWeight.
WritePortal();
 
  266     auto superarcDependentWeightPortal = superarcDependentWeight.
WritePortal();
 
  267     auto hyperarcDependentWeightPortal = hyperarcDependentWeight.
WritePortal();
 
  308     for (
vtkm::Id iteration = 0; iteration < nIterations; iteration++)
 
  311       vtkm::Id firstSupernode = firstSupernodePerIterationPortal.Get(iteration);
 
  312       vtkm::Id lastSupernode = firstSupernodePerIterationPortal.Get(iteration + 1);
 
  313       vtkm::Id firstHypernode = firstHypernodePerIterationPortal.Get(iteration);
 
  314       vtkm::Id lastHypernode = firstHypernodePerIterationPortal.Get(iteration + 1);
 
  338       for (
vtkm::Id supernode = firstSupernode; supernode < lastSupernode; supernode++)
 
  340         superarcDependentWeightPortal.Set(supernode,
 
  341                                           supernodeTransferWeightPortal.Get(supernode) +
 
  342                                             superarcIntrinsicWeightPortal.Get(supernode));
 
  346       for (
vtkm::Id supernode = firstSupernode + 1; supernode < lastSupernode; supernode++)
 
  347         superarcDependentWeightPortal.Set(supernode,
 
  348                                           superarcDependentWeightPortal.Get(supernode) +
 
  349                                             superarcDependentWeightPortal.Get(supernode - 1));
 
  355       for (
vtkm::Id supernode = lastSupernode - 1; supernode > firstSupernode; supernode--)
 
  358         vtkm::Id hyperparent = hyperparentsPortal.Get(supernode);
 
  359         vtkm::Id hyperparentSuperID = hypernodesPortal.Get(hyperparent);
 
  362         if (hyperparent == firstHypernode)
 
  366         superarcDependentWeightPortal.Set(
 
  368           superarcDependentWeightPortal.Get(supernode) -
 
  369             superarcDependentWeightPortal.Get(hyperparentSuperID - 1));
 
  373       for (
vtkm::Id hypernode = firstHypernode; hypernode < lastHypernode; hypernode++)
 
  383           lastSuperarc = hypernodesPortal.Get(hypernode + 1) - 1;
 
  386         hyperarcDependentWeightPortal.Set(hypernode,
 
  387                                           superarcDependentWeightPortal.Get(lastSuperarc));
 
  391         supernodeTransferWeightPortal.Set(hyperarcTarget,
 
  392                                           supernodeTransferWeightPortal.Get(hyperarcTarget) +
 
  393                                             hyperarcDependentWeightPortal.Get(hypernode));
 
  408     auto superarcDependentWeightPortal = superarcDependentWeight.
ReadPortal();
 
  409     auto superarcIntrinsicWeightPortal = superarcIntrinsicWeight.
ReadPortal();
 
  413     vtkm::Id nSuperarcs = nSupernodes - 1;
 
  423     auto noSuchElementArray =
 
  430     auto bestDownwardPortal = bestDownward.
WritePortal();
 
  439     auto superarcListPortal = superarcList.
WritePortal();
 
  442     std::cout << 
"Total Volume: " << totalVolume << std::endl;
 
  448     for (
vtkm::Id superarc = 0; superarc < nSuperarcs; superarc++)
 
  452         superarcListPortal.Set(superarc,
 
  454         upWeightPortal.Set(superarc, superarcDependentWeightPortal.Get(superarc));
 
  457         downWeightPortal.Set(superarc,
 
  458                              (totalVolume - superarcDependentWeightPortal.Get(superarc)) +
 
  459                                (superarcIntrinsicWeightPortal.Get(superarc) - 1));
 
  463         superarcListPortal.Set(superarc,
 
  465         downWeightPortal.Set(superarc, superarcDependentWeightPortal.Get(superarc));
 
  468         upWeightPortal.Set(superarc,
 
  469                            (totalVolume - superarcDependentWeightPortal.Get(superarc)) +
 
  470                              (superarcIntrinsicWeightPortal.Get(superarc) - 1));
 
  475     std::cout << 
"II A. Weights Computed" << std::endl;
 
  481     std::cout << std::endl;
 
  487     superarcSorter.
Allocate(nSuperarcs);
 
  488     auto superarcSorterPortal = superarcSorter.
WritePortal();
 
  489     for (
vtkm::Id superarc = 0; superarc < nSuperarcs; superarc++)
 
  490       superarcSorterPortal.Set(superarc, superarc);
 
  497     for (
vtkm::Id superarc = 0; superarc < nSuperarcs; superarc++)
 
  499       vtkm::Id superarcID = superarcSorterPortal.Get(superarc);
 
  500       const EdgePair& edge = superarcListPortal.Get(superarcID);
 
  502       if (superarc == nSuperarcs - 1)
 
  506         const EdgePair& nextEdge = superarcListPortal.Get(superarcSorterPortal.Get(superarc + 1));
 
  519     for (
vtkm::Id superarc = 0; superarc < nSuperarcs; superarc++)
 
  521       vtkm::Id superarcID = superarcSorterPortal.Get(superarc);
 
  522       const EdgePair& edge = superarcListPortal.Get(superarcID);
 
  524       if (superarc == nSuperarcs - 1)
 
  528         const EdgePair& nextEdge = superarcListPortal.Get(superarcSorterPortal.Get(superarc + 1));
 
  536     std::cout << 
"II. Best Edges Selected" << std::endl;
 
  540     std::cout << std::endl;
 
  566     auto noSuchElementArray =
 
  577       propagateBestUpDownWorklet;
 
  578     invoke(propagateBestUpDownWorklet, bestUpward, bestDownward, whichBranch);
 
  581     std::cout << 
"III. Branch Neighbours Identified" << std::endl;
 
  584     std::cout << std::endl;
 
  590     for (
vtkm::Id shifter = nSupernodes; shifter != 0; shifter >>= 1)
 
  597     for (
vtkm::Id iteration = 0; iteration < numLogSteps; iteration++)
 
  599       invoke(pointerDoubling, whichBranch);
 
  604     std::cout << 
"IV. Branch Chains Propagated" << std::endl;
 
  607     std::cout << std::endl;
 
  616       prepareChainToBranchWorklet;
 
  617     invoke(prepareChainToBranchWorklet, whichBranch, chainToBranch);
 
  623       finaliseChainToBranchWorklet;
 
  624     invoke(finaliseChainToBranchWorklet, whichBranch, chainToBranch);
 
  627     auto noSuchElementArrayNBranches =
 
  635     std::cout << 
"V. Branch Arrays Created" << std::endl;
 
  653     permutedBranches.
Allocate(nSupernodes);
 
  654     PermuteArray<vtkm::Id>(whichBranch, supernodeSorter, permutedBranches);
 
  657     permutedRegularID.
Allocate(nSupernodes);
 
  658     PermuteArray<vtkm::Id>(contourTree.
Supernodes, supernodeSorter, permutedRegularID);
 
  661     std::cout << 
"VI A. Sorted into Branches" << std::endl;
 
  669       whichBranchNewIdWorklet;
 
  670     invoke(whichBranchNewIdWorklet, chainToBranch, whichBranch);
 
  673       branchMinMaxSetWorklet(nSupernodes);
 
  674     invoke(branchMinMaxSetWorklet, supernodeSorter, whichBranch, branchMinimum, branchMaximum);
 
  677     std::cout << 
"VI. Branches Set" << std::endl;
 
  686       branchSaddleParentSetWorklet;
 
  687     invoke(branchSaddleParentSetWorklet,
 
  697     std::cout << 
"VII. Branches Constructed" << std::endl;
 
  708   template <
typename T, 
typename StorageType>
 
  719     bool dataFieldIsSorted)
 
  722       contourTreeSuperparents,
 
  723       contourTreeSupernodes,
 
  755       setFirstVertexForSuperparent;
 
  756     Invoke(setFirstVertexForSuperparent,
 
  759            firstVertexForSuperparent);
 
  762       computeIntrinsicWeight;
 
  763     Invoke(computeIntrinsicWeight,
 
  766            firstVertexForSuperparent,
 
  767            superarcIntrinsicWeight);
 
  772     auto noSuchElementArray =
 
  791     hyperarcScan<decltype(vtkm::Sum())>(contourTree.
Supernodes,
 
  809     Invoke(initArcs, sumValues, superarcIntrinsicWeight, contourTree.
Superarcs, arcs);
 
  815     Invoke(setBestUpDown, bestUpward, bestDownward, arcs);
 
  842     auto noSuchElementArray =
 
  875     for (std::size_t i = 1; i < minPath.size(); i++)
 
  877       minParentsPortal.Set(minPath[i], minPath[i - 1]);
 
  879     minParentsPortal.Set(minPath[0], 0);
 
  882     for (std::size_t i = 1; i < maxPath.size(); i++)
 
  884       maxParentsPortal.Set(maxPath[i], maxPath[i - 1]);
 
  886     maxParentsPortal.Set(maxPath[0], 0);
 
  890     Invoke(unmaskArrayWorklet, minValues);
 
  891     Invoke(unmaskArrayWorklet, maxValues);
 
  903     auto minHyperparentsPortal = minHyperparents.
WritePortal();
 
  904     auto maxHyperparentsPortal = maxHyperparents.
WritePortal();
 
  906     for (std::size_t i = 0; i < minPath.size(); i++)
 
  909       minHyperparentsPortal.Set(minPath[i],
 
  913     for (std::size_t i = 0; i < maxPath.size(); i++)
 
  916       maxHyperparentsPortal.Set(maxPath[i],
 
  939     hyperarcScan<decltype(vtkm::Minimum())>(contourTree.
Supernodes,
 
  963     hyperarcScan<decltype(vtkm::Maximum())>(contourTree.
Supernodes,
 
  981       incorporateParentMinimumWorklet(minOperator);
 
  982     Invoke(incorporateParentMinimumWorklet, minParents, contourTree.
Supernodes, minValues);
 
  986       incorporateParentMaximumWorklet(maxOperator);
 
  987     Invoke(incorporateParentMaximumWorklet, maxParents, contourTree.
Supernodes, maxValues);
 
  996     Invoke(initArcs, minParents, maxParents, minValues, maxValues, contourTree.
Superarcs, arcs);
 
 1000       computeSubtreeHeight;
 
 1001     Invoke(computeSubtreeHeight, fieldValues, ctSortOrder, contourTree.
Supernodes, arcs);
 
 1008     Invoke(setBestUpDown, bestUpward, bestDownward, arcs);
 
 1027     std::vector<vtkm::Id> path;
 
 1031     while (
MaskedIndex(parentsPortal.Get(current)) != 0)
 
 1033       path.push_back(current);
 
 1034       current = 
MaskedIndex(parentsPortal.Get(current));
 
 1036     path.push_back(current);
 
 1044                       const std::vector<vtkm::Id> path,
 
 1050     for (
auto i = path.size() - 2; i > 0; i--)
 
 1052       const auto vertex = path[i + 1];
 
 1053       const auto parent = path[i];
 
 1055       const auto vertexValue = minMaxIndex.Get(vertex);
 
 1056       const auto parentValue = minMaxIndex.Get(parent);
 
 1058       minMaxIndex.Set(parent, operation(vertexValue, parentValue));
 
 1068     const std::vector<vtkm::Id> path,
 
 1075     while (i < path.size())
 
 1078       hyperarcsPortal.Set(
MaskedIndex(hyperparentsPortal.Get(path[i])), path[i]);
 
 1080       Id currentHyperparent = 
MaskedIndex(hyperparentsPortal.Get(path[i]));
 
 1083       while (i < path.size() && 
MaskedIndex(hyperparentsPortal.Get(path[i])) == currentHyperparent)
 
 1085         const auto value = howManyUsedPortal.Get(
MaskedIndex(hyperparentsPortal.Get(path[i])));
 
 1086         howManyUsedPortal.Set(
MaskedIndex(hyperparentsPortal.Get(path[i])), value + 1);
 
 1092   template <
class BinaryFunctor>
 
 1101                            const BinaryFunctor operation,
 
 1108     auto supernodesPortal = supernodes.
ReadPortal();
 
 1109     auto hypernodesPortal = hypernodes.
ReadPortal();
 
 1110     auto hyperparentsPortal = hyperparents.
ReadPortal();
 
 1115                           firstSupernodePerIteration);
 
 1119       setFirstSupernodePerIteration;
 
 1120     invoke(setFirstSupernodePerIteration, whenTransferred, firstSupernodePerIteration);
 
 1122     auto firstSupernodePerIterationPortal = firstSupernodePerIteration.
WritePortal();
 
 1123     for (
vtkm::Id iteration = 1; iteration < nIterations; ++iteration)
 
 1125       if (firstSupernodePerIterationPortal.Get(iteration) == 0)
 
 1127         firstSupernodePerIterationPortal.Set(iteration,
 
 1128                                              firstSupernodePerIterationPortal.Get(iteration + 1));
 
 1133     firstSupernodePerIterationPortal.Set(nIterations, supernodesPortal.GetNumberOfValues());
 
 1138                           firstHypernodePerIteration);
 
 1139     auto firstHypernodePerIterationPortal = firstHypernodePerIteration.
WritePortal();
 
 1141     for (
vtkm::Id iteration = 0; iteration < nIterations; iteration++)
 
 1143       firstHypernodePerIterationPortal.Set(
 
 1144         iteration, hyperparentsPortal.Get(firstSupernodePerIterationPortal.Get(iteration)));
 
 1148     firstHypernodePerIterationPortal.Set(nIterations, hypernodesPortal.GetNumberOfValues());
 
 1153       addDependentWeightHypersweepWorklet(operation);
 
 1156     for (
vtkm::Id iteration = 0; iteration < nIterations; iteration++)
 
 1159       vtkm::Id firstHypernode = firstHypernodePerIterationPortal.Get(iteration);
 
 1160       vtkm::Id lastHypernode = firstHypernodePerIterationPortal.Get(iteration + 1);
 
 1170         minMaxIndex, firstSupernode, lastSupernode - firstSupernode);
 
 1172         hyperparentKeys, firstSupernode, lastSupernode - firstSupernode);
 
 1174         subarrayKeys, subarrayValues, subarrayValues, operation);
 
 1178         firstHypernode, 1, lastHypernode - firstHypernode);
 
 1181       invoke(addDependentWeightHypersweepWorklet,