/
streamoriginal.py
163 lines (130 loc) · 4.65 KB
/
streamoriginal.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
import loopy as lp
import numpy as np
import numpy.linalg as la
import ctypes
import ctypes.util
import os
from time import time
from tempfile import TemporaryDirectory
from loopy.tools import (empty_aligned, address_from_numpy,
build_ispc_shared_lib, cptr_from_numpy)
def transform(knl, vars, stream_dtype):
vars = [v.strip() for v in vars.split(",")]
knl = lp.assume(knl, "n>0")
knl = lp.split_iname(
knl, "i", 2**18, outer_tag="g.0", slabs=(0, 1))
knl = lp.split_iname(knl, "i_inner", 8, inner_tag="l.0")
knl = lp.add_and_infer_dtypes(knl, {
var: stream_dtype
for var in vars
})
knl = lp.set_argument_order(knl, vars + ["n"])
return knl
def gen_code(knl):
knl = lp.preprocess_kernel(knl)
knl = lp.get_one_scheduled_kernel(knl)
codegen_result = lp.generate_code_v2(knl)
return codegen_result.device_code() + "\n" + codegen_result.host_code()
NRUNS = 50
ALIGN_TO = 4096
ARRAY_SIZE = 2**28
STREAM_DTYPE = np.float32
STREAM_CTYPE = ctypes.c_float
INDEX_DTYPE = np.int32
INDEX_CTYPE = ctypes.c_int
def main(experiment):
print()
print("Task: ", experiment)
with open("tests/tasksys.cpp", "r") as ts_file:
tasksys_source = ts_file.read()
def make_knl(name, insn, vars):
knl = lp.make_kernel(
"{[i]: 0<=i<n}",
insn,
target=lp.ISPCTarget(), index_dtype=INDEX_DTYPE,
name="stream_"+name+"_tasks")
knl = transform(knl, vars, STREAM_DTYPE)
return knl
if experiment == "triad":
init_knl = make_knl("init", """
a[i] = 1
b[i] = 3
c[i] = 0
scalar = 7
""", "a,b,c,scalar")
triad_knl = make_knl("triad", """
a[i] = b[i] + scalar * c[i]
""", "a,b,c,scalar")
else:
init_knl = make_knl("init", """
a[i] = 1
b[i] = 9
""", "a,b")
copy_knl = make_knl("copy", """
a[i] = b[i]
""", "a,b")
with TemporaryDirectory() as tmpdir:
if experiment == "copy":
ispc_code = gen_code(init_knl) + gen_code(copy_knl)
else:
ispc_code = gen_code(init_knl) + gen_code(triad_knl)
print(ispc_code)
build_ispc_shared_lib(
tmpdir,
[("stream.ispc", ispc_code)],
[("tasksys.cpp", tasksys_source)],
cxx_options=["-g", "-fopenmp", "-DISPC_USE_OMP"],
ispc_options=([
"-g", "-O0", "--no-omit-frame-pointer",
"--target=avx2-i32x8",
#"--opt=force-aligned-memory",
"--opt=disable-loop-unroll",
#"--opt=fast-math",
"--opt=disable-fma",
"--addressing=32",
]
),
ispc_bin= "/home/ubuntu-boot/Desktop/ispc-v1.9.1-linux/ispc",
quiet=True,
)
knl_lib = ctypes.cdll.LoadLibrary(os.path.join(tmpdir, "shared.so"))
scalar = 5
a = empty_aligned(ARRAY_SIZE, dtype=STREAM_DTYPE, n=ALIGN_TO)
b = empty_aligned(ARRAY_SIZE, dtype=STREAM_DTYPE, n=ALIGN_TO)
c = empty_aligned(ARRAY_SIZE, dtype=STREAM_DTYPE, n=ALIGN_TO)
#print( hex(address_from_numpy(a)),
# hex(address_from_numpy(b)),
# hex(address_from_numpy(c)))
assert address_from_numpy(a) % ALIGN_TO == 0
assert address_from_numpy(b) % ALIGN_TO == 0
assert address_from_numpy(c) % ALIGN_TO == 0
if experiment == "copy":
g = knl_lib.stream_copy_tasks
x = [cptr_from_numpy(a),
cptr_from_numpy(b),
INDEX_CTYPE(ARRAY_SIZE),]
else:
g = knl_lib.stream_triad_tasks
x = [cptr_from_numpy(a),
cptr_from_numpy(b),
cptr_from_numpy(c),
STREAM_CTYPE(scalar),
INDEX_CTYPE(ARRAY_SIZE),]
for i in range(4):
g(*x)
def call_kernel():
g(*x)
for i in range(3):
call_kernel()
start_time = time()
for irun in range(NRUNS):
call_kernel()
elapsed = time() - start_time
print("Avg Time: ", elapsed/NRUNS)
by = 3 if experiment == "triad" else 2
print("MB: ", 1e-9*by*a.nbytes*NRUNS/elapsed, "GB/s")
#assert la.norm(a-b+scalar*c, np.inf) < np.finfo(STREAM_DTYPE).eps * 10
if __name__ == "__main__":
main("triad")
main("copy")
# vim: foldmethod=marker