-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaispeed.py
234 lines (165 loc) · 9.48 KB
/
chaispeed.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
# chaispeed.py
import logging
from collections import defaultdict
from scope.chaivars import *
from scope.chaiflow import *
class ChaiSpeed:
def __init__(self, global_variables, memory_indexes, next_free_memory_index):
self.global_variables = global_variables
self.memory_indexes = memory_indexes
self.next_free_memory_index = next_free_memory_index
def get_object_from_memory(self, pidentifier):
"""
Returns the object from memory
:param pidentifier: object identifier
:return: object
"""
return self.global_variables[pidentifier]
def count_occurences(self, parse_tree):
"""
Counts the number of occurences of variables.
:return:
"""
occurences = defaultdict(int)
for operation in parse_tree:
if isinstance(operation, Read):
occurences[operation.to_variable.pidentifier] += 1
elif isinstance(operation, Write):
if isinstance(operation.from_variable, Int):
occurences[operation.from_variable.pidentifier] += 1
elif isinstance(operation.from_variable, IntArrayElement):
occurences[operation.from_variable.array.pidentifier] += 1
if isinstance(operation.from_variable.value_holder, Int):
occurences[operation.from_variable.value_holder.pidentifier] += 1
elif isinstance(operation, Assign):
occurences[operation.identifier.pidentifier] += 1
if isinstance(operation.expression, Value):
if isinstance(operation.expression.a, Int):
occurences[operation.expression.a.pidentifier] += 1
elif isinstance(operation.expression.a, IntArrayElement):
occurences[operation.expression.a.array.pidentifier] += 1
if isinstance(operation.expression.a.value_holder, Int):
occurences[operation.expression.a.value_holder.pidentifier] += 1
elif isinstance(operation.expression, Operation):
if isinstance(operation.expression.a, Int):
occurences[operation.expression.a.pidentifier] += 1
elif isinstance(operation.expression.a, IntArrayElement):
occurences[operation.expression.a.array.pidentifier] += 1
if isinstance(operation.expression.a.value_holder, Int):
occurences[operation.expression.a.value_holder.pidentifier] += 1
if isinstance(operation.expression.b, Int):
occurences[operation.expression.b.pidentifier] += 1
elif isinstance(operation.expression.b, IntArrayElement):
occurences[operation.expression.b.array.pidentifier] += 1
if isinstance(operation.expression.b.value_holder, Int):
occurences[operation.expression.b.value_holder.pidentifier] += 1
elif isinstance(operation, (While, DoWhile, If, IfElse)):
bounty = 1
if isinstance(operation, (While, DoWhile)):
bounty = 500
if isinstance(operation.condition.p, Int):
occurences[operation.condition.p.pidentifier] += bounty
elif isinstance(operation.condition.p, IntArrayElement):
occurences[operation.condition.p.array.pidentifier] += bounty
if isinstance(operation.condition.p.value_holder, Int):
occurences[operation.condition.p.value_holder.pidentifier] += bounty
if isinstance(operation.condition.q, Int):
occurences[operation.condition.q.pidentifier] += bounty
elif isinstance(operation.condition.q, IntArrayElement):
occurences[operation.condition.q.array.pidentifier] += bounty
if isinstance(operation.condition.q.value_holder, Int):
occurences[operation.condition.q.value_holder.pidentifier] += bounty
internal_occurences = self.count_occurences(parse_tree=operation.commands)
for key, value in internal_occurences.items():
val = value
if isinstance(operation, (While, DoWhile)):
val = value * 20 # weight of loop
occurences[key] += val
if isinstance(operation, IfElse):
else_occurences = self.count_occurences(parse_tree=operation.alt_commands)
for key, value in else_occurences.items():
occurences[key] += value
elif isinstance(operation, (For, ForDownTo)):
# Iterator has the highest possible priority
occurences[operation.pidentifier.pidentifier] += 500
if isinstance(operation.from_val, Int):
occurences[operation.from_val.pidentifier] += 20
elif isinstance(operation.from_val, IntArrayElement):
occurences[operation.from_val.array.pidentifier] += 20
if isinstance(operation.from_val.value_holder, Int):
occurences[operation.from_val.value_holder.pidentifier] += 20
if isinstance(operation.to_val, Int):
occurences[operation.to_val.pidentifier] += 20
elif isinstance(operation.to_val, IntArrayElement):
occurences[operation.to_val.array.pidentifier] += 20
if isinstance(operation.to_val.value_holder, Int):
occurences[operation.to_val.value_holder.pidentifier] += 20
internal_occurences = self.count_occurences(parse_tree=operation.commands)
for key, value in internal_occurences.items():
occurences[key] += value * 20
return occurences
def rearrange_allocations(self, occurences):
"""
Changes the allocated memory block for declared variables given their occurences
:param occurences: defaultdict of occurences
:param self:
:return:
"""
current_globs = self.global_variables.copy()
current_mems = self.memory_indexes.copy()
logging.basicConfig(level=logging.CRITICAL)
logging.debug("Current globs: {}, current mems: {}".format(current_globs, current_mems))
logging.debug("Passed occurences: {}".format(occurences))
global_vars = {}
mem_indexes = {}
next_free_mem_index = 0
for pidentifier, value in sorted(occurences.items(), key=operator.itemgetter(1), reverse=True):
logging.debug("Pidentifier, value: ({}, {})".format(pidentifier, value))
if pidentifier in self.global_variables.keys() and pidentifier in self.memory_indexes.keys():
object = self.get_object_from_memory(pidentifier=pidentifier)
global_vars[pidentifier] = object
mem_indexes[pidentifier] = next_free_mem_index
if isinstance(object, Int):
if object.get_is_iterator():
global_vars[pidentifier].set_as_iterator()
# Declare iterator helpers here!
lbound = Int(pidentifier='for_lbound_{}'.format(object.pidentifier),
lineno=object.lineno)
lbound.set_value_has_been_set()
lbound.set_as_iterator()
ubound = Int(pidentifier='for_ubound_{}'.format(object.pidentifier),
lineno=object.lineno)
ubound.set_value_has_been_set()
ubound.set_as_iterator()
# Declare ubound, lbound
global_vars[lbound.pidentifier] = lbound
global_vars[ubound.pidentifier] = ubound
mem_indexes[lbound.pidentifier] = next_free_mem_index + 1
mem_indexes[ubound.pidentifier] = next_free_mem_index + 2
next_free_mem_index += 2
next_free_mem_index += 1
elif isinstance(object, IntArray):
next_free_mem_index += object.length
# Remove object
del self.memory_indexes[pidentifier]
del self.global_variables[pidentifier]
# if len(self.global_variables) > 0 or len(self.memory_indexes) > 0:
# # Panic mode
# logging.debug("Panic mode for rearranging vars. Globs contents: {}, indexes: {}".format(
# self.global_variables, self.memory_indexes))
# self.global_variables = current_globs
# self.memory_indexes = current_mems
# else:
# All is fine, so make the change!
# logging.debug("Making change to memory!")
logging.debug("Global vars: {}, mem_indexes: {} after all of that fun".format(global_vars, mem_indexes))
self.global_variables = global_vars
self.memory_indexes = mem_indexes
self.next_free_memory_index
def optimize_memory_allocations(self, parse_tree):
"""
Performs the optimizations of memory allocations.
:return:
"""
occurences = self.count_occurences(parse_tree=parse_tree)
self.rearrange_allocations(occurences=occurences)