Hi all,
I am using ForwardDiff to calculate a jacobian in a physical problem. Everything works and I am happy with the results. Now I would like to optimaze it but I am failing in doing so.
As a start, I have a very large wave function that I would like to derive once and then store the results. I perform the derivation as follows
jacobian(x->F(f,x...), p)
where F is my large function, f is a vector of frequencies and p are the parameters I am interested in deriving.
This function needs to be used by another function that projects it onto a detector (actually many) so I replaced the previous step with
jacobian(x->G_1(f,x...), p)
...
jacobian(x->G_n(f,x...), p)
where each G contains F.
There is an obvious bottleneck so i tried to compute just once the derivative of F and then assemble a jacobian using
F_value = F(f,p)
F_jacobian = jacobian(x->F(f,x...), p)
function G_1(f,p, F_value, F_jacobian)
G = Array{ForwardDiff.Dual}(undef, length(f))
for i in 1:length(f)
@views G[i] = ForwardDiff.Dual(F_value[i], F_jacobian[i,:]...)
end
# I corrected the tag in the Dual but removed here to simplify the description
# more stuff, function of F
end
This is just a sketch of what happens in the real code.
The main issue is that the step of filling the vector G is very slow.
In the following pic I wrote a function working with Floats and with ForwardDiff.Dual that reproduce the thing I would like to do. The function working with Duals is 60x times slower. Any advices?
Thanks