OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
cache_modified_output_iterator.cuh
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2011, Duane Merrill. All rights reserved.
3  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  ******************************************************************************/
28 
34 #pragma once
35 
36 #include <iterator>
37 #include <iostream>
38 
39 #include "../thread/thread_load.cuh"
40 #include "../thread/thread_store.cuh"
41 #include "../util_device.cuh"
42 #include "../util_namespace.cuh"
43 
44 #if (THRUST_VERSION >= 100700)
45  // This iterator is compatible with Thrust API 1.7 and newer
46  #include <thrust/iterator/iterator_facade.h>
47  #include <thrust/iterator/iterator_traits.h>
48 #endif // THRUST_VERSION
49 
50 
52 CUB_NS_PREFIX
53 
55 namespace cub {
56 
57 
105 template <
106  CacheStoreModifier MODIFIER,
107  typename ValueType,
108  typename OffsetT = ptrdiff_t>
110 {
111 private:
112 
113  // Proxy object
114  struct Reference
115  {
116  ValueType* ptr;
117 
119  __host__ __device__ __forceinline__ Reference(ValueType* ptr) : ptr(ptr) {}
120 
122  __device__ __forceinline__ ValueType operator =(ValueType val)
123  {
124  ThreadStore<MODIFIER>(ptr, val);
125  return val;
126  }
127  };
128 
129 public:
130 
131  // Required iterator traits
134  typedef void value_type;
135  typedef void pointer;
137 
138 #if (THRUST_VERSION >= 100700)
139  // Use Thrust's iterator categories so we can use these iterators in Thrust 1.7 (or newer) methods
140  typedef typename thrust::detail::iterator_facade_category<
141  thrust::device_system_tag,
142  thrust::random_access_traversal_tag,
143  value_type,
144  reference
145  >::type iterator_category;
146 #else
147  typedef std::random_access_iterator_tag iterator_category;
148 #endif // THRUST_VERSION
149 
150 private:
151 
152  ValueType* ptr;
153 
154 public:
155 
157  template <typename QualifiedValueType>
158  __host__ __device__ __forceinline__ CacheModifiedOutputIterator(
159  QualifiedValueType* ptr)
160  :
161  ptr(const_cast<typename RemoveQualifiers<QualifiedValueType>::Type *>(ptr))
162  {}
163 
165  __host__ __device__ __forceinline__ self_type operator++(int)
166  {
167  self_type retval = *this;
168  ptr++;
169  return retval;
170  }
171 
172 
174  __host__ __device__ __forceinline__ self_type operator++()
175  {
176  ptr++;
177  return *this;
178  }
179 
181  __host__ __device__ __forceinline__ reference operator*() const
182  {
183  return Reference(ptr);
184  }
185 
187  template <typename Distance>
188  __host__ __device__ __forceinline__ self_type operator+(Distance n) const
189  {
190  self_type retval(ptr + n);
191  return retval;
192  }
193 
195  template <typename Distance>
196  __host__ __device__ __forceinline__ self_type& operator+=(Distance n)
197  {
198  ptr += n;
199  return *this;
200  }
201 
203  template <typename Distance>
204  __host__ __device__ __forceinline__ self_type operator-(Distance n) const
205  {
206  self_type retval(ptr - n);
207  return retval;
208  }
209 
211  template <typename Distance>
212  __host__ __device__ __forceinline__ self_type& operator-=(Distance n)
213  {
214  ptr -= n;
215  return *this;
216  }
217 
219  __host__ __device__ __forceinline__ difference_type operator-(self_type other) const
220  {
221  return ptr - other.ptr;
222  }
223 
225  template <typename Distance>
226  __host__ __device__ __forceinline__ reference operator[](Distance n) const
227  {
228  return Reference(ptr + n);
229  }
230 
232  __host__ __device__ __forceinline__ bool operator==(const self_type& rhs)
233  {
234  return (ptr == rhs.ptr);
235  }
236 
238  __host__ __device__ __forceinline__ bool operator!=(const self_type& rhs)
239  {
240  return (ptr != rhs.ptr);
241  }
242 
244  friend std::ostream& operator<<(std::ostream& os, const self_type& itr)
245  {
246  return os;
247  }
248 };
249 
250  // end group UtilIterator
252 
253 } // CUB namespace
254 CUB_NS_POSTFIX // Optional outer namespace(s)
__host__ __device__ __forceinline__ difference_type operator-(self_type other) const
Distance.
Optional outer namespace(s)
__host__ __device__ __forceinline__ self_type & operator+=(Distance n)
Addition assignment.
__host__ __device__ __forceinline__ bool operator!=(const self_type &rhs)
Not equal to.
CacheStoreModifier
Enumeration of cache modifiers for memory store operations.
void value_type
The type of the element the iterator can point to.
__host__ __device__ __forceinline__ self_type operator-(Distance n) const
Subtraction.
OffsetT difference_type
Type to express the result of subtracting one iterator from another.
__host__ __device__ __forceinline__ self_type & operator-=(Distance n)
Subtraction assignment.
friend std::ostream & operator<<(std::ostream &os, const self_type &itr)
ostream operator
__host__ __device__ __forceinline__ reference operator[](Distance n) const
Array subscript.
OffsetT OffsetT
[in] Total number of input data items
A random-access output wrapper for storing array values using a PTX cache-modifier.
CacheModifiedOutputIterator self_type
My own type.
__host__ __device__ __forceinline__ reference operator *() const
Indirection.
__host__ __device__ __forceinline__ bool operator==(const self_type &rhs)
Equal to.
__host__ __device__ __forceinline__ CacheModifiedOutputIterator(QualifiedValueType *ptr)
Constructor.
__host__ __device__ __forceinline__ self_type operator++()
Prefix increment.
void pointer
The type of a pointer to an element the iterator can point to.
__host__ __device__ __forceinline__ self_type operator++(int)
Postfix increment.
__host__ __device__ __forceinline__ self_type operator+(Distance n) const
Addition.
Reference reference
The type of a reference to an element the iterator can point to.
std::random_access_iterator_tag iterator_category
The iterator category.
__device__ __forceinline__ ValueType operator=(ValueType val)
Assignment.
__host__ __device__ __forceinline__ Reference(ValueType *ptr)
Constructor.
Removes const and volatile qualifiers from type Tp.
Definition: util_type.cuh:222