//============================================================================ // 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 // 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 { // normal element-wise copy: template void ArrayCopyImpl(const InArrayType& in, OutArrayType& out, std::false_type /* Copy storage */) { // Find the device that already has a copy of the data: vtkm::cont::DeviceAdapterId devId = in.GetDeviceAdapterId(); // If the data is not on any device, let the runtime tracker pick an available // parallel copy algorithm. if (devId.GetValue() == VTKM_DEVICE_ADAPTER_UNDEFINED) { devId = vtkm::cont::make_DeviceAdapterId(VTKM_DEVICE_ADAPTER_ANY); } bool success = vtkm::cont::Algorithm::Copy(devId, in, out); if (!success && devId.GetValue() != VTKM_DEVICE_ADAPTER_ANY) { // Retry on any device if the first attempt failed. VTKM_LOG_S(vtkm::cont::LogLevel::Error, "Failed to run ArrayCopy on device '" << devId.GetName() << "'. Retrying on any device."); success = vtkm::cont::Algorithm::Copy(vtkm::cont::DeviceAdapterTagAny{}, in, out); } if (!success) { throw vtkm::cont::ErrorExecution("Failed to run ArrayCopy on any device."); } } // Copy storage for implicit arrays, must be of same type: template void ArrayCopyImpl(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); } } // 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. Output is writable: // -> Do element-wise copy (normal copy behavior) // 2. Output is not writable and arrays are same type: // -> just copy storage (special case for implicit array cloning) // 3. Output is not writable and arrays are different types: // -> 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 JustCopyStorage = std::integral_constant; // Static dispatch cases 1 & 2 detail::ArrayCopyImpl(source, destination, JustCopyStorage{}); } // Forward declaration // Cannot include VariantArrayHandle.h here due to circular dependency. template class VariantArrayHandleBase; namespace detail { struct ArrayCopyFunctor { template VTKM_CONT void operator()(const vtkm::cont::ArrayHandle& source, vtkm::cont::ArrayHandle& destination) const { vtkm::cont::ArrayCopy(source, destination); } }; } // namespace detail /// \brief Deep copies data in a `VariantArrayHandle` to an array of a known type. /// /// This form of `ArrayCopy` can be used to copy data from an unknown array type to /// an array of a known type. Note that regardless of the source type, the data will /// be deep copied. /// template VTKM_CONT void ArrayCopy(const vtkm::cont::VariantArrayHandleBase& source, vtkm::cont::ArrayHandle& destination) { source.CastAndCall(detail::ArrayCopyFunctor{}, destination); } } } // namespace vtkm::cont #endif //vtk_m_cont_ArrayCopy_h