[ 
https://issues.apache.org/jira/browse/MATH-1134?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14049576#comment-14049576
 ] 

Derek Scherger commented on MATH-1134:
--------------------------------------

I'm all for callers having the option to not initialize for partial derivatives 
on construction, as long as there is some explicit and obvious way to ensure 
that everything has been initialized before unleashing a bunch of threads on 
the interpolator.

At the moment, it's not obvious that there is any internal state that requires 
initialization before using the class, the only way to find out is to 
experience the problem of incomplete initialization, and then to look at the 
source to see what is going on and discover that there is lazy initialization 
being done in both the partialDerivative[XY...] and value methods. The solution 
I've landed on for now is to make an initial partialDerivativeX(x,y) call as 
the objects are constructed, but some sort of explicit init() or alternative 
constructor with eager/lazy init options would be better.

I'm not particularly fond of the idea of declaring methods synchronized unless 
that's really what is required to make them safe. The overhead there may be 
small, but that's relative and when running on many threads doing lots of 
computation it adds up, and sometimes becomes a significant problem, so if it 
can be avoided all the better. Similarly, recalculating the partials on every 
call is not very good if they can be computed once and re-used for every 
subsequent call, which seems like it could be a major performance win.



> unsafe initialization in BicubicSplineInterpolatingFunction
> -----------------------------------------------------------
>
>                 Key: MATH-1134
>                 URL: https://issues.apache.org/jira/browse/MATH-1134
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 3.3
>            Reporter: Derek Scherger
>            Priority: Minor
>
> The lazy initialization of the internal array of partialDerivatives in 
> BicubicSplineInterpolatingFunction is not thread safe. If multiple threads 
> call any of the partialDerivative functions concurrently one thread may start 
> the initialization and others will see the array is non-null and assume it is 
> fully initialized. If the internal array of partial derivatives was 
> initialized in the constructor this would not be a problem.
> i.e. the following check in partialDerivative(which, x, y)
>         if (partialDerivatives == null) {
>             computePartialDerivatives();
>         }
> will start the initialization. However in computePartialDerivatives()
>         partialDerivatives = new BivariateFunction[5][lastI][lastJ];
> makes it appear to other threads as the the initialization has completed when 
> it may not have.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to