Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ViewPlainPtr copy-/movable #2105

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 11 additions & 32 deletions include/alpaka/mem/view/ViewPlainPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,29 @@ namespace alpaka
{
//! The memory view to wrap plain pointers.
template<typename TDev, typename TElem, typename TDim, typename TIdx>
class ViewPlainPtr final : public internal::ViewAccessOps<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
struct ViewPlainPtr final : internal::ViewAccessOps<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
{
static_assert(!std::is_const_v<TIdx>, "The idx type of the view can not be const!");

using Dev = alpaka::Dev<TDev>;

public:
template<typename TExtent>
ALPAKA_FN_HOST ViewPlainPtr(TElem* pMem, Dev dev, TExtent const& extent = TExtent())
: m_pMem(pMem)
, m_dev(std::move(dev))
, m_extentElements(getExtentVecEnd<TDim>(extent))
, m_pitchBytes(detail::calculatePitchesFromExtents<TElem>(m_extentElements))
ALPAKA_FN_HOST ViewPlainPtr(TElem* pMem, TDev dev, TExtent const& extent = TExtent())
: ViewPlainPtr(pMem, std::move(dev), extent, detail::calculatePitchesFromExtents<TElem>(extent))
{
}

template<typename TExtent, typename TPitch>
ALPAKA_FN_HOST ViewPlainPtr(TElem* pMem, Dev const dev, TExtent const& extent, TPitch pitchBytes)
ALPAKA_FN_HOST ViewPlainPtr(TElem* pMem, TDev dev, TExtent const& extent, TPitch pitchBytes)
: m_pMem(pMem)
, m_dev(dev)
, m_extentElements(getExtentVecEnd<TDim>(extent))
, m_pitchBytes(subVecEnd<TDim>(static_cast<Vec<TDim, TIdx>>(pitchBytes)))
{
}

ViewPlainPtr(ViewPlainPtr const&) = default;
ALPAKA_FN_HOST
ViewPlainPtr(ViewPlainPtr&& other) noexcept
: m_pMem(other.m_pMem)
, m_dev(other.m_dev)
, m_extentElements(other.m_extentElements)
, m_pitchBytes(other.m_pitchBytes)
, m_dev(std::move(dev))
, m_extentElements(extent)
, m_pitchBytes(static_cast<Vec<TDim, TIdx>>(pitchBytes))
{
}
ALPAKA_FN_HOST
auto operator=(ViewPlainPtr const&) -> ViewPlainPtr& = delete;
ALPAKA_FN_HOST
auto operator=(ViewPlainPtr&&) -> ViewPlainPtr& = delete;

public:
TElem* const m_pMem;
Dev const m_dev;
Vec<TDim, TIdx> const m_extentElements;
Vec<TDim, TIdx> const m_pitchBytes;
TElem* m_pMem;
TDev m_dev;
Vec<TDim, TIdx> m_extentElements;
Vec<TDim, TIdx> m_pitchBytes;
};

// Trait specializations for ViewPlainPtr.
Expand Down
91 changes: 29 additions & 62 deletions test/unit/mem/view/src/ViewPlainPtrTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2022 Axel Huebl, Benjamin Worpitz, Erik Zenker, Bernhard Manfred Gruber, Jan Stephan
/* Copyright 2023 Axel Huebl, Benjamin Worpitz, Erik Zenker, Bernhard Manfred Gruber, Jan Stephan
* SPDX-License-Identifier: MPL-2.0
*/

Expand All @@ -17,37 +17,13 @@

#if BOOST_COMP_GNUC
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored \
"-Wcast-align" // "cast from 'std::uint8_t*' to 'Elem*' increases required alignment of target type"
// "cast from 'std::uint8_t*' to 'Elem*' increases required alignment of target type"
# pragma GCC diagnostic ignored "-Wcast-align"
#endif

namespace alpaka::test
{
template<typename TAcc, typename TDev, typename TElem, typename TDim, typename TIdx>
auto testViewPlainPtrImmutable(
alpaka::ViewPlainPtr<TDev, TElem, TDim, TIdx> const& view,
TDev const& dev,
alpaka::Vec<TDim, TIdx> const& extentView,
alpaka::Vec<TDim, TIdx> const& offsetView) -> void
{
alpaka::test::testViewImmutable<TElem>(view, dev, extentView, offsetView);
}

template<typename TAcc, typename TDev, typename TElem, typename TDim, typename TIdx>
auto testViewPlainPtrMutable(
alpaka::ViewPlainPtr<TDev, TElem, TDim, TIdx>& view,
TDev const& dev,
alpaka::Vec<TDim, TIdx> const& extentView,
alpaka::Vec<TDim, TIdx> const& offsetView) -> void
{
testViewPlainPtrImmutable<TAcc>(view, dev, extentView, offsetView);

using Queue = alpaka::test::DefaultQueue<TDev>;
Queue queue(dev);
alpaka::test::testViewMutable<TAcc>(queue, view);
}

template<typename TAcc, typename TElem>
template<typename TAcc, typename TElem, bool Const>
auto testViewPlainPtr() -> void
{
using Dev = alpaka::Dev<TAcc>;
Expand All @@ -69,32 +45,13 @@ namespace alpaka::test
alpaka::getExtents(buf),
alpaka::getPitchesInBytes(buf));

alpaka::test::testViewPlainPtrMutable<TAcc>(view, dev, extentView, offsetView);
}

template<typename TAcc, typename TElem>
auto testViewPlainPtrConst() -> void
{
using Dev = alpaka::Dev<TAcc>;
using Dim = alpaka::Dim<TAcc>;
using Idx = alpaka::Idx<TAcc>;
using View = alpaka::ViewPlainPtr<Dev, TElem, Dim, Idx>;

auto const platform = alpaka::Platform<TAcc>{};
auto const dev = alpaka::getDevByIdx(platform, 0);

auto const extentBuf = alpaka::test::extentBuf<Dim, Idx>;
auto buf = alpaka::allocBuf<TElem, Idx>(dev, extentBuf);

auto const extentView = extentBuf;
auto const offsetView = alpaka::Vec<Dim, Idx>::all(static_cast<Idx>(0));
View const view(
alpaka::getPtrNative(buf),
alpaka::getDev(buf),
alpaka::getExtents(buf),
alpaka::getPitchesInBytes(buf));

alpaka::test::testViewPlainPtrImmutable<TAcc>(view, dev, extentView, offsetView);
alpaka::test::testViewImmutable<TElem>(std::as_const(view), dev, extentView, offsetView);
if constexpr(!Const)
{
using Queue = alpaka::test::DefaultQueue<Dev>;
Queue queue(dev);
alpaka::test::testViewMutable<TAcc>(queue, view);
}
}

template<typename TAcc, typename TElem>
Expand All @@ -109,18 +66,28 @@ namespace alpaka::test
auto const dev = alpaka::getDevByIdx(platform, 0);

auto buf = alpaka::allocBuf<TElem, Idx>(dev, alpaka::test::extentBuf<Dim, Idx>);

View view(
alpaka::getPtrNative(buf),
alpaka::getDev(buf),
alpaka::getExtents(buf),
alpaka::getPitchesInBytes(buf));
auto nativePtr = alpaka::getPtrNative(buf);
View view(nativePtr, alpaka::getDev(buf), alpaka::getExtents(buf), alpaka::getPitchesInBytes(buf));

// copy-constructor
View viewCopy(view);
CHECK(alpaka::getPtrNative(viewCopy) == nativePtr);

// move-constructor
View viewMove(std::move(viewCopy));
CHECK(alpaka::getPtrNative(viewMove) == nativePtr);

auto buf2 = alpaka::allocBuf<TElem, Idx>(dev, alpaka::test::extentBuf<Dim, Idx>);
auto nativePtr2 = alpaka::getPtrNative(buf);
View view2(nativePtr2, alpaka::getDev(buf2), alpaka::getExtents(buf2), alpaka::getPitchBytesVec(buf2));

// copy-assign
viewCopy = view2;
CHECK(alpaka::getPtrNative(viewCopy) == nativePtr2);

// move-assign
viewMove = std::move(view2);
CHECK(alpaka::getPtrNative(viewMove) == nativePtr2);
}
} // namespace alpaka::test
#if BOOST_COMP_GNUC
Expand All @@ -129,12 +96,12 @@ namespace alpaka::test

TEMPLATE_LIST_TEST_CASE("viewPlainPtrTest", "[memView]", alpaka::test::TestAccs)
{
alpaka::test::testViewPlainPtr<TestType, float>();
alpaka::test::testViewPlainPtr<TestType, float, false>();
}

TEMPLATE_LIST_TEST_CASE("viewPlainPtrConstTest", "[memView]", alpaka::test::TestAccs)
{
alpaka::test::testViewPlainPtrConst<TestType, float>();
alpaka::test::testViewPlainPtr<TestType, float, true>();
}

TEMPLATE_LIST_TEST_CASE("viewPlainPtrOperatorTest", "[memView]", alpaka::test::TestAccs)
Expand Down