3 Mar 2010 21:22
[Cython] using the C API Declarations in several C++ files
Hi,
I am using the C API Declarations[0], which generate the file
_hermes_common_api.h, that I include in my C++ files. I am having
problems with initializing the module properly.
Here is an example of my C++ function, that internally calls scipy's
sparse solver to solve the system:
void solve_linear_system_scipy_cg(CooMatrix *mat, double *res)
{
if (import__hermes_common())
throw std::runtime_error("hermes_common failed to import.");
CSRMatrix M(mat);
insert_object("m", c2py_CSRMatrix(&M));
insert_object("rhs", c2numpy_double_inplace(res, mat->get_size()));
cmd("A = m.to_scipy_csr()");
cmd("from scipy.sparse.linalg import cg");
cmd("x, res = cg(A, rhs)");
double *x;
int n;
numpy2c_double_inplace(get_object("x"), &x, &n);
memcpy(res, x, n*sizeof(double));
}
the functions insert_object(), cmd(), c2numpy_double_inplace() etc.
are declared in my .pyx file to ease C++ and Python integration (the
whole code is at [1]). Everything works, as long as I call the
"import__hermes_common()" function. Since I call Python things all
over my C++ code, it's kind of annoying to always call this by hand.
(Continue reading)
RSS Feed