Skip to content

Commit 87d00d7

Browse files
authored
Merge pull request #48133 from fwyzard/better_range_check
Improve SoA out-of-bounds error message
2 parents 85d5c49 + d702b96 commit 87d00d7

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<use name="boost_header"/>
2+
<use name="fmt"/>
23
<use name="FWCore/Reflection" source_only="1"/>
34
<use name="FWCore/Utilities" source_only="1"/>

DataFormats/SoATemplate/interface/SoACommon.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
#include <cstring>
1111
#include <memory>
1212
#include <ostream>
13+
#include <sstream>
1314
#include <tuple>
1415
#include <type_traits>
1516

1617
#include <boost/preprocessor.hpp>
1718

19+
#include <fmt/format.h>
20+
1821
#include "FWCore/Utilities/interface/typedefs.h"
1922

2023
// CUDA attributes
@@ -32,20 +35,20 @@
3235

3336
// Exception throwing (or willful crash in kernels)
3437
#if defined(__CUDACC__) && defined(__CUDA_ARCH__)
35-
#define SOA_THROW_OUT_OF_RANGE(A) \
36-
{ \
37-
printf("%s\n", (A)); \
38-
__trap(); \
38+
#define SOA_THROW_OUT_OF_RANGE(A, I, R) \
39+
{ \
40+
printf("%s: index %d out of range %d\n", (A), (I), (R)); \
41+
__trap(); \
3942
}
4043
#elif defined(__HIPCC__) && defined(__HIP_DEVICE_COMPILE__)
41-
#define SOA_THROW_OUT_OF_RANGE(A) \
42-
{ \
43-
printf("%s\n", (A)); \
44-
abort(); \
44+
#define SOA_THROW_OUT_OF_RANGE(A, I, R) \
45+
{ \
46+
printf("%s: index %d out of range %d\n", (A), (I), (R)); \
47+
abort(); \
4548
}
4649
#else
47-
#define SOA_THROW_OUT_OF_RANGE(A) \
48-
{ throw std::out_of_range(A); }
50+
#define SOA_THROW_OUT_OF_RANGE(A, I, R) \
51+
{ throw std::out_of_range(fmt::format("{}: index {} out of range {}", (A), (I), (R))); }
4952
#endif
5053

5154
/* declare "scalars" (one value shared across the whole SoA) and "columns" (one value per element) */

DataFormats/SoATemplate/interface/SoAView.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ namespace cms::soa {
400400
LOCAL_NAME(size_type _soa_impl_index) { \
401401
if constexpr (rangeChecking == cms::soa::RangeChecking::enabled) { \
402402
if (_soa_impl_index >= base_type::elements_ or _soa_impl_index < 0) \
403-
SOA_THROW_OUT_OF_RANGE("Out of range index in mutable " #LOCAL_NAME "(size_type index)") \
403+
SOA_THROW_OUT_OF_RANGE("Out of range index in mutable " #LOCAL_NAME "(size_type index)", \
404+
_soa_impl_index, base_type::elements_) \
404405
} \
405406
return typename cms::soa::SoAAccessors<typename BOOST_PP_CAT(Metadata::TypeOf_, LOCAL_NAME)>:: \
406407
template ColumnType<BOOST_PP_CAT(Metadata::ColumnTypeOf_, LOCAL_NAME)>::template AccessType< \
@@ -438,7 +439,8 @@ namespace cms::soa {
438439
LOCAL_NAME(size_type _soa_impl_index) const { \
439440
if constexpr (rangeChecking == cms::soa::RangeChecking::enabled) { \
440441
if (_soa_impl_index >= elements_ or _soa_impl_index < 0) \
441-
SOA_THROW_OUT_OF_RANGE("Out of range index in const " #LOCAL_NAME "(size_type index)") \
442+
SOA_THROW_OUT_OF_RANGE("Out of range index in const " #LOCAL_NAME "(size_type index)", \
443+
_soa_impl_index, elements_) \
442444
} \
443445
return typename cms::soa::SoAAccessors<typename BOOST_PP_CAT(Metadata::TypeOf_, LOCAL_NAME)>:: \
444446
template ColumnType<BOOST_PP_CAT(Metadata::ColumnTypeOf_, LOCAL_NAME)>::template AccessType< \
@@ -786,7 +788,7 @@ namespace cms::soa {
786788
element operator[](size_type _soa_impl_index) { \
787789
if constexpr (rangeChecking == cms::soa::RangeChecking::enabled) { \
788790
if (_soa_impl_index >= base_type::elements_ or _soa_impl_index < 0) \
789-
SOA_THROW_OUT_OF_RANGE("Out of range index in " #VIEW "::operator[]") \
791+
SOA_THROW_OUT_OF_RANGE("Out of range index in " #VIEW "::operator[]", _soa_impl_index, base_type::elements_) \
790792
} \
791793
return element{_soa_impl_index, _ITERATE_ON_ALL_COMMA(_DECLARE_VIEW_ELEMENT_CONSTR_CALL, ~, VALUE_LIST)}; \
792794
} \
@@ -971,7 +973,7 @@ namespace cms::soa {
971973
const_element operator[](size_type _soa_impl_index) const { \
972974
if constexpr (rangeChecking == cms::soa::RangeChecking::enabled) { \
973975
if (_soa_impl_index >= elements_ or _soa_impl_index < 0) \
974-
SOA_THROW_OUT_OF_RANGE("Out of range index in " #CONST_VIEW "::operator[]") \
976+
SOA_THROW_OUT_OF_RANGE("Out of range index in " #CONST_VIEW "::operator[]", _soa_impl_index, elements_) \
975977
} \
976978
return const_element{ \
977979
_soa_impl_index, _ITERATE_ON_ALL_COMMA(_DECLARE_VIEW_CONST_ELEMENT_CONSTR_CALL, ~, VALUE_LIST) \

DataFormats/SoATemplate/test/BuildFile.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<use name="boost"/>
44
<use name="cuda"/>
55
<use name="eigen"/>
6+
<use name="DataFormats/SoATemplate"/>
67
<use name="HeterogeneousCore/CUDAUtilities"/>
78
</bin>
89
</iftool>
@@ -12,29 +13,34 @@
1213
<use name="boost"/>
1314
<use name="eigen"/>
1415
<use name="rocm"/>
16+
<use name="DataFormats/SoATemplate"/>
1517
<use name="HeterogeneousCore/ROCmUtilities"/>
1618
</bin>
1719
</iftool>
1820

1921
<bin file="SoAUnitTests.cc" name="SoAUnitTests">
2022
<use name="boost"/>
2123
<use name="catch2"/>
24+
<use name="DataFormats/SoATemplate"/>
2225
</bin>
2326

2427
<bin file="SoAGenericView_t.cc" name="SoAGenericView_t">
2528
<use name="boost"/>
2629
<use name="catch2"/>
2730
<use name="eigen"/>
31+
<use name="DataFormats/SoATemplate"/>
2832
</bin>
2933

3034
<bin file="SoACustomizedMethods_t.cc" name="SoACustomizedMethods_t">
3135
<use name="boost"/>
3236
<use name="catch2"/>
37+
<use name="DataFormats/SoATemplate"/>
3338
</bin>
3439

3540
<!-- This test triggers a bug in ROOT, so it's kept disabled
3641
<bin file="SoAStreamer_t.cpp" name="SoAStreamer_t">
3742
<use name="root"/>
43+
<use name="DataFormats/SoATemplate"/>
3844
</bin>
3945
-->
4046

0 commit comments

Comments
 (0)