/
carbonara.py
executable file
·372 lines (319 loc) · 13.7 KB
/
carbonara.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
# This file is carbonara library from Gnocchi (https://github.com/gnocchixyz/gnocchi)
import six
import time
import numpy
import re
import struct
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
#cuda.initialize_profiler()
UNIX_UNIVERSAL_START64 = numpy.datetime64("1970", 'ns')
ONE_SECOND = numpy.timedelta64(1, 's')
class BeforeEpochError(Exception):
"""Error raised when a timestamp before Epoch is used."""
def __init__(self, timestamp):
self.timestamp = timestamp
super(BeforeEpochError, self).__init__(
"%s is before Epoch" % timestamp)
def datetime64_to_epoch(dt):
return (dt - UNIX_UNIVERSAL_START64) / ONE_SECOND
def round_timestamp(ts, freq):
return UNIX_UNIVERSAL_START64 + numpy.floor(
(ts - UNIX_UNIVERSAL_START64) / freq) * freq
def make_timeseries(timestamps, values):
TIMESERIES_ARRAY_DTYPE = [('timestamps', '<datetime64[ns]'),
('values', '<d')]
l = len(timestamps)
if l != len(values):
raise ValueError("Timestamps and values must have the same length")
arr = numpy.zeros(l, dtype=TIMESERIES_ARRAY_DTYPE)
arr['timestamps'] = timestamps
arr['values'] = values
return arr
class TimeSerie(object):
def __init__(self, ts=None):
if ts is None:
ts = make_timeseries([], [])
self.ts = ts
def __eq__(self, other):
return (isinstance(other, TimeSerie) and
numpy.all(self.ts == other.ts))
@property
def timestamps(self):
return self.ts['timestamps']
@property
def values(self):
return self.ts['values']
@property
def first(self):
try:
return self.timestamps[0]
except IndexError:
return
@property
def last(self):
try:
return self.timestamps[-1]
except IndexError:
return
class AggregatedTimeSerie(TimeSerie):
_AGG_METHOD_PCT_RE = re.compile(r"([1-9][0-9]?)pct")
PADDED_SERIAL_LEN = struct.calcsize("<?d")
COMPRESSED_SERIAL_LEN = struct.calcsize("<Hd")
COMPRESSED_TIMESPAMP_LEN = struct.calcsize("<H")
def __init__(self, sampling, aggregation_method, ts=None):
super(AggregatedTimeSerie, self).__init__(ts)
self.sampling = sampling
self.aggregation_method = aggregation_method
def resample(self, sampling):
return AggregatedTimeSerie.from_grouped_serie(
self.group_serie(sampling), sampling, self.aggregation_method)
@classmethod
def from_data(cls, sampling, aggregation_method, timestamps,
values):
return cls(sampling=sampling,
aggregation_method=aggregation_method,
ts=make_timeseries(timestamps, values))
@staticmethod
def _get_agg_method(aggregation_method):
q = None
m = AggregatedTimeSerie._AGG_METHOD_PCT_RE.match(aggregation_method)
if m:
q = float(m.group(1))
aggregation_method_func_name = 'quantile'
else:
if not hasattr(GroupedGpuBasedTimeSeries, aggregation_method):
raise "UnknownAggregationMethod(aggregation_method)"
aggregation_method_func_name = aggregation_method
return aggregation_method_func_name, q
@classmethod
def from_grouped_serie(cls, grouped_serie, sampling, aggregation_method):
agg_name, q = cls._get_agg_method(aggregation_method)
return cls(sampling, aggregation_method,
ts=cls._resample_grouped(grouped_serie, agg_name,
q))
def group_serie(self, granularity, start=None):
# NOTE(jd) Our whole serialization system is based on Epoch, and we
# store unsigned integer, so we can't store anything before Epoch.
# Sorry!
if len(self.ts) != 0 and self.first < UNIX_UNIVERSAL_START64:
raise BeforeEpochError(self.first)
return GroupedGpuBasedTimeSeries(self.ts, granularity, start)
@staticmethod
def _resample_grouped(grouped_serie, agg_name, q=None):
agg_func = getattr(grouped_serie, agg_name)
return agg_func(q) if agg_name == 'quantile' else agg_func()
@classmethod
def benchmark(cls):
"""Run a speed benchmark!"""
POINTS_PER_SPLIT = 3600
points = POINTS_PER_SPLIT * 1000
points = 2 * 1024 * 6
points = 512*1024*6
points = 1024*1024*7
points = int(points)
sampling = numpy.timedelta64(5, 's')
resample = numpy.timedelta64(35, 's')
now = numpy.datetime64("2015-04-03 23:11")
timestamps = numpy.sort(numpy.array( [now + i * sampling for i in six.moves.range(points)]))
for title, values in [
("Simple continuous range", six.moves.range(points)),
#("Only zeros", [0] * (points)),
]:
def per_sec(t1, t0):
return 1 / ((t1 - t0) / serialize_times)
print(title)
serialize_times = 50
ts = cls.from_data(sampling, 'mean', timestamps, values)
pts = ts.ts.copy()
for agg in ['gpu_sum', 'cpu_sum' ]:
#for agg in ['sum']:
serialize_times = 3 if agg.endswith('pct') else 1
ts = cls(ts=pts, sampling=sampling,
aggregation_method=agg)
t0 = time.time()
for i in six.moves.range(serialize_times):
ts.resample(resample)
t1 = time.time()
print(" resample(%s) speed: %.2f Hz"
% (agg, per_sec(t1, t0)))
class GroupedGpuBasedTimeSeries(object):
def __init__(self, ts, granularity, start=None):
# NOTE(sileht): The whole class assumes ts is ordered and don't have
# duplicate timestamps, it uses numpy.unique that sorted list, but
# we always assume the orderd to be the same as the input.
self.granularity = granularity
self.start = start
if start is None:
self._ts = ts
self._ts_for_derive = ts
else:
self._ts = ts[numpy.searchsorted(ts['timestamps'], start):]
start_derive = start - granularity
self._ts_for_derive = ts[
numpy.searchsorted(ts['timestamps'], start_derive):
]
self.indexes = round_timestamp(self._ts['timestamps'], granularity)
self.tstamps, self.counts = numpy.unique(self.indexes,
return_counts=True)
self.data = self._ts['values']
self.data = cuda.pagelocked_empty_like(self._ts['values'].copy(order='C').astype(numpy.float32))
self.data[:] = self._ts['values'].copy(order='C').astype(numpy.float32)[:]
#self.data_gpu = cuda.mem_alloc(self.data.size * self.data.dtype.itemsize)
self.reduce_by = (self.granularity / numpy.timedelta64(5, 's'))
total_size = int(self.data.size * self.data.dtype.itemsize / self.reduce_by)
self.output = cuda.pagelocked_empty(total_size, self.data.dtype)
#self.op_gpu = cuda.mem_alloc(total_size)
def gpu_sum(self):
summod = SourceModule("""
__global__ void v1(float *a, int *i) {
int perthread=i[0];
int counter = i[0]-1;
int col = blockIdx.x * blockDim.x + threadIdx.x;
int row = blockIdx.y * blockDim.y + threadIdx.y;
int index = col * perthread + row;
for(;counter;counter--)
atomicAdd(a+index, a[index+counter]);
}
__global__ void v2(float *a, int *i) {
int perthread=i[0];
int blklimit=1024;
int blk=1;
int row, col, index;
int counter = 0;
for (blk=1;blk<blklimit;blk++) {
counter = perthread-1;
col = blockIdx.x * blockDim.x + threadIdx.x + blockDim.x*blk*perthread;
row = blockIdx.y * blockDim.y + threadIdx.y;
index = col + row;
int x = 0;
for(;counter;counter--){
x += a[index + counter];
}
a[index] = x;
}
}
/*
__global__ void v3(float *a, int *i) {
int perthread=i[0];
int gridsize = 4*4;
int blklimit=1024/gridsize;
int blk=1;
int row, col, index;
__shared__ float inter[6*1024/16];
int counter = 0;
for (blk=0;blk<blklimit;blk++) {
counter = perthread-1;
col = blockIdx.x * blockDim.x + threadIdx.x + blockDim.x*blk*perthread;
row = blockIdx.y * blockDim.y + threadIdx.y;
int tid = threadIdx.x;
for(counter=0;counter<perthread;counter++){
index = col + blockDim.x*counter + row;
inter[blockDim.x * counter + tid] = a[index];
}
__syncthreads();
counter = perthread-1;
int x = 0;
for(;counter;counter--){
x += inter[blockDim.x * counter + tid];
}
a[index] = x;
__syncthreads();
}
}
*/
__global__ void v4(float *data, int *i, float *output) {
int perthread=7;
int blklimit=128;
int blk=1;
int row, col, index;
extern __shared__ float inter[];
int counter = 0;
for (blk=0;blk<blklimit;blk++) {
counter = perthread-1;
col = blockIdx.x * blockDim.x + threadIdx.x + blockDim.x*blk*perthread;
row = blockIdx.y * blockDim.y + threadIdx.y;
int tid = threadIdx.x;
for(counter=0;counter<perthread;counter++){
index = col + blockDim.x*counter + row;
inter[blockDim.x * counter + tid] = data[index];
}
__syncthreads();
int x = 0;
for(counter=0;counter<perthread;counter++){
x += inter[blockDim.x * counter + tid];
}
index = col + row;
output[tid] = x;
__syncthreads();
}
}
""", options=['--generate-line-info'], keep=True)
t0 = time.time()
func = summod.get_function("v4")
eachblock_blksz = 128
each_stream = 1024 * 128
batches = 8
reduce_byarr = cuda.pagelocked_empty_like(numpy.array([self.reduce_by, eachblock_blksz]))
inputarray = cuda.mem_alloc(reduce_byarr.size * reduce_byarr.dtype.itemsize)
myStream = cuda.Stream()
cuda.memcpy_htod_async( inputarray, reduce_byarr, myStream)
#func.prepare([numpy.float32]*3)
holder = numpy.empty(each_stream , numpy.float32, "C")
stream_in_size = each_stream * self.data.dtype.itemsize * 7
stream_op_size = each_stream * self.data.dtype.itemsize
#func(self.data_gpu, cuda.In(numpy.array([6])), block=(1024, 1, 1), grid=(512, 1))
#func(self.data_gpu, cuda.In(numpy.array([6])), block=(1024, 1, 1), grid=(1024, 1))
streams = []
self.data_gpu = []
self.op_gpu = []
cuda.start_profiler()
for counter in range(batches):
stream = cuda.Stream()
streams.append(stream)
self.data_gpu.append(cuda.mem_alloc(stream_in_size))
self.op_gpu.append(cuda.mem_alloc(stream_op_size))
for counter in range(batches):
cuda.memcpy_htod_async(
self.data_gpu[counter],
self.data[each_stream*7*counter:each_stream*7*(counter+1) ],
streams[counter])
#cuda.memcpy_htod(self.data_gpu, self.data)
#func(self.data_gpu, cuda.In(numpy.array([6])), block=(1024, 1, 1))
# for counter in range(batches10):
#func.prepared_async_call(self.data_gpu[counter], inputarray, self.op_gpu[counter],
# func.prepared_async_call((gridsize, 1),
# (each_stream/gridsize, 1, 1),
# streams[counter],
# self.data_gpu[counter], inputarray, self.op_gpu[counter],
# shared_size=self.data.dtype.itemsize*7*each_stream,
# )
func( self.data_gpu[counter], inputarray, self.op_gpu[counter],
grid=(1, 1),
block=(each_stream/eachblock_blksz, 1, 1),
stream=streams[counter],
shared=self.data.dtype.itemsize*7*each_stream/eachblock_blksz,
)
#self.output[each_stream*counter:each_stream*(counter+1)] = cuda.from_device_like( self.op_gpu, holder, stream)
for counter in range(batches):
cuda.memcpy_dtoh_async(
self.output[each_stream*counter:each_stream*(counter+1)],
self.op_gpu[counter],
streams[counter])
#cuda.memcpy_dtoh(self.output, self.op_gpu)
[strm.synchronize() for strm in streams]
cuda.stop_profiler()
t1 = time.time()
print(" time to aggregate %0.7f msec" % (1000*(t1-t0)))
return
def cpu_sum(self):
t0 = time.time()
dat = make_timeseries(self.tstamps, numpy.bincount(
numpy.repeat(numpy.arange(self.counts.size), self.counts),
weights=self._ts['values']))
t1 = time.time()
print(" time to aggregate %0.7f msec" % (1000*(t1-t0)))
return dat
AggregatedTimeSerie.benchmark()