2021-01-14 22:25:09 +00:00
|
|
|
//============================================================================
|
|
|
|
// 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.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayCopy.h>
|
|
|
|
#include <vtkm/cont/DeviceAdapterList.h>
|
|
|
|
#include <vtkm/cont/Invoker.h>
|
|
|
|
|
|
|
|
#include <vtkm/worklet/WorkletMapField.h>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// Use a worklet because device adapter copies often have an issue with casting the values from the
|
|
|
|
// `ArrayHandleRecomineVec` that comes from `UnknownArrayHandle::CastAndCallWithExtractedArray`.
|
|
|
|
struct CopyWorklet : vtkm::worklet::WorkletMapField
|
|
|
|
{
|
|
|
|
using ControlSignature = void(FieldIn, FieldOut);
|
|
|
|
using ExecutionSignature = void(_1, _2);
|
|
|
|
using InputDomain = _1;
|
|
|
|
|
|
|
|
template <typename InType, typename OutType>
|
2021-02-03 00:30:02 +00:00
|
|
|
VTKM_EXEC void operator()(const InType& in, OutType& out) const
|
2021-01-14 22:25:09 +00:00
|
|
|
{
|
|
|
|
out = in;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UnknownCopyOnDevice
|
|
|
|
{
|
|
|
|
bool Called = false;
|
|
|
|
|
2021-08-06 19:16:19 +00:00
|
|
|
template <typename InType, typename OutType>
|
2021-01-14 22:25:09 +00:00
|
|
|
void operator()(vtkm::cont::DeviceAdapterId device,
|
2021-08-06 19:16:19 +00:00
|
|
|
const vtkm::cont::ArrayHandleRecombineVec<InType>& in,
|
|
|
|
const vtkm::cont::ArrayHandleRecombineVec<OutType>& out)
|
2021-01-14 22:25:09 +00:00
|
|
|
{
|
2021-08-06 19:16:19 +00:00
|
|
|
// Note: ArrayHandleRecombineVec returns the wrong value for IsOnDevice (always true).
|
|
|
|
// This is one of the consequences of ArrayHandleRecombineVec breaking assumptions of
|
|
|
|
// ArrayHandle. It does this by stuffing Buffer objects in another Buffer's meta data
|
|
|
|
// rather than listing them explicitly (where they can be queried). We get around this
|
|
|
|
// by pulling out one of the component arrays and querying that.
|
|
|
|
if (!this->Called &&
|
|
|
|
((device == vtkm::cont::DeviceAdapterTagAny{}) ||
|
|
|
|
(in.GetComponentArray(0).IsOnDevice(device))))
|
2021-01-14 22:25:09 +00:00
|
|
|
{
|
|
|
|
vtkm::cont::Invoker invoke(device);
|
|
|
|
invoke(CopyWorklet{}, in, out);
|
|
|
|
this->Called = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UnknownCopyFunctor2
|
|
|
|
{
|
2021-08-06 19:16:19 +00:00
|
|
|
template <typename OutType, typename InType>
|
|
|
|
void operator()(const vtkm::cont::ArrayHandleRecombineVec<OutType>& out,
|
|
|
|
const vtkm::cont::ArrayHandleRecombineVec<InType>& in) const
|
2021-01-14 22:25:09 +00:00
|
|
|
{
|
|
|
|
UnknownCopyOnDevice doCopy;
|
|
|
|
|
|
|
|
// Try to copy on a device that the data are already on.
|
|
|
|
vtkm::ListForEach(doCopy, VTKM_DEFAULT_DEVICE_ADAPTER_LIST{}, in, out);
|
|
|
|
|
|
|
|
// If it was not on any device, call one more time with any adapter to copy wherever.
|
|
|
|
doCopy(vtkm::cont::DeviceAdapterTagAny{}, in, out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UnknownCopyFunctor1
|
|
|
|
{
|
2021-08-06 19:16:19 +00:00
|
|
|
template <typename InType>
|
|
|
|
void operator()(const vtkm::cont::ArrayHandleRecombineVec<InType>& in,
|
|
|
|
vtkm::cont::UnknownArrayHandle& out) const
|
2021-01-14 22:25:09 +00:00
|
|
|
{
|
|
|
|
out.Allocate(in.GetNumberOfValues());
|
2021-01-14 23:56:13 +00:00
|
|
|
|
2021-08-06 19:16:19 +00:00
|
|
|
this->DoIt(in, out, typename std::is_same<vtkm::FloatDefault, InType>::type{});
|
2021-01-14 23:56:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 19:16:19 +00:00
|
|
|
template <typename InType>
|
|
|
|
void DoIt(const vtkm::cont::ArrayHandleRecombineVec<InType>& in,
|
|
|
|
vtkm::cont::UnknownArrayHandle& out,
|
|
|
|
std::false_type) const
|
2021-01-14 23:56:13 +00:00
|
|
|
{
|
|
|
|
// Source is not float.
|
2021-08-06 19:16:19 +00:00
|
|
|
if (out.IsBaseComponentType<InType>())
|
2021-01-14 23:56:13 +00:00
|
|
|
{
|
|
|
|
// Arrays have the same base component type. Copy directly.
|
2021-08-06 19:16:19 +00:00
|
|
|
UnknownCopyFunctor2{}(out.ExtractArrayFromComponents<InType>(), in);
|
2021-01-14 23:56:13 +00:00
|
|
|
}
|
|
|
|
else if (out.IsBaseComponentType<vtkm::FloatDefault>())
|
|
|
|
{
|
|
|
|
// Can copy anything to default float.
|
|
|
|
UnknownCopyFunctor2{}(out.ExtractArrayFromComponents<vtkm::FloatDefault>(), in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Arrays have different base types. To reduce the number of template paths from nxn to 3n,
|
|
|
|
// copy first to a temp array of default float.
|
|
|
|
vtkm::cont::UnknownArrayHandle temp = out.NewInstanceFloatBasic();
|
|
|
|
(*this)(in, temp);
|
|
|
|
vtkm::cont::ArrayCopy(temp, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 19:16:19 +00:00
|
|
|
template <typename InType>
|
|
|
|
void DoIt(const vtkm::cont::ArrayHandleRecombineVec<InType>& in,
|
|
|
|
vtkm::cont::UnknownArrayHandle& out,
|
|
|
|
std::true_type) const
|
2021-01-14 23:56:13 +00:00
|
|
|
{
|
|
|
|
// Source array is FloatDefault. That should be copiable to anything.
|
2021-01-14 22:25:09 +00:00
|
|
|
out.CastAndCallWithExtractedArray(UnknownCopyFunctor2{}, in);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace cont
|
|
|
|
{
|
|
|
|
|
|
|
|
void ArrayCopy(const vtkm::cont::UnknownArrayHandle& source,
|
2021-02-03 00:30:02 +00:00
|
|
|
vtkm::cont::UnknownArrayHandle& destination)
|
2021-01-14 22:25:09 +00:00
|
|
|
{
|
2021-02-03 00:30:02 +00:00
|
|
|
if (!destination.IsValid())
|
|
|
|
{
|
|
|
|
destination = source.NewInstanceBasic();
|
|
|
|
}
|
|
|
|
|
2021-08-06 19:16:19 +00:00
|
|
|
if (source.GetNumberOfValues() > 0)
|
|
|
|
{
|
|
|
|
source.CastAndCallWithExtractedArray(UnknownCopyFunctor1{}, destination);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
destination.ReleaseResources();
|
|
|
|
}
|
2021-01-14 22:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} // namespace vtkm::cont
|