2019-04-15 23:24:21 +00:00
|
|
|
//============================================================================
|
2015-04-13 20:43:18 +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.
|
2019-04-15 23:24:21 +00:00
|
|
|
//============================================================================
|
2015-04-13 20:43:18 +00:00
|
|
|
#ifndef vtk_m_cont_ArrayHandleTransform_h
|
|
|
|
#define vtk_m_cont_ArrayHandleTransform_h
|
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
2017-01-09 21:15:32 +00:00
|
|
|
#include <vtkm/cont/ErrorBadType.h>
|
|
|
|
#include <vtkm/cont/ErrorInternal.h>
|
2018-09-08 17:07:06 +00:00
|
|
|
#include <vtkm/cont/ExecutionAndControlObjectBase.h>
|
2018-09-05 16:07:23 +00:00
|
|
|
#include <vtkm/cont/RuntimeDeviceTracker.h>
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2019-07-18 00:39:00 +00:00
|
|
|
#include <vtkm/internal/ArrayPortalHelpers.h>
|
|
|
|
|
2018-10-30 13:49:41 +00:00
|
|
|
#include <vtkm/cont/serial/internal/DeviceAdapterRuntimeDetectorSerial.h>
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace internal
|
|
|
|
{
|
2015-09-22 14:11:51 +00:00
|
|
|
|
|
|
|
/// Tag used in place of an inverse functor.
|
2017-05-18 14:29:41 +00:00
|
|
|
struct NullFunctorType
|
|
|
|
{
|
|
|
|
};
|
2015-09-22 14:11:51 +00:00
|
|
|
|
2015-04-13 20:43:18 +00:00
|
|
|
/// \brief An array portal that transforms a value from another portal.
|
|
|
|
///
|
2017-05-26 17:53:28 +00:00
|
|
|
template <typename ValueType_,
|
|
|
|
typename PortalType_,
|
|
|
|
typename FunctorType_,
|
2017-05-18 14:29:41 +00:00
|
|
|
typename InverseFunctorType_ = NullFunctorType>
|
2016-12-19 19:39:17 +00:00
|
|
|
class VTKM_ALWAYS_EXPORT ArrayPortalTransform;
|
2015-09-22 14:11:51 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename ValueType_, typename PortalType_, typename FunctorType_>
|
|
|
|
class VTKM_ALWAYS_EXPORT
|
|
|
|
ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>
|
2015-04-13 20:43:18 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-08-16 15:34:21 +00:00
|
|
|
using PortalType = PortalType_;
|
|
|
|
using ValueType = ValueType_;
|
|
|
|
using FunctorType = FunctorType_;
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
ArrayPortalTransform(const PortalType& portal = PortalType(),
|
|
|
|
const FunctorType& functor = FunctorType())
|
|
|
|
: Portal(portal)
|
|
|
|
, Functor(functor)
|
|
|
|
{
|
|
|
|
}
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2016-08-02 15:26:29 +00:00
|
|
|
/// Copy constructor for any other ArrayPortalTransform with an iterator
|
2015-04-27 14:13:11 +00:00
|
|
|
/// type that can be copied to this iterator type. This allows us to do any
|
|
|
|
/// type casting that the iterators do (like the non-const to const cast).
|
2015-04-13 20:43:18 +00:00
|
|
|
///
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class OtherV, class OtherP, class OtherF>
|
|
|
|
VTKM_EXEC_CONT ArrayPortalTransform(const ArrayPortalTransform<OtherV, OtherP, OtherF>& src)
|
|
|
|
: Portal(src.GetPortal())
|
|
|
|
, Functor(src.GetFunctor())
|
|
|
|
{
|
|
|
|
}
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Id GetNumberOfValues() const { return this->Portal.GetNumberOfValues(); }
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
ValueType Get(vtkm::Id index) const { return this->Functor(this->Portal.Get(index)); }
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
const PortalType& GetPortal() const { return this->Portal; }
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
const FunctorType& GetFunctor() const { return this->Functor; }
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2015-09-22 14:11:51 +00:00
|
|
|
protected:
|
2015-04-13 20:43:18 +00:00
|
|
|
PortalType Portal;
|
|
|
|
FunctorType Functor;
|
|
|
|
};
|
|
|
|
|
2017-05-26 17:53:28 +00:00
|
|
|
template <typename ValueType_,
|
|
|
|
typename PortalType_,
|
|
|
|
typename FunctorType_,
|
2017-05-18 14:29:41 +00:00
|
|
|
typename InverseFunctorType_>
|
|
|
|
class VTKM_ALWAYS_EXPORT ArrayPortalTransform
|
|
|
|
: public ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>
|
2015-09-22 14:11:51 +00:00
|
|
|
{
|
2019-09-13 15:42:33 +00:00
|
|
|
using Writable = vtkm::internal::PortalSupportsSets<PortalType_>;
|
|
|
|
|
2015-09-22 14:11:51 +00:00
|
|
|
public:
|
2017-08-16 15:34:21 +00:00
|
|
|
using Superclass = ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>;
|
|
|
|
using PortalType = PortalType_;
|
|
|
|
using ValueType = ValueType_;
|
|
|
|
using FunctorType = FunctorType_;
|
|
|
|
using InverseFunctorType = InverseFunctorType_;
|
2015-09-22 14:11:51 +00:00
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
ArrayPortalTransform(const PortalType& portal = PortalType(),
|
|
|
|
const FunctorType& functor = FunctorType(),
|
|
|
|
const InverseFunctorType& inverseFunctor = InverseFunctorType())
|
|
|
|
: Superclass(portal, functor)
|
|
|
|
, InverseFunctor(inverseFunctor)
|
|
|
|
{
|
|
|
|
}
|
2015-09-22 14:11:51 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class OtherV, class OtherP, class OtherF, class OtherInvF>
|
|
|
|
VTKM_EXEC_CONT ArrayPortalTransform(
|
|
|
|
const ArrayPortalTransform<OtherV, OtherP, OtherF, OtherInvF>& src)
|
|
|
|
: Superclass(src)
|
|
|
|
, InverseFunctor(src.GetInverseFunctor())
|
|
|
|
{
|
|
|
|
}
|
2015-09-22 14:11:51 +00:00
|
|
|
|
2019-09-13 15:42:33 +00:00
|
|
|
template <typename Writable_ = Writable,
|
|
|
|
typename = typename std::enable_if<Writable_::value>::type>
|
|
|
|
VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2019-09-13 15:42:33 +00:00
|
|
|
this->Portal.Set(index, this->InverseFunctor(value));
|
2015-09-22 14:11:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_EXEC_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
const InverseFunctorType& GetInverseFunctor() const { return this->InverseFunctor; }
|
2015-09-22 14:11:51 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
InverseFunctorType InverseFunctor;
|
|
|
|
};
|
2015-04-27 14:13:11 +00:00
|
|
|
}
|
2020-11-17 14:42:29 +00:00
|
|
|
} // namespace vtkm::internal
|
2015-04-27 14:13:11 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace cont
|
|
|
|
{
|
2015-04-27 14:13:11 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace internal
|
|
|
|
{
|
2015-04-27 14:13:11 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
using NullFunctorType = vtkm::internal::NullFunctorType;
|
|
|
|
|
2018-09-08 17:07:06 +00:00
|
|
|
template <typename ProvidedFunctorType, typename FunctorIsExecContObject>
|
2018-09-05 16:07:23 +00:00
|
|
|
struct TransformFunctorManagerImpl;
|
|
|
|
|
2018-09-08 17:07:06 +00:00
|
|
|
template <typename ProvidedFunctorType>
|
|
|
|
struct TransformFunctorManagerImpl<ProvidedFunctorType, std::false_type>
|
2018-09-05 16:07:23 +00:00
|
|
|
{
|
2018-09-08 17:07:06 +00:00
|
|
|
VTKM_STATIC_ASSERT_MSG(!vtkm::cont::internal::IsExecutionObjectBase<ProvidedFunctorType>::value,
|
|
|
|
"Must use an ExecutionAndControlObject instead of an ExecutionObject.");
|
|
|
|
|
2018-09-05 16:07:23 +00:00
|
|
|
ProvidedFunctorType Functor;
|
2018-09-08 17:07:06 +00:00
|
|
|
using FunctorType = ProvidedFunctorType;
|
2018-09-05 16:07:23 +00:00
|
|
|
|
|
|
|
TransformFunctorManagerImpl() = default;
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
TransformFunctorManagerImpl(const ProvidedFunctorType& functor)
|
|
|
|
: Functor(functor)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2018-09-08 17:07:06 +00:00
|
|
|
ProvidedFunctorType PrepareForControl() const { return this->Functor; }
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT ProvidedFunctorType PrepareForExecution(vtkm::cont::DeviceAdapterId,
|
|
|
|
vtkm::cont::Token&) const
|
2018-09-08 17:07:06 +00:00
|
|
|
{
|
|
|
|
return this->Functor;
|
|
|
|
}
|
2018-09-05 16:07:23 +00:00
|
|
|
};
|
|
|
|
|
2018-09-08 17:07:06 +00:00
|
|
|
template <typename ProvidedFunctorType>
|
|
|
|
struct TransformFunctorManagerImpl<ProvidedFunctorType, std::true_type>
|
2018-09-05 16:07:23 +00:00
|
|
|
{
|
2018-09-08 17:07:06 +00:00
|
|
|
VTKM_IS_EXECUTION_AND_CONTROL_OBJECT(ProvidedFunctorType);
|
|
|
|
|
2018-09-05 16:07:23 +00:00
|
|
|
ProvidedFunctorType Functor;
|
2018-09-08 17:07:06 +00:00
|
|
|
// using FunctorType = decltype(std::declval<ProvidedFunctorType>().PrepareForControl());
|
2020-01-19 06:36:14 +00:00
|
|
|
// using FunctorType = decltype(Functor.PrepareForControl());
|
|
|
|
using FunctorType = vtkm::cont::internal::ControlObjectType<ProvidedFunctorType>;
|
2018-09-05 16:07:23 +00:00
|
|
|
|
|
|
|
TransformFunctorManagerImpl() = default;
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
TransformFunctorManagerImpl(const ProvidedFunctorType& functor)
|
|
|
|
: Functor(functor)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2020-01-19 06:36:14 +00:00
|
|
|
auto PrepareForControl() const
|
|
|
|
-> decltype(vtkm::cont::internal::CallPrepareForControl(this->Functor))
|
2018-09-08 17:07:06 +00:00
|
|
|
{
|
2020-01-19 06:36:14 +00:00
|
|
|
return vtkm::cont::internal::CallPrepareForControl(this->Functor);
|
2018-09-08 17:07:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT auto PrepareForExecution(vtkm::cont::DeviceAdapterId device,
|
|
|
|
vtkm::cont::Token& token) const
|
2020-01-19 06:36:14 +00:00
|
|
|
-> decltype(vtkm::cont::internal::CallPrepareForExecution(this->Functor, device, token))
|
2018-09-08 17:07:06 +00:00
|
|
|
{
|
2020-01-19 06:36:14 +00:00
|
|
|
return vtkm::cont::internal::CallPrepareForExecution(this->Functor, device, token);
|
2018-09-08 17:07:06 +00:00
|
|
|
}
|
2018-09-05 16:07:23 +00:00
|
|
|
};
|
|
|
|
|
2018-09-08 17:07:06 +00:00
|
|
|
template <typename ProvidedFunctorType>
|
2018-09-05 16:07:23 +00:00
|
|
|
struct TransformFunctorManager
|
|
|
|
: TransformFunctorManagerImpl<
|
|
|
|
ProvidedFunctorType,
|
2018-09-08 17:07:06 +00:00
|
|
|
typename vtkm::cont::internal::IsExecutionAndControlObjectBase<ProvidedFunctorType>::type>
|
2018-09-05 16:07:23 +00:00
|
|
|
{
|
|
|
|
using Superclass = TransformFunctorManagerImpl<
|
|
|
|
ProvidedFunctorType,
|
2018-09-08 17:07:06 +00:00
|
|
|
typename vtkm::cont::internal::IsExecutionAndControlObjectBase<ProvidedFunctorType>::type>;
|
2018-09-05 16:07:23 +00:00
|
|
|
using FunctorType = typename Superclass::FunctorType;
|
|
|
|
|
|
|
|
VTKM_CONT TransformFunctorManager() = default;
|
|
|
|
|
2019-04-30 15:57:12 +00:00
|
|
|
VTKM_CONT TransformFunctorManager(const TransformFunctorManager&) = default;
|
2018-09-05 16:07:23 +00:00
|
|
|
|
|
|
|
VTKM_CONT TransformFunctorManager(const ProvidedFunctorType& functor)
|
|
|
|
: Superclass(functor)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueType>
|
|
|
|
using TransformedValueType = decltype(std::declval<FunctorType>()(ValueType{}));
|
|
|
|
};
|
|
|
|
|
2017-05-30 14:00:01 +00:00
|
|
|
template <typename ArrayHandleType,
|
2017-05-26 17:53:28 +00:00
|
|
|
typename FunctorType,
|
2017-05-18 14:29:41 +00:00
|
|
|
typename InverseFunctorType = NullFunctorType>
|
|
|
|
struct VTKM_ALWAYS_EXPORT StorageTagTransform
|
|
|
|
{
|
2018-09-05 16:07:23 +00:00
|
|
|
using FunctorManager = TransformFunctorManager<FunctorType>;
|
|
|
|
using ValueType =
|
|
|
|
typename FunctorManager::template TransformedValueType<typename ArrayHandleType::ValueType>;
|
2017-05-18 14:29:41 +00:00
|
|
|
};
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2017-05-30 14:00:01 +00:00
|
|
|
template <typename ArrayHandleType, typename FunctorType>
|
|
|
|
class Storage<typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
|
|
|
|
StorageTagTransform<ArrayHandleType, FunctorType>>
|
2015-04-13 20:43:18 +00:00
|
|
|
{
|
2018-09-05 16:07:23 +00:00
|
|
|
using FunctorManager = TransformFunctorManager<FunctorType>;
|
2017-08-16 15:34:21 +00:00
|
|
|
using ValueType = typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType;
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
using SourceStorage =
|
|
|
|
Storage<typename ArrayHandleType::ValueType, typename ArrayHandleType::StorageTag>;
|
|
|
|
|
|
|
|
static constexpr vtkm::IdComponent NUM_METADATA_BUFFERS = 1;
|
|
|
|
|
|
|
|
public:
|
2020-12-07 22:19:24 +00:00
|
|
|
VTKM_STORAGE_NO_RESIZE;
|
|
|
|
VTKM_STORAGE_NO_WRITE_PORTAL;
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
using ReadPortalType =
|
|
|
|
vtkm::internal::ArrayPortalTransform<ValueType,
|
|
|
|
typename ArrayHandleType::ReadPortalType,
|
|
|
|
typename FunctorManager::FunctorType>;
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static vtkm::IdComponent GetNumberOfBuffers()
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return SourceStorage::GetNumberOfBuffers() + NUM_METADATA_BUFFERS;
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static vtkm::Id GetNumberOfValues(const vtkm::cont::internal::Buffer* buffers)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return SourceStorage::GetNumberOfValues(buffers + NUM_METADATA_BUFFERS);
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static ReadPortalType CreateReadPortal(const vtkm::cont::internal::Buffer* buffers,
|
|
|
|
vtkm::cont::DeviceAdapterId device,
|
|
|
|
vtkm::cont::Token& token)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-12-02 22:05:23 +00:00
|
|
|
if (device == vtkm::cont::DeviceAdapterTagUndefined{})
|
|
|
|
{
|
|
|
|
return ReadPortalType(
|
|
|
|
SourceStorage::CreateReadPortal(buffers + NUM_METADATA_BUFFERS, device, token),
|
|
|
|
buffers[0].GetMetaData<FunctorManager>().PrepareForControl());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ReadPortalType(
|
|
|
|
SourceStorage::CreateReadPortal(buffers + NUM_METADATA_BUFFERS, device, token),
|
|
|
|
buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token));
|
|
|
|
}
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
|
|
|
|
const ArrayHandleType& handle,
|
|
|
|
const FunctorType& functor = FunctorType())
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return vtkm::cont::internal::CreateBuffers(FunctorManager(functor), handle);
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static ArrayHandleType GetArray(const vtkm::cont::internal::Buffer* buffers)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
|
|
|
|
typename ArrayHandleType::StorageTag>(buffers +
|
|
|
|
NUM_METADATA_BUFFERS);
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static FunctorType GetFunctor(const vtkm::cont::internal::Buffer* buffers)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return buffers[0].GetMetaData<FunctorManager>().Functor;
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static NullFunctorType GetInverseFunctor(const vtkm::cont::internal::Buffer*)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return NullFunctorType{};
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-30 14:00:01 +00:00
|
|
|
template <typename ArrayHandleType, typename FunctorType, typename InverseFunctorType>
|
|
|
|
class Storage<
|
|
|
|
typename StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::ValueType,
|
|
|
|
StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>
|
2015-09-22 14:11:51 +00:00
|
|
|
{
|
2018-09-05 16:07:23 +00:00
|
|
|
using FunctorManager = TransformFunctorManager<FunctorType>;
|
|
|
|
using InverseFunctorManager = TransformFunctorManager<InverseFunctorType>;
|
2017-08-16 15:34:21 +00:00
|
|
|
using ValueType = typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType;
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
using SourceStorage =
|
|
|
|
Storage<typename ArrayHandleType::ValueType, typename ArrayHandleType::StorageTag>;
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
static constexpr vtkm::IdComponent NUM_METADATA_BUFFERS = 2;
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
public:
|
|
|
|
using ReadPortalType =
|
|
|
|
vtkm::internal::ArrayPortalTransform<ValueType,
|
|
|
|
typename ArrayHandleType::ReadPortalType,
|
|
|
|
typename FunctorManager::FunctorType,
|
|
|
|
typename InverseFunctorManager::FunctorType>;
|
|
|
|
using WritePortalType =
|
|
|
|
vtkm::internal::ArrayPortalTransform<ValueType,
|
|
|
|
typename ArrayHandleType::WritePortalType,
|
|
|
|
typename FunctorManager::FunctorType,
|
|
|
|
typename InverseFunctorManager::FunctorType>;
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT constexpr static vtkm::IdComponent GetNumberOfBuffers()
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return SourceStorage::GetNumberOfBuffers() + NUM_METADATA_BUFFERS;
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static vtkm::Id GetNumberOfValues(const vtkm::cont::internal::Buffer* buffers)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return SourceStorage::GetNumberOfValues(buffers + NUM_METADATA_BUFFERS);
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static void ResizeBuffers(vtkm::Id numValues,
|
|
|
|
vtkm::cont::internal::Buffer* buffers,
|
|
|
|
vtkm::CopyFlag preserve,
|
|
|
|
vtkm::cont::Token& token)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
SourceStorage::ResizeBuffers(numValues, buffers + NUM_METADATA_BUFFERS, preserve, token);
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static ReadPortalType CreateReadPortal(const vtkm::cont::internal::Buffer* buffers,
|
|
|
|
vtkm::cont::DeviceAdapterId device,
|
|
|
|
vtkm::cont::Token& token)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-12-02 22:05:23 +00:00
|
|
|
if (device == vtkm::cont::DeviceAdapterTagUndefined{})
|
|
|
|
{
|
|
|
|
return ReadPortalType(
|
|
|
|
SourceStorage::CreateReadPortal(buffers + NUM_METADATA_BUFFERS, device, token),
|
|
|
|
buffers[0].GetMetaData<FunctorManager>().PrepareForControl(),
|
|
|
|
buffers[1].GetMetaData<InverseFunctorManager>().PrepareForControl());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ReadPortalType(
|
|
|
|
SourceStorage::CreateReadPortal(buffers + NUM_METADATA_BUFFERS, device, token),
|
|
|
|
buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token),
|
|
|
|
buffers[1].GetMetaData<InverseFunctorManager>().PrepareForExecution(device, token));
|
|
|
|
}
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static WritePortalType CreateWritePortal(vtkm::cont::internal::Buffer* buffers,
|
|
|
|
vtkm::cont::DeviceAdapterId device,
|
|
|
|
vtkm::cont::Token& token)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return WritePortalType(
|
|
|
|
SourceStorage::CreateWritePortal(buffers + NUM_METADATA_BUFFERS, device, token),
|
|
|
|
buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token),
|
|
|
|
buffers[1].GetMetaData<InverseFunctorManager>().PrepareForExecution(device, token));
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
2015-09-22 14:11:51 +00:00
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
|
|
|
|
const ArrayHandleType& handle,
|
|
|
|
const FunctorType& functor = FunctorType(),
|
|
|
|
const InverseFunctorType& inverseFunctor = InverseFunctorType())
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return vtkm::cont::internal::CreateBuffers(
|
|
|
|
FunctorManager(functor), InverseFunctorManager(inverseFunctor), handle);
|
2015-09-22 14:11:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static ArrayHandleType GetArray(const vtkm::cont::internal::Buffer* buffers)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
|
|
|
|
typename ArrayHandleType::StorageTag>(buffers +
|
|
|
|
NUM_METADATA_BUFFERS);
|
2015-09-22 14:11:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static FunctorType GetFunctor(const vtkm::cont::internal::Buffer* buffers)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return buffers[0].GetMetaData<FunctorManager>().Functor;
|
2015-09-22 14:11:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:42:29 +00:00
|
|
|
VTKM_CONT static InverseFunctorType GetInverseFunctor(const vtkm::cont::internal::Buffer* buffers)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
return buffers[1].GetMetaData<InverseFunctorManager>().Functor;
|
2015-09-22 14:11:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-13 20:43:18 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
/// \brief Implicitly transform values of one array to another with a functor.
|
|
|
|
///
|
|
|
|
/// ArrayHandleTransforms is a specialization of ArrayHandle. It takes a
|
|
|
|
/// delegate array handle and makes a new handle that calls a given unary
|
|
|
|
/// functor with the element at a given index and returns the result of the
|
|
|
|
/// functor as the value of this array at that position. This transformation is
|
|
|
|
/// done on demand. That is, rather than make a new copy of the array with new
|
|
|
|
/// values, the transformation is done as values are read from the array. Thus,
|
|
|
|
/// the functor operator should work in both the control and execution
|
|
|
|
/// environments.
|
|
|
|
///
|
2017-05-30 14:00:01 +00:00
|
|
|
template <typename ArrayHandleType,
|
2017-05-26 17:53:28 +00:00
|
|
|
typename FunctorType,
|
2017-05-18 14:29:41 +00:00
|
|
|
typename InverseFunctorType = internal::NullFunctorType>
|
2015-09-22 14:11:51 +00:00
|
|
|
class ArrayHandleTransform;
|
|
|
|
|
2017-05-30 14:00:01 +00:00
|
|
|
template <typename ArrayHandleType, typename FunctorType>
|
|
|
|
class ArrayHandleTransform<ArrayHandleType, FunctorType, internal::NullFunctorType>
|
|
|
|
: public vtkm::cont::ArrayHandle<
|
|
|
|
typename internal::StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
|
|
|
|
internal::StorageTagTransform<ArrayHandleType, FunctorType>>
|
2015-04-13 20:43:18 +00:00
|
|
|
{
|
|
|
|
// If the following line gives a compile error, then the ArrayHandleType
|
|
|
|
// template argument is not a valid ArrayHandle type.
|
|
|
|
VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
|
|
|
|
|
2015-09-20 06:28:11 +00:00
|
|
|
public:
|
2015-10-21 19:36:27 +00:00
|
|
|
VTKM_ARRAY_HANDLE_SUBCLASS(
|
2017-05-26 17:53:28 +00:00
|
|
|
ArrayHandleTransform,
|
2017-05-30 14:00:01 +00:00
|
|
|
(ArrayHandleTransform<ArrayHandleType, FunctorType>),
|
|
|
|
(vtkm::cont::ArrayHandle<
|
|
|
|
typename internal::StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
|
|
|
|
internal::StorageTagTransform<ArrayHandleType, FunctorType>>));
|
2015-09-20 06:28:11 +00:00
|
|
|
|
|
|
|
private:
|
2017-08-16 15:34:21 +00:00
|
|
|
using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
|
2015-04-13 20:43:18 +00:00
|
|
|
|
2015-10-21 19:36:27 +00:00
|
|
|
public:
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_CONT
|
2020-11-25 16:02:44 +00:00
|
|
|
ArrayHandleTransform(const ArrayHandleType& handle,
|
|
|
|
const FunctorType& functor = FunctorType{},
|
|
|
|
internal::NullFunctorType = internal::NullFunctorType{})
|
2020-11-17 14:42:29 +00:00
|
|
|
: Superclass(StorageType::CreateBuffers(handle, functor))
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
}
|
2015-04-13 20:43:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// make_ArrayHandleTransform is convenience function to generate an
|
|
|
|
/// ArrayHandleTransform. It takes in an ArrayHandle and a functor
|
|
|
|
/// to apply to each element of the Handle.
|
2017-05-30 14:00:01 +00:00
|
|
|
template <typename HandleType, typename FunctorType>
|
|
|
|
VTKM_CONT vtkm::cont::ArrayHandleTransform<HandleType, FunctorType> make_ArrayHandleTransform(
|
2017-05-26 17:53:28 +00:00
|
|
|
HandleType handle,
|
|
|
|
FunctorType functor)
|
2015-04-13 20:43:18 +00:00
|
|
|
{
|
2017-05-30 14:00:01 +00:00
|
|
|
return ArrayHandleTransform<HandleType, FunctorType>(handle, functor);
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 14:11:51 +00:00
|
|
|
// ArrayHandleTransform with inverse functors enabled (no need to subclass from
|
|
|
|
// ArrayHandleTransform without inverse functors: nothing to inherit).
|
2017-05-30 14:00:01 +00:00
|
|
|
template <typename ArrayHandleType, typename FunctorType, typename InverseFunctorType>
|
2015-09-22 14:11:51 +00:00
|
|
|
class ArrayHandleTransform
|
2017-05-18 14:29:41 +00:00
|
|
|
: public vtkm::cont::ArrayHandle<
|
2017-05-30 14:00:01 +00:00
|
|
|
typename internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::
|
|
|
|
ValueType,
|
|
|
|
internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>
|
2015-09-22 14:11:51 +00:00
|
|
|
{
|
|
|
|
VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
|
|
|
|
|
|
|
|
public:
|
|
|
|
VTKM_ARRAY_HANDLE_SUBCLASS(
|
2017-05-18 14:29:41 +00:00
|
|
|
ArrayHandleTransform,
|
2017-05-30 14:00:01 +00:00
|
|
|
(ArrayHandleTransform<ArrayHandleType, FunctorType, InverseFunctorType>),
|
2017-05-18 14:29:41 +00:00
|
|
|
(vtkm::cont::ArrayHandle<
|
2017-05-30 14:00:01 +00:00
|
|
|
typename internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::
|
|
|
|
ValueType,
|
|
|
|
internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>));
|
2015-09-22 14:11:51 +00:00
|
|
|
|
|
|
|
private:
|
2017-08-16 15:34:21 +00:00
|
|
|
using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
|
2015-09-22 14:11:51 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
public:
|
2017-05-26 17:53:28 +00:00
|
|
|
ArrayHandleTransform(const ArrayHandleType& handle,
|
|
|
|
const FunctorType& functor = FunctorType(),
|
2017-05-18 14:29:41 +00:00
|
|
|
const InverseFunctorType& inverseFunctor = InverseFunctorType())
|
2020-11-17 14:42:29 +00:00
|
|
|
: Superclass(StorageType::CreateBuffers(handle, functor, inverseFunctor))
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
}
|
2019-12-09 22:56:08 +00:00
|
|
|
|
|
|
|
/// Implemented so that it is defined exclusively in the control environment.
|
|
|
|
/// If there is a separate device for the execution environment (for example,
|
|
|
|
/// with CUDA), then the automatically generated destructor could be
|
|
|
|
/// created for all devices, and it would not be valid for all devices.
|
|
|
|
///
|
|
|
|
~ArrayHandleTransform() {}
|
2020-11-17 14:42:29 +00:00
|
|
|
|
|
|
|
/// \brief Returns the `ArrayHandle` that is being transformed.
|
|
|
|
///
|
|
|
|
ArrayHandleType GetTransformedArray() const { return StorageType::GetArray(this->GetBuffers()); }
|
|
|
|
|
|
|
|
/// \brief Returns the functor transforming the `ArrayHandle`.
|
|
|
|
///
|
|
|
|
FunctorType GetFunctor() const { return StorageType::GetFunctor(this->GetBuffers()); }
|
|
|
|
|
|
|
|
/// \brief Returns the inverse functor transforming the `ArrayHandle`
|
|
|
|
///
|
|
|
|
InverseFunctorType GetInverseFunctor() const
|
|
|
|
{
|
|
|
|
return StorageType::GetInverseFunctor(this->GetBuffers());
|
|
|
|
}
|
2015-09-22 14:11:51 +00:00
|
|
|
};
|
|
|
|
|
2017-05-30 14:00:01 +00:00
|
|
|
template <typename HandleType, typename FunctorType, typename InverseFunctorType>
|
|
|
|
VTKM_CONT vtkm::cont::ArrayHandleTransform<HandleType, FunctorType, InverseFunctorType>
|
2015-09-22 14:11:51 +00:00
|
|
|
make_ArrayHandleTransform(HandleType handle, FunctorType functor, InverseFunctorType inverseFunctor)
|
|
|
|
{
|
2017-05-30 14:00:01 +00:00
|
|
|
return ArrayHandleTransform<HandleType, FunctorType, InverseFunctorType>(
|
2017-05-26 17:53:28 +00:00
|
|
|
handle, functor, inverseFunctor);
|
2015-09-22 14:11:51 +00:00
|
|
|
}
|
2015-04-13 20:43:18 +00:00
|
|
|
}
|
2017-05-30 14:00:01 +00:00
|
|
|
|
2015-04-13 20:43:18 +00:00
|
|
|
} // namespace vtkm::cont
|
|
|
|
|
2018-06-08 15:56:40 +00:00
|
|
|
//=============================================================================
|
|
|
|
// Specializations of serialization related classes
|
2019-09-12 21:19:36 +00:00
|
|
|
/// @cond SERIALIZATION
|
2018-06-08 15:56:40 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace cont
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename AH, typename Functor, typename InvFunctor>
|
2019-02-28 18:15:27 +00:00
|
|
|
struct SerializableTypeString<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
|
2018-06-08 15:56:40 +00:00
|
|
|
{
|
|
|
|
static VTKM_CONT const std::string& Get()
|
|
|
|
{
|
2019-02-28 18:15:27 +00:00
|
|
|
static std::string name = "AH_Transform<" + SerializableTypeString<AH>::Get() + "," +
|
|
|
|
SerializableTypeString<Functor>::Get() + "," + SerializableTypeString<InvFunctor>::Get() +
|
|
|
|
">";
|
2018-06-08 15:56:40 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename AH, typename Functor>
|
2019-02-28 18:15:27 +00:00
|
|
|
struct SerializableTypeString<vtkm::cont::ArrayHandleTransform<AH, Functor>>
|
2018-06-08 15:56:40 +00:00
|
|
|
{
|
|
|
|
static VTKM_CONT const std::string& Get()
|
|
|
|
{
|
2019-02-28 18:15:27 +00:00
|
|
|
static std::string name = "AH_Transform<" + SerializableTypeString<AH>::Get() + "," +
|
|
|
|
SerializableTypeString<Functor>::Get() + ">";
|
2018-06-08 15:56:40 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename AH, typename Functor, typename InvFunctor>
|
2019-02-28 18:15:27 +00:00
|
|
|
struct SerializableTypeString<vtkm::cont::ArrayHandle<
|
2018-06-08 15:56:40 +00:00
|
|
|
typename vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>::ValueType,
|
|
|
|
vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>>>
|
2019-02-28 18:15:27 +00:00
|
|
|
: SerializableTypeString<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
|
2018-06-08 15:56:40 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} // vtkm::cont
|
|
|
|
|
2019-02-05 16:02:29 +00:00
|
|
|
namespace mangled_diy_namespace
|
2018-06-08 15:56:40 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
template <typename AH, typename Functor>
|
|
|
|
struct Serialization<vtkm::cont::ArrayHandleTransform<AH, Functor>>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
using Type = vtkm::cont::ArrayHandleTransform<AH, Functor>;
|
|
|
|
using BaseType = vtkm::cont::ArrayHandle<typename Type::ValueType, typename Type::StorageTag>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
|
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
Type transformedArray = obj;
|
|
|
|
vtkmdiy::save(bb, obj.GetArray());
|
|
|
|
vtkmdiy::save(bb, obj.GetFunctor());
|
2018-06-08 15:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
|
|
|
|
{
|
|
|
|
AH array;
|
2019-02-05 16:02:29 +00:00
|
|
|
vtkmdiy::load(bb, array);
|
2018-06-08 15:56:40 +00:00
|
|
|
Functor functor;
|
2019-02-05 16:02:29 +00:00
|
|
|
vtkmdiy::load(bb, functor);
|
2018-06-08 15:56:40 +00:00
|
|
|
obj = vtkm::cont::make_ArrayHandleTransform(array, functor);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename AH, typename Functor, typename InvFunctor>
|
|
|
|
struct Serialization<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
using Type = vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>;
|
|
|
|
using BaseType = vtkm::cont::ArrayHandle<typename Type::ValueType, typename Type::StorageTag>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
|
|
|
|
{
|
2020-11-17 14:42:29 +00:00
|
|
|
Type transformedArray = obj;
|
|
|
|
vtkmdiy::save(bb, transformedArray.GetTransformedArray());
|
|
|
|
vtkmdiy::save(bb, transformedArray.GetFunctor());
|
|
|
|
vtkmdiy::save(bb, transformedArray.GetInverseFunctor());
|
2018-06-08 15:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
|
|
|
|
{
|
|
|
|
AH array;
|
2019-02-05 16:02:29 +00:00
|
|
|
vtkmdiy::load(bb, array);
|
2018-06-08 15:56:40 +00:00
|
|
|
Functor functor;
|
2019-02-05 16:02:29 +00:00
|
|
|
vtkmdiy::load(bb, functor);
|
2018-06-08 15:56:40 +00:00
|
|
|
InvFunctor invFunctor;
|
2019-02-05 16:02:29 +00:00
|
|
|
vtkmdiy::load(bb, invFunctor);
|
2018-06-08 15:56:40 +00:00
|
|
|
obj = vtkm::cont::make_ArrayHandleTransform(array, functor, invFunctor);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename AH, typename Functor, typename InvFunctor>
|
|
|
|
struct Serialization<vtkm::cont::ArrayHandle<
|
|
|
|
typename vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>::ValueType,
|
|
|
|
vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>>>
|
|
|
|
: Serialization<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
} // diy
|
2019-09-12 21:19:36 +00:00
|
|
|
/// @endcond SERIALIZATION
|
2018-06-08 15:56:40 +00:00
|
|
|
|
2015-04-13 20:43:18 +00:00
|
|
|
#endif //vtk_m_cont_ArrayHandleTransform_h
|