CUB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
tex_ref_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 (CUDA_VERSION >= 5050) || defined(DOXYGEN_ACTIVE) // This iterator is compatible with CUDA 5.5 and newer
46 
47 #if (THRUST_VERSION >= 100700) // This iterator is compatible with Thrust API 1.7 and newer
48  #include <thrust/iterator/iterator_facade.h>
49  #include <thrust/iterator/iterator_traits.h>
50 #endif // THRUST_VERSION
51 
52 
54 CUB_NS_PREFIX
55 
57 namespace cub {
58 
59 
60 /******************************************************************************
61  * Static file-scope Tesla/Fermi-style texture references
62  *****************************************************************************/
63 
64 #ifndef DOXYGEN_SHOULD_SKIP_THIS // Do not document
65 
66 // Anonymous namespace
67 namespace {
68 
70 template <typename T>
71 struct IteratorTexRef
72 {
74  template <int UNIQUE_ID>
75  struct TexId
76  {
77  // Largest texture word we can use in device
78  typedef typename UnitWord<T>::DeviceWord DeviceWord;
79  typedef typename UnitWord<T>::TextureWord TextureWord;
80 
81  // Number of texture words per T
82  enum {
83  DEVICE_MULTIPLE = sizeof(T) / sizeof(DeviceWord),
84  TEXTURE_MULTIPLE = sizeof(T) / sizeof(TextureWord)
85  };
86 
87  // Texture reference type
88  typedef texture<TextureWord> TexRef;
89 
90  // Texture reference
91  static TexRef ref;
92 
94  static cudaError_t BindTexture(void *d_in)
95  {
96  if (d_in)
97  {
98  cudaChannelFormatDesc tex_desc = cudaCreateChannelDesc<TextureWord>();
99  ref.channelDesc = tex_desc;
100  return (CubDebug(cudaBindTexture(NULL, ref, d_in)));
101  }
102 
103  return cudaSuccess;
104  }
105 
107  static cudaError_t UnbindTexture()
108  {
109  return CubDebug(cudaUnbindTexture(ref));
110  }
111 
113  template <typename Distance>
114  static __device__ __forceinline__ T Fetch(Distance tex_offset)
115  {
116  DeviceWord temp[DEVICE_MULTIPLE];
117  TextureWord *words = reinterpret_cast<TextureWord*>(temp);
118 
119  #pragma unroll
120  for (int i = 0; i < TEXTURE_MULTIPLE; ++i)
121  {
122  words[i] = tex1Dfetch(ref, (tex_offset * TEXTURE_MULTIPLE) + i);
123  }
124 
125  return reinterpret_cast<T&>(temp);
126  }
127  };
128 };
129 
130 // Texture reference definitions
131 template <typename T>
132 template <int UNIQUE_ID>
133 typename IteratorTexRef<T>::template TexId<UNIQUE_ID>::TexRef IteratorTexRef<T>::template TexId<UNIQUE_ID>::ref = 0;
134 
135 
136 } // Anonymous namespace
137 
138 
139 #endif // DOXYGEN_SHOULD_SKIP_THIS
140 
141 
142 
200 template <
201  typename T,
202  int UNIQUE_ID,
203  typename Offset = ptrdiff_t>
205 {
206 public:
207 
208  // Required iterator traits
210  typedef Offset difference_type;
211  typedef T value_type;
212  typedef T* pointer;
213  typedef T reference;
214 
215 #if (THRUST_VERSION >= 100700)
216  // Use Thrust's iterator categories so we can use these iterators in Thrust 1.7 (or newer) methods
217  typedef typename thrust::detail::iterator_facade_category<
218  thrust::device_system_tag,
219  thrust::random_access_traversal_tag,
220  value_type,
221  reference
222  >::type iterator_category;
223 #else
224  typedef std::random_access_iterator_tag iterator_category;
225 #endif // THRUST_VERSION
226 
227 private:
228 
229  T* ptr;
230  difference_type tex_offset;
231 
232  // Texture reference wrapper (old Tesla/Fermi-style textures)
233  typedef typename IteratorTexRef<T>::template TexId<UNIQUE_ID> TexId;
234 
235 public:
236 
238  __host__ __device__ __forceinline__ TexRefInputIterator()
239  :
240  ptr(NULL),
241  tex_offset(0)
242  {}
243 
245  cudaError_t BindTexture(
246  T *ptr,
247  size_t bytes,
248  size_t tex_offset = 0)
249  {
250  this->ptr = ptr;
251  this->tex_offset = tex_offset;
252  return TexId::BindTexture(ptr);
253  }
254 
256  cudaError_t UnbindTexture()
257  {
258  return TexId::UnbindTexture();
259  }
260 
262  __host__ __device__ __forceinline__ self_type operator++(int)
263  {
264  self_type retval = *this;
265  tex_offset++;
266  return retval;
267  }
268 
270  __host__ __device__ __forceinline__ self_type operator++()
271  {
272  tex_offset++;
273  return *this;
274  }
275 
277  __host__ __device__ __forceinline__ reference operator*() const
278  {
279 #if (CUB_PTX_ARCH == 0)
280  // Simply dereference the pointer on the host
281  return ptr[tex_offset];
282 #else
283  // Use the texture reference
284  return TexId::Fetch(tex_offset);
285 #endif
286  }
287 
289  template <typename Distance>
290  __host__ __device__ __forceinline__ self_type operator+(Distance n) const
291  {
292  self_type retval;
293  retval.ptr = ptr;
294  retval.tex_offset = tex_offset + n;
295  return retval;
296  }
297 
299  template <typename Distance>
300  __host__ __device__ __forceinline__ self_type& operator+=(Distance n)
301  {
302  tex_offset += n;
303  return *this;
304  }
305 
307  template <typename Distance>
308  __host__ __device__ __forceinline__ self_type operator-(Distance n) const
309  {
310  self_type retval;
311  retval.ptr = ptr;
312  retval.tex_offset = tex_offset - n;
313  return retval;
314  }
315 
317  template <typename Distance>
318  __host__ __device__ __forceinline__ self_type& operator-=(Distance n)
319  {
320  tex_offset -= n;
321  return *this;
322  }
323 
325  __host__ __device__ __forceinline__ difference_type operator-(self_type other) const
326  {
327  return tex_offset - other.tex_offset;
328  }
329 
331  template <typename Distance>
332  __host__ __device__ __forceinline__ reference operator[](Distance n) const
333  {
334  return *(*this + n);
335  }
336 
338  __host__ __device__ __forceinline__ pointer operator->()
339  {
340  return &(*(*this));
341  }
342 
344  __host__ __device__ __forceinline__ bool operator==(const self_type& rhs)
345  {
346  return ((ptr == rhs.ptr) && (tex_offset == rhs.tex_offset));
347  }
348 
350  __host__ __device__ __forceinline__ bool operator!=(const self_type& rhs)
351  {
352  return ((ptr != rhs.ptr) || (tex_offset != rhs.tex_offset));
353  }
354 
356  friend std::ostream& operator<<(std::ostream& os, const self_type& itr)
357  {
358  return os;
359  }
360 
361 };
362 
363 
364  // end group UtilIterator
366 
367 } // CUB namespace
368 CUB_NS_POSTFIX // Optional outer namespace(s)
369 
370 #endif // CUDA_VERSION