小编LeC*_*mpy的帖子

如何将 C 共享库中的多维数组作为 np.array 返回到 python?

我目前正在尝试找到一种方法,将多维数组(双精度)从 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)

c python arrays numpy shared-libraries

5
推荐指数
1
解决办法
596
查看次数

标签 统计

arrays ×1

c ×1

numpy ×1

python ×1

shared-libraries ×1