From bdbd4bcc1da78165678125aca99fbe3577d36830 Mon Sep 17 00:00:00 2001 From: Simeon Ehrig Date: Mon, 13 May 2024 16:59:12 +0200 Subject: [PATCH] add memory visibility to raw pointer view CI_FILTER: nope --- include/alpaka/mem/Visibility.hpp | 7 +- include/alpaka/mem/view/ViewPlainPtr.hpp | 70 ++++++++++------ include/alpaka/meta/IsTuple.hpp | 29 ------- include/alpaka/meta/TypeListOps.hpp | 33 +++++++- test/unit/mem/src/Visibility.cpp | 102 +++++++++++++++++++---- 5 files changed, 167 insertions(+), 74 deletions(-) delete mode 100644 include/alpaka/meta/IsTuple.hpp diff --git a/include/alpaka/mem/Visibility.hpp b/include/alpaka/mem/Visibility.hpp index 568c959da010..3e0c38f7bdec 100644 --- a/include/alpaka/mem/Visibility.hpp +++ b/include/alpaka/mem/Visibility.hpp @@ -6,7 +6,6 @@ #include "alpaka/mem/view/ViewAccessOps.hpp" #include "alpaka/meta/ForEachType.hpp" -#include "alpaka/meta/IsTuple.hpp" #include "alpaka/meta/TypeListOps.hpp" #include @@ -17,7 +16,7 @@ #define CREATE_MEM_VISIBILITY(mem_name) \ struct mem_name \ { \ - static std::string name() \ + static std::string get_name() \ { \ return #mem_name; \ } \ @@ -49,7 +48,7 @@ namespace alpaka template void operator()(std::vector& vs) { - vs.push_back(TTYPE::name()); + vs.push_back(TTYPE::get_name()); } }; } // namespace detail @@ -84,7 +83,7 @@ namespace alpaka } else { - return MemVisibilityType::name(); + return MemVisibilityType::get_name(); } } diff --git a/include/alpaka/mem/view/ViewPlainPtr.hpp b/include/alpaka/mem/view/ViewPlainPtr.hpp index ceb4d95aed32..97817822f460 100644 --- a/include/alpaka/mem/view/ViewPlainPtr.hpp +++ b/include/alpaka/mem/view/ViewPlainPtr.hpp @@ -8,9 +8,18 @@ #include "alpaka/dev/DevCpu.hpp" #include "alpaka/dev/DevGenericSycl.hpp" #include "alpaka/dev/DevUniformCudaHipRt.hpp" +#include "alpaka/mem/Visibility.hpp" #include "alpaka/mem/view/Traits.hpp" #include "alpaka/mem/view/ViewAccessOps.hpp" #include "alpaka/meta/DependentFalseType.hpp" +#include "alpaka/meta/TypeListOps.hpp" +#include "alpaka/platform/PlatformCpu.hpp" +#include "alpaka/platform/PlatformCpuSycl.hpp" +#include "alpaka/platform/PlatformCudaRt.hpp" +#include "alpaka/platform/PlatformFpgaSyclIntel.hpp" +#include "alpaka/platform/PlatformGenericSycl.hpp" +#include "alpaka/platform/PlatformGpuSyclIntel.hpp" +#include "alpaka/platform/PlatformHipRt.hpp" #include "alpaka/vec/Vec.hpp" #include @@ -19,8 +28,14 @@ namespace alpaka { //! The memory view to wrap plain pointers. - template - struct ViewPlainPtr final : internal::ViewAccessOps> + template< + typename TDev, + typename TElem, + typename TDim, + typename TIdx, + typename TMemVisibility = + typename alpaka::meta::toTuple>::type>::type> + struct ViewPlainPtr final : internal::ViewAccessOps> { static_assert(!std::is_const_v, "The idx type of the view can not be const!"); @@ -49,15 +64,21 @@ namespace alpaka namespace trait { //! The ViewPlainPtr device type trait specialization. - template - struct DevType> + template + struct DevType> { using type = alpaka::Dev; }; + template + struct MemVisibility> + { + using type = TMemVisibility; + }; + //! The ViewPlainPtr device get trait specialization. - template - struct GetDev> + template + struct GetDev> { static auto getDev(ViewPlainPtr const& view) -> alpaka::Dev { @@ -66,15 +87,15 @@ namespace alpaka }; //! The ViewPlainPtr dimension getter trait. - template - struct DimType> + template + struct DimType> { using type = TDim; }; //! The ViewPlainPtr memory element type get trait specialization. - template - struct ElemType> + template + struct ElemType> { using type = TElem; }; @@ -82,32 +103,32 @@ namespace alpaka namespace trait { - template - struct GetExtents> + template + struct GetExtents> { - ALPAKA_FN_HOST auto operator()(ViewPlainPtr const& view) const + ALPAKA_FN_HOST auto operator()(ViewPlainPtr const& view) const { return view.m_extentElements; } }; //! The ViewPlainPtr native pointer get trait specialization. - template - struct GetPtrNative> + template + struct GetPtrNative> { - static auto getPtrNative(ViewPlainPtr const& view) -> TElem const* + static auto getPtrNative(ViewPlainPtr const& view) -> TElem const* { return view.m_pMem; } - static auto getPtrNative(ViewPlainPtr& view) -> TElem* + static auto getPtrNative(ViewPlainPtr& view) -> TElem* { return view.m_pMem; } }; - template - struct GetPitchesInBytes> + template + struct GetPitchesInBytes> { ALPAKA_FN_HOST auto operator()(ViewPlainPtr const& view) const { @@ -174,18 +195,19 @@ namespace alpaka }; #endif //! The ViewPlainPtr offset get trait specialization. - template - struct GetOffsets> + template + struct GetOffsets> { - ALPAKA_FN_HOST auto operator()(ViewPlainPtr const&) const -> Vec + ALPAKA_FN_HOST auto operator()(ViewPlainPtr const&) const + -> Vec { return Vec::zeros(); } }; //! The ViewPlainPtr idx type trait specialization. - template - struct IdxType> + template + struct IdxType> { using type = TIdx; }; diff --git a/include/alpaka/meta/IsTuple.hpp b/include/alpaka/meta/IsTuple.hpp deleted file mode 100644 index ee4a920923ba..000000000000 --- a/include/alpaka/meta/IsTuple.hpp +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright 2024 Simeon Ehrig - * SPDX-License-Identifier: MPL-2.0 - */ - -#pragma once - -#include -#include - -// copied from https://stackoverflow.com/a/51073558/22035743 -namespace alpaka::meta -{ - template - struct IsTuple : std::false_type - { - }; - - template - struct IsTuple> : std::true_type - { - }; - - template - constexpr bool isTuple() - { - return IsTuple>::value; - } - -} // namespace alpaka::meta diff --git a/include/alpaka/meta/TypeListOps.hpp b/include/alpaka/meta/TypeListOps.hpp index 2d6bcfe7f45f..de9a59bb2453 100644 --- a/include/alpaka/meta/TypeListOps.hpp +++ b/include/alpaka/meta/TypeListOps.hpp @@ -1,9 +1,10 @@ -/* Copyright 2022 Bernhard Manfred Gruber +/* Copyright 2024 Bernhard Manfred Gruber, Simeon Ehrig * SPDX-License-Identifier: MPL-2.0 */ #pragma once +#include #include namespace alpaka::meta @@ -35,4 +36,34 @@ namespace alpaka::meta { static constexpr bool value = std::is_same_v || Contains, Value>::value; }; + + // copied from https://stackoverflow.com/a/51073558/22035743 + template + struct IsTuple : std::false_type + { + }; + + template + struct IsTuple> : std::true_type + { + }; + + template + constexpr bool isTuple() + { + return IsTuple>::value; + } + + template + struct toTuple + { + using type = std::tuple; + }; + + template + struct toTuple> + { + using type = std::tuple; + }; + } // namespace alpaka::meta diff --git a/test/unit/mem/src/Visibility.cpp b/test/unit/mem/src/Visibility.cpp index 65657de70ddd..00c5143c6bc2 100644 --- a/test/unit/mem/src/Visibility.cpp +++ b/test/unit/mem/src/Visibility.cpp @@ -12,28 +12,39 @@ using Dim = alpaka::DimInt<1>; using Idx = std::size_t; -using ExpectedTagsMemVisibilities = std::tuple< - std::tuple, - std::tuple, - std::tuple, - std::tuple, - std::tuple, - std::tuple, - std::tuple, - std::tuple, - std::tuple, - std::tuple>; +template +struct AccIsEnabledMemVisibilities : std::false_type +{ +}; + +template +struct AccIsEnabledMemVisibilities< + TTagMemView, + std::void_t, alpaka::DimInt<1>, int>>> : std::true_type +{ +}; + +using ExpectedTagsMemVisibilities = alpaka::meta::Filter< + std::tuple< + std::tuple, + std::tuple, + std::tuple, + std::tuple, + std::tuple, + std::tuple, + std::tuple, + std::tuple, + std::tuple, + std::tuple>, + AccIsEnabledMemVisibilities>; TEMPLATE_LIST_TEST_CASE("memoryVisibilityType", "[mem][visibility]", ExpectedTagsMemVisibilities) { using Tag = std::tuple_element_t<0, TestType>; using ExpectedMemVisibility = std::tuple_element_t<1, TestType>; - if constexpr(alpaka::AccIsEnabled::value) - { - using PltfType = alpaka::Platform>; - STATIC_REQUIRE(std::is_same_v::type, ExpectedMemVisibility>); - } + using PltfType = alpaka::Platform>; + STATIC_REQUIRE(std::is_same_v::type, ExpectedMemVisibility>); } using EnabledTagTagList = alpaka::meta::CartesianProduct; @@ -84,3 +95,62 @@ TEMPLATE_LIST_TEST_CASE("testHasSameMemView", "[mem][visibility]", EnabledTagTag STATIC_REQUIRE_FALSE(alpaka::hasSameMemView()); } } + +using EnabledTagTagMemVisibilityList + = alpaka::meta::CartesianProduct; + +TEMPLATE_LIST_TEST_CASE("testMemView", "[mem][visibility]", EnabledTagTagMemVisibilityList) +{ + using Tag1 = std::tuple_element_t<0, std::tuple_element_t<0, TestType>>; + using ExpectedMemVisibilityForTag1 = std::tuple_element_t<1, std::tuple_element_t<0, TestType>>; + using Tag2 = std::tuple_element_t<0, std::tuple_element_t<1, TestType>>; + using ExpectedMemVisibilityForTag2 = std::tuple_element_t<1, std::tuple_element_t<1, TestType>>; + + SUCCEED( + "Tag1: " << Tag1::get_name() << " + " << ExpectedMemVisibilityForTag1::get_name() + << "\nTag2: " << Tag2::get_name() << " + " << ExpectedMemVisibilityForTag1::get_name()); + + + constexpr Idx data_size = 10; + + using Acc1 = alpaka::TagToAcc; + using Acc2 = alpaka::TagToAcc; + + auto const plt1 = alpaka::Platform{}; + auto const plt2 = alpaka::Platform{}; + + auto const dev1 = alpaka::getDevByIdx(plt1, 0); + auto const dev2 = alpaka::getDevByIdx(plt2, 0); + + using Vec1D = alpaka::Vec, Idx>; + Vec1D const extents(Vec1D::all(data_size)); + + std::array data; + + // STATIC_REQUIRE(std::is_same_v>::type, std::tuple>); STATIC_REQUIRE(std::is_same_v::type, std::tuple>); + // STATIC_REQUIRE(std::is_same_v::type, std::tuple>); + + auto data_view1 = alpaka::createView(dev1, data.data(), extents); + STATIC_REQUIRE(std::is_same_v< + typename alpaka::trait::MemVisibility::type, + std::tuple>); + STATIC_REQUIRE(alpaka::hasSameMemView(plt1, data_view1)); + + auto data_view2 = alpaka::createView(dev2, data.data(), extents); + STATIC_REQUIRE(std::is_same_v< + typename alpaka::trait::MemVisibility::type, + std::tuple>); + STATIC_REQUIRE(alpaka::hasSameMemView(plt2, data_view2)); + + if constexpr(std::is_same_v) + { + STATIC_REQUIRE(alpaka::hasSameMemView(plt1, data_view2)); + STATIC_REQUIRE(alpaka::hasSameMemView(plt2, data_view1)); + } + else + { + STATIC_REQUIRE_FALSE(alpaka::hasSameMemView(plt1, data_view2)); + STATIC_REQUIRE_FALSE(alpaka::hasSameMemView(plt2, data_view1)); + } +}