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