diff --git a/include/alpaka/mem/view/ViewPlainPtr.hpp b/include/alpaka/mem/view/ViewPlainPtr.hpp index 5d6b23c5efb2..c3f225a9dc51 100644 --- a/include/alpaka/mem/view/ViewPlainPtr.hpp +++ b/include/alpaka/mem/view/ViewPlainPtr.hpp @@ -20,50 +20,29 @@ namespace alpaka { //! The memory view to wrap plain pointers. template - class ViewPlainPtr final : public internal::ViewAccessOps> + struct ViewPlainPtr final : internal::ViewAccessOps> { static_assert(!std::is_const_v, "The idx type of the view can not be const!"); - using Dev = alpaka::Dev; - - public: template - ALPAKA_FN_HOST ViewPlainPtr(TElem* pMem, Dev dev, TExtent const& extent = TExtent()) - : m_pMem(pMem) - , m_dev(std::move(dev)) - , m_extentElements(getExtentVecEnd(extent)) - , m_pitchBytes(detail::calculatePitchesFromExtents(m_extentElements)) + ALPAKA_FN_HOST ViewPlainPtr(TElem* pMem, TDev dev, TExtent const& extent = TExtent()) + : ViewPlainPtr(pMem, std::move(dev), extent, detail::calculatePitchesFromExtents(extent)) { } template - 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(extent)) - , m_pitchBytes(subVecEnd(static_cast>(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>(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 const m_extentElements; - Vec const m_pitchBytes; + TElem* m_pMem; + TDev m_dev; + Vec m_extentElements; + Vec m_pitchBytes; }; // Trait specializations for ViewPlainPtr. diff --git a/test/unit/mem/view/src/ViewPlainPtrTest.cpp b/test/unit/mem/view/src/ViewPlainPtrTest.cpp index 95aba0a6e79d..5e741d11f519 100644 --- a/test/unit/mem/view/src/ViewPlainPtrTest.cpp +++ b/test/unit/mem/view/src/ViewPlainPtrTest.cpp @@ -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 */ @@ -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 - auto testViewPlainPtrImmutable( - alpaka::ViewPlainPtr const& view, - TDev const& dev, - alpaka::Vec const& extentView, - alpaka::Vec const& offsetView) -> void - { - alpaka::test::testViewImmutable(view, dev, extentView, offsetView); - } - - template - auto testViewPlainPtrMutable( - alpaka::ViewPlainPtr& view, - TDev const& dev, - alpaka::Vec const& extentView, - alpaka::Vec const& offsetView) -> void - { - testViewPlainPtrImmutable(view, dev, extentView, offsetView); - - using Queue = alpaka::test::DefaultQueue; - Queue queue(dev); - alpaka::test::testViewMutable(queue, view); - } - - template + template auto testViewPlainPtr() -> void { using Dev = alpaka::Dev; @@ -69,32 +45,13 @@ namespace alpaka::test alpaka::getExtents(buf), alpaka::getPitchesInBytes(buf)); - alpaka::test::testViewPlainPtrMutable(view, dev, extentView, offsetView); - } - - template - auto testViewPlainPtrConst() -> void - { - using Dev = alpaka::Dev; - using Dim = alpaka::Dim; - using Idx = alpaka::Idx; - using View = alpaka::ViewPlainPtr; - - auto const platform = alpaka::Platform{}; - auto const dev = alpaka::getDevByIdx(platform, 0); - - auto const extentBuf = alpaka::test::extentBuf; - auto buf = alpaka::allocBuf(dev, extentBuf); - - auto const extentView = extentBuf; - auto const offsetView = alpaka::Vec::all(static_cast(0)); - View const view( - alpaka::getPtrNative(buf), - alpaka::getDev(buf), - alpaka::getExtents(buf), - alpaka::getPitchesInBytes(buf)); - - alpaka::test::testViewPlainPtrImmutable(view, dev, extentView, offsetView); + alpaka::test::testViewImmutable(std::as_const(view), dev, extentView, offsetView); + if constexpr(!Const) + { + using Queue = alpaka::test::DefaultQueue; + Queue queue(dev); + alpaka::test::testViewMutable(queue, view); + } } template @@ -109,18 +66,28 @@ namespace alpaka::test auto const dev = alpaka::getDevByIdx(platform, 0); auto buf = alpaka::allocBuf(dev, alpaka::test::extentBuf); - - 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(dev, alpaka::test::extentBuf); + 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 @@ -129,12 +96,12 @@ namespace alpaka::test TEMPLATE_LIST_TEST_CASE("viewPlainPtrTest", "[memView]", alpaka::test::TestAccs) { - alpaka::test::testViewPlainPtr(); + alpaka::test::testViewPlainPtr(); } TEMPLATE_LIST_TEST_CASE("viewPlainPtrConstTest", "[memView]", alpaka::test::TestAccs) { - alpaka::test::testViewPlainPtrConst(); + alpaka::test::testViewPlainPtr(); } TEMPLATE_LIST_TEST_CASE("viewPlainPtrOperatorTest", "[memView]", alpaka::test::TestAccs)