Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
5f3e9178
Commit
5f3e9178
authored
May 25, 2018
by
scoder
Committed by
GitHub
May 25, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2288 from gabrieldemarmiesse/memview_to_c
Add tutorial on how to call C function with numpy arrays.
parents
5bbeec38
de001cbb
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
0 deletions
+81
-0
docs/examples/memoryviews/C_func_file.c
docs/examples/memoryviews/C_func_file.c
+6
-0
docs/examples/memoryviews/memview_to_c.pyx
docs/examples/memoryviews/memview_to_c.pyx
+27
-0
docs/src/tutorial/clibraries.rst
docs/src/tutorial/clibraries.rst
+2
-0
docs/src/userguide/memoryviews.rst
docs/src/userguide/memoryviews.rst
+46
-0
No files found.
docs/examples/memoryviews/C_func_file.c
0 → 100644
View file @
5f3e9178
void
multiply_by_10_in_C
(
double
arr
[],
unsigned
int
n
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
arr
[
i
]
*=
10
;
}
}
docs/examples/memoryviews/memview_to_c.pyx
0 → 100644
View file @
5f3e9178
# distutils: sources=./C_func_file.c
# distutils: include_dirs=./
cdef
extern
from
"C_func_file.h"
:
void
multiply_by_10_in_C
(
double
*
,
unsigned
int
)
import
numpy
as
np
def
multiply_by_10
(
arr
):
# 'arr' is a one-dimensional numpy array
if
not
arr
.
flags
[
'C_CONTIGUOUS'
]:
arr
=
np
.
ascontiguousarray
(
arr
)
# Makes a contiguous copy of the numpy array.
cdef
double
[::
1
]
arr_memview
=
arr
multiply_by_10_in_C
(
&
arr_memview
[
0
],
arr_memview
.
shape
[
0
])
return
arr
a
=
np
.
ones
(
5
,
dtype
=
np
.
double
)
print
(
multiply_by_10
(
a
))
b
=
np
.
ones
(
10
,
dtype
=
np
.
double
)
b
=
b
[::
2
]
# b is not contiguous.
print
(
multiply_by_10
(
b
))
# but our function still works as expected.
docs/src/tutorial/clibraries.rst
View file @
5f3e9178
..
_using_c_libraries
:
******************
Using
C
libraries
******************
...
...
docs/src/userguide/memoryviews.rst
View file @
5f3e9178
...
...
@@ -396,6 +396,8 @@ like::
int [:, :, :] my_memoryview = obj
.. _c_and_fortran_contiguous_memoryviews:
C and Fortran contiguous memoryviews
------------------------------------
...
...
@@ -691,6 +693,50 @@ reject None input straight away in the signature, which is supported in Cython
Unlike object attributes of extension classes, memoryview slices are not
initialized to None.
Pass data from a C function via pointer
=======================================
Since use of pointers in C is ubiquitous, here we give a quick example of how
to call C functions whose arguments contain pointers. Let's suppose you want to
manage an array (allocate and deallocate) with NumPy (it can also be Python arrays, or
anything that supports the buffer interface), but you want to perform computation on this
array with an external C function implemented in :file:`C_func_file.c`:
.. literalinclude:: ../../examples/memoryviews/C_func_file.c
:linenos:
This file comes with a header file called :file:`C_func_file.h` containing::
void multiply_by_10_in_C(double arr[], unsigned int n);
where ``arr`` points to the array and ``n`` is its size.
You can call the function in a Cython file in the following way:
.. literalinclude:: ../../examples/memoryviews/memview_to_c.pyx
:linenos:
Several things to note:
- ``::1`` requests a C contiguous view, and fails if the buffer is not C contiguous.
See :ref:`c_and_fortran_contiguous_memoryviews`.
- ``&arr_memview[0]`` can be understood as 'the adress of the first element of the
memoryview'. For contiguous arrays, this is equivalent to the
start address of the flat memory buffer.
- ``arr_memview.shape[0]`` could have been replaced by ``arr_memview.size``,
``arr.shape[0]`` or ``arr.size``. But ``arr_memview.shape[0]`` is more efficient
because it doesn't require any Python interaction.
- ``multiply_by_10`` will perform computation in-place if the array passed is contiguous,
and will return a new numpy array if ``arr`` is not contiguous.
- If you are using Python arrays instead of numpy arrays, you don't need to check
if the data is stored contiguously as this is always the case. See :ref:`array-array`.
This way, you can call the C function similar to a normal Python function,
and leave all the memory management and cleanup to NumPy arrays and Python's
object handling. For the details of how to compile and
call functions in C files, see :ref:`using_c_libraries`.
.. _GIL: http://docs.python.org/dev/glossary.html#term-global-interpreter-lock
.. _new style buffers: http://docs.python.org/c-api/buffer.html
.. _pep 3118: http://www.python.org/peps/pep-3118.html
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment