Debian Codegraph @

Samples/2_Concepts_and_Techniques/MC_EstimatePiInlineP/inc/cudasharedmem.h

raw
1 /* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of NVIDIA CORPORATION nor the names of its
12 * contributors may be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef CUDASHAREDMEM_H
29 #define CUDASHAREDMEM_H
30
31 //****************************************************************************
32 // Because dynamically sized shared memory arrays are declared "extern",
33 // we can't templatize them directly. To get around this, we declare a
34 // simple wrapper struct that will declare the extern array with a different
35 // name depending on the type. This avoids compiler errors about duplicate
36 // definitions.
37 //
38 // To use dynamically allocated shared memory in a templatized __global__ or
39 // __device__ function, just replace code like this:
40 // template<class T>
41 // __global__ void
42 // foo( T* g_idata, T* g_odata)
43 // {
44 // // Shared mem size is determined by the host app at run time
45 // extern __shared__ T sdata[];
46 // ...
47 // x = sdata[i];
48 // sdata[i] = x;
49 // ...
50 // }
51 //
52 // With this:
53 // template<class T>
54 // __global__ void
55 // foo( T* g_idata, T* g_odata)
56 // {
57 // // Shared mem size is determined by the host app at run time
58 // SharedMemory<T> sdata;
59 // ...
60 // x = sdata[i];
61 // sdata[i] = x;
62 // ...
63 // }
64 //****************************************************************************
65
66 // This is the un-specialized struct. Note that we prevent instantiation of
67 // this struct by making it abstract (i.e. with pure virtual methods).
68 template <typename T>
69 struct SharedMemory {
70 // Ensure that we won't compile any un-specialized types
71 virtual __device__ T &operator*() = 0;
72 virtual __device__ T &operator[](int i) = 0;
73 };
74
75 #define BUILD_SHAREDMEMORY_TYPE(t, n) \
76 template <> \
77 struct SharedMemory<t> { \
78 __device__ t &operator*() { \
79 extern __shared__ t n[]; \
80 return *n; \
81 } \
82 __device__ t &operator[](int i) { \
83 extern __shared__ t n[]; \
84 return n[i]; \
85 } \
86 }
87
88 BUILD_SHAREDMEMORY_TYPE(int, s_int);
89 BUILD_SHAREDMEMORY_TYPE(unsigned int, s_uint);
90 BUILD_SHAREDMEMORY_TYPE(char, s_char);
91 BUILD_SHAREDMEMORY_TYPE(unsigned char, s_uchar);
92 BUILD_SHAREDMEMORY_TYPE(short, s_short);
93 BUILD_SHAREDMEMORY_TYPE(unsigned short, s_ushort);
94 BUILD_SHAREDMEMORY_TYPE(long, s_long);
95 BUILD_SHAREDMEMORY_TYPE(unsigned long, s_ulong);
96 BUILD_SHAREDMEMORY_TYPE(bool, s_bool);
97 BUILD_SHAREDMEMORY_TYPE(float, s_float);
98 BUILD_SHAREDMEMORY_TYPE(double, s_double);
99
100 #endif