OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
constant_input_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_namespace.cuh"
42 
43 #if (THRUST_VERSION >= 100700)
44  // This iterator is compatible with Thrust API 1.7 and newer
45  #include <thrust/iterator/iterator_facade.h>
46  #include <thrust/iterator/iterator_traits.h>
47 #endif // THRUST_VERSION
48 
49 
51 CUB_NS_PREFIX
52 
54 namespace cub {
55 
56 
93 template <
94  typename ValueType,
95  typename OffsetT = ptrdiff_t>
97 {
98 public:
99 
100  // Required iterator traits
103  typedef ValueType value_type;
104  typedef ValueType* pointer;
105  typedef ValueType reference;
106 
107 #if (THRUST_VERSION >= 100700)
108  // Use Thrust's iterator categories so we can use these iterators in Thrust 1.7 (or newer) methods
109  typedef typename thrust::detail::iterator_facade_category<
110  thrust::any_system_tag,
111  thrust::random_access_traversal_tag,
112  value_type,
113  reference
114  >::type iterator_category;
115 #else
116  typedef std::random_access_iterator_tag iterator_category;
117 #endif // THRUST_VERSION
118 
119 private:
120 
121  ValueType val;
122  OffsetT offset;
123 #ifdef _WIN32
124  OffsetT pad[CUB_MAX(1, (16 / sizeof(OffsetT) - 1))]; // Workaround for win32 parameter-passing bug (ulonglong2 argmin DeviceReduce)
125 #endif
126 
127 public:
128 
130  __host__ __device__ __forceinline__ ConstantInputIterator(
131  ValueType val,
132  OffsetT offset = 0)
133  :
134  val(val),
135  offset(offset)
136  {}
137 
139  __host__ __device__ __forceinline__ self_type operator++(int)
140  {
141  self_type retval = *this;
142  offset++;
143  return retval;
144  }
145 
147  __host__ __device__ __forceinline__ self_type operator++()
148  {
149  offset++;
150  return *this;
151  }
152 
154  __host__ __device__ __forceinline__ reference operator*() const
155  {
156  return val;
157  }
158 
160  template <typename Distance>
161  __host__ __device__ __forceinline__ self_type operator+(Distance n) const
162  {
163  self_type retval(val, offset + n);
164  return retval;
165  }
166 
168  template <typename Distance>
169  __host__ __device__ __forceinline__ self_type& operator+=(Distance n)
170  {
171  offset += n;
172  return *this;
173  }
174 
176  template <typename Distance>
177  __host__ __device__ __forceinline__ self_type operator-(Distance n) const
178  {
179  self_type retval(val, offset - n);
180  return retval;
181  }
182 
184  template <typename Distance>
185  __host__ __device__ __forceinline__ self_type& operator-=(Distance n)
186  {
187  offset -= n;
188  return *this;
189  }
190 
192  __host__ __device__ __forceinline__ difference_type operator-(self_type other) const
193  {
194  return offset - other.offset;
195  }
196 
198  template <typename Distance>
199  __host__ __device__ __forceinline__ reference operator[](Distance /*n*/) const
200  {
201  return val;
202  }
203 
205  __host__ __device__ __forceinline__ pointer operator->()
206  {
207  return &val;
208  }
209 
211  __host__ __device__ __forceinline__ bool operator==(const self_type& rhs)
212  {
213  return (offset == rhs.offset) && ((val == rhs.val));
214  }
215 
217  __host__ __device__ __forceinline__ bool operator!=(const self_type& rhs)
218  {
219  return (offset != rhs.offset) || (val!= rhs.val);
220  }
221 
223  friend std::ostream& operator<<(std::ostream& os, const self_type& itr)
224  {
225  os << "[" << itr.val << "," << itr.offset << "]";
226  return os;
227  }
228 
229 };
230 
231  // end group UtilIterator
233 
234 } // CUB namespace
235 CUB_NS_POSTFIX // Optional outer namespace(s)
__host__ __device__ __forceinline__ self_type operator+(Distance n) const
Addition.
__host__ __device__ __forceinline__ difference_type operator-(self_type other) const
Distance.
__host__ __device__ __forceinline__ reference operator *() const
Indirection.
ValueType * pointer
The type of a pointer to an element the iterator can point to.
Optional outer namespace(s)
OffsetT difference_type
Type to express the result of subtracting one iterator from another.
__host__ __device__ __forceinline__ bool operator!=(const self_type &rhs)
Not equal to.
__host__ __device__ __forceinline__ self_type & operator+=(Distance n)
Addition assignment.
__host__ __device__ __forceinline__ self_type & operator-=(Distance n)
Subtraction assignment.
A random-access input generator for dereferencing a sequence of homogeneous values.
ValueType reference
The type of a reference to an element the iterator can point to.
__host__ __device__ __forceinline__ bool operator==(const self_type &rhs)
Equal to.
OffsetT OffsetT
[in] Total number of input data items
__host__ __device__ __forceinline__ reference operator[](Distance) const
Array subscript.
std::random_access_iterator_tag iterator_category
The iterator category.
__host__ __device__ __forceinline__ pointer operator->()
Structure dereference.
ConstantInputIterator self_type
My own type.
__host__ __device__ __forceinline__ self_type operator-(Distance n) const
Subtraction.
__host__ __device__ __forceinline__ self_type operator++(int)
Postfix increment.
__host__ __device__ __forceinline__ ConstantInputIterator(ValueType val, OffsetT offset=0)
Constructor.
__host__ __device__ __forceinline__ self_type operator++()
Prefix increment.
ValueType value_type
The type of the element the iterator can point to.
#define CUB_MAX(a, b)
Select maximum(a, b)
Definition: util_macro.cuh:61
friend std::ostream & operator<<(std::ostream &os, const self_type &itr)
ostream operator