-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathstatespace_cuda_kernels.h
More file actions
355 lines (272 loc) · 9.25 KB
/
statespace_cuda_kernels.h
File metadata and controls
355 lines (272 loc) · 9.25 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef STATESPACE_CUDA_KERNELS_H_
#define STATESPACE_CUDA_KERNELS_H_
#ifdef __NVCC__
#include <cuda.h>
#elif __HIP__
#include <hip/hip_runtime.h>
#include "cuda2hip.h"
#endif
#include "util_cuda.h"
namespace qsim {
namespace detail {
template <typename FP1, typename FP2,
typename Op1, typename Op2, typename Op3, unsigned warp_size = 32>
__device__ __forceinline__ FP1 BlockReduce1(
uint64_t n, Op1 op1, Op2 op2, Op3 op3, const FP2* s1, const FP2* s2) {
extern __shared__ float shared[];
FP1* partial1 = (FP1*) shared;
unsigned tid = threadIdx.x;
unsigned warp = threadIdx.x / warp_size;
unsigned lane = threadIdx.x % warp_size;
uint64_t k0 = 2 * n * blockIdx.x * blockDim.x + 2 * tid - lane;
uint64_t k1 = k0 + 2 * n * blockDim.x;
FP1 r;
r = op1(s1[k0], s1[k0 + warp_size], s2[k0], s2[k0 + warp_size]);
while ((k0 += 2 * blockDim.x) < k1) {
r = op2(r, op1(s1[k0], s1[k0 + warp_size], s2[k0], s2[k0 + warp_size]));
}
partial1[tid] = r;
__shared__ FP1 partial2[warp_size];
if (tid < warp_size) {
partial2[tid] = 0;
}
__syncthreads();
FP1 val = WarpReduce(partial1[tid], op3);
if (lane == 0) {
partial2[warp] = val;
}
__syncthreads();
FP1 result = 0;
if (tid < warp_size) {
result = WarpReduce(partial2[tid], op3);
}
return result;
}
template <typename FP1, typename FP2,
typename Op1, typename Op2, typename Op3, unsigned warp_size = 32>
__device__ __forceinline__ FP1 BlockReduce1Masked(
uint64_t n, uint64_t mask, uint64_t bits, Op1 op1, Op2 op2, Op3 op3,
const FP2* s1, const FP2* s2) {
extern __shared__ float shared[];
FP1* partial1 = (FP1*) shared;
unsigned tid = threadIdx.x;
unsigned warp = threadIdx.x / warp_size;
unsigned lane = threadIdx.x % warp_size;
uint64_t k0 = 2 * n * blockIdx.x * blockDim.x + 2 * tid - lane;
uint64_t k1 = k0 + 2 * n * blockDim.x;
FP1 r = 0;
if (((k0 + lane) / 2 & mask) == bits) {
r = op1(s1[k0], s1[k0 + warp_size], s2[k0], s2[k0 + warp_size]);
}
while ((k0 += 2 * blockDim.x) < k1) {
if (((k0 + lane) / 2 & mask) == bits) {
r = op2(r, op1(s1[k0], s1[k0 + warp_size], s2[k0], s2[k0 + warp_size]));
}
}
partial1[tid] = r;
__shared__ FP1 partial2[warp_size];
if (tid < warp_size) {
partial2[tid] = 0;
}
__syncthreads();
FP1 val = WarpReduce(partial1[tid], op3);
if (lane == 0) {
partial2[warp] = val;
}
__syncthreads();
FP1 result = 0;
if (tid < warp_size) {
result = WarpReduce(partial2[tid], op3);
}
return result;
}
template <typename FP1, typename FP2,
typename Op2, typename Op3, unsigned warp_size = 32>
__device__ __forceinline__ FP1 BlockReduce2(
uint64_t n, uint64_t size, Op2 op2, Op3 op3, const FP2* s) {
extern __shared__ float shared[];
FP1* partial1 = (FP1*) shared;
unsigned tid = threadIdx.x;
uint64_t k0 = n * blockIdx.x * blockDim.x + tid;
uint64_t k1 = k0 + n * blockDim.x;
FP1 r = 0;
if (tid < size) {
r = s[k0];
while ((k0 += blockDim.x) < k1) {
r = op2(r, s[k0]);
}
}
partial1[tid] = r;
__shared__ FP1 partial2[warp_size];
if (tid < warp_size) {
partial2[tid] = 0;
}
__syncthreads();
FP1 val = WarpReduce(partial1[tid], op3);
if (threadIdx.x % warp_size == 0) {
partial2[threadIdx.x / warp_size] = val;
}
__syncthreads();
FP1 result = 0;
if (tid < warp_size) {
result = WarpReduce(partial2[tid], op3);
}
return result;
}
} // namespace detail
template <typename FP1, typename FP2, typename FP3,
typename Op1, typename Op2, typename Op3, unsigned warp_size = 32>
__global__ void Reduce1Kernel(uint64_t n, Op1 op1, Op2 op2, Op3 op3,
const FP2* s1, const FP2* s2, FP3* result) {
FP1 sum = detail::BlockReduce1<FP1>(n, op1, op2, op3, s1, s2);
if (threadIdx.x == 0) {
result[blockIdx.x] = sum;
}
}
template <typename FP1, typename FP2, typename FP3,
typename Op1, typename Op2, typename Op3, unsigned warp_size = 32>
__global__ void Reduce1MaskedKernel(uint64_t n, uint64_t mask, uint64_t bits,
Op1 op1, Op2 op2, Op3 op3,
const FP2* s1, const FP2* s2, FP3* result) {
FP1 sum =
detail::BlockReduce1Masked<FP1>(n, mask, bits, op1, op2, op3, s1, s2);
if (threadIdx.x == 0) {
result[blockIdx.x] = sum;
}
}
template <typename FP1, typename FP2, typename FP3,
typename Op2, typename Op3, unsigned warp_size = 32>
__global__ void Reduce2Kernel(
uint64_t n, uint64_t size, Op2 op2, Op3 op3, const FP2* s, FP3* result) {
FP1 sum = detail::BlockReduce2<FP1>(n, size, op2, op3, s);
if (threadIdx.x == 0) {
result[blockIdx.x] = sum;
}
}
template <typename FP, unsigned warp_size = 32>
__global__ void InternalToNormalOrderKernel(FP* state) {
unsigned lane = threadIdx.x % warp_size;
unsigned l = 2 * threadIdx.x - lane;
uint64_t k = 2 * uint64_t{blockIdx.x} * blockDim.x + l;
extern __shared__ float shared[];
FP* buf = (FP*) shared;
buf[l] = state[k];
buf[l + warp_size] = state[k + warp_size];
__syncthreads();
state[k + lane] = buf[l];
state[k + lane + 1] = buf[l + warp_size];
}
template <typename FP, unsigned warp_size = 32>
__global__ void NormalToInternalOrderKernel(FP* state) {
unsigned lane = threadIdx.x % warp_size;
unsigned l = 2 * threadIdx.x - lane;
uint64_t k = 2 * uint64_t{blockIdx.x} * blockDim.x + l;
extern __shared__ float shared[];
FP* buf = (FP*) shared;
buf[l] = state[k];
buf[l + warp_size] = state[k + warp_size];
__syncthreads();
state[k] = buf[l + lane];
state[k + warp_size] = buf[l + lane + 1];
}
template <typename FP, unsigned warp_size = 32>
__global__ void SetStateUniformKernel(FP v, uint64_t size, FP* state) {
unsigned lane = threadIdx.x % warp_size;
uint64_t k = 2 * (uint64_t{blockIdx.x} * blockDim.x + threadIdx.x) - lane;
state[k] = lane < size ? v : 0;
state[k + warp_size] = 0;
}
template <typename FP, unsigned warp_size = 32>
__global__ void AddKernel(const FP* state1, FP* state2) {
uint64_t k = uint64_t{blockIdx.x} * blockDim.x + threadIdx.x;
state2[k] += state1[k];
}
template <typename FP, unsigned warp_size = 32>
__global__ void MultiplyKernel(FP a, FP* state) {
uint64_t k = uint64_t{blockIdx.x} * blockDim.x + threadIdx.x;
state[k] *= a;
}
template <typename FP, unsigned warp_size = 32>
__global__ void CollapseKernel(uint64_t mask, uint64_t bits, FP r, FP* state) {
uint64_t k1 = uint64_t{blockIdx.x} * blockDim.x + threadIdx.x;
uint64_t k2 = 2 * k1 - threadIdx.x % warp_size;
if ((k1 & mask) == bits) {
state[k2] *= r;
state[k2 + warp_size] *= r;
} else {
state[k2] = 0;
state[k2 + warp_size] = 0;
}
}
template <typename FP, unsigned warp_size = 32>
__global__ void BulkSetAmplKernel(
uint64_t mask, uint64_t bits, FP re, FP im, bool exclude, FP* state) {
uint64_t k1 = uint64_t{blockIdx.x} * blockDim.x + threadIdx.x;
uint64_t k2 = 2 * k1 - threadIdx.x % warp_size;
bool set = ((k1 & mask) == bits) ^ exclude;
if (set) {
state[k2] = re;
state[k2 + warp_size] = im;
}
}
template <typename FP1, typename FP2, typename FP3, unsigned warp_size = 32>
__global__ void SampleKernel(unsigned num_blocks,
uint64_t n, uint64_t num_samples,
const FP1* rs, const FP2* ps, const FP3* state,
uint64_t *bitstrings) {
// Use just one thread. This can be somewhat slow.
if (threadIdx.x == 0) {
uint64_t m = 0;
double csum = 0;
for (unsigned block_id = 0; block_id < num_blocks; ++block_id) {
uint64_t km = n * blockDim.x;
uint64_t k0 = block_id * km;
for (uint64_t k = 0; k < km; ++k) {
uint64_t l = 2 * k0 + 64 * (k / 32) + k % 32;
FP3 re = state[l];
FP3 im = state[l + warp_size];
csum += re * re + im * im;
while (m < num_samples && rs[m] < csum) {
bitstrings[m++] = k0 + k;
}
}
}
}
}
template <typename FP, unsigned warp_size = 32>
__global__ void FindMeasuredBitsKernel(
uint64_t block_id, uint64_t n, double r, const FP* state, uint64_t* res) {
// Use just one thread. This can be somewhat slow, however, this is
// more or less consistent with CPU implementations.
if (threadIdx.x == 0) {
double csum = 0;
uint64_t km = n * blockDim.x;
uint64_t k0 = block_id * km;
for (uint64_t k = 0; k < km; ++k) {
uint64_t l = 2 * k0 + 64 * (k / 32) + k % 32;
FP re = state[l];
FP im = state[l + warp_size];
csum += re * re + im * im;
if (r < csum) {
*res = k0 + k;
return;
}
}
*res = k0 + n * blockDim.x - 1;
}
}
} // namespace qsim
#endif // STATESPACE_CUDA_KERNELS_H_