31
31
32
32
#include < type_traits>
33
33
34
+ #include < utility>
35
+
34
36
// / \file accessor.hpp
35
37
// / The file contains implementations of accessor class.
36
38
// /
@@ -224,6 +226,20 @@ template <typename DataT, int Dimensions = 1,
224
226
class accessor ;
225
227
226
228
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
+
227
243
void __SYCL_EXPORT constructorNotification (void *BufferObj, void *AccessorObj,
228
244
access::target Target,
229
245
access::mode Mode,
@@ -833,9 +849,7 @@ class __SYCL_SPECIAL_CLASS accessor :
833
849
template <int Dims = Dimensions> size_t getLinearIndex (id<Dims> Id) const {
834
850
835
851
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) {
839
853
Result = Result * getMemoryRange ()[I] + Id[I];
840
854
// We've already adjusted for the accessor's offset in the __init, so
841
855
// don't include it here in case of device.
@@ -849,7 +863,8 @@ class __SYCL_SPECIAL_CLASS accessor :
849
863
Result += getOffset ()[I];
850
864
#endif
851
865
#endif // __SYCL_DEVICE_ONLY__
852
- }
866
+ });
867
+
853
868
return Result;
854
869
}
855
870
@@ -1791,8 +1806,7 @@ class __SYCL_SPECIAL_CLASS accessor :
1791
1806
#ifdef __SYCL_DEVICE_ONLY__
1792
1807
size_t getTotalOffset () const {
1793
1808
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) {
1796
1810
TotalOffset = TotalOffset * impl.MemRange [I];
1797
1811
#if __cplusplus >= 201703L
1798
1812
if constexpr (!(PropertyListT::template has_property<
@@ -1802,7 +1816,7 @@ class __SYCL_SPECIAL_CLASS accessor :
1802
1816
#else
1803
1817
TotalOffset += impl.Offset [I];
1804
1818
#endif
1805
- }
1819
+ });
1806
1820
1807
1821
return TotalOffset;
1808
1822
}
0 commit comments