DPC++ Runtime
Runtime libraries for oneAPI DPC++
item.hpp
Go to the documentation of this file.
1 //==------------ item.hpp --- SYCL iteration item --------------------------==//
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 
11 #include <sycl/detail/defines.hpp>
12 #include <sycl/detail/helpers.hpp>
15 #include <sycl/id.hpp>
16 #include <sycl/range.hpp>
17 
18 #include <cstddef>
19 
20 namespace sycl {
22 namespace detail {
23 class Builder;
24 template <typename TransformedArgType, int Dims, typename KernelType>
25 class RoundedRangeKernel;
26 template <typename TransformedArgType, int Dims, typename KernelType>
27 class RoundedRangeKernelWithKH;
28 
29 namespace reduction {
30 template <int Dims>
32 } // namespace reduction
33 } // namespace detail
34 
39 template <int dimensions = 1, bool with_offset = true> class item {
40 #ifndef __SYCL_DISABLE_ITEM_TO_INT_CONV__
41  /* Helper class for conversion operator. Void type is not suitable. User
42  * cannot even try to get address of the operator __private_class(). User
43  * may try to get an address of operator void() and will get the
44  * compile-time error */
45  class __private_class;
46 
47  template <bool B, typename T>
49 #endif // __SYCL_DISABLE_ITEM_TO_INT_CONV__
50 public:
51  item() = delete;
52 
53  id<dimensions> get_id() const { return MImpl.MIndex; }
54 
55  size_t __SYCL_ALWAYS_INLINE get_id(int dimension) const {
56  size_t Id = MImpl.MIndex[dimension];
58  return Id;
59  }
60 
61  size_t __SYCL_ALWAYS_INLINE operator[](int dimension) const {
62  size_t Id = MImpl.MIndex[dimension];
64  return Id;
65  }
66 
67  range<dimensions> get_range() const { return MImpl.MExtent; }
68 
69  size_t __SYCL_ALWAYS_INLINE get_range(int dimension) const {
70  size_t Id = MImpl.MExtent[dimension];
72  return Id;
73  }
74 #ifndef __SYCL_DISABLE_ITEM_TO_INT_CONV__
75  operator EnableIfT<dimensions == 1, std::size_t>() const { return get_id(0); }
76 #endif // __SYCL_DISABLE_ITEM_TO_INT_CONV__
77  template <bool has_offset = with_offset>
78  __SYCL2020_DEPRECATED("offsets are deprecated in SYCL2020")
79  detail::enable_if_t<has_offset, id<dimensions>> get_offset() const {
80  return MImpl.MOffset;
81  }
82 
83  template <bool has_offset = with_offset>
84  __SYCL2020_DEPRECATED("offsets are deprecated in SYCL2020")
85  detail::enable_if_t<has_offset, size_t> __SYCL_ALWAYS_INLINE
86  get_offset(int dimension) const {
87  size_t Id = MImpl.MOffset[dimension];
89  return Id;
90  }
91 
92  template <bool has_offset = with_offset>
94  return detail::Builder::createItem<dimensions, true>(
95  MImpl.MExtent, MImpl.MIndex, /*Offset*/ {});
96  }
97 
99  size_t Id = MImpl.get_linear_id();
100  __SYCL_ASSUME_INT(Id);
101  return Id;
102  }
103 
104  item(const item &rhs) = default;
105 
106  item(item<dimensions, with_offset> &&rhs) = default;
107 
108  item &operator=(const item &rhs) = default;
109 
110  item &operator=(item &&rhs) = default;
111 
112  bool operator==(const item &rhs) const { return rhs.MImpl == MImpl; }
113 
114  bool operator!=(const item &rhs) const { return rhs.MImpl != MImpl; }
115 
116 protected:
117  template <bool has_offset = with_offset>
118  item(detail::enable_if_t<has_offset, const range<dimensions>> &extent,
119  const id<dimensions> &index, const id<dimensions> &offset)
120  : MImpl{extent, index, offset} {}
121 
122  template <bool has_offset = with_offset>
123  item(detail::enable_if_t<!has_offset, const range<dimensions>> &extent,
124  const id<dimensions> &index)
125  : MImpl{extent, index} {}
126 
127  friend class detail::Builder;
128 
129 private:
130  // Friend to get access to private method set_allowed_range().
131  template <typename, int, typename> friend class detail::RoundedRangeKernel;
132  template <typename, int, typename>
134  void set_allowed_range(const range<dimensions> rnwi) { MImpl.MExtent = rnwi; }
135 
136  template <int Dims>
137  friend item<Dims, false>
139 
141 };
142 
143 template <int Dims>
144 __SYCL_DEPRECATED("use sycl::ext::oneapi::experimental::this_item() instead")
145 item<Dims> this_item() {
146 #ifdef __SYCL_DEVICE_ONLY__
147  return detail::Builder::getElement(detail::declptr<item<Dims>>());
148 #else
149  throw sycl::exception(
150  sycl::make_error_code(sycl::errc::feature_not_supported),
151  "Free function calls are not supported on host");
152 #endif
153 }
154 
155 namespace ext::oneapi::experimental {
156 template <int Dims> item<Dims> this_item() {
157 #ifdef __SYCL_DEVICE_ONLY__
158  return sycl::detail::Builder::getElement(sycl::detail::declptr<item<Dims>>());
159 #else
160  throw sycl::exception(
161  sycl::make_error_code(sycl::errc::feature_not_supported),
162  "Free function calls are not supported on host");
163 #endif
164 }
165 } // namespace ext::oneapi::experimental
166 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
167 } // namespace sycl
sycl::_V1::detail::Builder
Definition: helpers.hpp:61
sycl::_V1::item::get_range
size_t __SYCL_ALWAYS_INLINE get_range(int dimension) const
Definition: item.hpp:69
sycl::_V1::ext::oneapi::experimental::this_item
item< Dims > this_item()
Definition: item.hpp:156
type_traits.hpp
sycl::_V1::make_error_code
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:92
sycl::_V1::detail::declptr
T * declptr()
Definition: helpers.hpp:49
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::item::item
item(detail::enable_if_t<!has_offset, const range< dimensions >> &extent, const id< dimensions > &index)
Definition: item.hpp:123
__SYCL_ALWAYS_INLINE
#define __SYCL_ALWAYS_INLINE
Definition: defines_elementary.hpp:25
sycl::_V1::item::get_id
id< dimensions > get_id() const
Definition: item.hpp:53
helpers.hpp
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
sycl::_V1::id< Dims >
id.hpp
__SYCL_DEPRECATED
#define __SYCL_DEPRECATED(message)
Definition: defines_elementary.hpp:46
sycl::_V1::detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: stl_type_traits.hpp:24
sycl::_V1::range< Dims >
sycl::_V1::item
Identifies an instance of the function object executing at each point in a range.
Definition: helpers.hpp:29
sycl::_V1::item::operator!=
bool operator!=(const item &rhs) const
Definition: item.hpp:114
sycl::_V1::ext::oneapi::experimental::operator=
annotated_arg & operator=(annotated_arg &)=default
sycl::_V1::item::get_linear_id
size_t __SYCL_ALWAYS_INLINE get_linear_id() const
Definition: item.hpp:98
range.hpp
sycl::_V1::detail::ItemBase< dimensions, with_offset >
defines.hpp
sycl::_V1::reduction
auto reduction(buffer< T, 1, AllocatorT > Var, handler &CGH, BinaryOperation Combiner, const property_list &PropList={})
Constructs a reduction object using the given buffer Var, handler CGH, reduction operation Combiner,...
Definition: reduction.hpp:2562
sycl::_V1::item::get_range
range< dimensions > get_range() const
Definition: item.hpp:67
__SYCL_ASSUME_INT
#define __SYCL_ASSUME_INT(x)
Definition: defines.hpp:18
sycl::_V1::detail::RoundedRangeKernel
Definition: handler.hpp:242
sycl::_V1::item::get_id
size_t __SYCL_ALWAYS_INLINE get_id(int dimension) const
Definition: item.hpp:55
sycl::_V1::detail::RoundedRangeKernelWithKH
Definition: handler.hpp:260
sycl::_V1::item::item
item(detail::enable_if_t< has_offset, const range< dimensions >> &extent, const id< dimensions > &index, const id< dimensions > &offset)
Definition: item.hpp:118
item_base.hpp
sycl::_V1::detail::reduction::getDelinearizedItem
item< Dims, false > getDelinearizedItem(range< Dims > Range, id< Dims > Id)
Definition: reduction_forward.hpp:47
sycl::_V1::item::operator==
bool operator==(const item &rhs) const
Definition: item.hpp:112
sycl::_V1::detail::conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: stl_type_traits.hpp:27
sycl::_V1::item::operator[]
size_t __SYCL_ALWAYS_INLINE operator[](int dimension) const
Definition: item.hpp:61
__SYCL2020_DEPRECATED
#define __SYCL2020_DEPRECATED(message)
Definition: defines_elementary.hpp:57