我在Python中编写了一些代码,其输出是一个numpy数组,现在我想将该输出发送到C++
代码,其中将执行大部分计算。
我尝试过使用cython的public cdef
,但我正在尝试使用某些问题。非常感谢你的帮助!这是我的代码:
pymodule.pyx
:
from pythonmodule import result # result is my numpy array
import numpy as np
cimport numpy as np
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
cdef public void cfunc():
print 'I am in here!!!'
cdef np.ndarray[np.float64_t, ndim=2, mode='c'] res = result
print res
一旦这是cython化,我打电话:
pymain.c
:
#include <Python.h>
#include <numpy/arrayobject.h>
#include "pymodule.h"
int main() {
Py_Initialize();
initpymodule();
test(2);
Py_Finalize();
}
int test(int a)
{
Py_Initialize();
initpymodule();
cfunc();
return 0;
}
我在NameError
处获得result
变量C++
。我已经尝试用指针定义它并间接从其他函数调用它,但该数组仍然是不可见的。我很确定答案很简单,但我不明白。谢谢你的帮助!
答案 0 :(得分:7)
NameError是因为Python无法找到模块,工作目录不会自动添加到PYTHONPATH
。在setenv("PYTHONPATH", ".", 1);
代码中使用setenv
和C/C++
修复此问题。
pythonmodule.py
:
import numpy as np
result = np.arange(20, dtype=np.float).reshape((2, 10))
您可以使用 public
关键字构建pymodule.pyx
以导出该数组。通过添加一些辅助功能,您通常不需要触摸Python和Numpy C-API
:
from pythonmodule import result
from libc.stdlib cimport malloc
import numpy as np
cimport numpy as np
cdef public np.ndarray getNPArray():
""" Return array from pythonmodule. """
return <np.ndarray>result
cdef public int getShape(np.ndarray arr, int shape):
""" Return Shape of the Array based on shape par value. """
return <int>arr.shape[1] if shape else <int>arr.shape[0]
cdef public void copyData(float *** dst, np.ndarray src):
""" Copy data from src numpy array to dst. """
cdef float **tmp
cdef int i, j, m = src.shape[0], n=src.shape[1];
# Allocate initial pointer
tmp = <float **>malloc(m * sizeof(float *))
if not tmp:
raise MemoryError()
# Allocate rows
for j in range(m):
tmp[j] = <float *>malloc(n * sizeof(float))
if not tmp[j]:
raise MemoryError()
# Copy numpy Array
for i in range(m):
for j in range(n):
tmp[i][j] = src[i, j]
# Assign pointer to dst
dst[0] = tmp
函数getNPArray
和getShape
分别返回数组及其形状。添加了copyData
以便只提取ndarray.data
并复制它,这样您就可以在不初始化解释器的情况下完成Python并完成工作。
示例程序(C
,C++
看起来应该相同)看起来像这样:
#include <Python.h>
#include "numpy/arrayobject.h"
#include "pyxmod.h"
#include <stdio.h>
void printArray(float **arr, int m, int n);
void getArray(float ***arr, int * m, int * n);
int main(int argc, char **argv){
// Holds data and shapes.
float **data = NULL;
int m, n;
// Gets array and then prints it.
getArray(&data, &m, &n);
printArray(data, m, n);
return 0;
}
void getArray(float ***data, int * m, int * n){
// setenv is important, makes python find
// modules in working directory
setenv("PYTHONPATH", ".", 1);
// Initialize interpreter and module
Py_Initialize();
initpyxmod();
// Use Cython functions.
PyArrayObject *arr = getNPArray();
*m = getShape(arr, 0);
*n = getShape(arr, 1);
copyData(data, arr);
if (data == NULL){ //really redundant.
fprintf(stderr, "Data is NULL\n");
return ;
}
Py_DECREF(arr);
Py_Finalize();
}
void printArray(float **arr, int m, int n){
int i, j;
for(i=0; i < m; i++){
for(j=0; j < n; j++)
printf("%f ", arr[i][j]);
printf("\n");
}
}
永远记得设置:
setenv("PYTHONPATH", ".", 1);
在之前 调用Py_Initialize
,以便Python可以在工作目录中找到模块。
其余的非常简单。它可能需要一些额外的错误检查,肯定需要一个函数来释放分配的内存。
按照您尝试的方式进行操作比使用它更麻烦,您可能最好使用numpy.save
将数组保存在npy
二进制文件中然后使用一些C++ library that reads that file for you