提交 61da262e authored 作者: nouiz's avatar nouiz

Merge pull request #649 from abergeron/cvm_gc

Add gc to CVM
...@@ -13,7 +13,7 @@ if config.compiledir not in sys.path: ...@@ -13,7 +13,7 @@ if config.compiledir not in sys.path:
sys.path.append(config.compiledir) sys.path.append(config.compiledir)
force_compile = False force_compile = False
version = 0.13 # must match constant returned in function get_version() version = 0.14 # must match constant returned in function get_version()
try: try:
......
...@@ -184,9 +184,8 @@ class Stack(VM): ...@@ -184,9 +184,8 @@ class Stack(VM):
""" """
def __init__(self, nodes, thunks, pre_call_clear, def __init__(self, nodes, thunks, pre_call_clear,
storage_map, compute_map, storage_map, compute_map, env, allow_gc,
env, allow_gc, dependencies=None, callback=None):
callback=None):
super(Stack, self).__init__(nodes, thunks, pre_call_clear) super(Stack, self).__init__(nodes, thunks, pre_call_clear)
self.allow_gc = allow_gc self.allow_gc = allow_gc
...@@ -211,16 +210,11 @@ class Stack(VM): ...@@ -211,16 +210,11 @@ class Stack(VM):
for prereq in ords[node]: for prereq in ords[node]:
node.destroy_dependencies += prereq.outputs node.destroy_dependencies += prereq.outputs
dependencies = self.dependencies = {} self.dependencies = dependencies
for k in storage_map:
dependencies[k] = [] if self.allow_gc and self.dependencies is None:
if k.owner and k.clients: raise ValueError("Must set dependencies when using GC")
ls = []
is_output = 0
for cl in k.clients:
if cl[0] is not 'output':
ls += cl[0].outputs
dependencies[k] += ls
if config.profile: if config.profile:
self.memory_size_map = {"nt8": 1, "t16": 2, "t32": 4, self.memory_size_map = {"nt8": 1, "t16": 2, "t32": 4,
"t64": 8, "128": 16} "t64": 8, "128": 16}
...@@ -454,6 +448,19 @@ class VM_Linker(link.LocalLinker): ...@@ -454,6 +448,19 @@ class VM_Linker(link.LocalLinker):
# admittedly confusing, and it could use some cleaning up. The base # admittedly confusing, and it could use some cleaning up. The base
# Linker object should probably go away completely. # Linker object should probably go away completely.
def compute_gc_dependencies(self, smap):
dependencies = {}
for k in smap:
dependencies[k] = []
if k.owner and k.clients:
ls = []
is_output = 0
for cl in k.clients:
if cl[0] is not 'output':
ls += cl[0].outputs
dependencies[k] += ls
return dependencies
def make_vm(self, nodes, thunks, def make_vm(self, nodes, thunks,
input_storage, output_storage, storage_map, input_storage, output_storage, storage_map,
post_thunk_clear, post_thunk_clear,
...@@ -467,10 +474,14 @@ class VM_Linker(link.LocalLinker): ...@@ -467,10 +474,14 @@ class VM_Linker(link.LocalLinker):
if self.callback is not None: if self.callback is not None:
if self.use_cloop: if self.use_cloop:
logger.warn('CLoop does not support callback, using Stack VM.') logger.warn('CLoop does not support callback, using Stack VM.')
deps = None
if self.allow_gc:
deps = self.compute_gc_dependencies(storage_map)
vm = Stack( vm = Stack(
nodes, thunks, pre_call_clear, nodes, thunks, pre_call_clear,
storage_map, compute_map, storage_map, compute_map,
self.env, self.allow_gc, self.env, self.allow_gc,
dependencies=deps,
callback=self.callback) callback=self.callback)
elif self.use_cloop: elif self.use_cloop:
# create a map from nodes to ints and vars to ints # create a map from nodes to ints and vars to ints
...@@ -500,6 +511,14 @@ class VM_Linker(link.LocalLinker): ...@@ -500,6 +511,14 @@ class VM_Linker(link.LocalLinker):
assert type(storage_map_list[0]) is list assert type(storage_map_list[0]) is list
assert type(compute_map_list[0]) is list assert type(compute_map_list[0]) is list
if self.allow_gc:
dependency_map=self.compute_gc_dependencies(storage_map)
dependency_map_list = [
[vars_idx[d] for d in dependency_map[vars_idx_inv[i]]]
for i in xrange(len(vars_idx_inv))]
else:
dependency_map_list = None
# build the pointers to node inputs and offsets # build the pointers to node inputs and offsets
base_input_output_list = [] base_input_output_list = []
node_n_inputs = [] node_n_inputs = []
...@@ -566,6 +585,7 @@ class VM_Linker(link.LocalLinker): ...@@ -566,6 +585,7 @@ class VM_Linker(link.LocalLinker):
node_prereqs=node_prereqs, node_prereqs=node_prereqs,
node_output_size=node_output_size, node_output_size=node_output_size,
update_storage=update_storage, update_storage=update_storage,
dependencies=dependency_map_list,
) )
assert c0 == sys.getrefcount(node_n_inputs) assert c0 == sys.getrefcount(node_n_inputs)
else: else:
...@@ -583,10 +603,14 @@ class VM_Linker(link.LocalLinker): ...@@ -583,10 +603,14 @@ class VM_Linker(link.LocalLinker):
thunks, thunks,
pre_call_clear) pre_call_clear)
else: else:
deps = None
if self.allow_gc:
deps = self.compute_gc_dependencies(storage_map)
vm = Stack( vm = Stack(
nodes, thunks, pre_call_clear, nodes, thunks, pre_call_clear,
storage_map, compute_map, storage_map, compute_map,
self.env, self.allow_gc self.env, self.allow_gc,
dependencies=deps
) )
return vm return vm
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论