提交 07c70749 authored 作者: James Bergstra's avatar James Bergstra

debugging, testing of WrapLinker

上级 20267813
......@@ -122,6 +122,40 @@ class _test_PerformLinker(unittest.TestCase):
fn = perform_linker(Env(*graph.clone([x, y,r], [e]))).make_function()
self.failUnless(fn(1.0,2.0,4.5) == 7.5)
def wrap_linker(env, linkers, wrapper):
lnk = WrapLinker(linkers, wrapper).accept(env)
return lnk
class _test_WrapLinker(unittest.TestCase):
def test0(self):
nodes = []
def wrap(i, node, th):
nodes.append(node.op)
x, y, z = inputs()
e = mul(add(x, y), div(x, y))
fn, i, o = wrap_linker(Env([x, y, z], [e]), [PerformLinker()], wrap).make_thunk()
i[0].data = 1
i[1].data = 2
fn()
self.failUnless(nodes == [div, add, mul], nodes)
self.failUnless(o[0].data is None)
def test1(self):
nodes = []
def wrap(i, node, th):
nodes.append(node.op)
th()
x, y, z = inputs()
e = mul(add(x, y), div(x, y))
fn, i, o = wrap_linker(Env([x, y, z], [e]), [PerformLinker()], wrap).make_thunk()
i[0].data = 1
i[1].data = 2
fn()
self.failUnless(nodes == [div, add, mul], nodes)
self.failUnless(o[0].data == 1.5, o[0].data)
# def test_disconnected_input_output(self):
......
......@@ -260,19 +260,23 @@ class WrapLinker(Linker):
This class makes it easier to run several L{LocalLinker}s in parallel, and
offers some control over how each thunk is run.
and they should all return the same order. A wrapper function must
be provided to execute the thunks, inspect the nodes, etc.
A wrapper function must be provided, and it can be used to execute the
thunks, inspect the nodes, print stuff out, etc.
@note:
The outputs of the first linker will be returned.
@note:
This linker ensures that each linker has its own storage for
inputs and outputs and intermediate results. There is no interference
between linkers.
"""
def __init__(self, env, linkers, wrapper, no_recycling = []):
def __init__(self, linkers, wrapper):
"""
Initialize a WrapLinker.
@type env: gof.Env
@param env: the env which we will link
@type linkers: list of L{LocalLinker} subclasses, whose make_all()
method returns thunks in the same order.
......@@ -287,22 +291,27 @@ class WrapLinker(Linker):
want to run the program, make sure to call the necessary thunks in this
function.)
"""
self.linkers = linkers
self.wrapper = wrapper
def accept(self, env, no_recycling = []):
"""
@type env: gof.Env
@param env: the env which we will link
@type no_recycling: a list of Results that belong to env.
@param no_recycling: If a Result is in no_recycling, L{WrapLinker} will clear
the output storage associated to it (for each linker in linkers) during
the computation to avoid reusing it.
@note:
This linker ensures that each linker has its own storage for
inputs and outputs and intermediate results. There is no interference
between linkers.
"""
self.env = env
self.linkers = linkers
self.wrapper = wrapper
self.no_recycling = no_recycling
for l in self.linkers:
l.accept(env, no_recycling)
return self
def pre(self, f, inputs, order, thunk_groups):
pass
......@@ -310,12 +319,10 @@ class WrapLinker(Linker):
def make_thunk(self, **kwargs):
no_recycling = self.no_recycling
instantiated_linkers = [linker(self.env, no_recycling = no_recycling)\
.make_all(**kwargs)
for linker in self.linkers]
make_all = [l.make_all(**kwargs) for l in self.linkers]
fns, input_lists, output_lists, thunk_lists, order_lists \
= zip(*instantiated_linkers)
= zip(*make_all)
order_list0 = order_lists[0]
for order_list in order_lists[1:]:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论