-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathruntime.py
More file actions
47 lines (43 loc) · 1.54 KB
/
runtime.py
File metadata and controls
47 lines (43 loc) · 1.54 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
import array
import math
import time
from vecpy.parser import Parser
from vecpy.compiler import Compiler
#Invokes the VecPy stack
def vectorize(func, options):
Compiler.compile(Parser.parse(func), options)
#Returns an aligned array (necessary for SSE/AVX)
def get_array(type, length, align=32, value=0):
#Check arguments
if type not in ('f', 'I'):
raise Exception('Invalid type')
if length <= 0:
raise Exception('Length must be positive')
if align < 16 or 2 ** int(math.log2(align)) != align:
raise Exception('Alignment must be a power of 2, at least 16')
#4 bytes per element
size = 4
num_elements = align // size
padding = num_elements - 1
#Allocate the (probably unaligned) buffer
buffer = array.array(type, [value for i in range(length + padding)])
#Find the offset needed to reach the boundary
base_address = buffer.buffer_info()[0]
offset = ((align - (base_address % align)) % align) // size
#Return the buffer starting at an aligned offset
return memoryview(buffer)[offset:length + offset]
#Returns aligned arrays
def get_arrays(num, type, length, align=32, value=0):
return [get_array(type, length, align, value) for i in range(num)]
#Calculates kernel runtime and speedup
def get_speedup(kernel1, kernel2):
#Execute both kernels
time1 = time.perf_counter()
kernel1()
time2 = time.perf_counter()
kernel2()
time3 = time.perf_counter()
#Return runtime and speedup
delta1 = time2 - time1
delta2 = time3 - time2
return (delta1, delta2, delta1 / delta2)