Skip to content

Commit d9e660d

Browse files
authored
Merge pull request cms-sw#43281 from ericcano/SoA_Fixups
SoA fixes (2nd round): fix and add broken / missing SoA (const) row-to-row assignment.
2 parents b867726 + b681fb3 commit d9e660d

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

DataFormats/SoATemplate/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ The buffer of the proper size is allocated, and the layout is populated with:
135135
// Allocation of aligned
136136
size_t elements = 100;
137137
using AlignedBuffer = std::unique_ptr<std::byte, decltype(std::free) *>;
138-
AlignedBuffer h_buf (reinterpret_cast<std::byte*>(aligned_alloc(SoA1LayoutAligned::byteAlignment, SoA1LayoutAligned::computeDataSize(elements))), std::free);
138+
AlignedBuffer h_buf (reinterpret_cast<std::byte*>(aligned_alloc(SoA1LayoutAligned::alignment, SoA1LayoutAligned::computeDataSize(elements))), std::free);
139139
SoA1LayoutAligned soaLayout(h_buf.get(), elements);
140140
```
141141

DataFormats/SoATemplate/interface/SoACommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ namespace cms::soa {
299299

300300
SOA_HOST_DEVICE SOA_INLINE RefToConst operator()() const {
301301
// PtrToConst type will add the restrict qualifyer if needed
302-
PtrToConst col = col_();
302+
PtrToConst col = col_;
303303
return col[idx_];
304304
}
305305

DataFormats/SoATemplate/interface/SoAView.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,11 @@ namespace cms::soa {
604604
element& operator=(const element& _soa_impl_other) { \
605605
_ITERATE_ON_ALL(_DECLARE_VIEW_ELEMENT_VALUE_COPY, ~, VALUE_LIST) \
606606
return *this; \
607+
} \
608+
SOA_HOST_DEVICE SOA_INLINE \
609+
element& operator=(const const_element& _soa_impl_other) { \
610+
_ITERATE_ON_ALL(_DECLARE_VIEW_ELEMENT_VALUE_COPY, ~, VALUE_LIST) \
611+
return *this; \
607612
}
608613
// clang-format on
609614

DataFormats/SoATemplate/test/BuildFile.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
</bin>
1717
</iftool>
1818

19+
<bin file="SoAUnitTests.cc" name="SoAUnitTests">
20+
<use name="boost"/>
21+
<use name="catch2"/>
22+
</bin>
23+
24+
1925
<!-- This test triggers a bug in ROOT, so it's kept disabled
2026
<bin file="SoAStreamer_t.cpp" name="SoAStreamer_t">
2127
<use name="root"/>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <memory>
2+
#include <tuple>
3+
4+
#define CATCH_CONFIG_MAIN
5+
#include "catch.hpp"
6+
#include "DataFormats/SoATemplate/interface/SoALayout.h"
7+
8+
// clang-format off
9+
GENERATE_SOA_LAYOUT(SimpleLayoutTemplate,
10+
SOA_COLUMN(float, x),
11+
SOA_COLUMN(float, y),
12+
SOA_COLUMN(float, z),
13+
SOA_COLUMN(float, t))
14+
// clang-format on
15+
16+
using SimpleLayout = SimpleLayoutTemplate<>;
17+
18+
TEST_CASE("SoATemplate") {
19+
const std::size_t slSize = 10;
20+
const std::size_t slBufferSize = SimpleLayout::computeDataSize(slSize);
21+
std::unique_ptr<std::byte, decltype(std::free) *> slBuffer{
22+
reinterpret_cast<std::byte *>(aligned_alloc(SimpleLayout::alignment, slBufferSize)), std::free};
23+
SimpleLayout sl{slBuffer.get(), slSize};
24+
SECTION("Row wide copies, row") {
25+
SimpleLayout::View slv{sl};
26+
SimpleLayout::ConstView slcv{sl};
27+
auto slv0 = slv[0];
28+
slv0.x() = 1;
29+
slv0.y() = 2;
30+
slv0.z() = 3;
31+
slv0.t() = 5;
32+
// Fill up
33+
for (SimpleLayout::View::size_type i = 1; i < slv.metadata().size(); ++i) {
34+
auto slvi = slv[i];
35+
slvi = slv[i - 1];
36+
auto slvix = slvi.x();
37+
slvi.x() += slvi.y();
38+
slvi.y() += slvi.z();
39+
slvi.z() += slvi.t();
40+
slvi.t() += slvix;
41+
}
42+
// Verification and const view access
43+
float x = 1, y = 2, z = 3, t = 5;
44+
for (SimpleLayout::View::size_type i = 0; i < slv.metadata().size(); ++i) {
45+
auto slvi = slv[i];
46+
auto slcvi = slcv[i];
47+
REQUIRE(slvi.x() == x);
48+
REQUIRE(slvi.y() == y);
49+
REQUIRE(slvi.z() == z);
50+
REQUIRE(slvi.t() == t);
51+
REQUIRE(slcvi.x() == x);
52+
REQUIRE(slcvi.y() == y);
53+
REQUIRE(slcvi.z() == z);
54+
REQUIRE(slcvi.t() == t);
55+
auto tx = x;
56+
x += y;
57+
y += z;
58+
z += t;
59+
t += tx;
60+
}
61+
}
62+
63+
SECTION("Row initializer, const view access, restrict disabling") {
64+
// With two views, we are creating (artificially) aliasing and should warn the compiler by turning restrict qualifiers off.
65+
using View = SimpleLayout::ViewTemplate<cms::soa::RestrictQualify::disabled, cms::soa::RangeChecking::Default>;
66+
using ConstView =
67+
SimpleLayout::ConstViewTemplate<cms::soa::RestrictQualify::disabled, cms::soa::RangeChecking::Default>;
68+
View slv{sl};
69+
ConstView slcv{sl};
70+
auto slv0 = slv[0];
71+
slv0 = {7, 11, 13, 17};
72+
// Fill up
73+
for (SimpleLayout::View::size_type i = 1; i < slv.metadata().size(); ++i) {
74+
auto slvi = slv[i];
75+
// Copy the const view
76+
slvi = slcv[i - 1];
77+
auto slvix = slvi.x();
78+
slvi.x() += slvi.y();
79+
slvi.y() += slvi.z();
80+
slvi.z() += slvi.t();
81+
slvi.t() += slvix;
82+
}
83+
// Verification and const view access
84+
auto [x, y, z, t] = std::make_tuple(7.0, 11.0, 13.0, 17.0);
85+
for (SimpleLayout::View::size_type i = 0; i < slv.metadata().size(); ++i) {
86+
auto slvi = slv[i];
87+
auto slcvi = slcv[i];
88+
REQUIRE(slvi.x() == x);
89+
REQUIRE(slvi.y() == y);
90+
REQUIRE(slvi.z() == z);
91+
REQUIRE(slvi.t() == t);
92+
REQUIRE(slcvi.x() == x);
93+
REQUIRE(slcvi.y() == y);
94+
REQUIRE(slcvi.z() == z);
95+
REQUIRE(slcvi.t() == t);
96+
auto tx = x;
97+
x += y;
98+
y += z;
99+
z += t;
100+
t += tx;
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)