// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen // SPDX-FileCopyrightText: Copyright (c) Kitware, Inc. // SPDX-FileCopyrightText: Copyright 2019 Sandia Corporation. // SPDX-FileCopyrightText: Copyright 2019 UT-Battelle, LLC. // SPDX-FileCopyrightText: Copyright 2019 Los Alamos National Security. // SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-LANL-USGov /** * @class vtkmDataArray * @brief Wraps a Viskores `ArrayHandle` inside a sub-class of `vtkGenericDataArray`. * * vtkmDataArray can be used to wrap an ArrayHandle with base component type of T. It is mainly * intended as a way to pass a Viskores ArrayHandle through a VTK pipeline in a zero-copy manner. * This is useful for implicit ArrayHandles or when unified memory is not being used. As long as the * underlying data is not accessed, device to host copying of the data is avoided. The ComputeRange * and ComputeFiniteRange functions have been overloaded to do the computation on the device side * using Viskores. This also avoids device-to-host memory transfers for this commonly used * operation. Individual elements of the underlying data can be accessed via the * `vtkGenericDataArray` API, but there are some limitations to keep in mind: * 1. Access can be quite slow compared to direct memory access and thus, should be avoided. * 2. Once the underlying data is accessed though this class, any modifications via the ArrayHandle * interface would result in undefined behaviour. * 3. Any modifications made through this class' API is not guaranteed to be reflected via the * ArrayHandle interface. * * @sa vtkGenericDataArray */ #ifndef vtkmDataArray_h #define vtkmDataArray_h #include "vtkAcceleratorsVTKmCoreModule.h" // For export macro #include "vtkGenericDataArray.h" #include "vtkmConfigCore.h" // For template export #include // For viskores::VecTraits #include // For viskores::cont::ArrayHandle #include // For viskores::cont::UnknownArrayHandle #include // For std::unique_ptr<> #include // For std::mutex namespace fromvtkm { VTK_ABI_NAMESPACE_BEGIN template class ArrayHandleHelperBase; template class ArrayHandleHelperUnknown; template class ArrayHandleHelperRead; template class ArrayHandleHelperWrite; VTK_ABI_NAMESPACE_END } // fromvtkm VTK_ABI_NAMESPACE_BEGIN template class vtkmDataArray : public vtkGenericDataArray, T, vtkArrayTypes::VTKM_DATA_ARRAY> { static_assert(std::is_arithmetic::value, "T must be an integral or floating-point type"); using GenericDataArrayType = vtkGenericDataArray, T, vtkArrayTypes::VTKM_DATA_ARRAY>; public: using SelfType = vtkmDataArray; vtkTemplateTypeMacro(SelfType, GenericDataArrayType); using typename Superclass::ArrayTypeTag; using typename Superclass::DataTypeTag; using typename Superclass::ValueType; static vtkmDataArray* New(); /// @brief Set the Viskores ArrayHandle to be wrapped /// void SetVtkmArrayHandle(const viskores::cont::UnknownArrayHandle& ah); /// @brief Get the underlying ArrayHandle. /// viskores::cont::UnknownArrayHandle GetVtkmUnknownArrayHandle() const; ///@{ /// If the data in the ArrayHandle has a basic layout, this does a shallow copy. /// Otherwise, it does a deep copy. void* GetVoidPointer(vtkIdType valueIdx) override; void* WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues) override; vtkDataArray::MemorySpace GetMemorySpace() override; void* GetDeviceVoidPointer(vtkIdType valueIdx) override; ///@} /// Support methods for \c vtkGenericDataArray. ValueType GetValue(vtkIdType valueIdx) const; void SetValue(vtkIdType valueIdx, ValueType value); void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const; void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple); ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const; void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value); protected: vtkmDataArray(); ~vtkmDataArray() override; /// Overrides for range computation. These use Viskores to perform the computations to avoid /// memory transfers using Superclass::ComputeScalarRange; bool ComputeScalarRange( double* ranges, const unsigned char* ghosts, unsigned char ghostsToSkip = 0xff) override; using Superclass::ComputeVectorRange; bool ComputeVectorRange( double range[2], const unsigned char* ghosts, unsigned char ghostsToSkip = 0xff) override; using Superclass::ComputeFiniteScalarRange; bool ComputeFiniteScalarRange( double* ranges, const unsigned char* ghosts, unsigned char ghostsToSkip = 0xff) override; using Superclass::ComputeFiniteVectorRange; bool ComputeFiniteVectorRange( double range[2], const unsigned char* ghosts, unsigned char ghostsToSkip = 0xff) override; /// concept methods for \c vtkGenericDataArray bool AllocateTuples(vtkIdType numberOfTuples); bool ReallocateTuples(vtkIdType numberOfTuples); private: // To access concept methods friend class vtkGenericDataArray; friend fromvtkm::ArrayHandleHelperBase; friend fromvtkm::ArrayHandleHelperUnknown; template friend class fromvtkm::ArrayHandleHelperRead; template friend class fromvtkm::ArrayHandleHelperWrite; mutable std::unique_ptr> Helper; mutable std::mutex Mutex; vtkmDataArray(const vtkmDataArray&) = delete; void operator=(const vtkmDataArray&) = delete; }; // Declare vtkArrayDownCast implementations for vtkmDataArray: vtkArrayDownCast_TemplateFastCastMacro(vtkmDataArray); //============================================================================= template inline vtkmDataArray::BaseComponentType>* make_vtkmDataArray( const viskores::cont::ArrayHandle& ah) { auto ret = vtkmDataArray::BaseComponentType>::New(); ret->SetVtkmArrayHandle(ah); return ret; } //============================================================================= template inline vtkmDataArray::BaseComponentType>* make_vtkmDataArray( const viskores::cont::ArrayHandle>& ah) { auto ret = vtkmDataArray::BaseComponentType>::New(); ret->SetVtkmArrayHandle(ah); return ret; } //============================================================================= #ifndef vtkmDataArray_cxx extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; extern template class VTKACCELERATORSVTKMCORE_TEMPLATE_EXPORT vtkmDataArray; #endif // vtkmDataArray_cxx VTK_ABI_NAMESPACE_END #endif // vtkmDataArray_h #include "vtkmlib/vtkmDataArray.hxx" // VTK-HeaderTest-Exclude: vtkmDataArray.h