1. 程式人生 > >ctypes的運用(把一個numpy陣列傳入c中)

ctypes的運用(把一個numpy陣列傳入c中)

1.把一個二維陣列轉化成一維的,傳入C

void show_matrix(int *matrix, int rows, int columns)
{
    int i, j;
    for (i=0; i<rows; i++) {
        for (j=0; j<columns; j++) {
            printf("matrix[%d][%d] = %d\n", i, j, matrix[i*rows + j]);
        }
    }
}

 編譯成動態庫供python呼叫 ,gcc -shared -O2 mystuff.c  -ldl -o mystuff.so


import ctypes
import numpy as np
 
lib = ctypes.cdll.LoadLibrary("./mystuff.so")
arr = np.zeros((3,5))
#arr = np.array([[1,2],[3,4]])
tmp = np.asarray(arr)
rows, cols = tmp.shape
dataptr = tmp.ctypes.data_as(ctypes.c_char_p)
lib.show_matrix(dataptr, rows, cols)

執行 python test.py

2把numpy的二維陣列打包成ctypes的標準陣列形式,傳送給C。

from ctypes import *
import numpy
 
def Convert1DToCArray(TYPE, ary):
    arow = TYPE(*ary.tolist())
    return arow
 
def Convert2DToCArray(ary):
    ROW = c_int * len(ary[0])
    rows = []
    for i in range(len(ary)):
        rows.append(Convert1DToCArray(ROW, ary[i]))
    MATRIX = ROW * len(ary)
    return MATRIX(*rows)
 
a=numpy.array([[1,2,2],[1,3,4]])
caa = Convert2DToCArray(a)
 
def ShowCArrayArray(caa):
    for row in caa:
        for col in row:
            print col
ShowCArrayArray(caa)
3  如果將python中list傳入C函式陣列,則需要提前轉換。
import ctypes
pyarray = [1, 2, 3, 4, 5]
carray = (ctypes.c_int * len(pyarray))(*pyarray) //有點類似malloc的方式生成carray
print so.sum_array(carray, len(pyarray))

如果如果需要將C array返回python,需要提前把array傳入,然後在C函式中修改,返回時再把c array轉換為np.array

pyarray = [1,2,3,4,5,6,7,8]
carray = (ctypes.c_int*len(pyarray))(*pyarray)
so.modify_array(carray, len(pyarray))
print np.array(carray)

output

[10 20 30 40 50 60 70 80]

也可以用形參方式提前定義函式介面,然後再傳入numpy結構

import ctypes
import numpy as np
from numpy.ctypeslib import ndpointer
so = ctypes.CDLL('./sum.so')
pyarray = np.array([1,2,3,4,5,6,7,8], dtype="int32")
fun = so.modify_array
fun.argtypes = [ndpointer(ctypes.c_int), ctypes.c_int]
fun.restype = None
fun(pyarray, len(pyarray))
print np.array(pyarray)

注意:numpy中的資料型別指定很重要,即dtype的設定