I want to implement a function in C which takes as input two matrices as numpy array and return another matrix.
First I have the function in C in the file f_2_mat_in.c
:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
double *f_2_mat_in(
const double *matrix_one,
size_t n,
size_t m,
const double *matrix_two,
size_t u,
size_t v
)
{
double *matrix_out = malloc(sizeof *matrix_out * n * m);
for (size_t i = 0; i < n; i++){
for (size_t j = 0; j < m; j++){
printf("%f ", matrix_one[i * m + j]);
matrix_out[i * m + j] = matrix_one[i * m + j];
printf("--\n");
}
}
for (size_t i = 0; i < u; i++){
for (size_t j = 0; j < v; j++){
printf("%f ", matrix_two[u * v + j]);
printf("--\n");
}
}
printf("\n");
return matrix_out;
}
Then I have the main.py
python script which usesf_2_mat_in
: c
from ctypes import c_void_p, c_double, c_size_t, c_int, cdll, POINTER
from numpy.ctypeslib import ndpointer
import pdb
import numpy as np
c_double_p1 = POINTER(c_double)
c_double_p2 = POINTER(c_double)
n = 5
m = 4
kernel_size = 3
u = 3
v = 6
matrix_one = np.random.randn(n, m).astype(c_double)
matrix_one[0][1] = np.nan
lib = cdll.LoadLibrary("f_2_mat_in.so")
f_2_mat_in = lib.f_2_mat_in
f_2_mat_in.argtypes = c_int, c_double_p1, c_size_t, c_size_t, c_double_p2, c_size_t, c_size_t
f_2_mat_in.restype = ndpointer(
dtype=c_double,
shape=(n,m),
flags='C')
f_2_mat_in.restype = ndpointer(
dtype=c_double,
shape=(u,v),
flags='C')
matrix_out = f_2_mat_in(
c_int(kernel_size),
matrix_one.ctypes.data_as(c_double_p1),
c_size_t(n),
c_size_t(m),
matrix_one.ctypes.data_as(c_double_p2),
c_size_t(u),
c_size_t(v))
print("matrix_one:", matrix_one)
print("matrix_two:", matrix_two)
print("matrix_out:", matrix_out)
print("matrix_one.shape:", matrix_one.shape)
print("matrix_two.shape:", matrix_two.shape)
print("matrix_out.shape:", matrix_out.shape)
print("in == out", matrix_one == matrix_out)
pdb.set_trace()
Finally I have bash script which compile the C script and run the pyhon script:
a=f_2_mat_in
cc -fPIC -shared -o $a.so $a.c && python main.py
I get the following error (in MacOS 11.6.1
):
Python(53609,0x111c84e00) malloc: can't allocate region
:*** mach_vm_map(size=5626830804037632, flags: 100) failed (error code=3)
Python(53609,0x111c84e00) malloc: *** set a breakpoint in malloc_error_break to debug
What am I doing wrong?