vtk-m2/vtkm/cont/cuda/internal/MakeThrustIterator.h

156 lines
4.7 KiB
C
Raw Normal View History

//============================================================================
// 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.
//
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_cont_cuda_internal_MakeThrustIterator_h
#define vtk_m_cont_cuda_internal_MakeThrustIterator_h
#include <vtkm/Pair.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/Types.h>
#include <vtkm/cont/ArrayPortalToIterators.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/internal/ExportMacros.h>
#include <vtkm/exec/cuda/internal/ArrayPortalFromThrust.h>
#include <vtkm/exec/cuda/internal/WrappedOperators.h>
// Disable warnings we check vtkm for but Thrust does not.
VTKM_THIRDPARTY_PRE_INCLUDE
#include <thrust/functional.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/transform_iterator.h>
2017-05-18 14:51:24 +00:00
#include <thrust/system/cuda/memory.h>
VTKM_THIRDPARTY_POST_INCLUDE
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace cont
{
namespace cuda
{
namespace internal
{
namespace detail
{
// Tags to specify what type of thrust iterator to use.
2017-05-18 14:29:41 +00:00
struct ThrustIteratorFromArrayPortalTag
{
};
struct ThrustIteratorDevicePtrTag
{
};
// Traits to help classify what thrust iterators will be used.
2017-05-18 14:29:41 +00:00
template <typename IteratorType>
struct ThrustIteratorTag
{
using Type = ThrustIteratorFromArrayPortalTag;
};
2017-05-18 14:29:41 +00:00
template <typename T>
struct ThrustIteratorTag<thrust::system::cuda::pointer<T>>
{
using Type = ThrustIteratorDevicePtrTag;
};
2017-05-18 14:29:41 +00:00
template <typename T>
struct ThrustIteratorTag<thrust::system::cuda::pointer<const T>>
{
using Type = ThrustIteratorDevicePtrTag;
};
2017-05-18 14:29:41 +00:00
template <typename PortalType, typename Tag>
struct IteratorChooser;
template <typename PortalType>
struct IteratorChooser<PortalType, detail::ThrustIteratorFromArrayPortalTag>
{
using Type = vtkm::exec::cuda::internal::IteratorFromArrayPortal<PortalType>;
};
2017-05-18 14:29:41 +00:00
template <typename PortalType>
struct IteratorChooser<PortalType, detail::ThrustIteratorDevicePtrTag>
{
using PortalToIteratorType = vtkm::cont::ArrayPortalToIterators<PortalType>;
using Type = typename PortalToIteratorType::IteratorType;
};
2017-05-18 14:29:41 +00:00
template <typename PortalType>
struct IteratorTraits
{
using PortalToIteratorType = vtkm::cont::ArrayPortalToIterators<PortalType>;
using Tag = typename detail::ThrustIteratorTag<typename PortalToIteratorType::IteratorType>::Type;
using IteratorType = typename IteratorChooser<PortalType, Tag>::Type;
};
2017-05-18 14:29:41 +00:00
template <typename PortalType>
VTKM_CONT typename IteratorTraits<PortalType>::IteratorType MakeIteratorBegin(
PortalType portal,
detail::ThrustIteratorFromArrayPortalTag)
{
return vtkm::exec::cuda::internal::IteratorFromArrayPortal<PortalType>(portal);
}
2017-05-18 14:29:41 +00:00
template <typename PortalType>
VTKM_CONT typename IteratorTraits<PortalType>::IteratorType MakeIteratorBegin(
PortalType portal,
detail::ThrustIteratorDevicePtrTag)
{
vtkm::cont::ArrayPortalToIterators<PortalType> iterators(portal);
return iterators.GetBegin();
}
2017-05-18 14:29:41 +00:00
template <typename PortalType>
VTKM_CONT typename IteratorTraits<PortalType>::IteratorType MakeIteratorEnd(
PortalType portal,
detail::ThrustIteratorFromArrayPortalTag)
2016-06-23 18:12:44 +00:00
{
2017-05-18 14:29:41 +00:00
vtkm::exec::cuda::internal::IteratorFromArrayPortal<PortalType> iterator(portal);
::thrust::advance(iterator, static_cast<std::size_t>(portal.GetNumberOfValues()));
2016-06-23 18:12:44 +00:00
return iterator;
}
2017-05-18 14:29:41 +00:00
template <typename PortalType>
VTKM_CONT typename IteratorTraits<PortalType>::IteratorType MakeIteratorEnd(
PortalType portal,
detail::ThrustIteratorDevicePtrTag)
2016-06-23 18:12:44 +00:00
{
vtkm::cont::ArrayPortalToIterators<PortalType> iterators(portal);
return iterators.GetEnd();
}
} // namespace detail
2017-05-18 14:29:41 +00:00
template <typename PortalType>
VTKM_CONT typename detail::IteratorTraits<PortalType>::IteratorType IteratorBegin(PortalType portal)
{
using IteratorTag = typename detail::IteratorTraits<PortalType>::Tag;
return detail::MakeIteratorBegin(portal, IteratorTag());
}
2017-05-18 14:29:41 +00:00
template <typename PortalType>
VTKM_CONT typename detail::IteratorTraits<PortalType>::IteratorType IteratorEnd(PortalType portal)
{
using IteratorTag = typename detail::IteratorTraits<PortalType>::Tag;
2016-06-23 18:12:44 +00:00
return detail::MakeIteratorEnd(portal, IteratorTag());
}
}
}
}
} //namespace vtkm::cont::cuda::internal
#endif