Skip to content

Commit 0867b48

Browse files
committed
SoA backend cleanup
1 parent 7554540 commit 0867b48

9 files changed

Lines changed: 235 additions & 293 deletions

File tree

DataFormats/Portable/interface/PortableCollectionCommon.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010

1111
namespace portablecollection {
1212

13+
template <int I, typename TQueue, typename Descriptor, typename ConstDescriptor>
14+
void deepCopy(TQueue& queue, Descriptor& dest, ConstDescriptor const& src) {
15+
if constexpr (I < ConstDescriptor::num_cols) {
16+
assert(std::get<I>(dest.buff).size_bytes() == std::get<I>(src.buff).size_bytes());
17+
alpaka::memcpy(
18+
queue,
19+
alpaka::createView(alpaka::getDev(queue), std::get<I>(dest.buff).data(), std::get<I>(dest.buff).size()),
20+
alpaka::createView(alpaka::getDev(queue), std::get<I>(src.buff).data(), std::get<I>(src.buff).size()));
21+
deepCopy<I + 1>(queue, dest, src);
22+
}
23+
}
24+
1325
template <std::integral Int>
1426
constexpr int size_cast(Int input) {
1527
if ((std::is_signed_v<Int> && input < 0) || input > std::numeric_limits<int>::max()) {

DataFormats/Portable/interface/PortableDeviceCollection.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,13 @@ class PortableDeviceCollection {
133133
void deepCopy(TQueue& queue, ConstView const& view) {
134134
ConstDescriptor desc{view};
135135
Descriptor desc_{view_};
136-
_deepCopy<0>(queue, desc_, desc);
136+
portablecollection::deepCopy<0>(queue, desc_, desc);
137137
}
138138

139139
// Either Layout::size_type for normal layouts or std::array<Layout::size_type, N> for SoABlocks layouts
140140
auto size() const { return layout_.metadata().size(); }
141141

142142
private:
143-
// Helper function implementing the recursive deep copy
144-
template <int I, typename TQueue>
145-
void _deepCopy(TQueue& queue, Descriptor& dest, ConstDescriptor const& src) {
146-
if constexpr (I < ConstDescriptor::num_cols) {
147-
assert(std::get<I>(dest.buff).size_bytes() == std::get<I>(src.buff).size_bytes());
148-
alpaka::memcpy(
149-
queue,
150-
alpaka::createView(alpaka::getDev(queue), std::get<I>(dest.buff).data(), std::get<I>(dest.buff).size()),
151-
alpaka::createView(alpaka::getDev(queue), std::get<I>(src.buff).data(), std::get<I>(src.buff).size()));
152-
_deepCopy<I + 1>(queue, dest, src);
153-
}
154-
}
155-
156143
// Data members
157144
std::optional<Buffer> buffer_; //!
158145
Layout layout_; //

DataFormats/Portable/interface/PortableHostCollection.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,26 +175,13 @@ class PortableHostCollection {
175175
void deepCopy(TQueue& queue, ConstView const& view) {
176176
ConstDescriptor desc{view};
177177
Descriptor desc_{view_};
178-
_deepCopy<0>(queue, desc_, desc);
178+
portablecollection::deepCopy<0>(queue, desc_, desc);
179179
}
180180

181181
// Either Layout::size_type for normal layouts or std::array<Layout::size_type, N> for SoABlocks layouts
182182
auto size() const { return layout_.metadata().size(); }
183183

184184
private:
185-
// Helper function implementing the recursive deep copy
186-
template <int I, typename TQueue>
187-
void _deepCopy(TQueue& queue, Descriptor& dest, ConstDescriptor const& src) {
188-
if constexpr (I < ConstDescriptor::num_cols) {
189-
assert(std::get<I>(dest.buff).size_bytes() == std::get<I>(src.buff).size_bytes());
190-
alpaka::memcpy(
191-
queue,
192-
alpaka::createView(alpaka::getDev(queue), std::get<I>(dest.buff).data(), std::get<I>(dest.buff).size()),
193-
alpaka::createView(alpaka::getDev(queue), std::get<I>(src.buff).data(), std::get<I>(src.buff).size()));
194-
_deepCopy<I + 1>(queue, dest, src);
195-
}
196-
}
197-
198185
// Data members
199186
std::optional<Buffer> buffer_; //!
200187
Layout layout_; //

DataFormats/Portable/test/alpaka/test_catch2_SoACopy.dev.cc

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
#include "HeterogeneousCore/AlpakaInterface/interface/workdivision.h"
1414

1515
using namespace ALPAKA_ACCELERATOR_NAMESPACE;
16+
using namespace Catch::Matchers;
1617

1718
using Vector5f = Eigen::Matrix<float, 5, 1>;
1819
using Vector15f = Eigen::Matrix<float, 15, 1>;
20+
using Matrix6x4d = Eigen::Matrix<double, 6, 4>;
1921

2022
GENERATE_SOA_LAYOUT(SoATemplate,
2123
SOA_COLUMN(float, quality),
@@ -25,6 +27,7 @@ GENERATE_SOA_LAYOUT(SoATemplate,
2527
SOA_COLUMN(float, pt),
2628
SOA_EIGEN_COLUMN(Vector5f, state),
2729
SOA_EIGEN_COLUMN(Vector15f, covariance),
30+
SOA_EIGEN_COLUMN(Matrix6x4d, matrix),
2831
SOA_SCALAR(int, nTracks),
2932
SOA_COLUMN(uint32_t, hitOffsets))
3033

@@ -81,19 +84,21 @@ TEST_CASE("test merge soa alpaka", "[SoAMerge][Alpaka]") {
8184
h_view1[i].pt() = static_cast<float>(5);
8285
h_view1[i].state().setConstant(6.f);
8386
h_view1[i].covariance().setConstant(7.f);
87+
h_view1[i].matrix().setConstant(8.0);
8488
h_view1[i].hitOffsets() = static_cast<uint32_t>(9);
8589
}
8690
h_view1.nTracks() = 8;
8791

8892
for (int i = 0; i < hostCollection2.size(); i++) {
89-
h_view2[i].quality() = static_cast<float>(10);
90-
h_view2[i].chi2() = static_cast<float>(11);
91-
h_view2[i].nLayers() = static_cast<int8_t>(12);
92-
h_view2[i].eta() = static_cast<float>(13);
93-
h_view2[i].pt() = static_cast<float>(14);
94-
h_view2[i].state().setConstant(15.f);
95-
h_view2[i].covariance().setConstant(16.f);
96-
h_view2[i].hitOffsets() = static_cast<uint32_t>(18);
93+
h_view2[i].quality() = static_cast<float>(11);
94+
h_view2[i].chi2() = static_cast<float>(12);
95+
h_view2[i].nLayers() = static_cast<int8_t>(13);
96+
h_view2[i].eta() = static_cast<float>(14);
97+
h_view2[i].pt() = static_cast<float>(15);
98+
h_view2[i].state().setConstant(16.f);
99+
h_view2[i].covariance().setConstant(17.f);
100+
h_view2[i].matrix().setConstant(18.0);
101+
h_view2[i].hitOffsets() = static_cast<uint32_t>(19);
97102
}
98103
h_view2.nTracks() = 17;
99104

@@ -132,13 +137,13 @@ TEST_CASE("test merge soa alpaka", "[SoAMerge][Alpaka]") {
132137
inCol2.data());
133138
} else if constexpr (std::get<columnIndex>(outDesc.columnTypes) == cms::soa::SoAColumnType::eigen) {
134139
using EigenType = std::tuple_element_t<columnIndex, decltype(outDesc.parameterTypes)>::ValueType;
135-
constexpr int num_rows = EigenType::RowsAtCompileTime;
140+
constexpr int nRows = EigenType::RowsAtCompileTime * EigenType::ColsAtCompileTime;
136141

137-
const auto strideOutput = std::get<1>(std::get<columnIndex>(outDesc.parameterTypes).tupleOrPointer());
138-
const auto strideInput1 = std::get<1>(std::get<columnIndex>(inDesc1.parameterTypes).tupleOrPointer());
139-
const auto strideInput2 = std::get<1>(std::get<columnIndex>(inDesc2.parameterTypes).tupleOrPointer());
142+
const auto strideOutput = std::get<columnIndex>(outDesc.parameterTypes).stride();
143+
const auto strideInput1 = std::get<columnIndex>(inDesc1.parameterTypes).stride();
144+
const auto strideInput2 = std::get<columnIndex>(inDesc2.parameterTypes).stride();
140145

141-
for (int i = 0; i < num_rows; ++i) {
146+
for (int i = 0; i < nRows; ++i) {
142147
const auto offsetOutput = i * strideOutput;
143148
const auto offsetIn1 = i * strideInput1;
144149
const auto offsetIn2 = i * strideInput2;
@@ -173,23 +178,23 @@ TEST_CASE("test merge soa alpaka", "[SoAMerge][Alpaka]") {
173178

174179
for (int i = 0; i < nTk1 + nTk2; i++) {
175180
if (i < nTk1) {
176-
REQUIRE(h_viewOut[i].quality() == Catch::Approx(1.0f));
177-
REQUIRE(h_viewOut[i].chi2() == Catch::Approx(2.0f));
178-
REQUIRE(h_viewOut[i].nLayers() == 3);
179-
REQUIRE(h_viewOut[i].eta() == Catch::Approx(4.0f));
180-
REQUIRE(h_viewOut[i].pt() == Catch::Approx(5.0f));
181-
REQUIRE(h_viewOut[i].state() == Vector5f(6.f, 6.f, 6.f, 6.f, 6.f));
182-
REQUIRE(h_viewOut[i].covariance() ==
183-
Vector15f(7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f, 7.f));
181+
REQUIRE_THAT(h_viewOut[i].quality(), WithinRel(h_view1[i].quality()));
182+
REQUIRE_THAT(h_viewOut[i].chi2(), WithinRel(h_view1[i].chi2()));
183+
REQUIRE(h_viewOut[i].nLayers() == h_view1[i].nLayers());
184+
REQUIRE_THAT(h_viewOut[i].eta(), WithinRel(h_view1[i].eta()));
185+
REQUIRE_THAT(h_viewOut[i].pt(), WithinRel(h_view1[i].pt()));
186+
REQUIRE(h_viewOut[i].state().isApprox(h_view1[i].state()));
187+
REQUIRE(h_viewOut[i].covariance().isApprox(h_view1[i].covariance()));
188+
REQUIRE(h_viewOut[i].matrix().isApprox(h_view1[i].matrix()));
184189
} else {
185-
REQUIRE(h_viewOut[i].quality() == Catch::Approx(10.0f));
186-
REQUIRE(h_viewOut[i].chi2() == Catch::Approx(11.0f));
187-
REQUIRE(h_viewOut[i].nLayers() == 12);
188-
REQUIRE(h_viewOut[i].eta() == Catch::Approx(13.0f));
189-
REQUIRE(h_viewOut[i].pt() == Catch::Approx(14.0f));
190-
REQUIRE(h_viewOut[i].state() == Vector5f(15.f, 15.f, 15.f, 15.f, 15.f));
191-
REQUIRE(h_viewOut[i].covariance() ==
192-
Vector15f(16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f, 16.f));
190+
REQUIRE_THAT(h_viewOut[i].quality(), WithinRel(h_view2[i - nTk1].quality()));
191+
REQUIRE_THAT(h_viewOut[i].chi2(), WithinRel(h_view2[i - nTk1].chi2()));
192+
REQUIRE(h_viewOut[i].nLayers() == h_view2[i - nTk1].nLayers());
193+
REQUIRE_THAT(h_viewOut[i].eta(), WithinRel(h_view2[i - nTk1].eta()));
194+
REQUIRE_THAT(h_viewOut[i].pt(), WithinRel(h_view2[i - nTk1].pt()));
195+
REQUIRE(h_viewOut[i].state().isApprox(h_view2[i - nTk1].state()));
196+
REQUIRE(h_viewOut[i].covariance().isApprox(h_view2[i - nTk1].covariance()));
197+
REQUIRE(h_viewOut[i].matrix().isApprox(h_view2[i - nTk1].matrix()));
193198
}
194199
}
195200
}

0 commit comments

Comments
 (0)