简体   繁体   English

从 C 扩展对 Numpy 阵列进行操作,无需 memory 副本

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

I'm new to C extensions for NumPy and I'm wondering if the following workflow is possible.我是 NumPy 的C 扩展的新手,我想知道以下工作流程是否可行。

  1. Pre-allocate an array in NumPy在 NumPy 中预分配一个数组
  2. Pass this array to a C extension将此数组传递给 C 扩展
  3. Modify array data in-place in C在 C 中就地修改数组数据
  4. Use the updated array in Python with standard NumPy functions将 Python 中的更新数组与标准 NumPy 函数一起使用

In particular, I'd like to do this while ensuring I'm making zero new copies of the data at any step.特别是,我想这样做,同时确保我在任何步骤都制作零个新的数据副本

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. 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. I'm also aware of Cython though I don't know if it does similar coercions or copies under the hood.我也知道 Cython,尽管我不知道它是否会在后台进行类似的强制或复制。 I'm specifically interested in simple indexed get- and set- operations on ndarray with numeric (eg. int32 ) values.我对具有数字(例如int32 )值的ndarray上的简单索引 get- 和 set- 操作特别感兴趣。

Could someone provide a minimal working example of creating a NumPy array, modifying it in-place in a C extension, and using the results in Python subsequently?有人可以提供一个最小的工作示例来创建 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 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

choosing between writing raw C module and using cython depends on the purpose of the module written.在编写原始 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 and prevent common mistakes that people do when writing C code (like memory management problems), as well as automate the compiler includes and allow an overall easier access to complicated functionality (like using numpy iterators). 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 of dll), or even create numpy array on C side and have it managed by python (but you will have to und 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 管理(但您必须取消erstand the numpy C API ).了解numpy C API )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM