From a92ad22c736901dd10c4ce64fedc0ed5569b77f9 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Tue, 26 Nov 2019 16:53:56 -0500 Subject: [PATCH] Improve Threshold runtime and compile performance Using GCC 9.2 the pre change vtkm::filter::Threshold would take 31.84sec to compile and use 1.1GB of memory. After these changes the filter takes 24.16sec to compile and uses 885MB of memory. --- vtkm/filter/Threshold.hxx | 10 ++++++ vtkm/worklet/ExtractGeometry.h | 4 --- vtkm/worklet/ExtractPoints.h | 4 --- vtkm/worklet/Mask.h | 4 --- vtkm/worklet/Threshold.h | 66 ++++++++++++---------------------- vtkm/worklet/ThresholdPoints.h | 4 --- 6 files changed, 32 insertions(+), 60 deletions(-) diff --git a/vtkm/filter/Threshold.hxx b/vtkm/filter/Threshold.hxx index 3f5fcef40..39f1962f4 100644 --- a/vtkm/filter/Threshold.hxx +++ b/vtkm/filter/Threshold.hxx @@ -31,9 +31,19 @@ public: template VTKM_EXEC bool operator()(const T& value) const { + return value >= static_cast(this->Lower) && value <= static_cast(this->Upper); } + //Needed to work with ArrayHandleVirtual + template + VTKM_EXEC bool operator()( + const vtkm::internal::ArrayPortalValueReference& value) const + { + using T = typename PortalType::ValueType; + return value.Get() >= static_cast(this->Lower) && value.Get() <= static_cast(this->Upper); + } + private: vtkm::Float64 Lower; vtkm::Float64 Upper; diff --git a/vtkm/worklet/ExtractGeometry.h b/vtkm/worklet/ExtractGeometry.h index ebb58c157..a4d9fd358 100644 --- a/vtkm/worklet/ExtractGeometry.h +++ b/vtkm/worklet/ExtractGeometry.h @@ -29,10 +29,6 @@ namespace worklet class ExtractGeometry { public: - struct BoolType : vtkm::ListTagBase - { - }; - //////////////////////////////////////////////////////////////////////////////////// // Worklet to identify cells within volume of interest class ExtractCellsByVOI : public vtkm::worklet::WorkletVisitCellsWithPoints diff --git a/vtkm/worklet/ExtractPoints.h b/vtkm/worklet/ExtractPoints.h index b8acea539..169559faa 100644 --- a/vtkm/worklet/ExtractPoints.h +++ b/vtkm/worklet/ExtractPoints.h @@ -28,10 +28,6 @@ namespace worklet class ExtractPoints { public: - struct BoolType : vtkm::ListTagBase - { - }; - //////////////////////////////////////////////////////////////////////////////////// // Worklet to identify points within volume of interest class ExtractPointsByVOI : public vtkm::worklet::WorkletVisitPointsWithCells diff --git a/vtkm/worklet/Mask.h b/vtkm/worklet/Mask.h index d54cd97e9..3d7f74201 100644 --- a/vtkm/worklet/Mask.h +++ b/vtkm/worklet/Mask.h @@ -29,10 +29,6 @@ namespace worklet class Mask { public: - struct BoolType : vtkm::ListTagBase - { - }; - template vtkm::cont::CellSetPermutation Run(const CellSetType& cellSet, const vtkm::Id stride) { diff --git a/vtkm/worklet/Threshold.h b/vtkm/worklet/Threshold.h index 29f1e8fa3..a9774af9b 100644 --- a/vtkm/worklet/Threshold.h +++ b/vtkm/worklet/Threshold.h @@ -11,8 +11,8 @@ #define vtkm_m_worklet_Threshold_h #include +#include #include -#include #include #include @@ -37,10 +37,6 @@ public: Cell }; - struct BoolType : vtkm::ListTagBase - { - }; - template class ThresholdByPointField : public vtkm::worklet::WorkletVisitCellsWithPoints { @@ -76,36 +72,20 @@ public: UnaryPredicate Predicate; }; - template - class ThresholdByCellField : public vtkm::worklet::WorkletVisitCellsWithPoints + struct ThresholdCopy : public vtkm::worklet::WorkletMapField { - public: - using ControlSignature = void(CellSetIn cellset, FieldInCell scalars, FieldOut passFlags); + using ControlSignature = void(FieldIn, FieldOut, WholeArrayIn); - using ExecutionSignature = _3(_2); - - VTKM_CONT - ThresholdByCellField() - : Predicate() + template + VTKM_EXEC void operator()(vtkm::Id& index, + ScalarType& output, + const WholeFieldIn& inputField) const { + output = inputField.Get(index); } - - VTKM_CONT - explicit ThresholdByCellField(const UnaryPredicate& predicate) - : Predicate(predicate) - { - } - - template - VTKM_EXEC bool operator()(const ScalarType& scalar) const - { - return this->Predicate(scalar); - } - - private: - UnaryPredicate Predicate; }; + template vtkm::cont::CellSetPermutation Run( const CellSetType& cellSet, @@ -115,25 +95,29 @@ public: { using OutputType = vtkm::cont::CellSetPermutation; - vtkm::cont::ArrayHandle passFlags; switch (fieldType) { case vtkm::cont::Field::Association::POINTS: { using ThresholdWorklet = ThresholdByPointField; + vtkm::cont::ArrayHandle passFlags; ThresholdWorklet worklet(predicate); DispatcherMapTopology dispatcher(worklet); dispatcher.Invoke(cellSet, field, passFlags); + + vtkm::cont::Algorithm::CopyIf(vtkm::cont::ArrayHandleIndex(passFlags.GetNumberOfValues()), + passFlags, + this->ValidCellIds); + break; } case vtkm::cont::Field::Association::CELL_SET: { - using ThresholdWorklet = ThresholdByCellField; - - ThresholdWorklet worklet(predicate); - DispatcherMapTopology dispatcher(worklet); - dispatcher.Invoke(cellSet, field, passFlags); + vtkm::cont::Algorithm::CopyIf(vtkm::cont::ArrayHandleIndex(field.GetNumberOfValues()), + field, + this->ValidCellIds, + predicate); break; } @@ -141,9 +125,6 @@ public: throw vtkm::cont::ErrorBadValue("Expecting point or cell field."); } - vtkm::cont::Algorithm::CopyIf( - vtkm::cont::ArrayHandleIndex(passFlags.GetNumberOfValues()), passFlags, this->ValidCellIds); - return OutputType(this->ValidCellIds, cellSet); } @@ -195,14 +176,11 @@ public: template vtkm::cont::ArrayHandle ProcessCellField( - const vtkm::cont::ArrayHandle in) const + const vtkm::cont::ArrayHandle& in) const { - // Use a temporary permutation array to simplify the mapping: - auto tmp = vtkm::cont::make_ArrayHandlePermutation(this->ValidCellIds, in); - - // Copy into an array with default storage: vtkm::cont::ArrayHandle result; - vtkm::cont::ArrayCopy(tmp, result); + DispatcherMapField dispatcher; + dispatcher.Invoke(this->ValidCellIds, result, in); return result; } diff --git a/vtkm/worklet/ThresholdPoints.h b/vtkm/worklet/ThresholdPoints.h index 11cc404b6..4bc20eb9e 100644 --- a/vtkm/worklet/ThresholdPoints.h +++ b/vtkm/worklet/ThresholdPoints.h @@ -25,10 +25,6 @@ namespace worklet class ThresholdPoints { public: - struct BoolType : vtkm::ListTagBase - { - }; - template class ThresholdPointField : public vtkm::worklet::WorkletVisitPointsWithCells {