<div dir="ltr"><div>Thanks for your email and patch. I had noticed that integration-qag appeared to be non-reentrant a couple years ago, and made a little progress into diagnosing the problem, but didn't get as far as you have. I think your solution is a good start. As far as callbacks defined through defmobject, it seems like this could be handled by let binding in the defmcallback expansion, since they are only ever called in the CBFN functions. What do you think?<br><br></div><div>As a side point, it looks like you're trying to do a multivariate integral. I think you will find the Monte Carlo integration better than nested calling of univariate integrations for this purpose.<br><br>Liam<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 9, 2015 at 1:37 PM, Eugene Zhemchugov <span dir="ltr"><<a href="mailto:jini.zh@gmail.com" target="_blank">jini.zh@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I was trying to evaluate a recurring integral when I ran into what I<br>
think a design flaw in the callbacks implementation. Consider the<br>
following code:<br>
<br>
(integration-qag (lambda (x)<br>
(integration-qag (lambda(y)<br>
(* (sin x) y))<br>
0d0 1d0 :gauss41))<br>
0d0 pi :gauss41)<br>
<br>
The result of the integration should be 1, but I get 4.93. Digging<br>
through the code, I figured out that callbacks are implemented in the<br>
following way:<br>
<br>
1. During the GSLL compilation, a callback is defined via the<br>
CFFI:DEFCALLBACK macro.<br>
2. The GSLL wrapper function assigns the function to be called to a<br>
dynamic variable and calls the underlying GSL function.<br>
3. The GSL function calls the CFFI callback.<br>
4. The CFFI callback calls the value stored in the dynamic variable.<br>
<br>
The relevant part of the GSLL wrapper function is generated by<br>
BODY-EXPAND. Here is a simplified version of the code generated for<br>
INTEGRATION-QAG:<br>
<br>
(defun integration-qag (function ...)<br>
(declare (special integration-qag-dynfn0))<br>
(setf integration-qag-dynfn0 function)<br>
(foreign-funcall "gsl_integration_qag" ...))<br>
<br>
The problem here is that if the function passed as a parameter to the<br>
GSLL function calls the same GSLL function again, as in the example<br>
above, the dynamic variable value is overwritten during the second call,<br>
and then the first GSL function ends up calling the second callback all<br>
the time. Seemingly, this can be fixed by binding instead of assigning<br>
the dynamic variable, i.e., by making BODY-EXPAND to generate code like<br>
this:<br>
<br>
(defun integration-qag (function ...)<br>
(let ((integration-qag-dynfn0 function))<br>
(declare (special integration-qag-dynfn0))<br>
(foreign-funcall "gsl_integration_qag" ...)))<br>
<br>
In this case the value of INTEGRATION-QAG-DYNFN0 is restored upon<br>
leaving the LET form. However, having patched the code this way, I broke<br>
callbacks introduced via the DEFMOBJECT macro. In this case the<br>
assignment happens in the REINITIALIZE-INSTANCE method specialized on<br>
this object, which is usually called in the construction phase. As an<br>
example, consider NONLINEAR-LEAST-SQUARES-<u></u>EXAMPLE. The assignment is<br>
made during the call to MAKE-NONLINEAR-FDFFIT, however the callback is<br>
executed during the call to ITERATE. I think that the binding should be<br>
performed in ITERATE, as soon as it is needed. ITERATE receives an<br>
instance of an object subclassed from CALLBACK-INCLUDED, hence it has<br>
access both to the functions to be called (the FUNCALLABLES slot of<br>
CALLBACK-INCLUDED) and the callback specification (the CBINFO slot). But<br>
I don't know what would be the best way to implement it. One possibility<br>
is to loop through function parameters in BODY-EXPAND and figure out<br>
which are instances of CALLBACK-INCLUDED. That will work only for<br>
methods, and only for the specialized parameters. Is it sufficient?<br>
Other approaches I can think of would require changing the DEFMFUN<br>
interface to specify which parameters should be used to construct the<br>
bindings. What solution would you propose? I can make a patch for it if<br>
you don't think that the changes are too drastic to trust not an<br>
upstream developer.<br>
<br>
You may find my partial patch in the attachement.<br>
<br>_______________________________________________<br>
Gsll-devel mailing list<br>
<a href="mailto:Gsll-devel@common-lisp.net">Gsll-devel@common-lisp.net</a><br>
<a href="http://mailman.common-lisp.net/cgi-bin/mailman/listinfo/gsll-devel" target="_blank">http://mailman.common-lisp.net/cgi-bin/mailman/listinfo/gsll-devel</a><br>
<br></blockquote></div><br></div></div>