Skip to content

Commit

Permalink
Merge pull request #44988 from fwyzard/enable_SoA_range_checking_141x
Browse files Browse the repository at this point in the history
Enable SoA range checking by default
  • Loading branch information
cmsbuild authored Jun 5, 2024
2 parents 0666285 + e8ae2ea commit aaa1cea
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
4 changes: 2 additions & 2 deletions DataFormats/SoATemplate/interface/SoACommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ namespace cms::soa {
namespace RestrictQualify {
constexpr bool enabled = true;
constexpr bool disabled = false;
constexpr bool Default = disabled;
constexpr bool Default = enabled;
} // namespace RestrictQualify

namespace RangeChecking {
constexpr bool enabled = true;
constexpr bool disabled = false;
constexpr bool Default = disabled;
constexpr bool Default = enabled;
} // namespace RangeChecking

template <typename T, bool RESTRICT_QUALIFY>
Expand Down
8 changes: 4 additions & 4 deletions DataFormats/SoATemplate/interface/SoALayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@
\
template <CMS_SOA_BYTE_SIZE_TYPE VIEW_ALIGNMENT = cms::soa::CacheLineSize::defaultSize, \
bool VIEW_ALIGNMENT_ENFORCEMENT = cms::soa::AlignmentEnforcement::relaxed, \
bool RESTRICT_QUALIFY = cms::soa::RestrictQualify::enabled, \
bool RANGE_CHECKING = cms::soa::RangeChecking::disabled> \
bool RESTRICT_QUALIFY = cms::soa::RestrictQualify::Default, \
bool RANGE_CHECKING = cms::soa::RangeChecking::Default> \
struct ViewTemplateFreeParams; \
\
/* dump the SoA internal structure */ \
Expand Down Expand Up @@ -539,7 +539,7 @@
using ConstViewTemplate = ConstViewTemplateFreeParams<ALIGNMENT, ALIGNMENT_ENFORCEMENT, RESTRICT_QUALIFY, \
RANGE_CHECKING>; \
\
using ConstView = ConstViewTemplate<cms::soa::RestrictQualify::enabled, cms::soa::RangeChecking::disabled>; \
using ConstView = ConstViewTemplate<cms::soa::RestrictQualify::Default, cms::soa::RangeChecking::Default>; \
\
/* Generate the mutable View template */ \
_GENERATE_SOA_TRIVIAL_VIEW(CLASS, \
Expand All @@ -552,7 +552,7 @@
template <bool RESTRICT_QUALIFY, bool RANGE_CHECKING> \
using ViewTemplate = ViewTemplateFreeParams<ALIGNMENT, ALIGNMENT_ENFORCEMENT, RESTRICT_QUALIFY, RANGE_CHECKING>; \
\
using View = ViewTemplate<cms::soa::RestrictQualify::enabled, cms::soa::RangeChecking::disabled>; \
using View = ViewTemplate<cms::soa::RestrictQualify::Default, cms::soa::RangeChecking::Default>; \
\
/* Trivial constuctor */ \
CLASS() \
Expand Down
8 changes: 4 additions & 4 deletions DataFormats/SoATemplate/interface/SoAView.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ namespace cms::soa {
#define _GENERATE_SOA_VIEW_PART_0(CONST_VIEW, VIEW, LAYOUTS_LIST, VALUE_LIST) \
template <CMS_SOA_BYTE_SIZE_TYPE VIEW_ALIGNMENT = cms::soa::CacheLineSize::defaultSize, \
bool VIEW_ALIGNMENT_ENFORCEMENT = cms::soa::AlignmentEnforcement::relaxed, \
bool RESTRICT_QUALIFY = cms::soa::RestrictQualify::enabled, \
bool RANGE_CHECKING = cms::soa::RangeChecking::disabled> \
bool RESTRICT_QUALIFY = cms::soa::RestrictQualify::Default, \
bool RANGE_CHECKING = cms::soa::RangeChecking::Default> \
struct VIEW : public CONST_VIEW<VIEW_ALIGNMENT, VIEW_ALIGNMENT_ENFORCEMENT, RESTRICT_QUALIFY, RANGE_CHECKING> { \
/* Declare the parametrized layouts as the default */ \
/*BOOST_PP_SEQ_CAT(_ITERATE_ON_ALL(_DECLARE_VIEW_LAYOUT_PARAMETRIZED_TEMPLATE, ~, LAYOUTS_LIST)) */ \
Expand Down Expand Up @@ -671,8 +671,8 @@ namespace cms::soa {
#define _GENERATE_SOA_CONST_VIEW_PART_0(CONST_VIEW, VIEW, LAYOUTS_LIST, VALUE_LIST) \
template <CMS_SOA_BYTE_SIZE_TYPE VIEW_ALIGNMENT = cms::soa::CacheLineSize::defaultSize, \
bool VIEW_ALIGNMENT_ENFORCEMENT = cms::soa::AlignmentEnforcement::relaxed, \
bool RESTRICT_QUALIFY = cms::soa::RestrictQualify::enabled, \
bool RANGE_CHECKING = cms::soa::RangeChecking::disabled> \
bool RESTRICT_QUALIFY = cms::soa::RestrictQualify::Default, \
bool RANGE_CHECKING = cms::soa::RangeChecking::Default> \
struct CONST_VIEW { \
/* these could be moved to an external type trait to free up the symbol names */ \
using self_type = CONST_VIEW;
Expand Down
37 changes: 36 additions & 1 deletion DataFormats/SoATemplate/test/SoAUnitTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <tuple>

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <catch.hpp>

#include "DataFormats/SoATemplate/interface/SoALayout.h"

// clang-format off
Expand All @@ -16,11 +17,16 @@ GENERATE_SOA_LAYOUT(SimpleLayoutTemplate,
using SimpleLayout = SimpleLayoutTemplate<>;

TEST_CASE("SoATemplate") {
// number of elements
const std::size_t slSize = 10;
// size in bytes
const std::size_t slBufferSize = SimpleLayout::computeDataSize(slSize);
// memory buffer aligned according to the layout requirements
std::unique_ptr<std::byte, decltype(std::free) *> slBuffer{
reinterpret_cast<std::byte *>(aligned_alloc(SimpleLayout::alignment, slBufferSize)), std::free};
// SoA layout
SimpleLayout sl{slBuffer.get(), slSize};

SECTION("Row wide copies, row") {
SimpleLayout::View slv{sl};
SimpleLayout::ConstView slcv{sl};
Expand Down Expand Up @@ -100,4 +106,33 @@ TEST_CASE("SoATemplate") {
t += tx;
}
}

SECTION("Range checking View") {
// Enable range checking
using View = SimpleLayout::ViewTemplate<cms::soa::RestrictQualify::Default, cms::soa::RangeChecking::enabled>;
View slv{sl};
int underflow = -1;
int overflow = slv.metadata().size();
// Check for under-and overflow in the row accessor
REQUIRE_THROWS_AS(slv[underflow], std::out_of_range);
REQUIRE_THROWS_AS(slv[overflow], std::out_of_range);
// Check for under-and overflow in the element accessors
REQUIRE_THROWS_AS(slv.x(underflow), std::out_of_range);
REQUIRE_THROWS_AS(slv.x(overflow), std::out_of_range);
}

SECTION("Range checking ConstView") {
// Enable range checking
using ConstView =
SimpleLayout::ConstViewTemplate<cms::soa::RestrictQualify::Default, cms::soa::RangeChecking::enabled>;
ConstView slcv{sl};
int underflow = -1;
int overflow = slcv.metadata().size();
// Check for under-and overflow in the row accessor
REQUIRE_THROWS_AS(slcv[underflow], std::out_of_range);
REQUIRE_THROWS_AS(slcv[overflow], std::out_of_range);
// Check for under-and overflow in the element accessors
REQUIRE_THROWS_AS(slcv.x(underflow), std::out_of_range);
REQUIRE_THROWS_AS(slcv.x(overflow), std::out_of_range);
}
}

0 comments on commit aaa1cea

Please sign in to comment.