stdgpu/iterator.h Source File

stdgpu/iterator.h Source File#

stdgpu: stdgpu/iterator.h Source File
stdgpu Latest
Efficient STL-like Data Structures on the GPU
iterator.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_ITERATOR_H
17#define STDGPU_ITERATOR_H
18
28#include <thrust/detail/pointer.h>
29#include <thrust/detail/reference.h> // Only forward declaration included by thrust/detail/pointer.h
30
31#include <stdgpu/cstddef.h>
32#include <stdgpu/platform.h>
33
34namespace stdgpu
35{
36
42template <typename T>
43using device_ptr = thrust::pointer<T, thrust::device_system_tag>;
44
50template <typename T>
51using host_ptr = thrust::pointer<T, thrust::host_system_tag>;
52
53} // namespace stdgpu
54
56namespace std // NOLINT(cert-dcl58-cpp)
57{
58
59template <typename T>
60struct iterator_traits<stdgpu::device_ptr<T>> // NOLINT(cert-dcl58-cpp)
61{
62 using difference_type = typename std::iterator_traits<T*>::difference_type;
63 using value_type = typename std::iterator_traits<T*>::value_type;
64 using pointer = typename std::iterator_traits<T*>::pointer;
65 using reference = typename std::iterator_traits<T*>::reference;
66 using iterator_category = typename stdgpu::device_ptr<T>::iterator_category;
67};
68
69template <typename T>
70struct iterator_traits<stdgpu::host_ptr<T>> // NOLINT(cert-dcl58-cpp)
71{
72 using difference_type = typename std::iterator_traits<T*>::difference_type;
73 using value_type = typename std::iterator_traits<T*>::value_type;
74 using pointer = typename std::iterator_traits<T*>::pointer;
75 using reference = typename std::iterator_traits<T*>::reference;
76 using iterator_category = typename stdgpu::host_ptr<T>::iterator_category;
77};
78
79} // namespace std
81
82namespace stdgpu
83{
84
92template <typename T>
93STDGPU_HOST_DEVICE device_ptr<T>
94make_device(T* device_array);
95
103template <typename T>
105make_host(T* host_array);
106
115template <typename T>
117size(T* array);
118
126template <>
128size(void* array);
129
137template <typename T>
139host_begin(T* host_array);
140
148template <typename T>
150host_end(T* host_array);
151
159template <typename T>
161device_begin(T* device_array);
162
170template <typename T>
172device_end(T* device_array);
173
181template <typename T>
183host_begin(const T* host_array);
184
192template <typename T>
194host_end(const T* host_array);
195
203template <typename T>
205device_begin(const T* device_array);
206
214template <typename T>
216device_end(const T* device_array);
217
225template <typename T>
227host_cbegin(const T* host_array);
228
236template <typename T>
238host_cend(const T* host_array);
239
247template <typename T>
249device_cbegin(const T* device_array);
250
258template <typename T>
260device_cend(const T* device_array);
261
269template <typename C>
270auto
271host_begin(C& host_container) -> decltype(host_container.host_begin());
272
280template <typename C>
281auto
282host_end(C& host_container) -> decltype(host_container.host_end());
283
291template <typename C>
292auto
293device_begin(C& device_container) -> decltype(device_container.device_begin());
294
302template <typename C>
303auto
304device_end(C& device_container) -> decltype(device_container.device_end());
305
313template <typename C>
314auto
315host_begin(const C& host_container) -> decltype(host_container.host_begin());
316
324template <typename C>
325auto
326host_end(const C& host_container) -> decltype(host_container.host_end());
327
335template <typename C>
336auto
337device_begin(const C& device_container) -> decltype(device_container.device_begin());
338
346template <typename C>
347auto
348device_end(const C& device_container) -> decltype(device_container.device_end());
349
357template <typename C>
358auto
359host_cbegin(const C& host_container) -> decltype(host_begin(host_container));
360
368template <typename C>
369auto
370host_cend(const C& host_container) -> decltype(host_end(host_container));
371
379template <typename C>
380auto
381device_cbegin(const C& device_container) -> decltype(device_begin(device_container));
382
390template <typename C>
391auto
392device_cend(const C& device_container) -> decltype(device_end(device_container));
393
394namespace detail
395{
396
397template <typename Container>
398struct back_insert_iterator_base;
399
400template <typename Container>
401struct front_insert_iterator_base;
402
403template <typename Container>
404struct insert_iterator_base;
405
406} // namespace detail
407
413template <typename Container>
414class back_insert_iterator : public detail::back_insert_iterator_base<Container>::type
415{
416public:
417 using container_type = Container;
420 using super_t = typename detail::back_insert_iterator_base<Container>::type;
421
422 friend class thrust::iterator_core_access;
424
430 explicit back_insert_iterator(Container& c);
431
432private:
433 STDGPU_HOST_DEVICE typename super_t::reference
434 dereference() const;
435
436 Container _c;
437};
438
445template <typename Container>
447back_inserter(Container& c);
448
454template <typename Container>
455class front_insert_iterator : public detail::front_insert_iterator_base<Container>::type
456{
457public:
458 using container_type = Container;
461 using super_t = typename detail::front_insert_iterator_base<Container>::type;
462
463 friend class thrust::iterator_core_access;
465
471 explicit front_insert_iterator(Container& c);
472
473private:
474 STDGPU_HOST_DEVICE typename super_t::reference
475 dereference() const;
476
477 Container _c;
478};
479
486template <typename Container>
488front_inserter(Container& c);
489
498template <typename Container>
499class insert_iterator : public detail::insert_iterator_base<Container>::type
500{
501public:
502 using container_type = Container;
505 using super_t = typename detail::insert_iterator_base<Container>::type;
506
507 friend class thrust::iterator_core_access;
509
515 explicit insert_iterator(Container& c);
516
517private:
518 STDGPU_HOST_DEVICE typename super_t::reference
519 dereference() const;
520
521 Container _c;
522};
523
530template <typename Container>
532inserter(Container& c);
533
534} // namespace stdgpu
535
536#include <stdgpu/impl/iterator_detail.h>
537
538#endif // STDGPU_ITERATOR_H
STDGPU_HOST_DEVICE back_insert_iterator(Container &c)
Constructor.
Container container_type
Definition: iterator.h:417
An output iterator which inserts elements into a container using push_back.
Definition: iterator.h:415
Container container_type
Definition: iterator.h:458
STDGPU_HOST_DEVICE front_insert_iterator(Container &c)
Constructor.
An output iterator which inserts elements into a container using push_front.
Definition: iterator.h:456
STDGPU_HOST_DEVICE insert_iterator(Container &c)
Constructor.
Container container_type
Definition: iterator.h:502
An output iterator which inserts elements into a container using insert.
Definition: iterator.h:500
std::ptrdiff_t index64_t
std::ptrdiff_t
Definition: cstddef.h:48
STDGPU_HOST_DEVICE insert_iterator< Container > inserter(Container &c)
Constructs an insert_iterator.
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
host_ptr< const T > host_cend(const T *host_array)
Creates a constant pointer to the end of the given host array.
host_ptr< T > host_begin(T *host_array)
Creates a pointer to the begin of the given host array.
device_ptr< const T > device_cbegin(const T *device_array)
Creates a constant pointer to the begin of the given device array.
device_ptr< const T > device_cend(const T *device_array)
Creates a constant pointer to the end of the given device array.
host_ptr< const T > host_cbegin(const T *host_array)
Creates a constant pointer to the begin of the given host array.
STDGPU_HOST_DEVICE back_insert_iterator< Container > back_inserter(Container &c)
Constructs a back_insert_iterator.
device_ptr< T > device_begin(T *device_array)
Creates a pointer to the begin of the given device array.
STDGPU_HOST_DEVICE device_ptr< T > make_device(T *device_array)
Constructs a device_ptr object.
device_ptr< T > device_end(T *device_array)
Creates a pointer to the end of the given device array.
STDGPU_HOST_DEVICE host_ptr< T > make_host(T *host_array)
Constructs a host_ptr object.
index64_t size(T *array)
Finds the size (number of elements) of the given dynamically allocated array.
host_ptr< T > host_end(T *host_array)
Creates a pointer to the end of the given host array.
STDGPU_HOST_DEVICE front_insert_iterator< Container > front_inserter(Container &c)
Constructs a front_insert_iterator.
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