memoryview

Boolean numpy arrays with Cython

∥☆過路亽.° 提交于 2021-02-05 04:01:13
问题 I have a numpy boolean array: myarr = np.array([[False, True], [True, False]]) If I try to initialise a Cython MemoryView with it, like this: cdef bint[:,:] mymem = myarr I get this error: ValueError: Does not understand character buffer dtype format string ('?') If I do this instead, it works fine: cdef np.int_t[:,:] mymem = np.int_(myarr) How can I store a boolean numpy array using Cython MemoryViews? 回答1: I ran into the same problem some time ago. Unfortunately I did not find a direct

Boolean numpy arrays with Cython

夙愿已清 提交于 2021-02-05 03:55:50
问题 I have a numpy boolean array: myarr = np.array([[False, True], [True, False]]) If I try to initialise a Cython MemoryView with it, like this: cdef bint[:,:] mymem = myarr I get this error: ValueError: Does not understand character buffer dtype format string ('?') If I do this instead, it works fine: cdef np.int_t[:,:] mymem = np.int_(myarr) How can I store a boolean numpy array using Cython MemoryViews? 回答1: I ran into the same problem some time ago. Unfortunately I did not find a direct

Boolean numpy arrays with Cython

亡梦爱人 提交于 2021-02-05 03:55:28
问题 I have a numpy boolean array: myarr = np.array([[False, True], [True, False]]) If I try to initialise a Cython MemoryView with it, like this: cdef bint[:,:] mymem = myarr I get this error: ValueError: Does not understand character buffer dtype format string ('?') If I do this instead, it works fine: cdef np.int_t[:,:] mymem = np.int_(myarr) How can I store a boolean numpy array using Cython MemoryViews? 回答1: I ran into the same problem some time ago. Unfortunately I did not find a direct

Initialise Cython Memoryview efficiently

北慕城南 提交于 2021-01-27 07:22:28
问题 I'm currently setting my MemoryView s in my Cython pyx file as follows: @cython.boundscheck(False) cdef int[:] fill_memview(): # This happens inside a big loop so needs to be fast cdef int[:] x = np.empty(10) for i in range(10): x[i] = i return x cdef stupid_loop(): for i in range(10000): fill_memview() When I compile the pyx file with cython -a foo.pyx the line cdef int[:] x = np.empty(10) shows in the resulting annotated html file in dark yellow (meaning it has lots of Python calls slowing

Python: writing to memory in a single operation

自作多情 提交于 2020-07-13 15:47:02
问题 I'm writing a userspace driver for accessing FPGA registers in Python 3.5 that mmap s the FPGA's PCI address space, obtains a memoryview to provide direct access to the memory-mapped register space, and then uses struct.pack_into("<I", ...) to write a 32-bit value into the selected 32-bit aligned address. def write_u32(address, data): assert address % 4 == 0, "Address must be 32-bit aligned" path = path.lib.Path("/dev/uio0") file_size = path.stat().st_size with path.open(mode='w+b') as f: mv

Python: writing to memory in a single operation

為{幸葍}努か 提交于 2020-07-13 15:45:57
问题 I'm writing a userspace driver for accessing FPGA registers in Python 3.5 that mmap s the FPGA's PCI address space, obtains a memoryview to provide direct access to the memory-mapped register space, and then uses struct.pack_into("<I", ...) to write a 32-bit value into the selected 32-bit aligned address. def write_u32(address, data): assert address % 4 == 0, "Address must be 32-bit aligned" path = path.lib.Path("/dev/uio0") file_size = path.stat().st_size with path.open(mode='w+b') as f: mv

Python: writing to memory in a single operation

谁都会走 提交于 2020-07-13 15:43:00
问题 I'm writing a userspace driver for accessing FPGA registers in Python 3.5 that mmap s the FPGA's PCI address space, obtains a memoryview to provide direct access to the memory-mapped register space, and then uses struct.pack_into("<I", ...) to write a 32-bit value into the selected 32-bit aligned address. def write_u32(address, data): assert address % 4 == 0, "Address must be 32-bit aligned" path = path.lib.Path("/dev/uio0") file_size = path.stat().st_size with path.open(mode='w+b') as f: mv

Python: writing to memory in a single operation

扶醉桌前 提交于 2020-07-13 15:42:32
问题 I'm writing a userspace driver for accessing FPGA registers in Python 3.5 that mmap s the FPGA's PCI address space, obtains a memoryview to provide direct access to the memory-mapped register space, and then uses struct.pack_into("<I", ...) to write a 32-bit value into the selected 32-bit aligned address. def write_u32(address, data): assert address % 4 == 0, "Address must be 32-bit aligned" path = path.lib.Path("/dev/uio0") file_size = path.stat().st_size with path.open(mode='w+b') as f: mv

Cython memoryviews: wrapping c function with array parameter to pass numpy array

一个人想着一个人 提交于 2020-07-09 07:12:05
问题 I am trying to use Cython to wrap c function with an array parameter ( quick_sort() ), so I can pass a numpy array to it. I've searched the documentation, SO and web for a working, minimal example but didn't find it. I've tried several possibilities but without any progress, so please help me to figure it out. Here are my files: quicksort.c #include <stdio.h> void quick_sort (int* a, int n) { int i, j, p, t; if (n < 2) return; p = a[n / 2]; for (i = 0, j = n - 1;; i++, j--) { while (a[i] < p)

How to wrap a C pointer and length in a new-style buffer object in Cython?

南笙酒味 提交于 2020-04-06 02:39:09
问题 I'm writing a Python 2.7 extension module in Cython. How do I create a Python object implementing the new-style buffer interface that wraps a chunk of memory given to me by a C library? The chunk of memory is just a string of bytes, not a structure or multidimensional array. I'm given a const void * pointer and a length, and some details about how long the pointer stays valid. I can't copy the memory—that would kill performance for my application. With the old-style buffer objects I could