-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdependencies_templates.py
273 lines (249 loc) · 13.8 KB
/
dependencies_templates.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
from typing import Tuple
import random
from parse_input import DataClass
class DependenciesTemplates:
def __init__(self, loops_dependencies):
self.ld = loops_dependencies
def flow_dependency(self, dest_array_name: str, source_array_name: str, optimize: bool, mix_in: str) -> str:
"""
:param dest_array_name: A[a][a][a]
:param source_array_name: A[a-4][a-1][a-5]
:param optimize: True
:param mix_in: random
:return: 'A[a][a][a]=A[a-4][a-1][a-5]+B[a][a][a] '
"""
arr_name = dest_array_name.partition('[')[0]
arr_def = (arr_name, self.ld.data.all_arrays[arr_name])
if mix_in == 'random':
result = self.gen_random_part(dest_array_name, source_array_name, optimize, arr_def)
else:
result = self.gen_scalar_part(dest_array_name, source_array_name, optimize)
return result
def gen_random_part(self, dest_array_name: str, source_array_name: str, optimize: bool,
arr_def: Tuple[str, Tuple[int]]) -> str:
"""
:param self:
:param dest_array_name: 'A[a][a][a]'
:param source_array_name: 'A[a-4][a-1][a-5]'
:param optimize: True
:param arr_def: ('A', (32, 64, 32))
:return: A[a][a][a]=A[a-4][a-1][a-5]*12
"""
stmt_body = {}
if optimize:
stmt_body['destination'] = [dest_array_name]
stmt_body['source'] = [
f'{source_array_name}{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)}']
if dest_array_name in self.ld.indexed_arrays_destination.keys() and source_array_name in self.ld.indexed_arrays_source.keys():
result = ""
else:
result = stmt_body['destination'][0] + '=' + stmt_body['source'][0]
self.ld.populate_values(dest_array_name, source_array_name)
else:
stmt_body['destination'] = [dest_array_name,
f'{self.ld.get_destination_array(self.ld.data.unique_arrays_write)}'] #gen_random_stmt
stmt_body['source'] = [
f'{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)[1:]}',
f'{source_array_name}{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)}']
result = self.gen_based_on_usage_flow(stmt_body)
return result
def gen_scalar_part(self, dest_array_name: str, source_array_name: str, optimize: bool) -> str:
"""
:param dest_array_name: 'A[a][a][a]'
:param source_array_name: 'A[a-5][a-4][a-1]'
:param optimize: 'True'
:return: 'A[a][a][a]=A[a-5][a-4][a-1]-25'
"""
# print("gen_scalar_part")
# print("dest_array_name", dest_array_name, type(dest_array_name))
# print("source_array_name", source_array_name, type(source_array_name))
# print("optimize", optimize, type(optimize))
# print("dest_array_name", dest_array_name, type(dest_array_name))
stmt_body = {}
if optimize:
stmt_body['destination'] = [dest_array_name]
stmt_body['source'] = [
f'{source_array_name}{random.choice(self.ld.maths_symbols)}{self.ld.gen_random_numerical_value()}']
# result = gen_one_line_flow(stmt_body)
if dest_array_name in self.ld.indexed_arrays_destination.keys() and source_array_name in self.ld.indexed_arrays_source.keys():
result = ""
else:
result = stmt_body['destination'][0] + '=' + stmt_body['source'][0]
self.ld.populate_values(dest_array_name, source_array_name)
else:
stmt_body['destination'] = [dest_array_name, f'{self.ld.generate_variable_name("float ")}']
stmt_body['source'] = [f'{self.ld.gen_random_numerical_value()}',
f'{source_array_name}{random.choice(self.ld.maths_symbols)}{self.ld.gen_random_numerical_value()}']
result = self.gen_based_on_usage_flow(stmt_body)
# print("result", result, type(result))
return result
def gen_based_on_usage_flow(self, stmt_body):
array = stmt_body['destination'][0]
if self.satisfies_flow(array):
dest_usage = self.ld.indexed_arrays_destination[array]['first_usage_line']
source_usage = self.ld.indexed_arrays_source[array]['first_usage_line']
if dest_usage <= source_usage:
result = ""
else:
result = self.gen_full_stmt_flow(stmt_body)
elif array in self.ld.indexed_arrays_destination.keys():
result = self.gen_one_line_flow(stmt_body, 1)
else:
result = self.gen_full_stmt_flow(stmt_body)
return result
def satisfies_flow(self, array):
return array in self.ld.indexed_arrays_destination.keys() and array in self.ld.indexed_arrays_source.keys()
def gen_one_line_flow(self, stmt_body, element):
destination = stmt_body['destination'][element]
source = stmt_body['source'][element]
self.ld.populate_values(destination, source)
self.ld.inc_stmt_counter()
result = destination + '=' + source
return result
def gen_full_stmt_flow(self, stmt_body):
result = self.gen_one_line_flow(stmt_body, 0) + ';\n'
result += self.ld.add_indent() + self.gen_one_line_flow(stmt_body, 1)
return result
def anti_dependency(self, dest_array_name, source_array_name, optimize, mix_in):
arr_name = dest_array_name.partition('[')[0]
arr_def = (arr_name, self.ld.data.all_arrays[arr_name])
if mix_in == 'random':
result = self.gen_random_part_anti(dest_array_name, source_array_name, optimize, arr_def)
else:
result = self.gen_scalar_part_anti(dest_array_name, source_array_name, optimize)
return result
def gen_random_part_anti(self, dest_array_name, source_array_name, optimize, arr_def):
stmt_body = {}
if optimize:
stmt_body['destination'] = [dest_array_name]
stmt_body['source'] = [
f'{source_array_name}{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)}']
# result = gen_based_on_usage_flow(stmt_body,0)
if dest_array_name in self.ld.indexed_arrays_destination.keys() and source_array_name in self.ld.indexed_arrays_source.keys():
result = ""
else:
result = stmt_body['destination'][0] + '=' + stmt_body['source'][0]
self.ld.populate_values(stmt_body['destination'][0], stmt_body['source'][0])
else:
stmt_body['destination'] = [f'{self.ld.get_destination_array(self.ld.data.unique_arrays_write)}', #gen_random_stmt
dest_array_name]
stmt_body['source'] = [
f'{source_array_name}{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)}',
f'{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)[1:]}']
result = self.gen_based_on_usage_anti(stmt_body, source_array_name)
return result
def gen_scalar_part_anti(self, dest_array_name, source_array_name, optimize):
stmt_body = {}
if optimize:
stmt_body['destination'] = [dest_array_name]
stmt_body['source'] = [
f'{source_array_name}{random.choice(self.ld.maths_symbols)}{self.ld.gen_random_numerical_value()}']
if dest_array_name in self.ld.indexed_arrays_destination.keys() and source_array_name in self.ld.indexed_arrays_source.keys():
result = ""
# result = gen_one_line_flow(stmt_body, 0)
else:
result = stmt_body['destination'][0] + '=' + stmt_body['source'][0]
self.ld.populate_values(stmt_body['destination'][0], stmt_body['source'][0])
else:
stmt_body['destination'] = [f'{self.ld.generate_variable_name(self.ld.data.type)}', dest_array_name]
stmt_body['source'] = [
f'{source_array_name}{random.choice(self.ld.maths_symbols)}{self.ld.gen_random_numerical_value()}',
f'{self.ld.gen_random_numerical_value()}']
result = self.gen_based_on_usage_anti(stmt_body, source_array_name)
return result
def gen_based_on_usage_anti(self, stmt_body, source_array_name):
array = source_array_name
if self.satisfies_anti(array):
dest_usage = self.ld.indexed_arrays_destination[array]['first_usage_line']
source_usage = self.ld.indexed_arrays_source[array]['first_usage_line']
if source_usage < dest_usage:
result = ""
else:
result = self.gen_full_stmt_anti(stmt_body)
elif array in self.ld.indexed_arrays_destination.keys():
result = self.gen_one_line_anti(stmt_body, 1)
else:
result = self.gen_full_stmt_anti(stmt_body)
return result
def satisfies_anti(self, array):
return array in self.ld.indexed_arrays_destination.keys() and array in self.ld.indexed_arrays_source.keys()
def gen_one_line_anti(self, stmt_body, element):
destination = stmt_body['destination'][element]
source = stmt_body['source'][element]
self.ld.populate_values(destination, source)
self.ld.inc_stmt_counter()
result = destination + '=' + source
return result
def gen_full_stmt_anti(self, stmt_body):
result = self.gen_one_line_anti(stmt_body, 0) + ';\n'
result += self.ld.add_indent() + self.gen_one_line_anti(stmt_body, 1)
return result
def output_dependency(self, dest_array_name, source_array_name, __, mix_in):
arr_name = dest_array_name.partition('[')[0]
arr_def = (arr_name, self.ld.data.all_arrays[arr_name])
stmt_body = {}
if mix_in == 'random':
stmt_body['destination'] = [f'{dest_array_name}', f'{source_array_name}']
stmt_body['source'] = [
f'{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)[1:]}',
f'{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)[1:]}']
result = self.gen_based_on_usage_output(dest_array_name, stmt_body)
else:
stmt_body['destination'] = [f'{dest_array_name}', f'{source_array_name}']
stmt_body['source'] = [f'{self.ld.gen_random_numerical_value()}', f'{self.ld.gen_random_numerical_value()}']
result = self.gen_based_on_usage_output(dest_array_name, stmt_body)
return result
def gen_based_on_usage_output(self, dest_array_name, stmt_body):
if dest_array_name in self.ld.indexed_arrays_destination.keys():
dest_usage = self.ld.indexed_arrays_destination[dest_array_name]['occurrences_count']
if dest_usage >= 2:
result = ""
elif dest_usage == 1:
result = self.gen_stmt_output(stmt_body, 0)
else:
result = self.gen_stmt_output(stmt_body, 0) + ';\n'
result += self.ld.add_indent() + self.gen_stmt_output(stmt_body, 1)
return result
def gen_stmt_output(self, stmt_body, element):
destination = stmt_body['destination'][element]
source = stmt_body['source'][element]
self.ld.populate_values(destination, source)
self.ld.inc_stmt_counter()
result = destination + '=' + source
return result
def input_dependency(self, dest_array_name, source_array_name, __, mix_in):
arr_name = source_array_name.partition('[')[0]
arr_def = (arr_name, self.ld.data.all_arrays[arr_name])
stmt_body = {}
if mix_in == 'random':
stmt_body['destination'] = [f'{self.ld.get_destination_array(self.ld.data.unique_arrays_write)}',
f'{self.ld.get_destination_array(self.ld.data.unique_arrays_write)}']
stmt_body['source'] = [
f'{dest_array_name}{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)}',
f'{source_array_name}{self.ld.gen_calc_for_read(random.choice(self.ld.data.rand_num_of_calculations), arr_def)}']
result = self.gen_based_on_usage(source_array_name, arr_def, stmt_body)
else:
stmt_body['destination'] = [f'{self.ld.generate_variable_name(self.ld.data.type)}',
f'{self.ld.generate_variable_name(self.ld.data.type)}']
stmt_body['source'] = [f'{dest_array_name}{random.choice(self.ld.maths_symbols)}{self.ld.gen_random_numerical_value()}',
f'{source_array_name}{random.choice(self.ld.maths_symbols)}{self.ld.gen_random_numerical_value()}']
result = self.gen_based_on_usage(source_array_name, arr_def, stmt_body)
return result
def gen_based_on_usage(self, source_array_name, arr_def, stmt_body):
if source_array_name in self.ld.indexed_arrays_source.keys():
source_usage = self.ld.indexed_arrays_source[source_array_name]['occurrences_count']
if source_usage >= 2:
result = ""
elif source_usage == 1:
result = self.gen_stmt_input(stmt_body, 0)
else:
result = self.gen_stmt_input(stmt_body, 0) + ';\n'
result += self.ld.add_indent() + self.gen_stmt_input(stmt_body, 1)
return result
def gen_stmt_input(self, stmt_body, element):
destination = stmt_body['destination'][element]
source = stmt_body['source'][element]
self.ld.populate_values(destination, source)
result = destination + '=' + source
self.ld.inc_stmt_counter()
return result