提交 0d04e12b authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Remove struct_id and use the node name.

It is suitably unique and, more importantly, shared with the rest of the methods.
上级 7d70c3a4
......@@ -43,9 +43,6 @@ There are less methods to define for an Op than for a Type:
that a python exception is set) if your C code needs to
raise an exception.
``sub['struct_id']``
The integer id passed to the various _struct methods.
.. method:: c_code_cleanup(node, name, input_names, output_names, sub)
......@@ -99,15 +96,12 @@ There are less methods to define for an Op than for a Type:
module is initialized, before anything else is executed and is
specialized for a particular apply of an :ref:`op`.
.. method:: c_init_code_struct(node, struct_id, sub)
.. method:: c_init_code_struct(node, name, sub)
Allows you to specify code that will be inserted in the struct
constructor of the Op. This is for code which should be
executed once per thunk (Apply node, more or less).
`struct_id` is an integer guaranteed to be unique inside the
struct.
`sub` is a dictionary of extras parameters to the
c_code_init_code_struct method. It contains the following
values:
......@@ -131,31 +125,25 @@ There are less methods to define for an Op than for a Type:
if the code is the same for each apply of an op. It will be
inserted at global scope.
.. method:: c_support_code_struct(node, struct_id)
.. method:: c_support_code_struct(node, name)
Allows you to specify helper functions of variables that will
be specific to one particular thunk. These are inserted at
struct scope.
`struct_id` is an integer guaranteed to be unique inside the
struct.
:note:
You cannot specify kernels in the code returned by this since
that isn't supported by CUDA. You should place your kernels
in :meth:`c_support_code()` or :meth:`c_support_code_apply()`
and call them from this code.
You cannot specify CUDA kernels in the code returned by this
since that isn't supported by CUDA. You should place your
kernels in :meth:`c_support_code()` or
:meth:`c_support_code_apply()` and call them from this code.
.. method:: c_cleanup_code_struct(node, struct_id)
.. method:: c_cleanup_code_struct(node, name)
Allows you to specify code that will be inserted in the struct
destructor of the Op. This is for cleaninp up allocations and
stuff like this when the thunk is released (when you "free" a
compiled function using this op).
`struct_id` is an integer guaranteed to be unique inside the
struct.
.. method:: infer_shape(node, (i0_shapes,i1_shapes,...))
Allow optimizations to lift the Shape op over this op. An
......
......@@ -658,7 +658,6 @@ class CLinker(link.Linker):
# Make the CodeBlock for c_code
sub['id'] = id
sub['struct_id'] = id + 1
sub['fail'] = failure_code(sub)
sub_struct = dict()
......@@ -692,7 +691,7 @@ class CLinker(link.Linker):
" didn't return a string for c_init_code_apply")
try:
struct_init = op.c_init_code_struct(node, id + 1, sub_struct)
struct_init = op.c_init_code_struct(node, name, sub_struct)
assert isinstance(struct_init, basestring), (
str(node.op) +
" didn't return a string for c_init_code_struct")
......@@ -700,7 +699,7 @@ class CLinker(link.Linker):
pass
try:
struct_support = op.c_support_code_struct(node, id + 1)
struct_support = op.c_support_code_struct(node, name)
assert isinstance(struct_support, basestring), (
str(node.op) +
" didn't return a string for c_support_code_struct")
......@@ -708,7 +707,7 @@ class CLinker(link.Linker):
pass
try:
struct_cleanup = op.c_cleanup_code_struct(node, id + 1)
struct_cleanup = op.c_cleanup_code_struct(node, name)
assert isinstance(struct_cleanup, basestring), (
str(node.op) +
" didn't return a string for c_cleanup_code_struct")
......
......@@ -184,7 +184,8 @@ class Apply(Node):
:note:
tags are copied from self to the returned instance.
"""
cp = self.__class__(self.op, self.inputs, [output.clone() for output in self.outputs])
cp = self.__class__(self.op, self.inputs,
[output.clone() for output in self.outputs])
cp.tag = copy(self.tag)
return cp
......
......@@ -322,17 +322,15 @@ class CLinkerOp(CLinkerObject):
raise utils.MethodNotDefined("c_init_code_apply", type(self),
self.__class__.__name__)
def c_init_code_struct(self, node, struct_id, sub):
def c_init_code_struct(self, node, name, sub):
"""
Optional: return a code string specific to the apply
to be inserted in the struct initialization code.
:param node: an Apply instance in the graph being compiled
:param struct_id: a number that serves to uniquely identify
this code. The c_code will receive another
sub parameter named struct_id that will
contain this name.
:param name: a unique name to distinguish you variables from
those of other nodes.
:param sub: a dictionary of values to substitute in the code.
Most notably it contains a 'fail' entry that you
......@@ -345,17 +343,15 @@ class CLinkerOp(CLinkerObject):
raise utils.MethodNotDefined("c_init_code_apply", type(self),
self.__class__.__name__)
def c_support_code_struct(self, node, struct_id):
def c_support_code_struct(self, node, name):
"""Optional: Return utility code for use by an `Op` that will be
inserted at struct scope, that can be specialized for the
support of a particular `Apply` node.
:param node: an Apply instance in the graph being compiled
:param struct_id: a number that serves to uniquely identify
this code. The c_code will receive another
sub parameter named struct_id that will
contain this name.
:param name: a unique name to distinguish you variables from
those of other nodes.
:Exceptions:
- `MethodNotDefined`: Subclass does not implement this method
......@@ -364,17 +360,15 @@ class CLinkerOp(CLinkerObject):
raise utils.MethodNotDefined("c_support_code_struct",
type(self), self.__class__.__name__)
def c_cleanup_code_struct(self, node, struct_id):
def c_cleanup_code_struct(self, node, name):
"""
Optional: return a code string specific to the apply to be
inserted in the struct cleanup code.
:param node: an Apply instance in the graph being compiled
:param struct_id: a number that serves to uniquely identify
this code. The c_code will receive another
sub parameter named struct_id that will
contain this name.
:param name: a unique name to distinguish you variables from
those of other nodes.
:Exceptions:
- `MethodNotDefined`: the subclass does not override this method
......
......@@ -94,20 +94,20 @@ class StructOp(Op):
def make_node(self, i):
return Apply(self, [i], [scalar.uint64()])
def c_support_code_struct(self, node, struct_id):
return "npy_uint64 counter%d;" % (struct_id,)
def c_support_code_struct(self, node, name):
return "npy_uint64 counter%s;" % (name,)
def c_init_code_struct(self, node, struct_id, sub):
return "counter%d = 0;" % (struct_id,)
def c_init_code_struct(self, node, name, sub):
return "counter%s = 0;" % (name,)
def c_code(self, node, name, input_names, outputs_names, sub):
return """
%(out)s = counter%(sid)s;
counter%(sid)s++;
""" % dict(out=outputs_names[0], sid=sub['struct_id'])
%(out)s = counter%(name)s;
counter%(name)s++;
""" % dict(out=outputs_names[0], name=name)
def c_code_cache_version(self):
return (0,)
return (1,)
class TestOp:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论