提交 33851f68 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Fix more errors and warnings in doc generation

上级 bfd8a94a
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
New C code generation? New C code generation?
====================== ======================
== Issues == Issues
======
There are several issues with the current way C code is generated: There are several issues with the current way C code is generated:
* Ops cannot declare their own persistent variables. * Ops cannot declare their own persistent variables.
...@@ -13,91 +14,92 @@ There are several issues with the current way C code is generated: ...@@ -13,91 +14,92 @@ There are several issues with the current way C code is generated:
* It is currently impossible to specialize support code based on the self. * It is currently impossible to specialize support code based on the self.
* Caching of the generated code for graphs is greatly suboptimal. * Caching of the generated code for graphs is greatly suboptimal.
== Structure == Structure
=========
Currently, the general structure of the generated C code is approximately as follows: Currently, the general structure of the generated C code is approximately as follows:
{{{ .. code-block:: c
<imports>
<weave type converters> <imports>
<op/result support code> <weave type converters>
<op/result support code>
struct my_computation {
<input/output storage> struct my_computation {
<persistent fields> <input/output storage>
init(<input/output storage>) { <initialize persistent fields> } <persistent fields>
cleanup { <clean up persistent fields> } init(<input/output storage>) { <initialize persistent fields> }
run { <run the computation> } cleanup { <clean up persistent fields> }
}; run { <run the computation> }
};
<runner for the struct>
PyObject* instantiate(PyObject* args) { <runner for the struct>
<weave stuff> PyObject* instantiate(PyObject* args) {
<make up a CObject out of the runner and a my_computation instance> <weave stuff>
<weave stuff> <make up a CObject out of the runner and a my_computation instance>
} <weave stuff>
<python exports for instantiate> }
}}} <python exports for instantiate>
The module produced via that method then has to be used as such: The module produced via that method then has to be used as such::
{{{
obj = module.instantiate(error_storage, input_storage, output_storage, orphan_storage) obj = module.instantiate(error_storage, input_storage, output_storage, orphan_storage)
cutils.run_cthunk(obj) cutils.run_cthunk(obj)
}}}
We would like to get rid of weave dependencies, avoid name conflicts with the support code and have a nicer user interface for the produced module. The proposed new structure is as follows: We would like to get rid of weave dependencies, avoid name conflicts with the support code and have a nicer user interface for the produced module. The proposed new structure is as follows:
{{{ .. code-block:: c
<imports>
<imports>
struct op1 {
<persistent variables> struct op1 {
<support code> <persistent variables>
init() { <initialize persistent fields> } <support code>
cleanup { <clean up persistent fields> } init() { <initialize persistent fields> }
run(<inputs>) { <run the computation for op1> } cleanup { <clean up persistent fields> }
}; run(<inputs>) { <run the computation for op1> }
};
struct op2 { <same> };
... struct op2 { <same> };
struct opN { <ditto> };
struct driver {
op1 o1; op2 o2; ... opN oN;
<input storage>
<output storage>
init(<storage>) { <initialize ops, storage> }
cleanup() { <free storage?> }
run() {
<extract inputs>
o1.run(input1, input2);
o2.run(o1.output1);
... ...
oN.run(...); struct opN { <ditto> };
<sync outputs>
} struct driver {
} op1 o1; op2 o2; ... opN oN;
<input storage>
PyObject* <name>(PyObject* inputs) { <output storage>
<init driver, input/output storage> init(<storage>) { <initialize ops, storage> }
<put inputs in input storage> cleanup() { <free storage?> }
driver.run() run() {
<free input storage> <extract inputs>
<return output storage> o1.run(input1, input2);
} o2.run(o1.output1);
...
PyObject* <name>_driver(PyObject* storage) { oN.run(...);
<init driver with storage> <sync outputs>
<return driver> }
} }
<export <name> and <name>_driver> PyObject* <name>(PyObject* inputs) {
}}} <init driver, input/output storage>
<put inputs in input storage>
driver.run()
<free input storage>
<return output storage>
}
PyObject* <name>_driver(PyObject* storage) {
<init driver with storage>
<return driver>
}
<export <name> and <name>_driver>
Gains: Gains:
* support code can be put inside a struct and become private to the Op * support code can be put inside a struct and become private to the Op
* we can export several functions that can be used directly, eg {{{z = module.add(1, 2)}}} * we can export several functions that can be used directly, eg ``z = module.add(1, 2)``
* this won't do filtering like {{{Result.filter}}} so the usefulness is limited by that * this won't do filtering like ``Result.filter`` so the usefulness is limited by that
* the sequence of operations might be clearer to read * the sequence of operations might be clearer to read
* we can use more descriptive names in each Op struct representing its input names (if we can find them using the inspect module) without worrying about name conflicts * we can use more descriptive names in each Op struct representing its input names (if we can find them using the inspect module) without worrying about name conflicts
...@@ -106,14 +108,15 @@ Losses: ...@@ -106,14 +108,15 @@ Losses:
* make functions static and inline as much as possible * make functions static and inline as much as possible
== Caching == Caching
=======
The current way of caching is from a hash of the generated code. That is inefficient because code has to be generated each time, which might be a costly process. Furthermore, usage of hashing in sets make it difficult to ensure a consistent ordering of Ops in graphs where several orderings are valid, so the generated C code is potentially different each time. Here is a proposal for a better way to compute the hash: The current way of caching is from a hash of the generated code. That is inefficient because code has to be generated each time, which might be a costly process. Furthermore, usage of hashing in sets make it difficult to ensure a consistent ordering of Ops in graphs where several orderings are valid, so the generated C code is potentially different each time. Here is a proposal for a better way to compute the hash:
* Result_hash = Result version + Result desc * Result_hash = Result version + Result desc
* Op_hash = Op version + Op desc + input/output hashes * Op_hash = Op version + Op desc + input/output hashes
* Env_hash = Env version + combination of the Op hashes and their traversal order wrt a consistent traversal method * Env_hash = Env version + combination of the Op hashes and their traversal order wrt a consistent traversal method
The version could be set explicitly via a {{{__version__}}} field or it could simply be equal to the file's last modification date. We could also have a {{{__nocache__}}} field indicating that code produced by the Op or Result cannot be cached. The version could be set explicitly via a ``__version__`` field or it could simply be equal to the file's last modification date. We could also have a ``__nocache__`` field indicating that code produced by the Op or Result cannot be cached.
It should also be easier to bypass the cache (eg an option to CLinker to regenerate the code). It should also be easier to bypass the cache (eg an option to CLinker to regenerate the code).
......
...@@ -174,7 +174,8 @@ internal state, and returns the old state value. ...@@ -174,7 +174,8 @@ internal state, and returns the old state value.
>>> accumulator = function([inc], state, updates=[(state, state+inc)]) >>> accumulator = function([inc], state, updates=[(state, state+inc)])
This code introduces a few new concepts. The ``shared`` function constructs This code introduces a few new concepts. The ``shared`` function constructs
so-called :term:`shared variables`. These are hybrid symbolic and non-symbolic so-called :term:`shared variables <shared variable>`.
These are hybrid symbolic and non-symbolic
variables. Shared variables can be used in symbolic expressions just like variables. Shared variables can be used in symbolic expressions just like
the objects returned by ``dmatrices(...)`` but they also have an internal the objects returned by ``dmatrices(...)`` but they also have an internal
value, that defines the value taken by this symbolic variable in *all* the value, that defines the value taken by this symbolic variable in *all* the
......
...@@ -343,10 +343,10 @@ class Method(Component): ...@@ -343,10 +343,10 @@ class Method(Component):
"""Compile a function for this Method. """Compile a function for this Method.
:param allocate_all: if True, storage will be :param allocate_all: if True, storage will be
allocated for all needed Variables even if there is no allocated for all needed Variables even if there is no
associated storage for them in the memo. If allocate_all is associated storage for them in the memo. If allocate_all is
False, storage will only be allocated for Variables that are False, storage will only be allocated for Variables that are
reachable from the inputs list. reachable from the inputs list.
:returns: a function that implements this method :returns: a function that implements this method
:rtype: `Function` instance :rtype: `Function` instance
...@@ -593,7 +593,8 @@ class Composite(Component): ...@@ -593,7 +593,8 @@ class Composite(Component):
""" """
Generator that yields (path, component) pairs in a flattened Generator that yields (path, component) pairs in a flattened
hierarchy of composites and components, where path is a hierarchy of composites and components, where path is a
sequence of keys such that sequence of keys such that::
component is self[path[0]][path[1]]... component is self[path[0]][path[1]]...
If include_self is True, the list will include the Composite If include_self is True, the list will include the Composite
...@@ -865,12 +866,13 @@ def register_wrapper(condition, wrapper): ...@@ -865,12 +866,13 @@ def register_wrapper(condition, wrapper):
""" """
:type condition: function x -> bool :type condition: function x -> bool
:param condition: this function should return True iff `wrapper` can sensibly turn x into a :param condition: this function should return True iff `wrapper` can
Component. sensibly turn x into a Component.
:type wrapper: function x -> Component :type wrapper: function x -> Component
:param wrapper: this function should convert `x` into an instance of a Component subclass. :param wrapper: this function should convert `x` into an instance of
a Component subclass.
""" """
__autowrappers.append((condition, wrapper)) __autowrappers.append((condition, wrapper))
...@@ -947,8 +949,8 @@ class ModuleInstance(ComponentDictInstanceNoInit): ...@@ -947,8 +949,8 @@ class ModuleInstance(ComponentDictInstanceNoInit):
WRITEME WRITEME
:note: ModuleInstance is meant to be instantiated by Module. This differs :note: ModuleInstance is meant to be instantiated by Module. This differs
from ComponentDictInstance on a key point, which is that getattr from ComponentDictInstance on a key point, which is that getattr
does a similar thing to getitem. does a similar thing to getitem.
:note: ModuleInstance is compatible for use as ComponentDict.InstanceType. :note: ModuleInstance is compatible for use as ComponentDict.InstanceType.
""" """
...@@ -1169,7 +1171,8 @@ FancyModuleInstance = ModuleInstance ...@@ -1169,7 +1171,8 @@ FancyModuleInstance = ModuleInstance
def func_to_mod(f): def func_to_mod(f):
""" """
Creates a dummy module, with external member variables for the input Creates a dummy module, with external member variables for the input
parameters required by the function f, and a member output defined as: parameters required by the function f, and a member output defined as::
output <= f(**kwinit) output <= f(**kwinit)
""" """
def make(**kwinit): def make(**kwinit):
......
...@@ -59,17 +59,19 @@ class CLinkerType(CLinkerObject): ...@@ -59,17 +59,19 @@ class CLinkerType(CLinkerObject):
:type name: string :type name: string
:param sub: a dictionary of special codes. Most importantly sub['fail']. See CLinker :param sub: a dictionary of special codes. Most importantly
for more info on `sub` and ``fail``. sub['fail']. See CLinker for more info on `sub` and ``fail``.
:type sub: dict string -> string :type sub: dict string -> string
:note: It is important to include the `name` inside of variables which are declared :note: It is important to include the `name` inside of variables which
here, so that name collisions do not occur in the source file that is generated. are declared here, so that name collisions do not occur in the
source file that is generated.
:note: The variable called ``name`` is not necessarily defined yet where this code is :note: The variable called ``name`` is not necessarily defined yet
inserted. This code might be inserted to create class variables for example, whereas where this code is inserted. This code might be inserted to
the variable ``name`` might only exist inside certain functions in that class. create class variables for example, whereas the variable ``name``
might only exist inside certain functions in that class.
:todo: Why should variable declaration fail? Is it even allowed to? :todo: Why should variable declaration fail? Is it even allowed to?
...@@ -87,9 +89,10 @@ class CLinkerType(CLinkerObject): ...@@ -87,9 +89,10 @@ class CLinkerType(CLinkerObject):
return "addr_of_%(name)s = NULL;" return "addr_of_%(name)s = NULL;"
:note: The variable called ``name`` is not necessarily defined yet where this code is :note: The variable called ``name`` is not necessarily defined yet
inserted. This code might be inserted in a class constructor for example, whereas where this code is inserted. This code might be inserted in a
the variable ``name`` might only exist inside certain functions in that class. class constructor for example, whereas the variable ``name``
might only exist inside certain functions in that class.
:todo: Why should variable initialization fail? Is it even allowed to? :todo: Why should variable initialization fail? Is it even allowed to?
""" """
...@@ -107,7 +110,7 @@ class CLinkerType(CLinkerObject): ...@@ -107,7 +110,7 @@ class CLinkerType(CLinkerObject):
exception and insert "%(fail)s". exception and insert "%(fail)s".
:todo: Point out that template filling (via sub) is now performed :todo: Point out that template filling (via sub) is now performed
by this function. --jpt by this function. --jpt
Example: Example:
...@@ -116,15 +119,17 @@ class CLinkerType(CLinkerObject): ...@@ -116,15 +119,17 @@ class CLinkerType(CLinkerObject):
return "if (py_%(name)s == Py_None)" + \\\ return "if (py_%(name)s == Py_None)" + \\\
addr_of_%(name)s = &py_%(name)s;" + \\\ addr_of_%(name)s = &py_%(name)s;" + \\\
"else" + \\\ "else" + \\\
{ PyErr_SetString(PyExc_ValueError, 'was expecting None'); %(fail)s;}" { PyErr_SetString(PyExc_ValueError, \\\
'was expecting None'); %(fail)s;}"
:param name: the name of the ``PyObject *`` pointer that will the value for this Type :param name: the name of the ``PyObject *`` pointer that will
store the value for this Type
:type name: string :type name: string
:param sub: a dictionary of special codes. Most importantly sub['fail']. See CLinker :param sub: a dictionary of special codes. Most importantly
for more info on `sub` and ``fail``. sub['fail']. See CLinker for more info on `sub` and ``fail``.
:type sub: dict string -> string :type sub: dict string -> string
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论