PeriDyno 1.0.0
Loading...
Searching...
No Matches
SharedMemory.h
Go to the documentation of this file.
1/*
2 * Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
3 *
4 * Please refer to the NVIDIA end user license agreement (EULA) associated
5 * with this source code for terms and conditions that govern your use of
6 * this software. Any use, reproduction, disclosure, or distribution of
7 * this software and related documentation outside the terms of the EULA
8 * is strictly prohibited.
9 *
10 */
11
12#ifndef _SHAREDMEM_H_
13#define _SHAREDMEM_H_
14
15//****************************************************************************
16// Because dynamically sized shared memory arrays are declared "extern",
17// we can't templatize them directly. To get around this, we declare a
18// simple wrapper struct that will declare the extern array with a different
19// name depending on the type. This avoids compiler errors about duplicate
20// definitions.
21//
22// To use dynamically allocated shared memory in a templatized __global__ or
23// __device__ function, just replace code like this:
24//
25//
26// template<class T>
27// __global__ void
28// foo( T* g_idata, T* g_odata)
29// {
30// // Shared mem size is determined by the host app at run time
31// extern __shared__ T sdata[];
32// ...
33// doStuff(sdata);
34// ...
35// }
36//
37// With this
38// template<class T>
39// __global__ void
40// foo( T* g_idata, T* g_odata)
41// {
42// // Shared mem size is determined by the host app at run time
43// SharedMemory<T> smem;
44// T* sdata = smem.getPointer();
45// ...
46// doStuff(sdata);
47// ...
48// }
49//****************************************************************************
50
51// This is the un-specialized struct. Note that we prevent instantiation of this
52// struct by putting an undefined symbol in the function body so it won't compile.
53template <typename T>
55{
56 // Ensure that we won't compile any un-specialized types
57 __device__ T *getPointer()
58 {
59 extern __device__ void error(void);
60 error();
61 return NULL;
62 }
63};
64
65// Following are the specializations for the following types.
66// int, uint, char, uchar, short, ushort, long, ulong, bool, float, and double
67// One could also specialize it for user-defined types.
68
69template <>
70struct SharedMemory <int>
71{
72 __device__ int *getPointer()
73 {
74 extern __shared__ int s_int[];
75 return s_int;
76 }
77};
78
79template <>
80struct SharedMemory <unsigned int>
81{
82 __device__ unsigned int *getPointer()
83 {
84 extern __shared__ unsigned int s_uint[];
85 return s_uint;
86 }
87};
88
89template <>
90struct SharedMemory <char>
91{
92 __device__ char *getPointer()
93 {
94 extern __shared__ char s_char[];
95 return s_char;
96 }
97};
98
99template <>
100struct SharedMemory <unsigned char>
101{
102 __device__ unsigned char *getPointer()
103 {
104 extern __shared__ unsigned char s_uchar[];
105 return s_uchar;
106 }
107};
108
109template <>
110struct SharedMemory <short>
111{
112 __device__ short *getPointer()
113 {
114 extern __shared__ short s_short[];
115 return s_short;
116 }
117};
118
119template <>
120struct SharedMemory <unsigned short>
121{
122 __device__ unsigned short *getPointer()
123 {
124 extern __shared__ unsigned short s_ushort[];
125 return s_ushort;
126 }
127};
128
129template <>
130struct SharedMemory <long>
131{
132 __device__ long *getPointer()
133 {
134 extern __shared__ long s_long[];
135 return s_long;
136 }
137};
138
139template <>
140struct SharedMemory <unsigned long>
141{
142 __device__ unsigned long *getPointer()
143 {
144 extern __shared__ unsigned long s_ulong[];
145 return s_ulong;
146 }
147};
148
149template <>
150struct SharedMemory <bool>
151{
152 __device__ bool *getPointer()
153 {
154 extern __shared__ bool s_bool[];
155 return s_bool;
156 }
157};
158
159template <>
160struct SharedMemory <float>
161{
162 __device__ float *getPointer()
163 {
164 extern __shared__ float s_float[];
165 return s_float;
166 }
167};
168
169template <>
170struct SharedMemory <double>
171{
172 __device__ double *getPointer()
173 {
174 extern __shared__ double s_double[];
175 return s_double;
176 }
177};
178
179
180#endif //_SHAREDMEM_H_
#define T(t)
__device__ bool * getPointer()
__device__ char * getPointer()
__device__ double * getPointer()
__device__ float * getPointer()
__device__ int * getPointer()
__device__ long * getPointer()
__device__ short * getPointer()
__device__ unsigned char * getPointer()
__device__ unsigned int * getPointer()
__device__ unsigned long * getPointer()
__device__ unsigned short * getPointer()
__device__ T * getPointer()