Skip to content

Commit c19262a

Browse files
Use template to guarantee loop unrolling
1 parent de52645 commit c19262a

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

sycl/include/sycl/accessor.hpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#include <type_traits>
3333

34+
#include <utility>
35+
3436
/// \file accessor.hpp
3537
/// The file contains implementations of accessor class.
3638
///
@@ -224,6 +226,20 @@ template <typename DataT, int Dimensions = 1,
224226
class accessor;
225227

226228
namespace detail {
229+
// To ensure loop unrolling is done when processing dimensions.
230+
template <size_t... Inds, class F>
231+
void dim_loop_impl(std::integer_sequence<size_t, Inds...>, F &&f) {
232+
#if __cplusplus >= 201703L
233+
(f(Inds), ...);
234+
#else
235+
(void)std::initializer_list<int>{((void)(f(Inds)), 0)...};
236+
#endif
237+
}
238+
239+
template <size_t count, class F> void dim_loop(F &&f) {
240+
dim_loop_impl(std::make_index_sequence<count>{}, std::forward<F>(f));
241+
}
242+
227243
void __SYCL_EXPORT constructorNotification(void *BufferObj, void *AccessorObj,
228244
access::target Target,
229245
access::mode Mode,
@@ -833,9 +849,7 @@ class __SYCL_SPECIAL_CLASS accessor :
833849
template <int Dims = Dimensions> size_t getLinearIndex(id<Dims> Id) const {
834850

835851
size_t Result = 0;
836-
// Unroll the following loop for both host and device code
837-
__SYCL_UNROLL(3)
838-
for (int I = 0; I < Dims; ++I) {
852+
detail::dim_loop<Dims>([&, this](size_t I) {
839853
Result = Result * getMemoryRange()[I] + Id[I];
840854
// We've already adjusted for the accessor's offset in the __init, so
841855
// don't include it here in case of device.
@@ -849,7 +863,8 @@ class __SYCL_SPECIAL_CLASS accessor :
849863
Result += getOffset()[I];
850864
#endif
851865
#endif // __SYCL_DEVICE_ONLY__
852-
}
866+
});
867+
853868
return Result;
854869
}
855870

@@ -1791,8 +1806,7 @@ class __SYCL_SPECIAL_CLASS accessor :
17911806
#ifdef __SYCL_DEVICE_ONLY__
17921807
size_t getTotalOffset() const {
17931808
size_t TotalOffset = 0;
1794-
__SYCL_UNROLL(3)
1795-
for (int I = 0; I < Dimensions; ++I) {
1809+
detail::dim_loop<Dimensions>([&, this](size_t I) {
17961810
TotalOffset = TotalOffset * impl.MemRange[I];
17971811
#if __cplusplus >= 201703L
17981812
if constexpr (!(PropertyListT::template has_property<
@@ -1802,7 +1816,7 @@ class __SYCL_SPECIAL_CLASS accessor :
18021816
#else
18031817
TotalOffset += impl.Offset[I];
18041818
#endif
1805-
}
1819+
});
18061820

18071821
return TotalOffset;
18081822
}

0 commit comments

Comments
 (0)