diff --git a/vtkm/worklet/contourtree_distributed/TreeGrafter.h b/vtkm/worklet/contourtree_distributed/TreeGrafter.h index eb6a1d740..2d0e569ef 100644 --- a/vtkm/worklet/contourtree_distributed/TreeGrafter.h +++ b/vtkm/worklet/contourtree_distributed/TreeGrafter.h @@ -272,43 +272,30 @@ private: // method to grow a vector without loosing the original data values. The input array // is modified or replaced template - inline static void resizeIndexVector(vtkm::cont::ArrayHandle& thearray, - vtkm::Id newSize, - ValueType fillValue) + static void ResizeIndexVector(vtkm::cont::ArrayHandle& thearray, + vtkm::Id newSize, + ValueType fillValue) { - if (thearray.GetNumberOfValues() == 0 && newSize > 0) // new allocation + vtkm::Id oldSize = thearray.GetNumberOfValues(); + // Simply return if the size of the array does not change + if (oldSize == newSize) { - auto tempConstArray = vtkm::cont::ArrayHandleConstant(fillValue, newSize); - vtkm::cont::Algorithm::Copy(tempConstArray, thearray); + return; } - else if (newSize < thearray.GetNumberOfValues()) // shrink + + // Resize the array but keep the original values + thearray.Allocate(newSize, vtkm::CopyFlag::On); + + // Add the fill values to the array if we increased the size of the array + if (oldSize < newSize) { - thearray.Shrink(newSize); - } - else if (newSize > thearray.GetNumberOfValues()) // grow - { - vtkm::cont::ArrayHandle newarray; - newarray.Allocate(newSize); - vtkm::cont::Algorithm::CopySubRange(thearray, // copy - 0, // start copying from index - thearray.GetNumberOfValues(), // num values to copy - newarray, // copy to - 0 // start copy to index + vtkm::cont::Algorithm::CopySubRange( + vtkm::cont::ArrayHandleConstant(fillValue, newSize - oldSize), // copy + 0, // start copying from first index + newSize - oldSize, // num values to copy + thearray, // target array to copy to + oldSize // start copy to after oldSize ); - vtkm::Id numNewValues = newSize - thearray.GetNumberOfValues(); - auto tempConstArray = vtkm::cont::ArrayHandleConstant(fillValue, numNewValues); - vtkm::cont::Algorithm::CopySubRange(tempConstArray, // copy - 0, // start copying from first index - tempConstArray.GetNumberOfValues(), // num values to copy - newarray, // copy to - thearray.GetNumberOfValues() // start copy to index - ); - // swap in the newarray - thearray = newarray; - } - else // The size does not change. - { - // Nothing to do } } }; // class TreeGrafter @@ -642,7 +629,7 @@ template void TreeGrafter::FindCriticalPoints() { // FindCriticalPoints() // allocate memory for type of supernode - this->resizeIndexVector(this->SupernodeType, + this->ResizeIndexVector(this->SupernodeType, this->ContourTree.Supernodes.GetNumberOfValues(), vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); // Reset the UpNeighbour and DownNeighbour array @@ -1092,13 +1079,13 @@ void TreeGrafter::CopyNewHypernodes( { // Resize array to length totalNHypernodes and fill new values with NO_SUCH_ELEMENT (or 0) (while keeping original values) // NOTE: hierarchicalTree.Superchildren is initalized here but not used by this function - this->resizeIndexVector(hierarchicalTree.Hypernodes, + this->ResizeIndexVector(hierarchicalTree.Hypernodes, totalNHypernodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); - this->resizeIndexVector(hierarchicalTree.Hyperarcs, + this->ResizeIndexVector(hierarchicalTree.Hyperarcs, totalNHypernodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); - this->resizeIndexVector( + this->ResizeIndexVector( hierarchicalTree.Superchildren, totalNHypernodes, static_cast(0)); } // B. Copy in the hypernodes & hyperarcs @@ -1143,22 +1130,22 @@ void TreeGrafter::CopyNewSupernodes( vtkm::Id nNewSupernodes = this->NewSupernodes.GetNumberOfValues(); vtkm::Id totalNSupernodes = nOldSupernodes + nNewSupernodes; // Resize array to length totalNHypernodes and fill new values with NO_SUCH_ELEMENT (while keeping original values) - this->resizeIndexVector(hierarchicalTree.Supernodes, + this->ResizeIndexVector(hierarchicalTree.Supernodes, totalNSupernodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); - this->resizeIndexVector(hierarchicalTree.Superarcs, + this->ResizeIndexVector(hierarchicalTree.Superarcs, totalNSupernodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); - this->resizeIndexVector(hierarchicalTree.Hyperparents, + this->ResizeIndexVector(hierarchicalTree.Hyperparents, totalNSupernodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); - this->resizeIndexVector(hierarchicalTree.Super2Hypernode, + this->ResizeIndexVector(hierarchicalTree.Super2Hypernode, totalNSupernodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); - this->resizeIndexVector(hierarchicalTree.WhichRound, + this->ResizeIndexVector(hierarchicalTree.WhichRound, totalNSupernodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); - this->resizeIndexVector(hierarchicalTree.WhichIteration, + this->ResizeIndexVector(hierarchicalTree.WhichIteration, totalNSupernodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); @@ -1166,7 +1153,7 @@ void TreeGrafter::CopyNewSupernodes( vtkm::Id nOldNodes = hierarchicalTree.RegularNodeGlobalIds.GetNumberOfValues(); vtkm::Id nNewNodes = this->NewNodes.GetNumberOfValues(); vtkm::Id totalNNodes = nOldNodes + nNewNodes; - this->resizeIndexVector(hierarchicalTree.Superparents, + this->ResizeIndexVector(hierarchicalTree.Superparents, totalNNodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); @@ -1259,7 +1246,7 @@ void TreeGrafter::CopyNewNodes( vtkm::Id totalNNodes = nOldNodes + nNewNodes; // A. We start by finding & copying the global IDs for every regular node - this->resizeIndexVector(hierarchicalTree.RegularNodeGlobalIds, + this->ResizeIndexVector(hierarchicalTree.RegularNodeGlobalIds, totalNNodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); // TODO: The original code created a separate array newNodesGloablId that was set to NO_SUCH_ELEMENT first but we should only need the fancy array here and save the memory @@ -1283,7 +1270,7 @@ void TreeGrafter::CopyNewNodes( // B. Next, we transfer the data values // TODO: Hamish, why are data values initalized with NO_SUCH_ELEMENT? Is this needed or can simply allocate? - this->resizeIndexVector( + this->ResizeIndexVector( hierarchicalTree.DataValues, totalNNodes, static_cast(vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT)); @@ -1304,7 +1291,7 @@ void TreeGrafter::CopyNewNodes( // C. Then we add the new array indices to the sort and resort it // Resize and initialize hierarchicalTree.RegularNodeSortOrder with NO_SUCH_ELEMENT // TODO: We should be able to shortcut this since the last values are set next in the CopySubrange - this->resizeIndexVector(hierarchicalTree.RegularNodeSortOrder, + this->ResizeIndexVector(hierarchicalTree.RegularNodeSortOrder, totalNNodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); { @@ -1332,7 +1319,7 @@ void TreeGrafter::CopyNewNodes( // D. now loop through the supernodes to set their lookup index from regular IDs // Resize and initialize hierarchicalTree.Regular2Supernode with NO_SUCH_ELEMENT - this->resizeIndexVector(hierarchicalTree.Regular2Supernode, + this->ResizeIndexVector(hierarchicalTree.Regular2Supernode, totalNNodes, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); { @@ -1434,7 +1421,7 @@ void TreeGrafter::CopyIterationDetails( #endif // and set the per round iteration counts. There may be smarter ways of doing this, but . . . - this->resizeIndexVector( + this->ResizeIndexVector( hierarchicalTree.FirstSupernodePerIteration[static_cast(theRound)], this->NumTransferIterations, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT); @@ -1458,7 +1445,7 @@ void TreeGrafter::CopyIterationDetails( #endif // Initalize hierarchicalTree.FirstHypernodePerIteration with NO_SUCH_ELEMENT - this->resizeIndexVector( + this->ResizeIndexVector( hierarchicalTree.FirstHypernodePerIteration[static_cast(theRound)], this->NumTransferIterations, vtkm::worklet::contourtree_augmented::NO_SUCH_ELEMENT);