forked from chreissel/hepaccelerate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysisgpu.py
299 lines (242 loc) · 9.49 KB
/
analysisgpu.py
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
#need to set these explicitly
import os
os.environ["NUMBAPRO_NVVM"] = "/usr/local/cuda-9.2/nvvm/lib64/libnvvm.so"
os.environ["NUMBAPRO_LIBDEVICE"] = "/usr/local/cuda-9.2/nvvm/libdevice/"
from numba import cuda
import cupy
import math
import numpy as np
import hepaccelerate
@cuda.jit(device=True)
def searchsorted_devfunc(arr, val):
ret = -1
#overflow
if val > arr[-1]:
return len(arr)
for i in range(len(arr)):
if val <= arr[i]:
ret = i
break
return ret
@cuda.jit
def searchsorted_kernel(vals, arr, inds_out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for i in range(xi, len(vals), xstride):
inds_out[i] = searchsorted_devfunc(arr, vals[i])
def searchsorted(arr, vals):
"""
Find indices to insert vals into arr to preserve order.
"""
ret = cupy.zeros_like(vals, dtype=cupy.int32)
searchsorted_kernel[32, 1024](vals, arr, ret)
return ret
@cuda.jit
def fill_histogram(data, weights, bins, out_w, out_w2):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for i in range(xi, len(data), xstride):
bin_idx = searchsorted_devfunc(bins, data[i])
if bin_idx >=0 and bin_idx < len(out_w):
cuda.atomic.add(out_w, bin_idx, weights[i])
cuda.atomic.add(out_w2, bin_idx, weights[i]**2)
@cuda.jit
def select_opposite_sign_muons_cudakernel(muon_charges_content, muon_charges_offsets, content_mask_in, content_mask_out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for iev in range(xi, muon_charges_offsets.shape[0]-1, xstride):
start = muon_charges_offsets[iev]
end = muon_charges_offsets[iev + 1]
ch1 = 0
idx1 = -1
ch2 = 0
idx2 = -1
for imuon in range(start, end):
if not content_mask_in[imuon]:
continue
if idx1 == -1:
ch1 = muon_charges_content[imuon]
idx1 = imuon
continue
else:
ch2 = muon_charges_content[imuon]
if (ch2 != ch1):
idx2 = imuon
content_mask_out[idx1] = 1
content_mask_out[idx2] = 1
break
return
@cuda.jit
def sum_in_offsets_cudakernel(content, offsets, mask_rows, mask_content, out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for iev in range(xi, offsets.shape[0]-1, xstride):
if not mask_rows[iev]:
continue
start = offsets[iev]
end = offsets[iev + 1]
for ielem in range(start, end):
if mask_content[ielem]:
out[iev] += content[ielem]
@cuda.jit
def max_in_offsets_cudakernel(content, offsets, mask_rows, mask_content, out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for iev in range(xi, offsets.shape[0]-1, xstride):
if not mask_rows[iev]:
continue
start = offsets[iev]
end = offsets[iev + 1]
first = True
accum = 0
for ielem in range(start, end):
if mask_content[ielem]:
if first or content[ielem] > accum:
accum = content[ielem]
first = False
out[iev] = accum
@cuda.jit
def min_in_offsets_cudakernel(content, offsets, mask_rows, mask_content, out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for iev in range(xi, offsets.shape[0]-1, xstride):
if not mask_rows[iev]:
continue
start = offsets[iev]
end = offsets[iev + 1]
first = True
accum = 0
for ielem in range(start, end):
if mask_content[ielem]:
if first or content[ielem] < accum:
accum = content[ielem]
first = False
out[iev] = accum
@cuda.jit
def get_in_offsets_cudakernel(content, offsets, indices, mask_rows, mask_content, out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for iev in range(xi, offsets.shape[0]-1, xstride):
if not mask_rows[iev]:
continue
start = offsets[iev]
end = offsets[iev + 1]
index_to_get = 0
for ielem in range(start, end):
if mask_content[ielem]:
if index_to_get == indices[iev]:
out[iev] = content[ielem]
break
else:
index_to_get += 1
@cuda.jit
def min_in_offsets_cudakernel(content, offsets, mask_rows, mask_content, out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for iev in range(xi, offsets.shape[0]-1, xstride):
if not mask_rows[iev]:
continue
start = offsets[iev]
end = offsets[iev + 1]
first = True
accum = 0
for ielem in range(start, end):
if mask_content[ielem]:
if first or content[ielem] < accum:
accum = content[ielem]
first = False
out[iev] = accum
def sum_in_offsets(struct, content, mask_rows, mask_content, dtype=None):
if not dtype:
dtype = content.dtype
sum_offsets = cupy.zeros(len(struct.offsets) - 1, dtype=dtype)
sum_in_offsets_cudakernel[32, 1024](content, struct.offsets, mask_rows, mask_content, sum_offsets)
cuda.synchronize()
return sum_offsets
def max_in_offsets(struct, content, mask_rows, mask_content):
max_offsets = cupy.zeros(len(struct.offsets) - 1, dtype=content.dtype)
max_in_offsets_cudakernel[32, 1024](content, struct.offsets, mask_rows, mask_content, max_offsets)
cuda.synchronize()
return max_offsets
def min_in_offsets(struct, content, mask_rows, mask_content):
max_offsets = cupy.zeros(len(struct.offsets) - 1, dtype=content.dtype)
min_in_offsets_cudakernel[32, 1024](content, struct.offsets, mask_rows, mask_content, max_offsets)
cuda.synchronize()
return max_offsets
def select_muons_opposite_sign(muons, in_mask):
out_mask = cupy.invert(muons.make_mask())
select_opposite_sign_muons_cudakernel[32,1024](muons.charge, muons.offsets, in_mask, out_mask)
cuda.synchronize()
return out_mask
def get_in_offsets(content, offsets, indices, mask_rows, mask_content):
out = cupy.zeros(len(offsets) - 1, dtype=content.dtype)
get_in_offsets_cudakernel[32, 1024](content, offsets, indices, mask_rows, mask_content, out)
cuda.synchronize()
return out
"""
For all events (N), mask the objects in the first collection (M1) if they are closer than dr2 to any object in the second collection (M2).
etas1: etas of the first object, array of (M1, )
phis1: phis of the first object, array of (M1, )
mask1: mask (enabled) of the first object, array of (M1, )
offsets1: offsets of the first object, array of (N, )
etas2: etas of the second object, array of (M2, )
phis2: phis of the second object, array of (M2, )
mask2: mask (enabled) of the second object, array of (M2, )
offsets2: offsets of the second object, array of (N, )
mask_out: output mask, array of (M1, )
"""
@cuda.jit
def mask_deltar_first_cudakernel(etas1, phis1, mask1, offsets1, etas2, phis2, mask2, offsets2, dr2, mask_out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for iev in range(xi, len(offsets1)-1, xstride):
a1 = offsets1[iev]
b1 = offsets1[iev+1]
a2 = offsets2[iev]
b2 = offsets2[iev+1]
for idx1 in range(a1, b1):
if not mask1[idx1]:
continue
eta1 = etas1[idx1]
phi1 = phis1[idx1]
for idx2 in range(a2, b2):
if not mask2[idx2]:
continue
eta2 = etas2[idx2]
phi2 = phis2[idx2]
deta = abs(eta1 - eta2)
dphi = (phi1 - phi2 + math.pi) % (2*math.pi) - math.pi
#if first object is closer than dr2, mask element will be *disabled*
passdr = ((deta**2 + dphi**2) < dr2)
mask_out[idx1] = mask_out[idx1] | passdr
def mask_deltar_first(objs1, mask1, objs2, mask2, drcut):
assert(mask1.shape == objs1.eta.shape)
assert(mask2.shape == objs2.eta.shape)
assert(objs1.offsets.shape == objs2.offsets.shape)
mask_out = cupy.zeros_like(objs1.eta, dtype=cupy.bool)
mask_deltar_first_cudakernel[32, 1024](
objs1.eta, objs1.phi, mask1, objs1.offsets,
objs2.eta, objs2.phi, mask2, objs2.offsets,
drcut**2, mask_out
)
cuda.synchronize()
mask_out = cupy.invert(mask_out)
return mask_out
def histogram_from_vector(data, weights, bins):
out_w = cupy.zeros(len(bins) - 1, dtype=cupy.float32)
out_w2 = cupy.zeros(len(bins) - 1, dtype=cupy.float32)
fill_histogram[32, 1024](data, weights, bins, out_w, out_w2)
return cupy.asnumpy(out_w), cupy.asnumpy(out_w2), cupy.asnumpy(bins)
@cuda.jit
def get_bin_contents_cudakernel(values, edges, contents, out):
xi = cuda.grid(1)
xstride = cuda.gridsize(1)
for i in range(xi, len(values), xstride):
v = values[i]
ibin = searchsorted_devfunc(edges, v)
if ibin>=0 and ibin < len(contents):
out[i] = contents[ibin]
def get_bin_contents(values, edges, contents, out):
assert(values.shape == out.shape)
assert(edges.shape[0] == contents.shape[0]+1)
get_bin_contents_cudakernel[32, 1024](values, edges, contents, out)