Enabled parallel evaluation of gradients for each value function
@@ -7,6 +7,11 @@ package net.finmath.optimizer;
import java.util.Map;
import java.util.concurrent.ExecutorService;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import org.jblas.util.Functions;
import net.finmath.montecarlo.automaticdifferentiation.RandomVariableDifferentiableInterface;
import net.finmath.stochastic.RandomVariableInterface;
@@ -139,10 +144,12 @@ public abstract class StochasticLevenbergMarquardtAD extends StochasticLevenberg
isRandomVariableDifferentiable = (values[valueIndex] instanceof RandomVariableDifferentiableInterface) && isRandomVariableDifferentiable;
}
-
if(isRandomVariableDifferentiable) {
+ // Parallel evaluation of gradients for each value function
+ Map<Integer,Map<Long, RandomVariableInterface>> gradients = IntStream.range(0, values.length).parallel().boxed().collect(Collectors.toConcurrentMap(Function.identity(), valueIndex -> ((RandomVariableDifferentiableInterface)values[valueIndex]).getGradient()));
+
for (int valueIndex = 0; valueIndex < values.length; valueIndex++) {
- Map<Long, RandomVariableInterface> gradient = ((RandomVariableDifferentiableInterface)values[valueIndex]).getGradient();
+ Map<Long, RandomVariableInterface> gradient = gradients.get(valueIndex);
for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
derivatives[parameterIndex][valueIndex] = gradient.get(((RandomVariableDifferentiableInterface)parameters[parameterIndex]).getID());
if(derivatives[parameterIndex][valueIndex] != null) {
