Skip to content

Commit 45d649e

Browse files
authored
Merge pull request #291 from richardotis/master
LambdaDouble/LLVMDouble: Add cdef nogil function wrappers for fast calling
2 parents d21ec88 + bb5b40c commit 45d649e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

symengine/lib/symengine_wrapper.pxd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ cdef class _Lambdify(object):
3939

4040
cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse)
4141
cdef _load(self, const string &s)
42+
cdef void unsafe_real_ptr(self, double *inp, double *out) nogil
4243
cpdef unsafe_real(self,
4344
double[::1] inp, double[::1] out,
4445
int inp_offset=*, int out_offset=*)
46+
cdef void unsafe_complex_ptr(self, double complex *inp, double complex *out) nogil
4547
cpdef unsafe_complex(self, double complex[::1] inp, double complex[::1] out,
4648
int inp_offset=*, int out_offset=*)
4749
cpdef eval_real(self, inp, out)
@@ -51,7 +53,9 @@ cdef class LambdaDouble(_Lambdify):
5153
cdef vector[symengine.LambdaRealDoubleVisitor] lambda_double
5254
cdef vector[symengine.LambdaComplexDoubleVisitor] lambda_double_complex
5355
cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse)
56+
cdef void unsafe_real_ptr(self, double *inp, double *out) nogil
5457
cpdef unsafe_real(self, double[::1] inp, double[::1] out, int inp_offset=*, int out_offset=*)
58+
cdef void unsafe_complex_ptr(self, double complex *inp, double complex *out) nogil
5559
cpdef unsafe_complex(self, double complex[::1] inp, double complex[::1] out, int inp_offset=*, int out_offset=*)
5660
cpdef as_scipy_low_level_callable(self)
5761

@@ -60,5 +64,6 @@ IF HAVE_SYMENGINE_LLVM:
6064
cdef vector[symengine.LLVMDoubleVisitor] lambda_double
6165
cdef _init(self, symengine.vec_basic& args_, symengine.vec_basic& outs_, cppbool cse)
6266
cdef _load(self, const string &s)
67+
cdef void unsafe_real_ptr(self, double *inp, double *out) nogil
6368
cpdef unsafe_real(self, double[::1] inp, double[::1] out, int inp_offset=*, int out_offset=*)
6469
cpdef as_scipy_low_level_callable(self)

symengine/lib/symengine_wrapper.pyx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4507,11 +4507,19 @@ cdef class _Lambdify(object):
45074507
cdef _load(self, const string &s):
45084508
raise ValueError("Not supported")
45094509

4510+
cdef void unsafe_real_ptr(self, double *inp, double *out) nogil:
4511+
with gil:
4512+
raise ValueError("Not supported")
4513+
45104514
cpdef unsafe_real(self,
45114515
double[::1] inp, double[::1] out,
45124516
int inp_offset=0, int out_offset=0):
45134517
raise ValueError("Not supported")
45144518

4519+
cdef void unsafe_complex_ptr(self, double complex *inp, double complex *out) nogil:
4520+
with gil:
4521+
raise ValueError("Not supported")
4522+
45154523
cpdef unsafe_complex(self, double complex[::1] inp, double complex[::1] out,
45164524
int inp_offset=0, int out_offset=0):
45174525
raise ValueError("Not supported")
@@ -4690,11 +4698,17 @@ cdef class LambdaDouble(_Lambdify):
46904698
self.lambda_double_complex.resize(1)
46914699
self.lambda_double_complex[0].init(args_, outs_, cse)
46924700

4701+
cdef void unsafe_real_ptr(self, double *inp, double *out) nogil:
4702+
self.lambda_double[0].call(out, inp)
4703+
46934704
cpdef unsafe_real(self, double[::1] inp, double[::1] out, int inp_offset=0, int out_offset=0):
4694-
self.lambda_double[0].call(&out[out_offset], &inp[inp_offset])
4705+
self.unsafe_real_ptr(&inp[inp_offset], &out[out_offset])
4706+
4707+
cdef void unsafe_complex_ptr(self, double complex *inp, double complex *out) nogil:
4708+
self.lambda_double_complex[0].call(out, inp)
46954709

46964710
cpdef unsafe_complex(self, double complex[::1] inp, double complex[::1] out, int inp_offset=0, int out_offset=0):
4697-
self.lambda_double_complex[0].call(&out[out_offset], &inp[inp_offset])
4711+
self.unsafe_complex_ptr(&inp[inp_offset], &out[out_offset])
46984712

46994713
cpdef as_scipy_low_level_callable(self):
47004714
from ctypes import c_double, c_void_p, c_int, cast, POINTER, CFUNCTYPE
@@ -4726,8 +4740,11 @@ IF HAVE_SYMENGINE_LLVM:
47264740
return llvm_loading_func, (self.args_size, self.tot_out_size, self.out_shapes, self.real, \
47274741
self.n_exprs, self.order, self.accum_out_sizes, self.numpy_dtype, s)
47284742

4743+
cdef void unsafe_real_ptr(self, double *inp, double *out) nogil:
4744+
self.lambda_double[0].call(out, inp)
4745+
47294746
cpdef unsafe_real(self, double[::1] inp, double[::1] out, int inp_offset=0, int out_offset=0):
4730-
self.lambda_double[0].call(&out[out_offset], &inp[inp_offset])
4747+
self.unsafe_real_ptr(&inp[inp_offset], &out[out_offset])
47314748

47324749
cpdef as_scipy_low_level_callable(self):
47334750
from ctypes import c_double, c_void_p, c_int, cast, POINTER, CFUNCTYPE

0 commit comments

Comments
 (0)