Skip to content

Commit

Permalink
vector lambda_double -> unique_ptr lambda_visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed May 31, 2024
1 parent 7472cce commit a1872dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
9 changes: 5 additions & 4 deletions symengine/lib/symengine_wrapper.in.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

cimport symengine
from symengine cimport RCP, map_basic_basic, rcp_const_basic
from libcpp.memory cimport unique_ptr
from libcpp.vector cimport vector
from libcpp.string cimport string
from libcpp cimport bool as cppbool
Expand Down Expand Up @@ -54,7 +55,7 @@ cdef class LambdaDouble(_Lambdify):
int inp_offset=*, int out_offset=*)

cdef class LambdaComplexDouble(_Lambdify):
cdef vector[symengine.LambdaComplexDoubleVisitor] lambda_double
cdef unique_ptr[symengine.LambdaComplexDoubleVisitor] lambda_visitor
cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse)
cpdef unsafe_complex(self, double complex[::1] inp, double complex[::1] out, int inp_offset=*, int out_offset=*)

Expand All @@ -63,22 +64,22 @@ IF HAVE_SYMENGINE_LLVM:
cdef int opt_level

cdef class LLVMDouble(_LLVMLambdify):
cdef vector[symengine.LLVMDoubleVisitor] lambda_double
cdef unique_ptr[symengine.LLVMDoubleVisitor] lambda_visitor
cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse)
cdef _load(self, const string &s)
cpdef unsafe_real(self, double[::1] inp, double[::1] out, int inp_offset=*, int out_offset=*)
cpdef as_scipy_low_level_callable(self)
cpdef as_ctypes(self)

cdef class LLVMFloat(_LLVMLambdify):
cdef vector[symengine.LLVMFloatVisitor] lambda_double
cdef unique_ptr[symengine.LLVMFloatVisitor] lambda_visitor
cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse)
cdef _load(self, const string &s)
cpdef unsafe_real(self, float[::1] inp, float[::1] out, int inp_offset=*, int out_offset=*)

IF HAVE_SYMENGINE_LLVM_LONG_DOUBLE:
cdef class LLVMLongDouble(_LLVMLambdify):
cdef vector[symengine.LLVMLongDoubleVisitor] lambda_double
cdef unique_ptr[symengine.LLVMLongDoubleVisitor] lambda_visitor
cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse)
cdef _load(self, const string &s)
cpdef unsafe_real(self, long double[::1] inp, long double[::1] out, int inp_offset=*, int out_offset=*)
66 changes: 33 additions & 33 deletions symengine/lib/symengine_wrapper.in.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5156,27 +5156,27 @@ cdef class LambdaDouble(_Lambdify):
pass

cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse):
self.lambda_double.resize(1)
self.lambda_double[0].init(args_, outs_, cse)
self.lambda_visitor.reset(new symengine.LLVMDoubleVisitor())
deref(self.lambda_visitor).init(args_, outs_, cse)

cpdef unsafe_real(self, double[::1] inp, double[::1] out, int inp_offset=0, int out_offset=0):
self.lambda_double[0].call(&out[out_offset], &inp[inp_offset])
deref(self.lambda_visitor).call(&out[out_offset], &inp[inp_offset])

cpdef unsafe_eval(self, inp, out, unsigned nbroadcast=1):
cdef double[::1] c_inp, c_out
cdef unsigned idx
c_inp = np.ascontiguousarray(inp.ravel(order=self.order), dtype=self.numpy_dtype)
c_out = out
for idx in range(nbroadcast):
self.lambda_double[0].call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])
deref(self.lambda_visitor).call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])

cpdef as_scipy_low_level_callable(self):
from ctypes import c_double, c_void_p, c_int, cast, POINTER, CFUNCTYPE
if self.tot_out_size > 1:
raise RuntimeError("SciPy LowLevelCallable supports only functions with 1 output")
addr1 = cast(<size_t>&_scipy_callback_lambda_real,
CFUNCTYPE(c_double, c_int, POINTER(c_double), c_void_p))
addr2 = cast(<size_t>&self.lambda_double[0], c_void_p)
addr2 = cast(<size_t>self.lambda_visitor.get(), c_void_p)
return create_low_level_callable(self, addr1, addr2)

