Skip to content

[SYCL] Optimize 1D accessor. #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions sycl/include/CL/sycl/accessor2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,16 @@ class accessor :
using PtrType = typename detail::PtrValueType<DataT, AS>::type *;

template <int Dims = Dimensions> size_t getLinearIndex(id<Dims> Id) const {

#ifdef __SYCL_DEVICE_ONLY__
// Pointer is already adjusted for 1D case.
if (Dimensions == 1)
return Id[0];
#endif // __SYCL_DEVICE_ONLY__

size_t Result = 0;
for (int I = 0; I < Dims; ++I)
Result = Result * getOrigRange()[I] + get_offset()[I] + Id[I];
Result = Result * getOrigRange()[I] + getOffset()[I] + Id[I];
return Result;
}

Expand All @@ -301,10 +308,12 @@ class accessor :
getRange()[I] = AccessRange[I];
getOrigRange()[I] = MemRange[I];
}
// In case of 1D buffer, adjust pointer during initialization rather
// then each time in operator[] or get_pointer functions.
if (1 == AdjustedDim)
MData += Offset[0];
}

void *getPtr() { return MData; }

PtrType getQualifiedPtr() const { return MData; }
#else

Expand Down Expand Up @@ -466,7 +475,8 @@ class accessor :
template <int Dims = Dimensions,
typename = enable_if_t<IsAccessAnyWrite && Dims == 0>>
operator RefType() const {
return *(getQualifiedPtr() + get_offset()[0]);
const size_t LinearIndex = getLinearIndex(id<Dimensions>());
return *(getQualifiedPtr() + LinearIndex);
}

template <int Dims = Dimensions,
Expand All @@ -479,13 +489,15 @@ class accessor :
template <int Dims = Dimensions,
typename = enable_if_t<IsAccessAnyWrite && Dims == 1>>
RefType operator[](size_t Index) const {
return getQualifiedPtr()[Index + get_offset()[0]];
const size_t LinearIndex = getLinearIndex(id<Dimensions>(Index));
return getQualifiedPtr()[LinearIndex];
}

template <int Dims = Dimensions,
typename = enable_if_t<IsAccessReadOnly && Dims == 0>>
operator DataT() const {
return *(getQualifiedPtr() + get_offset()[0]);
const size_t LinearIndex = getLinearIndex(id<AdjustedDim>());
return *(getQualifiedPtr() + LinearIndex);
}

template <int Dims = Dimensions,
Expand All @@ -498,14 +510,17 @@ class accessor :
template <int Dims = Dimensions,
typename = enable_if_t<IsAccessReadOnly && Dims == 1>>
DataT operator[](size_t Index) const {
return getQualifiedPtr()[Index + get_offset()[0]];
const size_t LinearIndex = getLinearIndex(id<Dimensions>(Index));
return getQualifiedPtr()[LinearIndex];
}

template <
int Dims = Dimensions,
typename = enable_if_t<AccessMode == access::mode::atomic && Dims == 0>>
operator atomic<DataT, AS>() const {
return atomic<DataT, AS>(multi_ptr<DataT, AS>(getQualifiedPtr()));
const size_t LinearIndex = getLinearIndex(id<AdjustedDim>());
return atomic<DataT, AS>(
multi_ptr<DataT, AS>(getQualifiedPtr() + LinearIndex));
}

template <
Expand All @@ -521,14 +536,15 @@ class accessor :
int Dims = Dimensions,
typename = enable_if_t<AccessMode == access::mode::atomic && Dims == 1>>
atomic<DataT, AS> operator[](size_t Index) const {
const size_t LinearIndex = getLinearIndex(id<AdjustedDim>(Index));
return atomic<DataT, AS>(
multi_ptr<DataT, AS>(getQualifiedPtr() + Index + get_offset()[0]));
multi_ptr<DataT, AS>(getQualifiedPtr() + LinearIndex));
}

template <int Dims = Dimensions, typename = enable_if_t<(Dims > 1)>>
typename AccessorCommonT::template AccessorSubscript<Dims - 1>
operator[](size_t Index) const {
return AccessorSubscript<Dims - 1>(*this, Index + get_offset()[0]);
return AccessorSubscript<Dims - 1>(*this, Index);
}

template <
Expand Down