DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
accessor_impl.hpp
Go to the documentation of this file.
1 //==------------ accessor_impl.hpp - SYCL standard header file -------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
14 #include <CL/sycl/id.hpp>
15 #include <CL/sycl/range.hpp>
16 #include <CL/sycl/stl.hpp>
17 
19 namespace sycl {
20 
21 namespace ext {
22 namespace intel {
23 namespace experimental {
24 namespace esimd {
25 namespace detail {
26 // Forward declare a "back-door" access class to support ESIMD.
27 class AccessorPrivateProxy;
28 } // namespace detail
29 } // namespace esimd
30 } // namespace experimental
31 } // namespace intel
32 } // namespace ext
33 
34 namespace detail {
35 
36 class Command;
37 
38 // The class describes a requirement to access a SYCL memory object such as
39 // sycl::buffer and sycl::image. For example, each accessor used in a kernel,
40 // except one with access target "local", adds such requirement for the command
41 // group.
42 
43 template <int Dims> class AccessorImplDevice {
44 public:
45  AccessorImplDevice() = default;
47  range<Dims> MemoryRange)
48  : Offset(Offset), AccessRange(AccessRange), MemRange(MemoryRange) {}
49 
53 
54  bool operator==(const AccessorImplDevice &Rhs) const {
55  return (Offset == Rhs.Offset && AccessRange == Rhs.AccessRange &&
56  MemRange == Rhs.MemRange);
57  }
58 };
59 
60 template <int Dims> class LocalAccessorBaseDevice {
61 public:
63  : AccessRange(Size),
64  MemRange(InitializedVal<Dims, range>::template get<0>()) {}
65  // TODO: Actually we need only one field here, but currently compiler requires
66  // all of them.
70 
71  bool operator==(const LocalAccessorBaseDevice &Rhs) const {
72  return (AccessRange == Rhs.AccessRange);
73  }
74 };
75 
76 class __SYCL_EXPORT AccessorImplHost {
77 public:
78  AccessorImplHost(id<3> Offset, range<3> AccessRange, range<3> MemoryRange,
79  access::mode AccessMode, detail::SYCLMemObjI *SYCLMemObject,
80  int Dims, int ElemSize, int OffsetInBytes = 0,
81  bool IsSubBuffer = false, bool IsESIMDAcc = false)
82  : MOffset(Offset), MAccessRange(AccessRange), MMemoryRange(MemoryRange),
83  MAccessMode(AccessMode), MSYCLMemObj(SYCLMemObject), MDims(Dims),
84  MElemSize(ElemSize), MOffsetInBytes(OffsetInBytes),
85  MIsSubBuffer(IsSubBuffer), MIsESIMDAcc(IsESIMDAcc) {}
86 
88 
90  : MOffset(Other.MOffset), MAccessRange(Other.MAccessRange),
91  MMemoryRange(Other.MMemoryRange), MAccessMode(Other.MAccessMode),
92  MSYCLMemObj(Other.MSYCLMemObj), MDims(Other.MDims),
93  MElemSize(Other.MElemSize), MOffsetInBytes(Other.MOffsetInBytes),
94  MIsSubBuffer(Other.MIsSubBuffer), MIsESIMDAcc(Other.MIsESIMDAcc) {}
95 
96  // The resize method provides a way to change the size of the
97  // allocated memory and corresponding properties for the accessor.
98  // These are normally fixed for the accessor, but this capability
99  // is needed to support the stream class.
100  // Stream implementation creates an accessor with initial size for
101  // work item. But the number of work items is not available during
102  // stream construction. The resize method allows to update the accessor
103  // as the information becomes available to the handler.
104 
105  void resize(size_t GlobalSize);
106 
108  // The size of accessing region.
110  // The size of memory object this requirement is created for.
113 
115 
116  unsigned int MDims;
117  unsigned int MElemSize;
118  unsigned int MOffsetInBytes;
120 
121  void *MData = nullptr;
122 
123  Command *MBlockedCmd = nullptr;
124 
125  bool PerWI = false;
126 
127  // Outdated, leaving to preserve ABI.
128  // TODO: Remove during next major release.
130 };
131 
132 using AccessorImplPtr = std::shared_ptr<AccessorImplHost>;
133 
135 public:
136  AccessorBaseHost(id<3> Offset, range<3> AccessRange, range<3> MemoryRange,
137  access::mode AccessMode, detail::SYCLMemObjI *SYCLMemObject,
138  int Dims, int ElemSize, int OffsetInBytes = 0,
139  bool IsSubBuffer = false) {
140  impl = std::shared_ptr<AccessorImplHost>(new AccessorImplHost(
141  Offset, AccessRange, MemoryRange, AccessMode, SYCLMemObject, Dims,
142  ElemSize, OffsetInBytes, IsSubBuffer));
143  }
144 
145 protected:
146  id<3> &getOffset() { return impl->MOffset; }
147  range<3> &getAccessRange() { return impl->MAccessRange; }
148  range<3> &getMemoryRange() { return impl->MMemoryRange; }
149  void *getPtr() { return impl->MData; }
150  unsigned int getElemSize() const { return impl->MElemSize; }
151 
152  const id<3> &getOffset() const { return impl->MOffset; }
153  const range<3> &getAccessRange() const { return impl->MAccessRange; }
154  const range<3> &getMemoryRange() const { return impl->MMemoryRange; }
155  void *getPtr() const { return const_cast<void *>(impl->MData); }
156 
157  template <class Obj>
158  friend decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject);
159 
160  template <typename, int, access::mode, access::target, access::placeholder,
161  typename>
162  friend class accessor;
163 
165 
166 private:
167  friend class sycl::ext::intel::experimental::esimd::detail::
168  AccessorPrivateProxy;
169 };
170 
171 class __SYCL_EXPORT LocalAccessorImplHost {
172 public:
173  LocalAccessorImplHost(sycl::range<3> Size, int Dims, int ElemSize)
174  : MSize(Size), MDims(Dims), MElemSize(ElemSize),
175  MMem(Size[0] * Size[1] * Size[2] * ElemSize) {}
176 
178  int MDims;
180  std::vector<char> MMem;
181 };
182 
183 using LocalAccessorImplPtr = std::shared_ptr<LocalAccessorImplHost>;
184 
186 public:
187  LocalAccessorBaseHost(sycl::range<3> Size, int Dims, int ElemSize) {
188  impl = std::shared_ptr<LocalAccessorImplHost>(
189  new LocalAccessorImplHost(Size, Dims, ElemSize));
190  }
191  sycl::range<3> &getSize() { return impl->MSize; }
192  const sycl::range<3> &getSize() const { return impl->MSize; }
193  void *getPtr() { return impl->MMem.data(); }
194  void *getPtr() const {
195  return const_cast<void *>(reinterpret_cast<void *>(impl->MMem.data()));
196  }
197 
198  int getNumOfDims() { return impl->MDims; }
199  int getElementSize() { return impl->MElemSize; }
200 
201 protected:
202  template <class Obj>
203  friend decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject);
204 
205  std::shared_ptr<LocalAccessorImplHost> impl;
206 };
207 
209 
210 void __SYCL_EXPORT addHostAccessorAndWait(Requirement *Req);
211 
212 #if __cplusplus > 201402L
213 
214 template <typename MayBeTag1, typename MayBeTag2>
215 constexpr access::mode deduceAccessMode() {
216  // property_list = {} is not properly detected by deduction guide,
217  // when parameter is passed without curly braces: access(buffer, no_init)
218  // thus simplest approach is to check 2 last arguments for being a tag
219  if constexpr (std::is_same<MayBeTag1,
221  std::is_same<MayBeTag2,
223  return access::mode::read;
224  }
225 
226  if constexpr (std::is_same<MayBeTag1,
228  std::is_same<MayBeTag2,
230  return access::mode::write;
231  }
232 
233  if constexpr (
234  std::is_same<MayBeTag1,
235  mode_target_tag_t<access::mode::read,
236  access::target::constant_buffer>>::value ||
237  std::is_same<MayBeTag2,
238  mode_target_tag_t<access::mode::read,
239  access::target::constant_buffer>>::value) {
240  return access::mode::read;
241  }
242 
243  return access::mode::read_write;
244 }
245 
246 template <typename MayBeTag1, typename MayBeTag2>
247 constexpr access::target deduceAccessTarget(access::target defaultTarget) {
248  if constexpr (
249  std::is_same<MayBeTag1,
250  mode_target_tag_t<access::mode::read,
251  access::target::constant_buffer>>::value ||
252  std::is_same<MayBeTag2,
253  mode_target_tag_t<access::mode::read,
254  access::target::constant_buffer>>::value) {
255  return access::target::constant_buffer;
256  }
257 
258  return defaultTarget;
259 }
260 
261 #endif
262 
263 } // namespace detail
264 } // namespace sycl
265 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::detail::LocalAccessorBaseHost::getSize
sycl::range< 3 > & getSize()
Definition: accessor_impl.hpp:191
cl::sycl::detail::AccessorImplDevice::Offset
id< Dims > Offset
Definition: accessor_impl.hpp:50
cl::sycl::detail::AccessorImplHost::MDims
unsigned int MDims
Definition: accessor_impl.hpp:116
cl::sycl::detail::AccessorBaseHost::getPtr
void * getPtr() const
Definition: accessor_impl.hpp:155
cl::sycl::detail::LocalAccessorImplHost::MDims
int MDims
Definition: accessor_impl.hpp:178
cl::sycl::detail::AccessorBaseHost::AccessorBaseHost
AccessorBaseHost(id< 3 > Offset, range< 3 > AccessRange, range< 3 > MemoryRange, access::mode AccessMode, detail::SYCLMemObjI *SYCLMemObject, int Dims, int ElemSize, int OffsetInBytes=0, bool IsSubBuffer=false)
Definition: accessor_impl.hpp:136
sycl_mem_obj_i.hpp
cl::sycl::detail::LocalAccessorBaseHost::getPtr
void * getPtr() const
Definition: accessor_impl.hpp:194
cl::sycl::detail::LocalAccessorImplHost
Definition: accessor_impl.hpp:171
cl::sycl::detail::LocalAccessorBaseHost::getNumOfDims
int getNumOfDims()
Definition: accessor_impl.hpp:198
cl::sycl::detail::AccessorImplDevice
Definition: accessor_impl.hpp:43
cl::sycl::access::placeholder
placeholder
Definition: access.hpp:43
cl::sycl::detail::LocalAccessorImplPtr
std::shared_ptr< LocalAccessorImplHost > LocalAccessorImplPtr
Definition: accessor_impl.hpp:183
cl::sycl::detail::AccessorImplHost::MIsESIMDAcc
bool MIsESIMDAcc
Definition: accessor_impl.hpp:129
stl.hpp
cl::sycl::id< Dims >
cl::sycl::detail::LocalAccessorBaseDevice::AccessRange
range< Dims > AccessRange
Definition: accessor_impl.hpp:67
cl::sycl::detail::InitializedVal
Definition: common.hpp:221
cl::sycl::detail::LocalAccessorBaseHost::getElementSize
int getElementSize()
Definition: accessor_impl.hpp:199
access.hpp
cl::sycl::detail::LocalAccessorBaseDevice
Definition: accessor_impl.hpp:60
cl::sycl::detail::AccessorImplDevice::AccessRange
range< Dims > AccessRange
Definition: accessor_impl.hpp:51
cl::sycl::detail::AccessorBaseHost::impl
AccessorImplPtr impl
Definition: accessor_impl.hpp:164
cl::sycl::detail::write
void write(GlobalBufAccessorT &GlobalFlushBuf, size_t FlushBufferSize, unsigned WIOffset, const char *Str, unsigned Len, unsigned Padding=0)
Definition: stream.hpp:110
cl::sycl::detail::LocalAccessorBaseHost
Definition: accessor_impl.hpp:185
id.hpp
cl::sycl::range< Dims >
cl::sycl::detail::LocalAccessorImplHost::MSize
sycl::range< 3 > MSize
Definition: accessor_impl.hpp:177
cl::sycl::detail::get
Definition: tuple.hpp:59
cl::sycl::detail::LocalAccessorImplHost::MMem
std::vector< char > MMem
Definition: accessor_impl.hpp:180
cl::sycl::detail::AccessorImplHost::MElemSize
unsigned int MElemSize
Definition: accessor_impl.hpp:117
export.hpp
cl::sycl::detail::LocalAccessorBaseDevice::MemRange
range< Dims > MemRange
Definition: accessor_impl.hpp:68
cl::sycl::detail::AccessorImplHost
Definition: accessor_impl.hpp:76
cl::sycl::detail::LocalAccessorBaseHost::getPtr
void * getPtr()
Definition: accessor_impl.hpp:193
cl::sycl::detail::AccessorImplHost::MOffsetInBytes
unsigned int MOffsetInBytes
Definition: accessor_impl.hpp:118
cl::sycl::detail::AccessorImplHost::MOffset
id< 3 > MOffset
Definition: accessor_impl.hpp:107
cl::sycl::detail::AccessorImplHost::MIsSubBuffer
bool MIsSubBuffer
Definition: accessor_impl.hpp:119
range.hpp
cl::sycl::detail::AccessorImplPtr
std::shared_ptr< AccessorImplHost > AccessorImplPtr
Definition: accessor_impl.hpp:132
cl::sycl::detail::AccessorBaseHost::getElemSize
unsigned int getElemSize() const
Definition: accessor_impl.hpp:150
cl::sycl::accessor
Buffer accessor.
Definition: accessor.hpp:225
cl::sycl::access::target
target
Definition: access.hpp:17
cl::sycl::detail::LocalAccessorBaseDevice::operator==
bool operator==(const LocalAccessorBaseDevice &Rhs) const
Definition: accessor_impl.hpp:71
cl::sycl::detail::Command
The Command class represents some action that needs to be performed on one or more memory objects.
Definition: commands.hpp:93
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::AccessorImplHost::MSYCLMemObj
detail::SYCLMemObjI * MSYCLMemObj
Definition: accessor_impl.hpp:114
cl::sycl::detail::AccessorImplHost::AccessorImplHost
AccessorImplHost(id< 3 > Offset, range< 3 > AccessRange, range< 3 > MemoryRange, access::mode AccessMode, detail::SYCLMemObjI *SYCLMemObject, int Dims, int ElemSize, int OffsetInBytes=0, bool IsSubBuffer=false, bool IsESIMDAcc=false)
Definition: accessor_impl.hpp:78
cl::sycl::detail::addHostAccessorAndWait
void addHostAccessorAndWait(Requirement *Req)
Definition: accessor_impl.cpp:35
cl::sycl::detail::AccessorImplDevice::AccessorImplDevice
AccessorImplDevice(id< Dims > Offset, range< Dims > AccessRange, range< Dims > MemoryRange)
Definition: accessor_impl.hpp:46
cl::sycl::detail::LocalAccessorImplHost::LocalAccessorImplHost
LocalAccessorImplHost(sycl::range< 3 > Size, int Dims, int ElemSize)
Definition: accessor_impl.hpp:173
cl::sycl::detail::AccessorBaseHost::getPtr
void * getPtr()
Definition: accessor_impl.hpp:149
cl::sycl::detail::AccessorImplHost::MAccessMode
access::mode MAccessMode
Definition: accessor_impl.hpp:112
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:198
cl::sycl::detail::LocalAccessorBaseDevice::Offset
id< Dims > Offset
Definition: accessor_impl.hpp:69
cl::sycl::detail::AccessorImplDevice::operator==
bool operator==(const AccessorImplDevice &Rhs) const
Definition: accessor_impl.hpp:54
cl::sycl::detail::AccessorImplHost::MMemoryRange
range< 3 > MMemoryRange
Definition: accessor_impl.hpp:111
cl::sycl::detail::AccessorImplHost::AccessorImplHost
AccessorImplHost(const AccessorImplHost &Other)
Definition: accessor_impl.hpp:89
cl::sycl::detail::AccessorBaseHost
Definition: accessor_impl.hpp:134
cl::sycl::detail::LocalAccessorBaseDevice::LocalAccessorBaseDevice
LocalAccessorBaseDevice(sycl::range< Dims > Size)
Definition: accessor_impl.hpp:62
cl::sycl::detail::LocalAccessorBaseHost::getSize
const sycl::range< 3 > & getSize() const
Definition: accessor_impl.hpp:192
cl::sycl::access::mode
mode
Definition: access.hpp:28
cl::sycl::detail::AccessorImplHost::MAccessRange
range< 3 > MAccessRange
Definition: accessor_impl.hpp:109
cl::sycl::mode_tag_t
Definition: access.hpp:66
cl::sycl::detail::SYCLMemObjI
Definition: sycl_mem_obj_i.hpp:28
cl::sycl::detail::AccessorImplDevice::MemRange
range< Dims > MemRange
Definition: accessor_impl.hpp:52
cl::sycl::detail::LocalAccessorBaseHost::impl
std::shared_ptr< LocalAccessorImplHost > impl
Definition: accessor_impl.hpp:205
cl::sycl::detail::LocalAccessorBaseHost::LocalAccessorBaseHost
LocalAccessorBaseHost(sycl::range< 3 > Size, int Dims, int ElemSize)
Definition: accessor_impl.hpp:187
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12
cl::sycl::detail::LocalAccessorImplHost::MElemSize
int MElemSize
Definition: accessor_impl.hpp:179