我目前正在尝试找到一种方法,将多维数组(双精度)从 C 中的共享库返回到 python 并使其成为 np.array。我当前的方法如下所示:
共享库(“utils.c”)
#include <stdio.h>
void somefunction(double *inputMatrix, int d1_inputMatrix, int d2_inputMatrix, int h_inputMatrix, int w_inputMatrix, double *kernel, int d1_kernel, int d2_kernel, int h_kernel, int w_kernel, int stride) {
double result[d1_kernel][d2_kernel][d2_inputMatrix][h_inputMatrix-h_kernel+1][w_inputMatrix-w_kernel+1];
// ---some operation--
return result;
}
Run Code Online (Sandbox Code Playgroud)
现在,我使用以下命令编译 utils.c:cc -fPIC -shared -o utils.so utils.c
python ("somefile.py")
from ctypes import *
import numpy as np
so_file = "/home/benni/Coding/5.PK/Code/utils.so"
utils = CDLL(so_file)
INT = c_int64
ND_POINTER_4 = np.ctypeslib.ndpointer(dtype=np.float64, ndim=4, flags="C")
utils.convolve.argtypes = [ND_POINTER_4, INT, INT, INT, INT, …Run Code Online (Sandbox Code Playgroud)