Skip to content

Commit

Permalink
[SYCL] Change read only accessor return const reference (#2142)
Browse files Browse the repository at this point in the history
This should improve performance in some cases.
Also fixed memcpy-in-vec-as.cpp as the patch reveals problems
in the test.
  • Loading branch information
romanovvlad authored Jul 21, 2020
1 parent c7f397a commit 03db009
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
10 changes: 6 additions & 4 deletions sycl/include/CL/sycl/accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ class accessor_common {
AccessMode == access::mode::read_write;

using RefType = detail::const_if_const_AS<AS, DataT> &;
using ConstRefType = const DataT &;
using PtrType = detail::const_if_const_AS<AS, DataT> *;

using AccType =
Expand Down Expand Up @@ -299,7 +300,7 @@ class accessor_common {

template <int CurDims = SubDims,
typename = detail::enable_if_t<CurDims == 1 && IsAccessReadOnly>>
DataT operator[](size_t Index) const {
ConstRefType operator[](size_t Index) const {
MIDs[Dims - SubDims] = Index;
return MAccessor[MIDs];
}
Expand Down Expand Up @@ -767,6 +768,7 @@ class accessor :
using ConcreteASPtrType = typename detail::PtrValueType<DataT, AS>::type *;

using RefType = detail::const_if_const_AS<AS, DataT> &;
using ConstRefType = const DataT &;
using PtrType = detail::const_if_const_AS<AS, DataT> *;

template <int Dims = Dimensions> size_t getLinearIndex(id<Dims> Id) const {
Expand Down Expand Up @@ -1199,9 +1201,9 @@ class accessor :
return *(getQualifiedPtr() + LinearIndex);
}

template <int Dims = Dimensions,
typename = detail::enable_if_t<(Dims > 0) && IsAccessReadOnly>>
DataT operator[](id<Dimensions> Index) const {
template <int Dims = Dimensions>
typename detail::enable_if_t<(Dims > 0) && IsAccessReadOnly, ConstRefType>
operator[](id<Dimensions> Index) const {
const size_t LinearIndex = getLinearIndex(Index);
return getQualifiedPtr()[LinearIndex];
}
Expand Down
4 changes: 2 additions & 2 deletions sycl/test/basic_tests/accessor/addrspace_exposure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ int main() {
Cgh.single_task<class test>([=]() {
static_assert(std::is_same<decltype(GlobalRWAcc[0]), int &>::value,
"Incorrect type from global read-write accessor");
static_assert(std::is_same<decltype(GlobalRAcc[0]), int>::value,
static_assert(std::is_same<decltype(GlobalRAcc[0]), const int &>::value,
"Incorrect type from global read accessor");
static_assert(std::is_same<decltype(ConstantAcc[0]), int>::value,
static_assert(std::is_same<decltype(ConstantAcc[0]), const int &>::value,
"Incorrect type from constant accessor");
static_assert(std::is_same<decltype(LocalAcc[0]), int &>::value,
"Incorrect type from local accessor");
Expand Down
12 changes: 8 additions & 4 deletions sycl/test/regression/memcpy-in-vec-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ int main() {
cl::sycl::range<1>(1));
cl::sycl::queue Queue;
Queue.submit([&](cl::sycl::handler &cgh) {
auto In = InBuf.get_access<cl::sycl::access::mode::write>(cgh);
auto Out = OutBuf.get_access<cl::sycl::access::mode::read>(cgh);
auto In = InBuf.get_access<cl::sycl::access::mode::read>(cgh);
auto Out = OutBuf.get_access<cl::sycl::access::mode::write>(cgh);
cgh.single_task<class as_op>(
[=]() { Out[0] = In[0].as<res_vec_type>(); });
});
}
std::cout << res.s0() << " " << res.s1() << " " << res.s2() << " " << res.s3()
<< std::endl;

if (res.s0() != 513 || res.s1() != 1027 || res.s2() != 1541 || res.s3() != 2055) {
std::cerr << "Incorrect result" << std::endl;
return 1;
}

return 0;
}

0 comments on commit 03db009

Please sign in to comment.