cpdef as_ctypes(self):
Expand All @@ -5191,7 +5191,7 @@ cdef class LambdaDouble(_Lambdify):
from ctypes import c_double, c_void_p, c_int, cast, POINTER, CFUNCTYPE
addr1 = cast(<size_t>&_ctypes_callback_lambda_real,
CFUNCTYPE(c_void_p, POINTER(c_double), POINTER(c_double), c_void_p))
addr2 = cast(<size_t>&self.lambda_double[0], c_void_p)
addr2 = cast(<size_t>self.lambda_visitor.get(), c_void_p)
return addr1, addr2


Expand All @@ -5201,19 +5201,19 @@ cdef class LambdaComplexDouble(_Lambdify):
pass

cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse):
self.lambda_double.resize(1)
self.lambda_double[0].init(args_, outs_, cse)
self.lambda_visitor.reset(new symengine.LambdaComplexDoubleVisitor())
deref(self.lambda_visitor).init(args_, outs_, cse)

cpdef unsafe_complex(self, double complex[::1] inp, double complex[::1] out, int inp_offset=0, int out_offset=0):
self.lambda_double[0].call(&out[out_offset], &inp[inp_offset])
deref(self.lambda_visitor).call(&out[out_offset], &inp[inp_offset])

cpdef unsafe_eval(self, inp, out, unsigned nbroadcast=1):
cdef double complex[::1] c_inp, c_out
cdef unsigned idx
c_inp = np.ascontiguousarray(inp.ravel(order=self.order), dtype=self.numpy_dtype)
c_out = out
for idx in range(nbroadcast):
self.lambda_double[0].call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])
deref(self.lambda_visitor).call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])


IF HAVE_SYMENGINE_LLVM:
Expand All @@ -5222,31 +5222,31 @@ IF HAVE_SYMENGINE_LLVM:
self.opt_level = opt_level

cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse):
self.lambda_double.resize(1)
self.lambda_double[0].init(args_, outs_, cse, self.opt_level)
self.lambda_visitor.reset(new symengine.LLVMDoubleVisitor())
deref(self.lambda_visitor).init(args_, outs_, cse, self.opt_level)

cdef _load(self, const string &s):
self.lambda_double.resize(1)
self.lambda_double[0].loads(s)
self.lambda_visitor.reset(new symengine.LLVMDoubleVisitor())
deref(self.lambda_visitor).loads(s)

def __reduce__(self):
"""
Interface for pickle. Note that the resulting object is platform dependent.
"""
cdef bytes s = self.lambda_double[0].dumps()
cdef bytes s = deref(self.lambda_visitor).dumps()
return llvm_loading_func, (self.args_size, self.tot_out_size, self.out_shapes, self.real, \
self.n_exprs, self.order, self.accum_out_sizes, self.numpy_dtype, s)

cpdef unsafe_real(self, double[::1] inp, double[::1] out, int inp_offset=0, int out_offset=0):
self.lambda_double[0].call(&out[out_offset], &inp[inp_offset])
deref(self.lambda_visitor).call(&out[out_offset], &inp[inp_offset])

cpdef unsafe_eval(self, inp, out, unsigned nbroadcast=1):
cdef double[::1] c_inp, c_out
cdef unsigned idx
c_inp = np.ascontiguousarray(inp.ravel(order=self.order), dtype=self.numpy_dtype)
c_out = out
for idx in range(nbroadcast):
self.lambda_double[0].call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])
deref(self.lambda_visitor).call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])

cpdef as_scipy_low_level_callable(self):
from ctypes import c_double, c_void_p, c_int, cast, POINTER, CFUNCTYPE
Expand All @@ -5256,7 +5256,7 @@ IF HAVE_SYMENGINE_LLVM:
raise RuntimeError("SciPy LowLevelCallable supports only functions with 1 output")
addr1 = cast(<size_t>&_scipy_callback_llvm_real,
CFUNCTYPE(c_double, c_int, POINTER(c_double), c_void_p))
addr2 = cast(<size_t>&self.lambda_double[0], c_void_p)
addr2 = cast(<size_t>self.lambda_visitor.get(), c_void_p)
return create_low_level_callable(self, addr1, addr2)

