-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefixsum.py
206 lines (176 loc) · 5.83 KB
/
prefixsum.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
import os
import sys
localtest = True
here = os.path.dirname(os.path.abspath(__file__))
if localtest == True:
sys.path = [os.path.join(here, "..", "vulkanese", "vulkanese")] + sys.path
from vulkanese import *
import numpy as np
from platform_constants import default_constants as platformConstantsDict
here = os.path.dirname(os.path.abspath(__file__))
class PREFIX_SUM(ComputeShader):
def __init__(
self,
constantsDict,
instance,
device,
X,
devnum=0,
DEBUG=False,
buffType="float64_t",
memProperties=0
| VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
| VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
| VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
):
constantsDict["CAST_PRECISION"] = 100000
constantsDict["PROCTYPE"] = buffType
constantsDict["LOG2_THREADS_PER_WORKGROUP"] = 7
constantsDict["THREADS_PER_WORKGROUP"] = (
1 << constantsDict["LOG2_THREADS_PER_WORKGROUP"]
)
constantsDict["OPS_PER_THREAD"] = 1
constantsDict["WORKGROUP_COUNT"] = int(
np.prod(np.shape(X))
/ (constantsDict["THREADS_PER_WORKGROUP"] * constantsDict["OPS_PER_THREAD"])
)
print(constantsDict["WORKGROUP_COUNT"])
constantsDict["THREADS_PER_DISPATCH"] = (
constantsDict["THREADS_PER_WORKGROUP"] * constantsDict["WORKGROUP_COUNT"]
)
# device selection and instantiation
self.instance_inst = instance
self.device = device
self.constantsDict = constantsDict
shader_basename = "shaders/prefix"
self.dim2index = {}
memProperties = (
0
| VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
| VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
| VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
)
buffers = [
StorageBuffer(
device=self.device,
name="inbuf",
memtype=buffType,
qualifier="readonly",
dimensionNames=np.shape(X),
dimensionVals=np.shape(X),
memProperties=memProperties,
),
StorageBuffer(
device=self.device,
name="outbuf",
memtype=buffType,
qualifier="writeonly",
dimensionNames=np.shape(X),
dimensionVals=np.shape(X),
memProperties=memProperties,
),
# work_buf[0] is the tile id
# work_buf[i * 4 + 1] is the flag for tile i
# work_buf[i * 4 + 2] is the aggregate for tile i
# work_buf[i * 4 + 3] is the prefix for tile i
StorageBuffer(
device=self.device,
name="work_buf",
memtype="uint",
qualifier="",
dimensionNames=["??"],
dimensionVals=[constantsDict["WORKGROUP_COUNT"]*4],
memProperties=memProperties,
),
]
if DEBUG:
buffers += [
DebugBuffer(
device=self.device,
name="thisAdd",
dimensionNames=np.shape(X),
dimensionVals=np.shape(X),
memProperties=memProperties,
)
]
# Compute Stage: the only stage
ComputeShader.__init__(
self,
sourceFilename=os.path.join(
here, "shaders/prefix.c"
), # can be GLSL or SPIRV
parent=self.instance_inst,
constantsDict=self.constantsDict,
device=self.device,
name=shader_basename,
stage=VK_SHADER_STAGE_COMPUTE_BIT,
buffers=buffers,
DEBUG=DEBUG,
dim2index=self.dim2index,
memProperties=memProperties,
workgroupShape=[constantsDict["WORKGROUP_COUNT"], 1, 1],
compressBuffers=True,
)
def debugRun(self):
vstart = time.time()
self.run()
vlen = time.time() - vstart
print("vlen " + str(vlen))
return self.outbuf.getAsNumpyArray()
import time
def numpyTest(X):
# get numpy time, for comparison
print("--- RUNNING NUMPY TEST ---")
for i in range(10):
nstart = time.time()
nval = np.sum(X)
nlen = time.time() - nstart
print("nlen " + str(nlen))
return nval
def floatTest(X, instance, expectation):
print("--- RUNNING FLOAT TEST ---")
devnum = 0
device = instance.getDevice(devnum)
s = PREFIX_SUM(
platformConstantsDict,
instance=instance,
device=device,
X=X.astype(np.float32),
buffType="float",
memProperties=0 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
# | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
| VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
)
s.inbuf.setBuffer(X)
for i in range(10):
vval = s.debugRun()
print(expectation)
print(vval)
print(np.sum(vval))
print(np.allclose(expectation, vval/s.constantsDict["CAST_PRECISION"]))
device.release()
def float64Test(X, instance, expectation):
print("--- RUNNING FLOAT64_T TEST ---")
devnum = 0
device = instance.getDevice(devnum)
s = PREFIX_SUM(
platformConstantsDict,
instance=instance,
device=device,
X=X,
buffType="float64_t",
)
s.inbuf.setBuffer(X)
for i in range(10):
vval = s.debugRun()
print(vval)
print(np.allclose(expectation, vval/s.constantsDict["CAST_PRECISION"]))
device.release()
if __name__ == "__main__":
signalLen = 1024
X = np.arange(signalLen)
# begin GPU test
instance = Instance(verbose=False)
nval = numpyTest(X)
floatTest(X, instance, expectation=nval)
#float64Test(X, instance, expectation=nval)