I've written a simple cython wrapper for donlp2, a C optimization library. The library uses global variables extensively and assumes the caller has written functions with pre-defined names so the function can call them. (e.g., there is a function ef and egradf that evaluate the function and its gradient, respectively)
The wrapper was pretty simple to write using "cdef extern" for the global variables and "cdef public" to create the functions the C library expected. I also used view.array to cast double* pointers to cython arrays that could be passed to python functions. Doing that my wrapper is able to use the C library to optimize functions & gradients defined in pure python.
Below is the wrapper code:
from libc.string cimport strcpy
from cython cimport view
cdef extern void donlp2()
#global varaibles used by donlp2
#only import those variables that are necessary
cdef extern int n
cdef extern int nlin
cdef extern int nonlin
cdef extern int nstep
cdef extern int iterma
cdef extern int icf
cdef extern int icgf
cdef extern double *x
cdef extern char name[41]
cdef extern double del0
cdef extern double tau
cdef extern double tau0
cdef extern int analyt
cdef extern double epsdif
cdef extern int nreset
cdef extern int silent
cdef extern double *low
cdef extern double *up
cdef extern double optite
#Below are used only if bloc is True
cdef extern double *xtr
cdef extern double *fu
cdef extern double **fugrad
cdef extern int bloc
class DonlpProblem:
"""
Contains all the inputs, including python functions, to
solve a constrained nonlinear problem using donlp2.
"""
def __init__(self,
x0,
ef,
egradf,
low,
up,
nonlin,
activeConstraintTolerance,
name,
bloc=False,
eval_extern=None,
econ=None,
maxIter=4000,
maxBacktrackIter=20,
descentVsFeasibilityWeight=0.1,
analyticDerivatives=True,
silent=False,
nreset=4):
self.bloc = bloc
if self.bloc:
self.eval_extern = eval_extern
else:
self.ef = ef
self.egradf = egradf
self.econ = econ
self.n = x0.size
assert(nonlin+self.n == low.size)
assert(nonlin+self.n == up.size)
self.x0 = x0
self.low = low
self.up = up
self.nonlin = nonlin
self.maxIter = maxIter
self.maxBacktrackIter = maxBacktrackIter
self.name = name
self.activeConstraintTolerance = activeConstraintTolerance
self.descentVsFeasibilityWeight = descentVsFeasibilityWeight
self.silent = silent
self.analyticDerivatives = analyticDerivatives
self.nreset = nreset
def run(self):
"""
Solve problem using donlp2.
"""
global globalDonlpProblem
globalDonlpProblem = self
donlp2()
def _user_init_size(self):
"""
Set global variables related to problem size and maximum number
of iterations.
"""
global n, nlin, nonlin, iterma, nstep
n = self.n
nlin = 0
nonlin = self.nonlin
iterma = self.maxIter
nstep = self.maxBacktrackIter
def _user_init(self):
"""
Initialize various problem data unrelated to sizes. This includes
the problem name, initial point, tolerances, bound constraints,
and whether analytic gradients are given.
"""
global name, x, del0, tau0, tau, analyt, epsdif, nreset
global silent, low, up, bloc
strcpy(name, self.name)
for i, xi in enumerate(self.x0):
x[i+1] = xi
for i, lowi in enumerate(self.low):
low[i+1] = lowi
for i, upi in enumerate(self.up):
up[i+1] = upi
bloc = <int> self.bloc
del0 = self.activeConstraintTolerance
tau0 = 0.5e0
tau = self.descentVsFeasibilityWeight
analyt = <int>self.analyticDerivatives
epsdif = 0.e0
nreset = self.nreset
silent = <int>self.silent
cdef public void user_init_size():
"""
Called by donlp, delegate to problem object.
"""
globalDonlpProblem._user_init_size()
cdef public void user_init(void):
"""
Called by donlp, delegate to problem object.
"""
globalDonlpProblem._user_init()
cdef public void ef(double *x, double *fx):
"""
Called by donlp, delegate to problem object.
"""
global icf
icf += 1
cdef int xSize = globalDonlpProblem.n+1
cdef view.array xarr = <double[:xSize]> x
fx[0] = globalDonlpProblem.ef(xarr[1:])
cdef public void egradf(double *x, double *gradf):
"""
Called by donlp, delegate to problem object.
"""
global icgf
icgf += 1
cdef int xSize = globalDonlpProblem.n+1
cdef view.array xarr = <double[:xSize]> x
cdef view.array gradArr = <double [:xSize]> gradf
globalDonlpProblem.egradf(xarr[1:], gradArr[1:])
cdef public void eval_extern(int mode):
"""
Called by donlp, delegate to problem object.
"""
global icf, icgf
global fu, fugrad
cdef int xSize = globalDonlpProblem.n+1
cdef view.array xarr = <double[:xSize]> xtr
if mode == 1:
icf += 1
fu[0] = globalDonlpProblem.eval_extern(mode, xarr[1:])
elif mode == 2:
icf += 1
icgf += 1
tmp1, tmp2 = globalDonlpProblem.eval_extern(mode, xarr[1:])
fu[0] = tmp1
for i in range(tmp2.size):
fugrad[i+1][0] = tmp2[i]
cdef public void econ(int type, int *liste, double *x,
double *con, int *err):
pass
cdef public void econgrad(int *liste, int shift,
double *x, double **grad):
pass
cdef public void newx(double *x, double *u, int itstep,
double **accinf, int *cont):
cont[0] = 1
cdef public void setup(void):
pass
cdef public void solchk(void):
pass
The wrapper code works for some simple toy cases, like the one below:
import cydon
import numpy as np
def main():
def ef(x):
return 100*(x[1]-x[0]**2)**2 + (x[0]-1)**2
def egradf(x, g):
g[0] = 200*(x[0]**2-x[1])*x[0] + 2*(x[0]-1)
g[1] = 200*(x[1] - x[0]**2)
x0 = np.array([15,-15])
n = x0.size
low = -1.0e10 * np.ones(n)
up = 1.0e10 * np.ones(n)
def eval_extern(mode, x):
fx = 100*(x[1]-x[0]**2)**2 + (x[0]-1)**2
if mode == 1:
return fx
elif mode == 2:
gradfx = np.ones(2)
gradfx[0] = 200*(x[0]**2-x[1])*x[0] + 2*(x[0]-1)
gradfx[1] = 200*(x[1] - x[0]**2)
return fx, gradfx
problem = cydon.DonlpProblem(
x0=x0,
ef=None,
egradf=None,
bloc=True,
eval_extern=eval_extern,
activeConstraintTolerance=1.00e-1,
low=low,
up=up,
nonlin=0,
silent=False,
name="blabloc"
)
problem.run()
if __name__ == "__main__":
main()
The problem I actually want to solve involves more setup, using array operations with numpy and cvxopt. When I create it the code promptly segfaults. Stepping through in gdb and using valgrind only reveals that a line in the optimization library that looks like:
foo = malloc_wrapper(size);
terminates with the following error from valgrind:
==31631== Process terminating with default action of signal 11 (SIGSEGV)
==31631== Bad permissions for mapped region at address 0x8BFF930
==31631== at 0x17984DBC: global_mem_malloc (donlp2.c:8690)
==31631== by 0x17985FA1: donlp2 (donlp2.c:204)
==31631== by 0x179504D2: __pyx_pw_5cydon_12DonlpProblem_3run (cydon.c:2215)
==31631== by 0x4E78BD7: PyObject_Call (abstract.c:2529)
==31631== by 0x4F1BFA1: PyEval_EvalFrameEx (ceval.c:4239)
==31631== by 0x4F22C08: PyEval_EvalCodeEx (ceval.c:3253)
==31631== by 0x4F209B4: PyEval_EvalFrameEx (ceval.c:4117)
==31631== by 0x4F21E47: PyEval_EvalFrameEx (ceval.c:4107)
==31631== by 0x4F22C08: PyEval_EvalCodeEx (ceval.c:3253)
==31631== by 0x4F22C81: PyEval_EvalCode (ceval.c:667)
==31631== by 0x4F46350: PyRun_FileExFlags (pythonrun.c:1370)
==31631== by 0x4F465F6: PyRun_SimpleFileExFlags (pythonrun.c:948)
The segfault happens before the C library has done any real work. It's simply initializing variables. Line 8690 is
foo = malloc_wrapper(sizeOfMalloc);
and line 204 is simply the call
global_mem_malloc();
In an included header file foo is defined to be double*. Note that the memory allocation inside malloc_wrapper succeeded and the function successfully returned. It's the write to foo that is failing.
Any suggestions how to narrow down what is causing this, or how to fix it?