Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
420e53c9
提交
420e53c9
authored
9月 29, 2014
作者:
Pierre Luc Carrier
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Highlighted variables and function names.
上级
07334890
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
55 行增加
和
54 行删除
+55
-54
extending_theano_c.txt
doc/tutorial/extending_theano_c.txt
+55
-54
没有找到文件。
doc/tutorial/extending_theano_c.txt
浏览文件 @
420e53c9
...
@@ -27,9 +27,9 @@ Python C-API
...
@@ -27,9 +27,9 @@ Python C-API
Python provides a C-API to allows the manipulation of python objects from C
Python provides a C-API to allows the manipulation of python objects from C
code. In this API, all variables that represent Python objects are of type
code. In this API, all variables that represent Python objects are of type
`
PyObject *
`. All objects have a pointer to their type object and a reference
`
`PyObject *`
`. All objects have a pointer to their type object and a reference
count field (that is shared with the python side). Most python methods have
count field (that is shared with the python side). Most python methods have
an equivalent C function that can be called on the `
PyObject *
` pointer.
an equivalent C function that can be called on the `
`PyObject *`
` pointer.
As such, manipulating a PyObject instance is often straight-forward but it
As such, manipulating a PyObject instance is often straight-forward but it
is important to properly manage its reference count. Failing to do so can
is important to properly manage its reference count. Failing to do so can
...
@@ -55,14 +55,14 @@ two macros most often used in Theano C ops.
...
@@ -55,14 +55,14 @@ two macros most often used in Theano C ops.
.. method:: void Py_XINCREF(PyObject *o)
.. method:: void Py_XINCREF(PyObject *o)
Increments the reference count of object
o. Without effect if the object
Increments the reference count of object
``o``. Without effect if the
is NULL.
object
is NULL.
.. method:: void Py_XDECREF(PyObject *o)
.. method:: void Py_XDECREF(PyObject *o)
Decrements the reference count of object
o. If the reference count reaches
Decrements the reference count of object
``o``. If the reference count
0, it will trigger a call of the object's deallocation function. Without
reaches 0, it will trigger a call of the object's deallocation function.
effect if the object is NULL.
Without
effect if the object is NULL.
The general principle, in the reference counting paradigm, is that the owner
The general principle, in the reference counting paradigm, is that the owner
of a reference to an object is responsible for disposing properly of it.
of a reference to an object is responsible for disposing properly of it.
...
@@ -106,7 +106,7 @@ representation does not have to occupy a continuous region in memory. In fact,
...
@@ -106,7 +106,7 @@ representation does not have to occupy a continuous region in memory. In fact,
it can be C-contiguous, F-contiguous or non-contiguous. C-contiguous means
it can be C-contiguous, F-contiguous or non-contiguous. C-contiguous means
that the data is not only contiguous in memory but also that it is organized
that the data is not only contiguous in memory but also that it is organized
such that the index of the latest dimension changes the fastest. If the
such that the index of the latest dimension changes the fastest. If the
following array
x
following array
.. code-block:: python
.. code-block:: python
...
@@ -114,22 +114,23 @@ following array x
...
@@ -114,22 +114,23 @@ following array x
[4, 5, 6]]
[4, 5, 6]]
is C-contiguous, it means that, in memory, the six values contained in the
is C-contiguous, it means that, in memory, the six values contained in the
array x are stored in the order [1, 2, 3, 4, 5, 6] (the first value is x[0,0],
array ``x`` are stored in the order ``[1, 2, 3, 4, 5, 6]`` (the first value is
the second value is x[0,1], the third value is x[0,2], the fourth value is
``x[0,0]``, the second value is ``x[0,1]``, the third value is ``x[0,2]``, the,
x[1,0], etc). F-contiguous (or Fortran Contiguous) also means that the data is
fourth value is ``x[1,0]``, etc). F-contiguous (or Fortran Contiguous) also
contiguous but that it is organized such that the index of the latest
means that the data is contiguous but that it is organized such that the index
dimension changes the slowest. If the array x is F-contiguous, it means that,
of the latest dimension changes the slowest. If the array ``x`` is
in memory, the values appear in the order [1, 4, 2, 5, 3, 6] (the first
F-contiguous, it means that, in memory, the values appear in the order
value is x[0,0], the second value is x[1,0], the third value is x[0,1], etc).
``[1, 4, 2, 5, 3, 6]`` (the first value is ``x[0,0]``, the second value is
``x[1,0]``, the third value is ``x[0,1]``, etc).
Finally, the internal data can be non-contiguous. In this case, it occupies
Finally, the internal data can be non-contiguous. In this case, it occupies
a non-contiguous region in memory but it is still stored in an organized
a non-contiguous region in memory but it is still stored in an organized
fashion : the distance between the element
x[i,j] and the element x[i+1,j]
fashion : the distance between the element
``x[i,j]`` and the element
of the array is constant over all valid values of i and j, just as the
``x[i+1,j]`` of the array is constant over all valid values of ``i`` and
distance between the element x[i,j] and the element x[i,j+1] of the array
``j``, just as the distance between the element ``x[i,j]`` and the element
is constant over all valid values of i and j. This distance between
``x[i,j+1]`` of the array is constant over all valid values of ``i`` and ``j``.
consecutive elements of an array over a given dimension, is called the
This distance between consecutive elements of an array over a given dimension,
stride of that dimension.
is called the
stride of that dimension.
Accessing NumPy ndarrays' data and properties
Accessing NumPy ndarrays' data and properties
...
@@ -143,30 +144,30 @@ The following macros serve to access various attributes of NumPy ndarrays.
...
@@ -143,30 +144,30 @@ The following macros serve to access various attributes of NumPy ndarrays.
.. method:: int PyArray_NDIM(PyArrayObject* arr)
.. method:: int PyArray_NDIM(PyArrayObject* arr)
Returns the number of dimensions in the the array pointed by
arr
Returns the number of dimensions in the the array pointed by
``arr``
.. method:: npy_intp* PyArray_DIMS(PyArrayObject* arr)
.. method:: npy_intp* PyArray_DIMS(PyArrayObject* arr)
Returns a pointer on the first element of
arr's internal array describing
Returns a pointer on the first element of
``arr``'s internal array
its dimensions. This internal array contains as many elements as the
describing its dimensions. This internal array contains as many elements
a
rray arr
has dimensions.
a
s the array ``arr``
has dimensions.
The macro
PyArray_SHAPE is a synonym of PyArray_DIMS : it has the same
The macro
``PyArray_SHAPE()`` is a synonym of ``PyArray_DIMS()`` : it has
effect and is used in an identical way.
the same
effect and is used in an identical way.
.. method:: npy_intp* PyArray_STRIDES(PyArrayObject* arr)
.. method:: npy_intp* PyArray_STRIDES(PyArrayObject* arr)
Returns a pointer on the first element of
arr's internal array describing
Returns a pointer on the first element of
``arr``'s internal array
the stride for each of its dimension. This array has as many elements as
describing the stride for each of its dimension. This array has as many
the number of dimensions in arr. In this array, the strides are expressed
elements as the number of dimensions in ``arr``. In this array, the
in number of bytes.
strides are expressed
in number of bytes.
.. method:: PyArray_Descr* PyArray_DESCR(PyArrayObject* arr)
.. method:: PyArray_Descr* PyArray_DESCR(PyArrayObject* arr)
Returns a reference to the object representing the dtype of the array.
Returns a reference to the object representing the dtype of the array.
The macro
PyArray_DTYPE is a synonym of the PyArray_DESCR() : it has the
The macro
``PyArray_DTYPE()`` is a synonym of the ``PyArray_DESCR()`` : it
same effect and is used in an identical way.
has the
same effect and is used in an identical way.
:note:
:note:
This is a borrowed reference so you do not need to decrement its
This is a borrowed reference so you do not need to decrement its
...
@@ -202,28 +203,28 @@ The following functions allow the creation and copy of NumPy arrays :
...
@@ -202,28 +203,28 @@ The following functions allow the creation and copy of NumPy arrays :
.. method:: PyObject* PyArray_EMPTY(int nd, npy_intp* dims, typenum dtype,
.. method:: PyObject* PyArray_EMPTY(int nd, npy_intp* dims, typenum dtype,
int fortran)
int fortran)
Constructs a new ndarray with the number of dimensions specified by
nd,
Constructs a new ndarray with the number of dimensions specified by
shape specified by dims and data type specified by dtype. If fortran is
``nd``, shape specified by ``dims`` and data type specified by ``dtype``.
equal to 0, the data is organized in a C-contiguous layout, otherwise it
If ``fortran`` is equal to 0, the data is organized in a C-contiguous
is organized in a F-contiguous layout. The array elements are not
layout, otherwise it is organized in a F-contiguous layout. The array
initialized in any way.
elements are not
initialized in any way.
The function
PyArray_Empty()
performs the same function as the macro
The function
``PyArray_Empty()``
performs the same function as the macro
PyArray_EMPTY()
but the data type is given as a pointer to a
``PyArray_EMPTY()``
but the data type is given as a pointer to a
PyArray_Descr object instead of a typenum
.
``PyArray_Descr`` object instead of a ``typenum``
.
.. method:: PyObject* PyArray_ZEROS(int nd, npy_intp* dims, typenum dtype,
.. method:: PyObject* PyArray_ZEROS(int nd, npy_intp* dims, typenum dtype,
int fortran)
int fortran)
Constructs a new ndarray with the number of dimensions specified by
nd,
Constructs a new ndarray with the number of dimensions specified by
shape specified by dims and data type specified by dtype. If fortran is
``nd``, shape specified by ``dims`` and data type specified by ``dtype``.
equal to 0, the data is organized in a C-contiguous layout, otherwise it
If ``fortran`` is equal to 0, the data is organized in a C-contiguous
is organized in a F-contiguous layout. Every element in the array is
layout, otherwise it is organized in a F-contiguous layout. Every element
initialized to 0.
in
the array is in
itialized to 0.
The function
PyArray_Zeros()
performs the same function as the macro
The function
``PyArray_Zeros()``
performs the same function as the macro
PyArray_ZEROS()
but the data type is given as a pointer to a
``PyArray_ZEROS()``
but the data type is given as a pointer to a
PyArray_Descr object instead of a typenum.
``PyArray_Descr`` object instead of a ``typenum``.
.. method:: PyArrayObject* PyArray_GETCONTIGUOUS(PyObject* op):
.. method:: PyArrayObject* PyArray_GETCONTIGUOUS(PyObject* op):
...
@@ -238,7 +239,7 @@ Methods the C Op needs to define
...
@@ -238,7 +239,7 @@ Methods the C Op needs to define
There is a key difference between an op defining a Python implementation for
There is a key difference between an op defining a Python implementation for
its computation and defining a C implementation. In the case of a Python
its computation and defining a C implementation. In the case of a Python
implementation, the op defines a function
perform()
which executes the
implementation, the op defines a function
``perform()``
which executes the
required Python code to realize the op. In the case of a C implementation,
required Python code to realize the op. In the case of a C implementation,
however, the op does **not** define a function that will execute the C code; it
however, the op does **not** define a function that will execute the C code; it
instead defines functions that will **return** the C code to the caller.
instead defines functions that will **return** the C code to the caller.
...
@@ -305,9 +306,9 @@ used.
...
@@ -305,9 +306,9 @@ used.
of the C variable representing the first input of the op is given by
of the C variable representing the first input of the op is given by
``input_names[0]``. You should therefore use this name in your
``input_names[0]``. You should therefore use this name in your
C code to interact with that variable. ``output_names`` is used
C code to interact with that variable. ``output_names`` is used
identically to ``input_names``, but for the op
s'
outputs.
identically to ``input_names``, but for the op
's
outputs.
Finally, `
sub
` is a dictionary of extras parameters to the c_code
Finally, `
`sub`
` is a dictionary of extras parameters to the c_code
method. Among other things, it contains ``sub['fail']`` which is a string
method. Among other things, it contains ``sub['fail']`` which is a string
of C code that you should execute (after ensuring that a Python exception
of C code that you should execute (after ensuring that a Python exception
is set) if your C code needs to raise an exception.
is set) if your C code needs to raise an exception.
...
@@ -342,7 +343,7 @@ used.
...
@@ -342,7 +343,7 @@ used.
described.
described.
Complet
e C Op example
Simpl
e C Op example
=====================
=====================
In this section, we put together every concept that was covered in this
In this section, we put together every concept that was covered in this
...
@@ -371,7 +372,7 @@ storage with the right shape and number of dimensions.
...
@@ -371,7 +372,7 @@ storage with the right shape and number of dimensions.
:note:
:note:
Given the simple nature of this op, there was no need to use the
Given the simple nature of this op, there was no need to use the
c_support_code()
function.
``c_support_code()``
function.
.. code-block:: python
.. code-block:: python
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论