cpdef as_ctypes(self):
Expand All @@ -5273,71 +5273,71 @@ IF HAVE_SYMENGINE_LLVM:
raise RuntimeError("Lambda function has to be real")
addr1 = cast(<size_t>&_ctypes_callback_llvm_real,
CFUNCTYPE(c_void_p, POINTER(c_double), POINTER(c_double), c_void_p))
addr2 = cast(<size_t>&self.lambda_double[0], c_void_p)
addr2 = cast(<size_t>self.lambda_visitor.get(), c_void_p)
return addr1, addr2

cdef class LLVMFloat(_LLVMLambdify):
def __cinit__(self, args, *exprs, cppbool real=True, order='C', cppbool cse=False, cppbool _load=False, opt_level=3, dtype=None):
self.opt_level = opt_level

cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse):
self.lambda_double.resize(1)
self.lambda_double[0].init(args_, outs_, cse, self.opt_level)
self.lambda_visitor.reset(new symengine.LLVMFloatVisitor())
deref(self.lambda_visitor).init(args_, outs_, cse, self.opt_level)

cdef _load(self, const string &s):
self.lambda_double.resize(1)
self.lambda_double[0].loads(s)
self.lambda_visitor.reset(new symengine.LLVMFloatVisitor())
deref(self.lambda_visitor).loads(s)

def __reduce__(self):
"""
Interface for pickle. Note that the resulting object is platform dependent.
"""
cdef bytes s = self.lambda_double[0].dumps()
cdef bytes s = deref(self.lambda_visitor).dumps()
return llvm_float_loading_func, (self.args_size, self.tot_out_size, self.out_shapes, self.real, \
self.n_exprs, self.order, self.accum_out_sizes, self.numpy_dtype, s)

cpdef unsafe_real(self, float[::1] inp, float[::1] out, int inp_offset=0, int out_offset=0):
self.lambda_double[0].call(&out[out_offset], &inp[inp_offset])
deref(self.lambda_visitor).call(&out[out_offset], &inp[inp_offset])

cpdef unsafe_eval(self, inp, out, unsigned nbroadcast=1):
cdef float[::1] c_inp, c_out
cdef unsigned idx
c_inp = np.ascontiguousarray(inp.ravel(order=self.order), dtype=self.numpy_dtype)
c_out = out
for idx in range(nbroadcast):
self.lambda_double[0].call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])
deref(self.lambda_visitor).call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])

IF HAVE_SYMENGINE_LLVM_LONG_DOUBLE:
cdef class LLVMLongDouble(_LLVMLambdify):
def __cinit__(self, args, *exprs, cppbool real=True, order='C', cppbool cse=False, cppbool _load=False, opt_level=3, dtype=None):
self.opt_level = opt_level

cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse):
self.lambda_double.resize(1)
self.lambda_double[0].init(args_, outs_, cse, self.opt_level)
self.lambda_visitor.reset(new symengine.LLVMLongDoubleVisitor())
deref(self.lambda_visitor).init(args_, outs_, cse, self.opt_level)

cdef _load(self, const string &s):
self.lambda_double.resize(1)
self.lambda_double[0].loads(s)
self.lambda_visitor.reset(new symengine.LLVMLongDoubleVisitor())
deref(self.lambda_visitor).loads(s)

def __reduce__(self):
"""
Interface for pickle. Note that the resulting object is platform dependent.
"""
cdef bytes s = self.lambda_double[0].dumps()
cdef bytes s = deref(self.lambda_visitor).dumps()
return llvm_long_double_loading_func, (self.args_size, self.tot_out_size, self.out_shapes, self.real, \
self.n_exprs, self.order, self.accum_out_sizes, self.numpy_dtype, s)

cpdef unsafe_real(self, long double[::1] inp, long double[::1] out, int inp_offset=0, int out_offset=0):
self.lambda_double[0].call(&out[out_offset], &inp[inp_offset])
deref(self.lambda_visitor).call(&out[out_offset], &inp[inp_offset])

cpdef unsafe_eval(self, inp, out, unsigned nbroadcast=1):
cdef long double[::1] c_inp, c_out
cdef unsigned idx
c_inp = np.ascontiguousarray(inp.ravel(order=self.order), dtype=self.numpy_dtype)
c_out = out
for idx in range(nbroadcast):
self.lambda_double[0].call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])
deref(self.lambda_visitor).call(&c_out[idx*self.tot_out_size], &c_inp[idx*self.args_size])

def llvm_loading_func(*args):
return LLVMDouble(args, _load=True)
Expand Down

0 comments on commit a1872dd

Please sign in to comment.