//============================================================================ // Copyright (c) Kitware, Inc. // All rights reserved. // See LICENSE.txt for details. // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notice for more information. //============================================================================ #ifndef vtk_m_cont_ArrayCopy_h #define vtk_m_cont_ArrayCopy_h #include #include #include #include #include #include #include #include // TODO: When virtual arrays are available, compile the implementation in a .cxx/.cu file. Common // arrays are copied directly but anything else would be copied through virtual methods. namespace vtkm { namespace cont { namespace detail { // Element-wise copy. template void ArrayCopyWithAlgorithm(const InArrayType& source, OutArrayType& destination) { // Current implementation of Algorithm::Copy will first try to copy on devices where the // data is already available. vtkm::cont::Algorithm::Copy(source, destination); } // TODO: Remove last argument once ArryHandleNewStyle becomes ArrayHandle template void ArrayCopyOldImpl(const InArrayType& in, OutArrayType& out, std::false_type /* Copy storage */) { ArrayCopyWithAlgorithm(in, out); } // Copy storage for implicit arrays, must be of same type: // TODO: This will go away once ArrayHandleNewStyle becomes ArrayHandle. template void ArrayCopyOldImpl(const ArrayType& in, ArrayType& out, std::true_type /* Copy storage */) { // This is only called if in/out are the same type and the handle is not // writable. This allows read-only implicit array handles to be copied. auto newStorage = in.GetStorage(); out = ArrayType(newStorage); } // TODO: This will go away once ArrayHandleNewStyle becomes ArrayHandle. template VTKM_CONT void ArrayCopyImpl(const InArrayType& source, OutArrayType& destination, std::false_type /* New style */) { using SameTypes = std::is_same; using IsWritable = vtkm::cont::internal::IsWritableArrayHandle; using JustCopyStorage = std::integral_constant; ArrayCopyOldImpl(source, destination, JustCopyStorage{}); } // TODO: ArrayHandleNewStyle will eventually become ArrayHandle, in which case this // will become ArrayCopyWithAlgorithm template VTKM_CONT void ArrayCopyImpl(const vtkm::cont::ArrayHandle& source, vtkm::cont::ArrayHandle& destination, std::true_type /* New style */) { VTKM_STATIC_ASSERT((!std::is_same::value || !std::is_same::value)); ArrayCopyWithAlgorithm(source, destination); } // TODO: ArrayHandleNewStyle will eventually become ArrayHandle, in which case this // will become the only version with the same array types. template VTKM_CONT void ArrayCopyImpl(const vtkm::cont::ArrayHandle& source, vtkm::cont::ArrayHandle& destination, std::true_type /* New style */) { destination.DeepCopyFrom(source); } } // namespace detail /// \brief Does a deep copy from one array to another array. /// /// Given a source \c ArrayHandle and a destination \c ArrayHandle, this /// function allocates the destination \c ArrayHandle to the correct size and /// deeply copies all the values from the source to the destination. /// /// This method will attempt to copy the data using the device that the input /// data is already valid on. If the input data is only valid in the control /// environment, the runtime device tracker is used to try to find another /// device. /// /// This should work on some non-writable array handles as well, as long as /// both \a source and \a destination are the same type. /// /// @{ /// template VTKM_CONT void ArrayCopy(const vtkm::cont::ArrayHandle& source, vtkm::cont::ArrayHandle& destination) { using InArrayType = vtkm::cont::ArrayHandle; using OutArrayType = vtkm::cont::ArrayHandle; using SameTypes = std::is_same; using IsWritable = vtkm::cont::internal::IsWritableArrayHandle; // There are three cases handled here: // 1. The arrays are the same type: // -> Deep copy the buffers and the Storage object // 2. The arrays are different and the output is writable: // -> Do element-wise copy // 3. The arrays are different and the output is not writable: // -> fail (cannot copy) // Give a nice error message for case 3: VTKM_STATIC_ASSERT_MSG(IsWritable::value || SameTypes::value, "Cannot copy to a read-only array with a different " "type than the source."); using IsOldStyle = std::is_base_of, InArrayType>; // Static dispatch cases 1 & 2 detail::ArrayCopyImpl(source, destination, std::integral_constant{}); } VTKM_CONT_EXPORT void ArrayCopy(const vtkm::cont::UnknownArrayHandle& source, vtkm::cont::UnknownArrayHandle& destination); template VTKM_CONT void ArrayCopy(const vtkm::cont::UnknownArrayHandle& source, vtkm::cont::ArrayHandle& destination) { using DestType = vtkm::cont::ArrayHandle; if (source.CanConvert()) { ArrayCopy(source.AsArrayHandle(), destination); } else { vtkm::cont::UnknownArrayHandle destWrapper(destination); ArrayCopy(source, destWrapper); // Destination array should not change, but just in case. destWrapper.AsArrayHandle(destination); } } /// @} /// \brief Copies from an unknown to a known array type. /// /// Often times you have an array of an unknown type (likely from a data set), /// and you need it to be of a particular type (or can make a reasonable but uncertain /// assumption about it being a particular type). You really just want a shallow /// copy (a reference in a concrete `ArrayHandle`) if that is possible. /// /// `ArrayCopyShallowIfPossible` pulls an array of a specific type from an /// `UnknownArrayHandle`. If the type is compatible, it will perform a shallow copy. /// If it is not possible, a deep copy is performed to get it to the correct type. /// template VTKM_CONT void ArrayCopyShallowIfPossible(const vtkm::cont::UnknownArrayHandle source, vtkm::cont::ArrayHandle& destination) { using DestType = vtkm::cont::ArrayHandle; if (source.CanConvert()) { source.AsArrayHandle(destination); } else { vtkm::cont::UnknownArrayHandle destWrapper(destination); ArrayCopy(source, destWrapper); // Destination array should not change, but just in case. destWrapper.AsArrayHandle(destination); } } } } // namespace vtkm::cont #endif //vtk_m_cont_ArrayCopy_h