NVBIO
Main Page
Modules
Classes
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
nvbio
basic
cuda
ldg.h
Go to the documentation of this file.
1
/*
2
* nvbio
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
#pragma once
29
30
#if defined(__CUDACC__)
31
#include <cub/cub.cuh>
32
#endif
33
34
#include <
nvbio/basic/types.h
>
35
#include <
nvbio/basic/iterator.h
>
36
37
namespace
nvbio {
38
namespace
cuda {
39
43
template
<
typename
T>
44
struct
ldg_pointer
45
{
46
typedef
T
value_type
;
47
typedef
value_type
reference
;
48
typedef
value_type
const_reference
;
49
typedef
value_type
*
pointer
;
50
typedef
typename
std::iterator_traits<const T*>::difference_type
difference_type
;
51
typedef
std::random_access_iterator_tag
iterator_category
;
52
55
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
56
ldg_pointer
() {}
57
60
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
61
ldg_pointer
(
const
T* base) :
m_base
( base ) {}
62
65
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
66
ldg_pointer
(
const
ldg_pointer
& it) :
m_base
( it.
m_base
) {}
67
70
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
71
value_type
operator[]
(
const
uint32
i)
const
72
{
73
#if __CUDA_ARCH__ >= 350
74
return
__ldg(
m_base
+ i );
75
#else
76
return
m_base
[i];
77
#endif
78
}
79
82
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
83
value_type
operator*
()
const
84
{
85
#if __CUDA_ARCH__ >= 350
86
return
__ldg(
m_base
);
87
#else
88
return
*
m_base
;
89
#endif
90
}
91
94
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
95
ldg_pointer<T>
&
operator++
()
96
{
97
++
m_base
;
98
return
*
this
;
99
}
100
103
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
104
ldg_pointer<T>
operator++
(
int
i)
105
{
106
ldg_pointer<T>
r(
m_base
);
107
++
m_base
;
108
return
r;
109
}
110
113
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
114
ldg_pointer<T>
&
operator--
()
115
{
116
--
m_base
;
117
return
*
this
;
118
}
119
122
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
123
ldg_pointer<T>
operator--
(
int
i)
124
{
125
ldg_pointer<T>
r(
m_base
);
126
--
m_base
;
127
return
r;
128
}
129
132
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
133
ldg_pointer<T>
operator+
(
const
difference_type
i)
const
134
{
135
return
ldg_pointer
(
m_base
+ i );
136
}
137
140
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
141
ldg_pointer<T>
operator-
(
const
difference_type
i)
const
142
{
143
return
ldg_pointer
(
m_base
- i );
144
}
145
148
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
149
ldg_pointer<T>
&
operator+=
(
const
difference_type
i)
150
{
151
m_base
+= i;
152
return
*
this
;
153
}
154
157
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
158
ldg_pointer<T>
&
operator-=
(
const
difference_type
i)
159
{
160
m_base
-= i;
161
return
*
this
;
162
}
163
166
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
167
difference_type
operator-
(
const
ldg_pointer<T>
it)
const
168
{
169
return
m_base
- it.
m_base
;
170
}
171
174
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
175
ldg_pointer
&
operator=
(
const
ldg_pointer<T>
& it)
176
{
177
m_base
= it.
m_base
;
178
return
*
this
;
179
}
180
181
const
T*
m_base
;
182
};
183
186
template
<
typename
T>
187
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
188
ldg_pointer<T>
make_ldg_pointer
(
const
T* it)
189
{
190
return
ldg_pointer<T>
( it );
191
}
192
193
197
enum
CacheLoadModifier
198
{
199
LOAD_DEFAULT
,
200
LOAD_CA
,
201
LOAD_CG
,
202
LOAD_CS
,
203
LOAD_CV
,
204
LOAD_LDG
,
205
LOAD_VOLATILE
,
206
};
207
208
#if defined(__CUDACC__)
209
template
<CacheLoadModifier MOD>
struct
cub_load_mod {};
210
211
template
<>
struct
cub_load_mod<
LOAD_DEFAULT
> {
static
const
cub::CacheLoadModifier
MOD
=
cub::LOAD_DEFAULT
; };
212
template
<>
struct
cub_load_mod<
LOAD_CA
> {
static
const
cub::CacheLoadModifier
MOD
=
cub::LOAD_CA
; };
213
template
<>
struct
cub_load_mod<
LOAD_CG
> {
static
const
cub::CacheLoadModifier
MOD
=
cub::LOAD_CG
; };
214
template
<>
struct
cub_load_mod<
LOAD_CS
> {
static
const
cub::CacheLoadModifier
MOD
=
cub::LOAD_CS
; };
215
template
<>
struct
cub_load_mod<
LOAD_CV
> {
static
const
cub::CacheLoadModifier
MOD
=
cub::LOAD_CV
; };
216
template
<>
struct
cub_load_mod<
LOAD_LDG
> {
static
const
cub::CacheLoadModifier
MOD
=
cub::LOAD_LDG
; };
217
#endif
218
222
template
<
typename
T, CacheLoadModifier MOD>
223
struct
load_pointer
224
{
225
typedef
T
value_type
;
226
typedef
value_type
reference
;
227
typedef
value_type
const_reference
;
228
typedef
value_type
*
pointer
;
229
typedef
typename
std::iterator_traits<const T*>::difference_type
difference_type
;
230
typedef
std::random_access_iterator_tag
iterator_category
;
231
234
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
235
load_pointer
() {}
236
239
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
240
load_pointer
(
const
T* base) :
m_base
( base ) {}
241
244
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
245
load_pointer
(
const
load_pointer
& it) :
m_base
( it.
m_base
) {}
246
249
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
250
value_type
operator[]
(
const
uint32
i)
const
251
{
252
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 350
253
return
cub::ThreadLoad<cub_load_mod<MOD>::MOD
>(
const_cast<
T*
>
(
m_base
+ i) );
254
#else
255
return
m_base
[i];
256
#endif
257
}
258
261
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
262
value_type
operator*
()
const
263
{
264
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 350
265
return
cub::ThreadLoad<cub_load_mod<MOD>::MOD
>(
const_cast<
T*
>
(
m_base
) );
266
#else
267
return
*
m_base
;
268
#endif
269
}
270
273
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
274
load_pointer<T,MOD>
&
operator++
()
275
{
276
++
m_base
;
277
return
*
this
;
278
}
279
282
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
283
load_pointer<T,MOD>
operator++
(
int
i)
284
{
285
load_pointer<T,MOD>
r(
m_base
);
286
++
m_base
;
287
return
r;
288
}
289
292
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
293
load_pointer<T,MOD>
&
operator--
()
294
{
295
--
m_base
;
296
return
*
this
;
297
}
298
301
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
302
load_pointer<T,MOD>
operator--
(
int
i)
303
{
304
load_pointer<T,MOD>
r(
m_base
);
305
--
m_base
;
306
return
r;
307
}
308
311
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
312
load_pointer<T,MOD>
operator+
(
const
difference_type
i)
const
313
{
314
return
load_pointer
(
m_base
+ i );
315
}
316
319
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
320
load_pointer<T,MOD>
operator-
(
const
difference_type
i)
const
321
{
322
return
load_pointer
(
m_base
- i );
323
}
324
327
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
328
load_pointer<T,MOD>
&
operator+=
(
const
difference_type
i)
329
{
330
m_base
+= i;
331
return
*
this
;
332
}
333
336
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
337
load_pointer<T,MOD>
&
operator-=
(
const
difference_type
i)
338
{
339
m_base
-= i;
340
return
*
this
;
341
}
342
345
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
346
difference_type
operator-
(
const
load_pointer<T,MOD>
it)
const
347
{
348
return
m_base
- it.
m_base
;
349
}
350
353
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
354
load_pointer
&
operator=
(
const
load_pointer<T,MOD>
& it)
355
{
356
m_base
= it.
m_base
;
357
return
*
this
;
358
}
359
360
const
T*
m_base
;
361
};
362
365
template
<CacheLoadModifier MOD,
typename
T>
366
NVBIO_FORCEINLINE
NVBIO_HOST_DEVICE
367
load_pointer<T,MOD>
make_load_pointer
(
const
T* it)
368
{
369
return
load_pointer<T,MOD>
( it );
370
}
371
372
}
// namespace cuda
373
}
// namespace nvbio
Generated on Wed Feb 25 2015 08:32:54 for NVBIO by
1.8.4