stdgpu/ranges.h Source File

stdgpu/ranges.h Source File#

stdgpu: stdgpu/ranges.h Source File
stdgpu Latest
Efficient STL-like Data Structures on the GPU
ranges.h
Go to the documentation of this file.
1/*
2 * Copyright 2019 Patrick Stotko
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef STDGPU_RANGES_H
17#define STDGPU_RANGES_H
18
28#include <thrust/iterator/transform_iterator.h>
29
30#include <stdgpu/cstddef.h>
31#include <stdgpu/iterator.h>
32#include <stdgpu/platform.h>
33
34namespace stdgpu
35{
36
42template <typename T>
44{
45public:
47 using value_type = typename iterator::value_type;
48 using difference_type = typename iterator::difference_type;
49 using reference = typename iterator::reference;
54 device_range() = default;
55
60 explicit device_range(T* p);
61
69
77
85
91 begin() const noexcept;
92
98 end() const noexcept;
99
105 size() const;
106
111 [[nodiscard]] STDGPU_HOST_DEVICE bool
112 empty() const;
113
114private:
115 iterator _begin = {};
116 iterator _end = {};
117};
118
124template <typename T>
126{
127public:
129 using value_type = typename iterator::value_type;
130 using difference_type = typename iterator::difference_type;
131 using reference = typename iterator::reference;
136 host_range() = default;
137
142 explicit host_range(T* p);
143
151
159
167
173 begin() const noexcept;
174
180 end() const noexcept;
181
187 size() const;
188
193 [[nodiscard]] STDGPU_HOST_DEVICE bool
194 empty() const;
195
196private:
197 iterator _begin = {};
198 iterator _end = {};
199};
200
207template <typename R, typename UnaryFunction>
209{
210public:
211 using iterator = thrust::transform_iterator<UnaryFunction,
212 typename R::iterator>;
214 using value_type = typename iterator::value_type;
215 using difference_type = typename iterator::difference_type;
216 using reference = typename iterator::reference;
221 transform_range() = default;
222
228 explicit transform_range(R r);
229
236 transform_range(R r, UnaryFunction f);
237
243 begin() const noexcept;
244
250 end() const noexcept;
251
257 size() const;
258
263 [[nodiscard]] STDGPU_HOST_DEVICE bool
264 empty() const;
265
266private:
267 iterator _begin = {};
268 iterator _end = {};
269};
270
271namespace detail
272{
273
274template <typename T>
275class select;
276
277} // namespace detail
278
284template <typename T>
286
292template <typename T>
294
295} // namespace stdgpu
296
297#include <stdgpu/impl/ranges_detail.h>
298
299#endif // STDGPU_RANGES_H
typename iterator::difference_type difference_type
Definition: ranges.h:48
device_ptr< T > iterator
Definition: ranges.h:46
typename iterator::value_type value_type
Definition: ranges.h:47
device_range(T *p)
Constructor with automatic size inference from the given pointer.
STDGPU_HOST_DEVICE device_range(T *p, index64_t n)
Constructor.
STDGPU_HOST_DEVICE iterator begin() const noexcept
An iterator to the begin of the range.
STDGPU_HOST_DEVICE iterator end() const noexcept
An iterator to the end of the range.
STDGPU_HOST_DEVICE index64_t size() const
The size.
STDGPU_HOST_DEVICE bool empty() const
Checks if the range is empty.
STDGPU_HOST_DEVICE device_range(iterator begin, iterator end)
Constructor.
STDGPU_HOST_DEVICE device_range(iterator begin, index64_t n)
Constructor.
typename iterator::reference reference
Definition: ranges.h:49
A class representing a device range over an array.
Definition: ranges.h:44
device_range()=default
Empty constructor.
STDGPU_HOST_DEVICE host_range(iterator begin, iterator end)
Constructor.
host_range(T *p)
Constructor with automatic size inference from the given pointer.
STDGPU_HOST_DEVICE host_range(iterator begin, index64_t n)
Constructor.
STDGPU_HOST_DEVICE iterator end() const noexcept
An iterator to the end of the range.
host_ptr< T > iterator
Definition: ranges.h:128
typename iterator::reference reference
Definition: ranges.h:131
STDGPU_HOST_DEVICE iterator begin() const noexcept
An iterator to the begin of the range.
STDGPU_HOST_DEVICE host_range(T *p, index64_t n)
Constructor.
typename iterator::difference_type difference_type
Definition: ranges.h:130
typename iterator::value_type value_type
Definition: ranges.h:129
STDGPU_HOST_DEVICE bool empty() const
Checks if the range is empty.
STDGPU_HOST_DEVICE index64_t size() const
The size.
A class representing a host range over an array.
Definition: ranges.h:126
host_range()=default
Empty constructor.
thrust::transform_iterator< UnaryFunction, typename R::iterator > iterator
Definition: ranges.h:213
STDGPU_HOST_DEVICE transform_range(R r, UnaryFunction f)
Constructor.
typename iterator::difference_type difference_type
Definition: ranges.h:215
STDGPU_HOST_DEVICE index64_t size() const
The size.
typename iterator::value_type value_type
Definition: ranges.h:214
typename iterator::reference reference
Definition: ranges.h:216
STDGPU_HOST_DEVICE transform_range(R r)
Constructor.
STDGPU_HOST_DEVICE iterator end() const noexcept
An iterator to the end of the range.
STDGPU_HOST_DEVICE iterator begin() const noexcept
An iterator to the begin of the range.
STDGPU_HOST_DEVICE bool empty() const
Checks if the range is empty.
A class representing range where a transformation is applied first.
Definition: ranges.h:209
transform_range()=default
Empty constructor.
std::ptrdiff_t index64_t
std::ptrdiff_t
Definition: cstddef.h:48
thrust::pointer< T, thrust::device_system_tag > device_ptr
A host pointer class allowing to call thrust algorithms without explicitly using the respective execu...
Definition: iterator.h:43
thrust::pointer< T, thrust::host_system_tag > host_ptr
A host pointer class allowing to call thrust algorithms without explicitly using the respective execu...
Definition: iterator.h:51
#define STDGPU_HOST_DEVICE
Platform-independent host device function annotation.
Definition: platform.h:77