-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43310 from makortel/portableCollectionDef
Allow Portable{Collection,Object}<T, TDev> to be used also independently of ALPAKA_ACCELERATOR_NAMESPACE
- Loading branch information
Showing
8 changed files
with
139 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,55 @@ | ||
#ifndef DataFormats_Portable_interface_PortableCollection_h | ||
#define DataFormats_Portable_interface_PortableCollection_h | ||
|
||
#include "HeterogeneousCore/AlpakaInterface/interface/traits.h" | ||
#include <alpaka/alpaka.hpp> | ||
|
||
#include "DataFormats/Portable/interface/PortableHostCollection.h" | ||
#include "DataFormats/Portable/interface/PortableDeviceCollection.h" | ||
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToDevice.h" | ||
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToHost.h" | ||
|
||
namespace traits { | ||
|
||
// trait for a generic SoA-based product | ||
template <typename T, typename TDev, typename = std::enable_if_t<alpaka::isDevice<TDev>>> | ||
class PortableCollectionTrait; | ||
struct PortableCollectionTrait { | ||
using CollectionType = PortableDeviceCollection<T, TDev>; | ||
}; | ||
|
||
// specialise for host device | ||
template <typename T> | ||
struct PortableCollectionTrait<T, alpaka_common::DevHost> { | ||
using CollectionType = PortableHostCollection<T>; | ||
}; | ||
|
||
} // namespace traits | ||
|
||
// type alias for a generic SoA-based product | ||
template <typename T, typename TDev, typename = std::enable_if_t<alpaka::isDevice<TDev>>> | ||
using PortableCollection = typename traits::PortableCollectionTrait<T, TDev>::CollectionType; | ||
|
||
// define how to copy PortableCollection between host and device | ||
namespace cms::alpakatools { | ||
template <typename TLayout, typename TDevice> | ||
struct CopyToHost<PortableDeviceCollection<TLayout, TDevice>> { | ||
template <typename TQueue> | ||
static auto copyAsync(TQueue& queue, PortableDeviceCollection<TLayout, TDevice> const& srcData) { | ||
PortableHostCollection<TLayout> dstData(srcData->metadata().size(), queue); | ||
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer()); | ||
return dstData; | ||
} | ||
}; | ||
|
||
template <typename TLayout> | ||
struct CopyToDevice<PortableHostCollection<TLayout>> { | ||
template <typename TQueue> | ||
static auto copyAsync(TQueue& queue, PortableHostCollection<TLayout> const& srcData) { | ||
using TDevice = typename alpaka::trait::DevType<TQueue>::type; | ||
PortableDeviceCollection<TLayout, TDevice> dstData(srcData->metadata().size(), queue); | ||
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer()); | ||
return dstData; | ||
} | ||
}; | ||
} // namespace cms::alpakatools | ||
|
||
#endif // DataFormats_Portable_interface_PortableCollection_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<bin name="TestDataFormatsPortableOnHost" file="test_catch2_main.cc,portableCollectionOnHost.cc,portableObjectOnHost.cc"> | ||
<use name="DataFormats/Portable"/> | ||
<use name="DataFormats/SoATemplate"/> | ||
<use name="catch2"/> | ||
</bin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <catch.hpp> | ||
|
||
#include "DataFormats/Portable/interface/PortableCollection.h" | ||
#include "DataFormats/Portable/interface/PortableHostCollection.h" | ||
#include "DataFormats/SoATemplate/interface/SoACommon.h" | ||
#include "DataFormats/SoATemplate/interface/SoALayout.h" | ||
#include "DataFormats/SoATemplate/interface/SoAView.h" | ||
|
||
namespace { | ||
GENERATE_SOA_LAYOUT(TestLayout, SOA_COLUMN(double, x), SOA_COLUMN(int32_t, id)) | ||
|
||
using TestSoA = TestLayout<>; | ||
|
||
constexpr auto s_tag = "[PortableCollection]"; | ||
} // namespace | ||
|
||
// This test is currently mostly about the code compiling | ||
TEST_CASE("Use of PortableCollection<T, TDev> on host code", s_tag) { | ||
auto const size = 10; | ||
PortableCollection<TestSoA, alpaka::DevCpu> coll(size, cms::alpakatools::host()); | ||
|
||
SECTION("Tests") { REQUIRE(coll->metadata().size() == size); } | ||
|
||
static_assert(std::is_same_v<PortableCollection<TestSoA, alpaka::DevCpu>, PortableHostCollection<TestSoA>>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <catch.hpp> | ||
|
||
#include "DataFormats/Portable/interface/PortableObject.h" | ||
#include "DataFormats/Portable/interface/PortableHostObject.h" | ||
|
||
namespace { | ||
struct Test { | ||
int a; | ||
float b; | ||
}; | ||
|
||
constexpr auto s_tag = "[PortableObject]"; | ||
} // namespace | ||
|
||
// This test is currently mostly about the code compiling | ||
TEST_CASE("Use of PortableObject<T> on host code", s_tag) { | ||
PortableObject<Test, alpaka::DevCpu> obj(cms::alpakatools::host()); | ||
obj->a = 42; | ||
|
||
SECTION("Tests") { REQUIRE(obj->a == 42); } | ||
|
||
static_assert(std::is_same_v<PortableObject<Test, alpaka::DevCpu>, PortableHostObject<Test>>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#define CATCH_CONFIG_MAIN | ||
#include <catch.hpp> |