簡體   English   中英

從 C 擴展對 Numpy 陣列進行操作,無需 memory 副本

[英]Operate on Numpy array from C extension without memory copy

我是 NumPy 的C 擴展的新手,我想知道以下工作流程是否可行。

  1. 在 NumPy 中預分配一個數組
  2. 將此數組傳遞給 C 擴展
  3. 在 C 中就地修改數組數據
  4. 將 Python 中的更新數組與標准 NumPy 函數一起使用

特別是,我想這樣做,同時確保我在任何步驟都制作零個新的數據副本

I'm familiar with boilerplate on the C side such as PyModuleDef , PyMethodDef , and the PyObject* arguments but a lot of examples I've seen involve coercion to C arrays which to my understanding involves copying and/or casting. 我也知道 Cython,盡管我不知道它是否會在后台進行類似的強制或復制。 我對具有數字(例如int32 )值的ndarray上的簡單索引 get- 和 set- 操作特別感興趣。

有人可以提供一個最小的工作示例來創建 NumPy 數組,在 C 擴展中就地修改它,然后使用 Python 中的結果嗎?

Cython doesn't create new copies of numpy arrays unless you specifically request it to do so using numpy functions, so it is as efficient as it can be when dealing with numpy arrays, seeWorking with NumPy

在編寫原始 C 模塊和使用 cython 之間進行選擇取決於編寫模塊的目的。 if you are writing a module that will only be used by python to do a very small specific task with numpy arrays as fast as possible, then by all means do use cython, as it will automate registering the module correctly as well as handle the memory並防止人們在編寫 C 代碼時犯的常見錯誤(如 memory 管理問題),以及自動化編譯器包含並允許整體更輕松地訪問復雜的功能(如使用 Z2EA9510C37F7F89E4941FF75F62F21)。

however if your module is going to be used in other languages and has to be run independently from python and has to be used with python without any overhead, and implements some complex C data structures and requires a lot of C functionality then by all means create your own C extension (or even a dll), and you can pass pointers to numpy arrays from python (using numpy.ctypeslib.as_ctypes_type ), or pass the python object itself and return it (but you must make a.pyd/so instead dll),甚至在 C 端創建 numpy 數組,並由 python 管理(但您必須取消了解numpy C API )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM