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
1a8ee2f9
Commit
1a8ee2f9
authored
Jun 20, 2018
by
scoder
Committed by
GitHub
Jun 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2369 from gabrieldemarmiesse/test_memoryview_1
Added tests to "Memoryviews" part 1
parents
0c329bac
29c73166
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
54 deletions
+53
-54
docs/examples/userguide/memoryviews/quickstart.pyx
docs/examples/userguide/memoryviews/quickstart.pyx
+52
-0
docs/src/userguide/memoryviews.rst
docs/src/userguide/memoryviews.rst
+1
-54
No files found.
docs/examples/userguide/memoryviews/quickstart.pyx
0 → 100644
View file @
1a8ee2f9
from
cython.view
cimport
array
as
cvarray
import
numpy
as
np
# Memoryview on a NumPy array
narr
=
np
.
arange
(
27
,
dtype
=
np
.
dtype
(
"i"
)).
reshape
((
3
,
3
,
3
))
cdef
int
[:,
:,
:]
narr_view
=
narr
# Memoryview on a C array
cdef
int
carr
[
3
][
3
][
3
]
cdef
int
[:,
:,
:]
carr_view
=
carr
# Memoryview on a Cython array
cyarr
=
cvarray
(
shape
=
(
3
,
3
,
3
),
itemsize
=
sizeof
(
int
),
format
=
"i"
)
cdef
int
[:,
:,
:]
cyarr_view
=
cyarr
# Show the sum of all the arrays before altering it
print
(
"NumPy sum of the NumPy array before assignments: %s"
%
narr
.
sum
())
# We can copy the values from one memoryview into another using a single
# statement, by either indexing with ... or (NumPy-style) with a colon.
carr_view
[...]
=
narr_view
cyarr_view
[:]
=
narr_view
# NumPy-style syntax for assigning a single value to all elements.
narr_view
[:,
:,
:]
=
3
# Just to distinguish the arrays
carr_view
[
0
,
0
,
0
]
=
100
cyarr_view
[
0
,
0
,
0
]
=
1000
# Assigning into the memoryview on the NumPy array alters the latter
print
(
"NumPy sum of NumPy array after assignments: %s"
%
narr
.
sum
())
# A function using a memoryview does not usually need the GIL
cpdef
int
sum3d
(
int
[:,
:,
:]
arr
)
nogil
:
cdef
size_t
i
,
j
,
k
cdef
int
total
=
0
I
=
arr
.
shape
[
0
]
J
=
arr
.
shape
[
1
]
K
=
arr
.
shape
[
2
]
for
i
in
range
(
I
):
for
j
in
range
(
J
):
for
k
in
range
(
K
):
total
+=
arr
[
i
,
j
,
k
]
return
total
# A function accepting a memoryview knows how to use a NumPy array,
# a C array, a Cython array...
print
(
"Memoryview sum of NumPy array is %s"
%
sum3d
(
narr
))
print
(
"Memoryview sum of C array is %s"
%
sum3d
(
carr
))
print
(
"Memoryview sum of Cython array is %s"
%
sum3d
(
cyarr
))
# ... and of course, a memoryview.
print
(
"Memoryview sum of C memoryview is %s"
%
sum3d
(
carr_view
))
docs/src/userguide/memoryviews.rst
View file @
1a8ee2f9
...
@@ -28,60 +28,7 @@ Quickstart
...
@@ -28,60 +28,7 @@ Quickstart
If you are used to working with NumPy, the following examples should get you
If you are used to working with NumPy, the following examples should get you
started with Cython memory views.
started with Cython memory views.
::
.. literalinclude:: ../../examples/userguide/memoryviews/quickstart.pyx
from cython.view cimport array as cvarray
import numpy as np
# Memoryview on a NumPy array
narr = np.arange(27, dtype=np.dtype("i")).reshape((3, 3, 3))
cdef int [:, :, :] narr_view = narr
# Memoryview on a C array
cdef int carr[3][3][3]
cdef int [:, :, :] carr_view = carr
# Memoryview on a Cython array
cyarr = cvarray(shape=(3, 3, 3), itemsize=sizeof(int), format="i")
cdef int [:, :, :] cyarr_view = cyarr
# Show the sum of all the arrays before altering it
print("NumPy sum of the NumPy array before assignments: %s" % narr.sum())
# We can copy the values from one memoryview into another using a single
# statement, by either indexing with ... or (NumPy-style) with a colon.
carr_view[...] = narr_view
cyarr_view[:] = narr_view
# NumPy-style syntax for assigning a single value to all elements.
narr_view[:, :, :] = 3
# Just to distinguish the arrays
carr_view[0, 0, 0] = 100
cyarr_view[0, 0, 0] = 1000
# Assigning into the memoryview on the NumPy array alters the latter
print("NumPy sum of NumPy array after assignments: %s" % narr.sum())
# A function using a memoryview does not usually need the GIL
cpdef int sum3d(int[:, :, :] arr) nogil:
cdef size_t i, j, k
cdef int total = 0
I = arr.shape[0]
J = arr.shape[1]
K = arr.shape[2]
for i in range(I):
for j in range(J):
for k in range(K):
total += arr[i, j, k]
return total
# A function accepting a memoryview knows how to use a NumPy array,
# a C array, a Cython array...
print("Memoryview sum of NumPy array is %s" % sum3d(narr))
print("Memoryview sum of C array is %s" % sum3d(carr))
print("Memoryview sum of Cython array is %s" % sum3d(cyarr))
# ... and of course, a memoryview.
print("Memoryview sum of C memoryview is %s" % sum3d(carr_view))
This code should give the following output::
This code should give the following output::
...
...
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