-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest_AST_translator.py
334 lines (257 loc) · 10.6 KB
/
test_AST_translator.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
import math
from typing import List
import unittest
import pykokkos as pk
from numpy.testing import assert_allclose
# Tests for translation of each node of type AST to C++
@pk.functor
class ASTTestReduceFunctor:
def __init__(self, threads: int, i_1: int, i_2: int, b_1: bool, b_2: bool):
self.threads: int = threads
self.i_1: int = i_1
self.i_2: int = i_2
self.b_1: bool = b_1
self.b_2: bool = b_2
self.view1D: pk.View1D[pk.int32] = pk.View([threads], pk.int32)
self.view2D: pk.View2D[pk.int32] = pk.View([threads, threads], pk.int32)
self.view3D: pk.View3D[pk.int32] = pk.View([threads, threads, threads], pk.int32)
@pk.workunit
def assign(self, tid: int, acc: pk.Acc[pk.double]) -> None:
x: int = self.i_1
acc += x + self.i_2
@pk.workunit
def aug_assign(self, tid: int, acc: pk.Acc[pk.double]) -> None:
x: int = self.i_1
x += self.i_2
acc += x
@pk.workunit
def math_constants(self, tid: int, acc: pk.Acc[pk.double]) -> None:
acc += math.pi + math.e + math.tau
@pk.workunit
def constants(self, tid: int) -> None:
int_constant: int = 5
bool_constant: bool = True
@pk.workunit
def subscript(self, tid: int) -> None:
self.view1D[tid] = self.i_1
for i in range(self.threads):
self.view2D[tid][i] = self.i_1
for i in range(self.threads):
for j in range(self.threads):
self.view3D[tid][i][j] = self.i_1
@pk.workunit
def bin_op(self, tid: int, acc: pk.Acc[pk.double]) -> None:
acc += self.i_1 + self.i_2
@pk.workunit
def unary_op(self, tid: int, acc: pk.Acc[pk.double]) -> None:
acc += (+ self.i_1)
@pk.workunit
def compare(self, tid: int, acc: pk.Acc[pk.double]) -> None:
if self.i_1 > self.i_2:
acc += self.i_1
else:
acc += self.i_2
@pk.workunit
def bool_op(self, tid: int, acc: pk.Acc[pk.double]) -> None:
if not self.b_1:
acc += self.i_1
else:
acc += self.i_2
@pk.workunit
def for_stmt(self, tid: int, acc: pk.Acc[pk.double]) -> None:
for i in range(self.i_1):
acc += self.i_2
@pk.workunit
def for_step_stmt(self, tid: int, acc: pk.Acc[pk.double]) -> None:
for i in range(self.i_2, self.i_1, self.i_2):
acc += self.i_2
@pk.workunit
def if_stmt(self, tid: int, acc: pk.Acc[pk.double]) -> None:
if self.b_1:
acc += self.i_1
acc += self.i_2
@pk.workunit
def elif_stmt(self, tid: int, acc: pk.Acc[pk.double]) -> None:
if self.b_1:
acc += self.i_1
elif self.b_2:
acc += self.i_2
@pk.workunit
def if_else_stmt(self, tid: int, acc: pk.Acc[pk.double]) -> None:
if self.b_1:
acc += self.i_1
else:
acc += self.i_2
@pk.workunit
def while_stmt(self, tid: int, acc: pk.Acc[pk.double]) -> None:
x: int = 0
while x < self.i_1:
acc += self.i_2
x += 1
@pk.workunit
def pass_stmt(self, tid: int) -> None:
pass
@pk.workunit
def docstring(self, tid: int) -> None:
"""
Test docstring
"""
@pk.workunit
def call(self, tid: int, acc: pk.Acc[pk.double]) -> None:
pk.printf("Testing printf: %d\n", self.i_1)
acc += abs(- self.i_1)
@pk.workunit
def break_stmt(self, tid: int, acc: pk.Acc[pk.double]) -> None:
for i in range(self.i_1):
acc += self.i_2
break
@pk.workunit
def continue_stmt(self, tid: int, acc: pk.Acc[pk.double]) -> None:
for i in range(self.i_1):
acc += self.i_2
continue
@pk.workunit
def ann_assign(self, tid: int, acc: pk.Acc[pk.double]) -> None:
x: int = self.i_1
acc += x
@pk.workunit
def list_type(self, tid: int, acc: pk.Acc[pk.double]) -> None:
x: List[int] = [self.i_1, self.i_2]
acc += x[0]
acc += x[1]
class TestASTTranslator(unittest.TestCase):
def setUp(self):
self.threads: int = 50
self.i_1: int = 7
self.i_2: int = 2
self.b_1: bool = False
self.b_2: bool = True
self.functor = ASTTestReduceFunctor(self.threads,
self.i_1, self.i_2,
self.b_1, self.b_2)
self.range_policy = pk.RangePolicy(pk.ExecutionSpace.Default, 0, self.threads)
def test_assign(self):
expected_result: int = self.threads * (self.i_1 + self.i_2)
result: int = pk.parallel_reduce(self.range_policy, self.functor.assign)
self.assertEqual(expected_result, result)
def test_aug_assign(self):
expected_result: int = self.threads * (self.i_1 + self.i_2)
result: int = pk.parallel_reduce(self.range_policy, self.functor.aug_assign)
self.assertEqual(expected_result, result)
def test_math_constants(self):
expected_result: float = self.threads * (math.pi + math.e + math.tau)
result: float = pk.parallel_reduce(self.range_policy, self.functor.math_constants)
assert_allclose(expected_result, result)
# def test_constants(self):
# self.assertEqual(5, self.workload.int_constant)
# self.assertEqual(True, self.workload.bool_constant)
def test_subscript(self):
expected_result = self.i_1
pk.parallel_for(self.range_policy, self.functor.subscript)
for i in range(self.threads):
self.assertEqual(expected_result, self.functor.view1D[i])
for j in range(self.threads):
self.assertEqual(expected_result, self.functor.view2D[i][j])
for k in range(self.threads):
self.assertEqual(expected_result, self.functor.view3D[i][j][k])
def test_bin_op(self):
expected_result: int = self.threads * (self.i_1 + self.i_2)
result: int = pk.parallel_reduce(self.range_policy, self.functor.bin_op)
self.assertEqual(expected_result, result)
def test_unary_op(self):
expected_result: int = self.threads * (self.i_1)
result: int = pk.parallel_reduce(self.range_policy, self.functor.unary_op)
self.assertEqual(expected_result, result)
def test_compare(self):
if self.i_1 > self.i_2:
expected_result: int = self.threads * (self.i_1)
else:
expected_result: int = self.threads * (self.i_2)
result: int = pk.parallel_reduce(self.range_policy, self.functor.compare)
self.assertEqual(expected_result, result)
def test_bool_op(self):
if not self.b_1:
expected_result: int = self.threads * (self.i_1)
else:
expected_result: int = self.threads * (self.i_2)
result: int = pk.parallel_reduce(self.range_policy, self.functor.bool_op)
self.assertEqual(expected_result, result)
def test_for_stmt(self):
expected_result: int = 0
for i in range(self.i_1):
expected_result += self.threads * self.i_2
result: int = pk.parallel_reduce(self.range_policy, self.functor.for_stmt)
self.assertEqual(expected_result, result)
def test_for_step_stmt(self):
expected_result: int = 0
for i in range(self.i_2, self.i_1, self.i_2):
expected_result += self.threads * self.i_2
result: int = pk.parallel_reduce(self.range_policy, self.functor.for_step_stmt)
self.assertEqual(expected_result, result)
def test_if_stmt(self):
expected_result: int = 0
if self.b_1:
expected_result += self.threads * self.i_1
expected_result += self.threads * self.i_2
result: int = pk.parallel_reduce(self.range_policy, self.functor.if_stmt)
self.assertEqual(expected_result, result)
def test_elif_stmt(self):
expected_result: int = 0
if self.b_1:
expected_result += self.threads * self.i_1
elif self.b_2:
expected_result += self.threads * self.i_2
result: int = pk.parallel_reduce(self.range_policy, self.functor.elif_stmt)
self.assertEqual(expected_result, result)
def test_if_else_stmt(self):
if self.b_1:
expected_result: int = self.threads * self.i_1
else:
expected_result: int = self.threads * self.i_2
result: int = pk.parallel_reduce(self.range_policy, self.functor.if_else_stmt)
self.assertEqual(expected_result, result)
def test_while_stmt(self):
x: int = 0
expected_result: int = 0
while x < self.i_1:
expected_result += self.threads * self.i_2
x += 1
result: int = pk.parallel_reduce(self.range_policy, self.functor.while_stmt)
self.assertEqual(expected_result, result)
def test_pass(self):
pk.parallel_for(self.range_policy, self.functor.pass_stmt)
def test_call(self):
expected_result: int = self.threads * abs(- self.i_1)
result: int = pk.parallel_reduce(self.range_policy, self.functor.call)
self.assertEqual(expected_result, result)
def test_break(self):
expected_result: int = 0
for i in range(self.i_1):
expected_result += self.threads * self.i_2
break
result: int = pk.parallel_reduce(self.range_policy, self.functor.break_stmt)
self.assertEqual(expected_result, result)
def test_continue(self):
expected_result: int = 0
for i in range(self.i_1):
expected_result += self.threads * self.i_2
continue
result: int = pk.parallel_reduce(self.range_policy, self.functor.continue_stmt)
self.assertEqual(expected_result, result)
def test_ann_assign(self):
expected_result: int = self.threads * self.i_1
result: int = pk.parallel_reduce(self.range_policy, self.functor.ann_assign)
self.assertEqual(expected_result, result)
def test_list_type(self):
expected_result: int = self.threads * (self.i_1 + self.i_2)
result: int = pk.parallel_reduce(self.range_policy, self.functor.list_type)
self.assertEqual(expected_result, result)
@pk.workunit
def scratch_with_double_float(team_member: pk.TeamMember):
scratch_mem_d: pk.ScratchView1D[double] = pk.ScratchView1D(team_member.team_scratch(0))
scratch_mem_f: pk.ScratchView1D[float] = pk.ScratchView1D(team_member.team_scratch(0))
def test_gh_180():
pk.parallel_for("double_float_scratch",
pk.TeamPolicy(2, 2), scratch_with_double_float)
if __name__ == "__main__":
unittest.main()