diff --git a/examples/clipping/Clipping.cxx b/examples/clipping/Clipping.cxx index 7a47bf82f..52fb79c01 100644 --- a/examples/clipping/Clipping.cxx +++ b/examples/clipping/Clipping.cxx @@ -63,14 +63,15 @@ int main(int argc, char *argv[]) input.GetField(0); vtkm::Float32 clipValue = boost::lexical_cast(argv[argc - 2]); - vtkm::worklet::Clip clip; + vtkm::worklet::Clip clip; vtkm::cont::Timer total; vtkm::cont::Timer timer; vtkm::cont::CellSetExplicit<> outputCellSet = clip.Run(input.GetCellSet(0), scalarField.GetData().ResetTypeList(vtkm::TypeListTagScalarAll()), - clipValue); + clipValue, + DeviceAdapter()); vtkm::Float64 clipTime = timer.GetElapsedTime(); vtkm::cont::DataSet output; @@ -78,7 +79,7 @@ int main(int argc, char *argv[]) timer.Reset(); vtkm::cont::DynamicArrayHandle coords = - clip.ProcessField(input.GetCoordinateSystem(0)); + clip.ProcessField(input.GetCoordinateSystem(0), DeviceAdapter()); vtkm::Float64 processCoordinatesTime = timer.GetElapsedTime(); output.AddCoordinateSystem(vtkm::cont::CoordinateSystem("coordinates", coords)); @@ -91,7 +92,8 @@ int main(int argc, char *argv[]) continue; // clip only supports point fields for now. } vtkm::cont::DynamicArrayHandle data = - clip.ProcessField(inField.GetData().ResetTypeList(vtkm::TypeListTagAll())); + clip.ProcessField(inField.GetData().ResetTypeList(vtkm::TypeListTagAll()), + DeviceAdapter()); output.AddField(vtkm::cont::Field(inField.GetName(), vtkm::cont::Field::ASSOC_POINTS, data)); } diff --git a/vtkm/worklet/Clip.h b/vtkm/worklet/Clip.h index 3b9298731..92904f966 100644 --- a/vtkm/worklet/Clip.h +++ b/vtkm/worklet/Clip.h @@ -94,15 +94,6 @@ void ResizeArrayHandle(const vtkm::cont::ArrayHandle &input, Algorithm::Schedule(copyArray, input.GetNumberOfValues()); } -template -VTKM_EXEC_EXPORT -void swap(T &v1, T &v2) -{ - T temp = v1; - v1 = v2; - v2 = temp; -} - template VTKM_EXEC_CONT_EXPORT T Scale(const T &val, vtkm::Float64 scale) @@ -247,32 +238,15 @@ std::ostream& operator<<(std::ostream &out, const EdgeInterpolation &val) << ", Weight: " << val.Weight << " }"; } - -template class Clip { -private: - typedef internal::ClipTables::DevicePortal ClipTablesPortal; - - typedef typename vtkm::cont::ArrayHandle - ::template ExecutionTypes::Portal IdPortal; - typedef typename vtkm::cont::ArrayHandle - ::template ExecutionTypes::PortalConst IdPortalConst; - typedef typename vtkm::cont::ArrayHandle - ::template ExecutionTypes::Portal EdgeInterpolationPortal; - typedef typename vtkm::cont::ArrayHandle - ::template ExecutionTypes::PortalConst - EdgeInterpolationPortalConst; - - typedef vtkm::cont::DeviceAdapterAlgorithm Algorithm; - - public: struct TypeClipStats : vtkm::ListTagBase { }; - + template class ComputeStats : public vtkm::worklet::WorkletMapPointToCell { + typedef internal::ClipTables::DevicePortal ClipTablesPortal; public: typedef void ControlSignature(CellSetIn cellset, FieldInPoint scalars, @@ -331,17 +305,20 @@ public: ClipTablesPortal ClipTables; }; - + template class GenerateCellSet : public vtkm::worklet::WorkletMapPointToCell { + typedef internal::ClipTables::DevicePortal ClipTablesPortal; public: + struct EdgeInterp : vtkm::ListTagBase { }; + typedef void ControlSignature(CellSetIn cellset, FieldInPoint scalars, FieldInCell clipTableIdxs, FieldInCell cellSetIdxs, ExecObject connectivityExplicit, - ExecObject interpolation, - ExecObject newPointsConnectivityReverseMap); + WholeArrayInOut< EdgeInterp > interpolation, + WholeArrayInOut< IdType > newPointsConnectivityReverseMap); typedef void ExecutionSignature(CellShape, _2, FromIndices, _3, _4, _5, _6, _7); VTKM_CONT_EXPORT @@ -353,7 +330,8 @@ public: template + typename InterpolationWholeArrayType, + typename ReverseMapWholeArrayType> VTKM_EXEC_EXPORT void operator()( CellShapeTag shape, @@ -361,11 +339,9 @@ public: const IndicesVecType &indices, vtkm::Id clipTableIdx, ClipStats cellSetIndices, - internal::ExecutionConnectivityExplicit connectivityExplicit, - vtkm::exec::ExecutionWholeArray - interpolation, - vtkm::exec::ExecutionWholeArray - newPointsConnectivityReverseMap) const + internal::ExecutionConnectivityExplicit& connectivityExplicit, + InterpolationWholeArrayType& interpolation, + ReverseMapWholeArrayType& newPointsConnectivityReverseMap) const { vtkm::Id idx = clipTableIdx; @@ -403,8 +379,8 @@ public: ei.Vertex2 = indices[edge[1]]; if (ei.Vertex1 > ei.Vertex2) { - internal::swap(ei.Vertex1, ei.Vertex2); - internal::swap(edge[0], edge[1]); + this->swap(ei.Vertex1, ei.Vertex2); + this->swap(edge[0], edge[1]); } ei.Weight = (static_cast(scalars[edge[0]]) - this->Value) / static_cast(scalars[edge[0]] - scalars[edge[1]]); @@ -417,6 +393,15 @@ public: } } + template + VTKM_EXEC_EXPORT + void swap(T &v1, T &v2) const + { + T temp = v1; + v1 = v2; + v2 = temp; + } + private: vtkm::Float64 Value; ClipTablesPortal ClipTables; @@ -427,8 +412,17 @@ public: // a worklet for updating connectivity. We are going with a custom worklet, that // combines lower-bounds computation and connectivity update, because this is // currently faster and uses less memory. + template class AmendConnectivity : public vtkm::exec::FunctorBase { + typedef typename vtkm::cont::ArrayHandle + ::template ExecutionTypes::Portal IdPortal; + typedef typename vtkm::cont::ArrayHandle + ::template ExecutionTypes::PortalConst IdPortalConst; + typedef typename vtkm::cont::ArrayHandle + ::template ExecutionTypes::PortalConst + EdgeInterpolationPortalConst; + public: VTKM_CONT_EXPORT AmendConnectivity(EdgeInterpolationPortalConst newPoints, @@ -487,12 +481,15 @@ public: { } - template + template vtkm::cont::CellSetExplicit<> Run(const vtkm::cont::DynamicCellSet &cellSet, const ScalarsArrayHandle &scalars, - vtkm::Float64 value) + vtkm::Float64 value, + DeviceAdapter device) { - DeviceAdapter device; + typedef vtkm::cont::DeviceAdapterAlgorithm Algorithm; + + typedef internal::ClipTables::DevicePortal ClipTablesPortal; ClipTablesPortal clipTablesDevicePortal = this->ClipTablesInstance.GetDevicePortal(device); @@ -501,9 +498,9 @@ public: vtkm::cont::ArrayHandle clipTableIdxs; vtkm::cont::ArrayHandle stats; - ComputeStats computeStats(value, clipTablesDevicePortal); - DispatcherMapTopology(computeStats).Invoke(cellSet, scalars, - clipTableIdxs, stats); + ComputeStats computeStats(value, clipTablesDevicePortal); + DispatcherMapTopology< ComputeStats >(computeStats) + .Invoke(cellSet, scalars, clipTableIdxs, stats); // compute offsets for each invocation ClipStats zero = { 0, 0, 0 }; @@ -525,21 +522,19 @@ public: cellToConnectivityMap.PrepareForOutput(total.NumberOfCells, device)); vtkm::cont::ArrayHandle newPoints; + newPoints.Allocate(total.NumberOfNewPoints); // reverse map from the new points to connectivity array vtkm::cont::ArrayHandle newPointsConnectivityReverseMap; + newPointsConnectivityReverseMap.Allocate(total.NumberOfNewPoints); - GenerateCellSet generateCellSet(value, clipTablesDevicePortal); - - DispatcherMapTopology(generateCellSet).Invoke(cellSet, scalars, - clipTableIdxs, cellSetIndices, outConnectivity, - vtkm::exec::ExecutionWholeArray< - EdgeInterpolation, - VTKM_DEFAULT_STORAGE_TAG, - DeviceAdapter>(newPoints, total.NumberOfNewPoints), - vtkm::exec::ExecutionWholeArray< - vtkm::Id, - VTKM_DEFAULT_STORAGE_TAG, - DeviceAdapter>(newPointsConnectivityReverseMap, total.NumberOfNewPoints)); + GenerateCellSet generateCellSet(value, clipTablesDevicePortal); + DispatcherMapTopology >(generateCellSet) + .Invoke(cellSet, scalars, + clipTableIdxs, cellSetIndices, + outConnectivity, + newPoints, + newPointsConnectivityReverseMap + ); cellSetIndices.ReleaseResources(); // Step 3. remove duplicates from the list of new points @@ -555,7 +550,7 @@ public: // Step 4. update the connectivity array with indexes to the new, unique points - AmendConnectivity computeNewPointsConnectivity( + AmendConnectivity computeNewPointsConnectivity( newPoints.PrepareForInput(device), uniqueNewPoints.PrepareForInput(device), newPointsConnectivityReverseMap.PrepareForInput(device), @@ -570,7 +565,7 @@ public: return output; } - template + template class ClipWithImplicitFunction { public: @@ -590,7 +585,10 @@ public: vtkm::ImplicitFunctionValue > clipScalars(handle, this->Function); - *this->Result = this->Clipper->Run(*this->CellSet, clipScalars, 0.0); + *this->Result = this->Clipper->Run(*this->CellSet, + clipScalars, + 0.0, + DeviceAdapter()); } private: @@ -600,19 +598,25 @@ public: vtkm::cont::CellSetExplicit<> *Result; }; - template + template vtkm::cont::CellSetExplicit<> Run(const vtkm::cont::DynamicCellSet &cellSet, const ImplicitFunction &clipFunction, - const vtkm::cont::CoordinateSystem &coords) + const vtkm::cont::CoordinateSystem &coords, + DeviceAdapter device) { + (void) device; vtkm::cont::CellSetExplicit<> output; - ClipWithImplicitFunction clip(this, cellSet, clipFunction, + ClipWithImplicitFunction clip(this, + cellSet, + clipFunction, &output); CastAndCall(coords, clip); return output; } + template class InterpolateField { public: @@ -623,6 +627,10 @@ public: typedef typename vtkm::cont::ArrayHandle ::template ExecutionTypes::Portal FieldPortal; + typedef typename vtkm::cont::ArrayHandle + ::template ExecutionTypes::PortalConst + EdgeInterpolationPortalConst; + VTKM_CONT_EXPORT Kernel(EdgeInterpolationPortalConst interpolation, vtkm::Id newPointsOffset, @@ -647,10 +655,23 @@ public: FieldPortal Field; }; + VTKM_CONT_EXPORT + InterpolateField( + vtkm::cont::ArrayHandle interpolationArray, + vtkm::Id newPointsOffset, + vtkm::cont::DynamicArrayHandle *output) + : InterpolationArray(interpolationArray), + NewPointsOffset(newPointsOffset), + Output(output) + { + } + template VTKM_CONT_EXPORT void operator()(const vtkm::cont::ArrayHandle &field) const { + typedef vtkm::cont::DeviceAdapterAlgorithm Algorithm; + vtkm::Id count = this->InterpolationArray.GetNumberOfValues(); vtkm::cont::ArrayHandle result; @@ -666,31 +687,22 @@ public: *(this->Output) = vtkm::cont::DynamicArrayHandle(result); } - VTKM_CONT_EXPORT - InterpolateField( - vtkm::cont::ArrayHandle interpolationArray, - vtkm::Id newPointsOffset, - vtkm::cont::DynamicArrayHandle *output) - : InterpolationArray(interpolationArray), - NewPointsOffset(newPointsOffset), - Output(output) - { - } - private: vtkm::cont::ArrayHandle InterpolationArray; vtkm::Id NewPointsOffset; vtkm::cont::DynamicArrayHandle *Output; }; - template + template vtkm::cont::DynamicArrayHandle ProcessField( - const FieldType &fieldData) const + const FieldType &fieldData, + DeviceAdapter device) const { + (void) device; vtkm::cont::DynamicArrayHandle output; - CastAndCall(fieldData, InterpolateField(this->NewPointsInterpolation, - this->NewPointsOffset, - &output)); + CastAndCall(fieldData, InterpolateField(this->NewPointsInterpolation, + this->NewPointsOffset, + &output)); return output; } diff --git a/vtkm/worklet/testing/UnitTestClipping.cxx b/vtkm/worklet/testing/UnitTestClipping.cxx index ecd8d34a3..f69474c5d 100644 --- a/vtkm/worklet/testing/UnitTestClipping.cxx +++ b/vtkm/worklet/testing/UnitTestClipping.cxx @@ -118,17 +118,18 @@ void TestClippingExplicit() { vtkm::cont::DataSet ds = MakeTestDatasetExplicit(); - vtkm::worklet::Clip clip; + vtkm::worklet::Clip clip; vtkm::cont::CellSetExplicit<> outputCellSet = clip.Run(ds.GetCellSet(0), ds.GetField("scalars").GetData(), - clipValue); + clipValue, + DeviceAdapter()); vtkm::cont::DynamicArrayHandle coords = - clip.ProcessField(ds.GetCoordinateSystem("coords")); + clip.ProcessField(ds.GetCoordinateSystem("coords"), DeviceAdapter()); vtkm::cont::DynamicArrayHandle scalars = - clip.ProcessField(ds.GetField("scalars")); + clip.ProcessField(ds.GetField("scalars"), DeviceAdapter()); vtkm::Id connectivitySize = 12; @@ -169,17 +170,18 @@ void TestClippingStrucutred() { vtkm::cont::DataSet ds = MakeTestDatasetStructured(); - vtkm::worklet::Clip clip; + vtkm::worklet::Clip clip; vtkm::cont::CellSetExplicit<> outputCellSet = clip.Run(ds.GetCellSet(0), ds.GetField("scalars").GetData(), - clipValue); + clipValue, + DeviceAdapter()); vtkm::cont::DynamicArrayHandle coords = - clip.ProcessField(ds.GetCoordinateSystem("coords")); + clip.ProcessField(ds.GetCoordinateSystem("coords"), DeviceAdapter()); vtkm::cont::DynamicArrayHandle scalars = - clip.ProcessField(ds.GetField("scalars")); + clip.ProcessField(ds.GetField("scalars"), DeviceAdapter()); vtkm::Id connectivitySize = 36; vtkm::Id fieldSize = 13; @@ -226,15 +228,18 @@ void TestClippingWithImplicitFunction() vtkm::cont::DataSet ds = MakeTestDatasetStructured(); - vtkm::worklet::Clip clip; + vtkm::worklet::Clip clip; vtkm::cont::CellSetExplicit<> outputCellSet = - clip.Run(ds.GetCellSet(0), sphere, ds.GetCoordinateSystem("coords")); + clip.Run(ds.GetCellSet(0), + sphere, + ds.GetCoordinateSystem("coords"), + DeviceAdapter()); vtkm::cont::DynamicArrayHandle coords = - clip.ProcessField(ds.GetCoordinateSystem("coords")); + clip.ProcessField(ds.GetCoordinateSystem("coords"), DeviceAdapter()); vtkm::cont::DynamicArrayHandle scalars = - clip.ProcessField(ds.GetField("scalars")); + clip.ProcessField(ds.GetField("scalars"), DeviceAdapter()); vtkm::Id connectivitySize = 36; vtkm::Id fieldSize = 13;