Merge topic 'create-buffers-function'

8d3f37664 CreateBuffers improvements suggested by Rob Maynard
c072e4ed2 Add a CreateBuffers helper function

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Robert Maynard <robert.maynard@kitware.com>
Merge-request: !2320
This commit is contained in:
Kenneth Moreland 2020-11-18 18:54:00 +00:00 committed by Kitware Robot
commit 3239add3a8
2 changed files with 102 additions and 20 deletions

@ -1530,6 +1530,107 @@ VTKM_NEVER_EXPORT VTKM_CONT inline void printSummary_ArrayHandle(
}
out << "]\n";
}
namespace internal
{
namespace detail
{
VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>&)
{
// Nothing left to add.
}
template <typename T, typename S, typename... Args>
VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
const vtkm::cont::ArrayHandle<T, S>& array,
const Args&... args)
{
vtkm::cont::internal::Buffer* arrayBuffers = array.GetBuffers();
buffers.insert(buffers.end(), arrayBuffers, arrayBuffers + array.GetNumberOfBuffers());
CreateBuffersImpl(buffers, args...);
}
template <typename... Args>
VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
const vtkm::cont::internal::Buffer& buffer,
const Args&... args)
{
buffers.push_back(buffer);
CreateBuffersImpl(buffers, args...);
}
template <typename... Args>
VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
const std::vector<vtkm::cont::internal::Buffer>& addbuffs,
const Args&... args)
{
buffers.insert(buffers.end(), addbuffs.begin(), addbuffs.end());
CreateBuffersImpl(buffers, args...);
}
template <typename T, typename S, typename... Args>
VTKM_CONT inline void CreateBuffersResolveArrays(std::vector<vtkm::cont::internal::Buffer>& buffers,
std::true_type,
const vtkm::cont::ArrayHandle<T, S>& array,
const Args&... args)
{
CreateBuffersImpl(buffers, array, args...);
}
template <typename MetaData, typename... Args>
VTKM_CONT inline void CreateBuffersResolveArrays(std::vector<vtkm::cont::internal::Buffer>& buffers,
std::false_type,
const MetaData& metadata,
const Args&... args)
{
vtkm::cont::internal::Buffer buffer;
buffer.SetMetaData(metadata);
buffers.push_back(std::move(buffer));
CreateBuffersImpl(buffers, args...);
}
template <typename Arg0, typename... Args>
VTKM_CONT inline void CreateBuffersImpl(std::vector<vtkm::cont::internal::Buffer>& buffers,
const Arg0& arg0,
const Args&... args)
{
// If the argument is a subclass of ArrayHandle, the template resolution will pick this
// overload instead of the correct ArrayHandle overload. To resolve that, check to see
// if the type is an `ArrayHandle` and use `CreateBuffersResolveArrays` to choose the
// right path.
using IsArray = typename vtkm::cont::internal::ArrayHandleCheck<Arg0>::type::type;
CreateBuffersResolveArrays(buffers, IsArray{}, arg0, args...);
}
} // namespace detail
/// \brief Create the buffers for an `ArrayHandle` specialization.
///
/// When creating an `ArrayHandle` specialization, it is important to build a
/// `std::vector` of `Buffer` objects. This function simplifies creating
/// these buffer objects. Simply pass as arguments the things you want in the
/// buffers. The parameters to `CreateBuffers` are added to the `Buffer` `vector`
/// in the order provided. The actual object(s) added depends on the type of
/// parameter:
///
/// - `ArrayHandle`: The buffers from the `ArrayHandle` are added to the list.
/// - `Buffer`: A copy of the buffer is added to the list.
/// - `std::vector<Buffer>`: A copy of all buffers in this vector are added to the list.
/// - Anything else: A buffer with the given object attached as metadata is
///
template <typename... Args>
VTKM_CONT inline std::vector<vtkm::cont::internal::Buffer> CreateBuffers(const Args&... args)
{
std::vector<vtkm::cont::internal::Buffer> buffers;
buffers.reserve(sizeof...(args));
detail::CreateBuffersImpl(buffers, args...);
return buffers;
}
} // namespace internal
}
} //namespace vtkm::cont

@ -279,25 +279,6 @@ public:
Storage3::CreateWritePortal(Buffers3(buffers), device, token));
}
VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
const vtkm::cont::ArrayHandle<T, ST1>& array1,
const vtkm::cont::ArrayHandle<T, ST2>& array2,
const vtkm::cont::ArrayHandle<T, ST3>& array3)
{
std::vector<vtkm::cont::internal::Buffer> destBuffers(
static_cast<std::size_t>(GetNumberOfBuffers()));
auto destIter = destBuffers.begin();
destIter = std::copy_n(
array1.GetBuffers(), static_cast<std::size_t>(Storage1::GetNumberOfBuffers()), destIter);
destIter = std::copy_n(
array2.GetBuffers(), static_cast<std::size_t>(Storage2::GetNumberOfBuffers()), destIter);
destIter = std::copy_n(
array3.GetBuffers(), static_cast<std::size_t>(Storage3::GetNumberOfBuffers()), destIter);
return destBuffers;
}
VTKM_CONT static vtkm::cont::ArrayHandle<T, ST1> GetArrayHandle1(
const vtkm::cont::internal::Buffer* buffers)
{
@ -353,7 +334,7 @@ public:
ArrayHandleCartesianProduct(const FirstHandleType& firstArray,
const SecondHandleType& secondArray,
const ThirdHandleType& thirdArray)
: Superclass(StorageType::CreateBuffers(firstArray, secondArray, thirdArray))
: Superclass(vtkm::cont::internal::CreateBuffers(firstArray, secondArray, thirdArray))
{
}