Questions tagged [memoryview]

memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying. Memory is generally interpreted as simple bytes.

The documentation on memoryview can be found here

144 questions
125
votes
6 answers

What exactly is the point of memoryview in Python?

Checking the documentation on memoryview: memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying. class memoryview(obj) Create a memoryview that references obj. obj must…
Basel Shishani
  • 7,735
  • 6
  • 50
  • 67
43
votes
4 answers

Cython Numpy warning about NPY_NO_DEPRECATED_API when using MemoryView

I am converting a Cython memoryview to a numpy array (to be able to use it in pure Python code): from libc.stdlib cimport realloc cimport numpy as np DTYPE = np.float64 ctypedef np.float64_t DTYPE_t cpdef np.ndarray[DTYPE_t] compute(DTYPE_t[:,::1]…
Amenhotep
  • 920
  • 1
  • 13
  • 18
37
votes
2 answers

When should a memoryview be used?

The full description of memoryview can be found here: Create a memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray. A memoryview has the notion of an…
zr.
  • 7,528
  • 11
  • 50
  • 84
28
votes
1 answer

Cython: Convert memory view to NumPy array

How to convert a typed memoryview to an NumPy array in cython? The docs have cimport numpy as np import numpy as np numpy_array = np.asarray( my_pointer) I took this for my case np.asarray(
embert
  • 7,336
  • 10
  • 49
  • 78
26
votes
1 answer

Cython typed memoryviews: what they really are?

The Cython documentation explains very well what they allow for, how you can declare them, and how to use them. However, it is still not clear to me what they really are. For example, a simple assignment from a numpy array like this: my_arr =…
Gioker
  • 649
  • 5
  • 14
20
votes
3 answers

Cython: Buffer type mismatch, expected 'int' but got 'long'

I'm having trouble passing in this memoryview of integers into this (rather trivial) function. Python is giving me this error: ValueError: Buffer dtype mismatch, expected 'int' but got 'long' Can someone help me understand what's going on?…
hlin117
  • 20,764
  • 31
  • 72
  • 93
19
votes
1 answer

Why does creating this memoryview raise a ValueError only when assigning to a variable?

Pythons memoryview does not support datetime64 or timedelta. Ok. But when I try to create a memoryview of a structured array that includes a datetime64 or timedelta, it appears to work... unless I assign it to a variable! In [19]:…
gerrit
  • 24,025
  • 17
  • 97
  • 170
15
votes
1 answer

Cython Memoryview as return value

Consider this dummy Cython code: #!python #cython: boundscheck=False #cython: wraparound=False #cython: initializedcheck=False #cython: cdivision=True #cython: nonecheck=False import numpy as np # iterator function cdef double[:] f(double[:]…
HenriV
  • 458
  • 6
  • 15
14
votes
2 answers

How to use Cython typed memoryviews to accept strings from Python?

How can I write a Cython function that takes a byte string object (a normal string, a bytearray, or another object that follows the buffer protocol) as a typed memoryview? According to the Unicode and Passing Strings Cython tutorial page, the…
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
12
votes
4 answers

Cython: Create memoryview without NumPy array?

Since I found memory-views handy and fast, I try to avoid creating NumPy arrays in cython and work with the views of the given arrays. However, sometimes it cannot be avoided, not to alter an existing array but create a new one. In upper functions…
embert
  • 7,336
  • 10
  • 49
  • 78
11
votes
2 answers

Buffers and Memoryview Objects explained for the non-C programmer

Python 2.7 has introduced a new API for buffers and memoryview objects. I read the documentation on them and I think I got the basic concept (accessing the internal data of an object in a raw form without copying it, which I suppose means a "faster…
mac
  • 42,153
  • 26
  • 121
  • 131
11
votes
1 answer

Cython: Memory view of freed memory

In Cython code, I can allocate some memory and wrap it in a memory view, e.g. like this: cdef double* ptr cdef double[::1] view ptr = PyMem_Malloc(N*sizeof('double')) view = ptr If I now free the memory using PyMem_Free(ptr),…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
11
votes
2 answers

Shared memory in mpi4py

I use a MPI (mpi4py) script (on a single node), which works with a very large object. In order to let all processes have access to the object, I distribute it through comm.bcast(). This copies the object to all processes and consumes a lot of…
Roman
  • 2,225
  • 5
  • 26
  • 55
11
votes
1 answer

How to create a memoryview for a non-contiguous memory location?

I have a fragmented structure in memory and I'd like to access it as a contiguous-looking memoryview. Is there an easy way to do this or should I implement my own solution? For example, consider a file format that consists of records. Each record…
molnarg
  • 2,775
  • 1
  • 19
  • 20
9
votes
2 answers

How to declare 2D c-arrays dynamically in Cython

I need to perform a lot of work using 2D numpy arrays of various sizes and I would like to offload these calculations onto cython. The idea is that my 2D numpy arrays would be passed from python to cython where it would be converted into c-array or…
phil_thy
  • 151
  • 1
  • 1
  • 10
1
2 3
9 10