CUB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
tex_obj_input_iterator.cuh
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2011, Duane Merrill. All rights reserved.
3  * Copyright (c) 2011-2014, 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_debug.cuh"
43 #include "../util_namespace.cuh"
44 
45 #if (THRUST_VERSION >= 100700)
46  // This iterator is compatible with Thrust API 1.7 and newer
47  #include <thrust/iterator/iterator_facade.h>
48  #include <thrust/iterator/iterator_traits.h>
49 #endif // THRUST_VERSION
50 
51 
53 CUB_NS_PREFIX
54 
56 namespace cub {
57 
108 template <
109  typename T,
110  typename Offset = ptrdiff_t>
112 {
113 public:
114 
115  // Required iterator traits
117  typedef Offset difference_type;
118  typedef T value_type;
119  typedef T* pointer;
120  typedef T reference;
121 
122 #if (THRUST_VERSION >= 100700)
123  // Use Thrust's iterator categories so we can use these iterators in Thrust 1.7 (or newer) methods
124  typedef typename thrust::detail::iterator_facade_category<
125  thrust::device_system_tag,
126  thrust::random_access_traversal_tag,
127  value_type,
128  reference
129  >::type iterator_category;
130 #else
131  typedef std::random_access_iterator_tag iterator_category;
132 #endif // THRUST_VERSION
133 
134 private:
135 
136  // Largest texture word we can use in device
137  typedef typename UnitWord<T>::TextureWord TextureWord;
138 
139  // Number of texture words per T
140  enum {
141  TEXTURE_MULTIPLE = sizeof(T) / sizeof(TextureWord)
142  };
143 
144 private:
145 
146  T* ptr;
147  difference_type tex_offset;
148  cudaTextureObject_t tex_obj;
149 
150 public:
151 
153  __host__ __device__ __forceinline__ TexObjInputIterator()
154  :
155  ptr(NULL),
156  tex_offset(0),
157  tex_obj(0)
158  {}
159 
161  cudaError_t BindTexture(
162  T *ptr,
163  size_t bytes,
164  size_t tex_offset = 0)
165  {
166  this->ptr = ptr;
167  this->tex_offset = tex_offset;
168 
169  cudaChannelFormatDesc channel_desc = cudaCreateChannelDesc<TextureWord>();
170  cudaResourceDesc res_desc;
171  cudaTextureDesc tex_desc;
172  memset(&res_desc, 0, sizeof(cudaResourceDesc));
173  memset(&tex_desc, 0, sizeof(cudaTextureDesc));
174  res_desc.resType = cudaResourceTypeLinear;
175  res_desc.res.linear.devPtr = ptr;
176  res_desc.res.linear.desc = channel_desc;
177  res_desc.res.linear.sizeInBytes = bytes;
178  tex_desc.readMode = cudaReadModeElementType;
179  return cudaCreateTextureObject(&tex_obj, &res_desc, &tex_desc, NULL);
180  }
181 
183  cudaError_t UnbindTexture()
184  {
185  return cudaDestroyTextureObject(tex_obj);
186  }
187 
189  __host__ __device__ __forceinline__ self_type operator++(int)
190  {
191  self_type retval = *this;
192  tex_offset++;
193  return retval;
194  }
195 
197  __host__ __device__ __forceinline__ self_type operator++()
198  {
199  tex_offset++;
200  return *this;
201  }
202 
204  __host__ __device__ __forceinline__ reference operator*() const
205  {
206 #if (CUB_PTX_ARCH == 0)
207  // Simply dereference the pointer on the host
208  return ptr[tex_offset];
209 #else
210  // Move array of uninitialized words, then alias and assign to return value
211  TextureWord words[TEXTURE_MULTIPLE];
212 
213  #pragma unroll
214  for (int i = 0; i < TEXTURE_MULTIPLE; ++i)
215  {
216  words[i] = tex1Dfetch<TextureWord>(
217  tex_obj,
218  (tex_offset * TEXTURE_MULTIPLE) + i);
219  }
220 
221  // Load from words
222  return *reinterpret_cast<T*>(words);
223 #endif
224  }
225 
227  template <typename Distance>
228  __host__ __device__ __forceinline__ self_type operator+(Distance n) const
229  {
230  self_type retval;
231  retval.ptr = ptr;
232  retval.tex_obj = tex_obj;
233  retval.tex_offset = tex_offset + n;
234  return retval;
235  }
236 
238  template <typename Distance>
239  __host__ __device__ __forceinline__ self_type& operator+=(Distance n)
240  {
241  tex_offset += n;
242  return *this;
243  }
244 
246  template <typename Distance>
247  __host__ __device__ __forceinline__ self_type operator-(Distance n) const
248  {
249  self_type retval;
250  retval.ptr = ptr;
251  retval.tex_obj = tex_obj;
252  retval.tex_offset = tex_offset - n;
253  return retval;
254  }
255 
257  template <typename Distance>
258  __host__ __device__ __forceinline__ self_type& operator-=(Distance n)
259  {
260  tex_offset -= n;
261  return *this;
262  }
263 
265  __host__ __device__ __forceinline__ difference_type operator-(self_type other) const
266  {
267  return tex_offset - other.tex_offset;
268  }
269 
271  template <typename Distance>
272  __host__ __device__ __forceinline__ reference operator[](Distance n) const
273  {
274  return *(*this + n);
275  }
276 
278  __host__ __device__ __forceinline__ pointer operator->()
279  {
280  return &(*(*this));
281  }
282 
284  __host__ __device__ __forceinline__ bool operator==(const self_type& rhs)
285  {
286  return ((ptr == rhs.ptr) && (tex_offset == rhs.tex_offset) && (tex_obj == rhs.tex_obj));
287  }
288 
290  __host__ __device__ __forceinline__ bool operator!=(const self_type& rhs)
291  {
292  return ((ptr != rhs.ptr) || (tex_offset != rhs.tex_offset) || (tex_obj != rhs.tex_obj));
293  }
294 
296  friend std::ostream& operator<<(std::ostream& os, const self_type& itr)
297  {
298  return os;
299  }
300 
301 };
302 
303 
304  // end group UtilIterator
306 
307 } // CUB namespace
308 CUB_NS_POSTFIX // Optional outer namespace(s)