1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#ifndef RAND_STATE_H
#define RAND_STATE_H
// This is shared by legacy IOFX and shaders
namespace nvidia
{
namespace apex
{
struct LCG_PRNG
{
unsigned int a, c;
PX_CUDA_CALLABLE PX_INLINE LCG_PRNG()
{
}
PX_CUDA_CALLABLE PX_INLINE LCG_PRNG(unsigned int a, unsigned int c)
{
this->a = a;
this->c = c;
}
static PX_CUDA_CALLABLE PX_INLINE LCG_PRNG getIdentity()
{
return LCG_PRNG(1, 0);
}
static PX_CUDA_CALLABLE PX_INLINE LCG_PRNG getDefault()
{
return LCG_PRNG(1103515245u, 12345u);
}
PX_CUDA_CALLABLE PX_INLINE LCG_PRNG& operator *= (const LCG_PRNG& rhs)
{
a *= rhs.a;
c *= rhs.a; c += rhs.c;
return *this;
}
PX_CUDA_CALLABLE PX_INLINE LCG_PRNG leapFrog(unsigned int leap) const
{
LCG_PRNG ret = getIdentity();
for (unsigned int i = 0; i < leap; ++i)
{
ret *= (*this);
}
return ret;
}
PX_CUDA_CALLABLE PX_INLINE unsigned int operator()(unsigned int x) const
{
return x * a + c;
}
};
struct RandState
{
explicit PX_CUDA_CALLABLE PX_INLINE RandState(unsigned int seed)
{
curr = seed;
}
PX_CUDA_CALLABLE PX_INLINE unsigned int next()
{
return (curr = LCG_PRNG::getDefault()(curr));
}
PX_CUDA_CALLABLE PX_INLINE float nextFloat()
{
return float(next()) * 0.00000000023283064365386962890625f;
}
PX_CUDA_CALLABLE PX_INLINE float nextFloat(float min, float max)
{
return min + nextFloat() * (max - min);
}
private:
unsigned int curr;
};
// For CUDA PRNG
struct PRNGInfo
{
unsigned int* g_stateSpawnSeed;
nvidia::LCG_PRNG* g_randBlock;
unsigned int seed;
nvidia::LCG_PRNG randThread;
nvidia::LCG_PRNG randGrid;
};
// For CUDA PRNG: device part
#ifdef __CUDACC__
//*
#if __CUDA_ARCH__ >= 300
#define RAND_SCAN_OP(ofs) \
{ \
unsigned int a = aData[scanIdx], c = cData[scanIdx]; \
unsigned int aOfs = __shfl_up(a, ofs), cOfs = __shfl_up(c, ofs); \
if (idxInWarp >= ofs) { a = a * aOfs; c = c * aOfs + cOfs; } \
aData[scanIdx] = a; cData[scanIdx] = c; \
}
#else
#define RAND_SCAN_OP(ofs) \
{ \
unsigned int a = aData[scanIdx], c = cData[scanIdx]; \
unsigned int aOfs = aData[scanIdx - ofs], cOfs = cData[scanIdx - ofs]; \
aData[scanIdx] = a * aOfs; cData[scanIdx] = c * aOfs + cOfs; \
}
#endif
/*/
//THIS CODE CRASH ON CUDA 5.0.35
#define RAND_SCAN_OP(ofs) \
{ \
nvidia::LCG_PRNG val(aData[scanIdx], cData[scanIdx]); \
nvidia::LCG_PRNG valOfs(aData[scanIdx - ofs], cData[scanIdx - ofs]); \
val *= valOfs; \
aData[scanIdx] = val.a; cData[scanIdx] = val.c; \
}
//*/
PX_INLINE __device__ void randScanWarp(unsigned int scanIdx, volatile unsigned int* aData, volatile unsigned int* cData, unsigned int idxInWarp)
{
RAND_SCAN_OP(1);
RAND_SCAN_OP(2);
RAND_SCAN_OP(4);
RAND_SCAN_OP(8);
RAND_SCAN_OP(16);
}
PX_INLINE __device__ nvidia::LCG_PRNG randScanBlock(nvidia::LCG_PRNG val, volatile unsigned int* aData, volatile unsigned int* cData)
{
const unsigned int idx = threadIdx.x;
const unsigned int idxInWarp = idx & (WARP_SIZE-1);
const unsigned int warpIdx = (idx >> LOG2_WARP_SIZE);
//setup scan
unsigned int scanIdx = (warpIdx << (LOG2_WARP_SIZE + 1)) + idxInWarp;
//write identity
aData[scanIdx] = 1;
cData[scanIdx] = 0;
scanIdx += WARP_SIZE;
//write value
aData[scanIdx] = val.a;
cData[scanIdx] = val.c;
randScanWarp(scanIdx, aData, cData, idxInWarp);
//read value
val.a = aData[scanIdx];
val.c = cData[scanIdx];
__syncthreads();
if (idxInWarp == WARP_SIZE-1)
{
const unsigned int idxWrite = warpIdx + WARP_SIZE;
aData[idxWrite] = val.a;
cData[idxWrite] = val.c;
}
__syncthreads();
if (warpIdx == 0)
{
randScanWarp(scanIdx, aData, cData, idxInWarp);
}
__syncthreads();
if (warpIdx > 0)
{
const unsigned int idxRead = warpIdx + WARP_SIZE - 1;
const nvidia::LCG_PRNG valWarp(aData[idxRead], cData[idxRead]);
val *= valWarp;
}
return val;
}
#endif
}
} // nvidia::apex::
#endif
|