Open IgorTo opened 4 years ago
For some reason matlab's kronSparse seems not to take into account the fact that the matrix will be sparse when computing the memory footprint of the resulting matrix. One option would be to rewrite the kronSparse function or a specialized function to compute
kron(a,speye(n))*b
however in your case it seems that simply adding parenteses to change the multiplication evaluation ordering solves the problem. You can use
F = @(x) diag(x.^2)*(A*x);
or with a fix from the last commit you can avoid creating the matrix diag(x.^2)
and use broadcasting (see here):
F = @(x) ((x.^2) .* A) * x;
F = @(x) (x.^2) .* (A * x);
The second line is faster to evaluate
Thank you so much! It is now very fast and does not return a memory error. Can't wait to try it in a Newton iteration for solving nonlinear elasticity PDEs.
Hi, I'm running into a similar memory error while trying to solve a system of PDEs. The problem comes from the multiplication of NxN sparse matrices of the form: A spdiags(v, 0, N, N) B
The call to spdiags() seems to be the issue. I replaced it with multiplications as you did above, but the performance is much worse. Any suggestions? Aside from the memory error with large simulations, I'm seeing good autodiff performance.
@d-cogswell , it would be great if you could provide me with some matrices A,B and vector v to replicate the problem. Did you try A(v.B)?
@martinResearch sure, here's an example. I tried the alternate multiplication you suggested but it's really slow. Is it performing a dense multiply?
N=10000;
A=spdiags([-ones(N,1),ones(N,1)],0:1,N,N);
u=rand(N,1);
%This is fast, but fails with a memory error for large N
%N=20000 fails for me
f1 = @(x) A*spdiags(x,0,N,N)*A*x;
tic
J1=AutoDiffJacobianAutoDiff(f1, u);
toc
%This works for large N but it's really slow
f2 = @(x) A*(x.*A)*x;
tic
J2=AutoDiffJacobianAutoDiff(f2, u);
toc
Using a different ordering of the multiplications, it runs fast for me:
f3 = @(x) A*(x.*(A*x));
tic
J3=AutoDiffJacobianAutoDiff(f3, u);
toc
it runs in 0.16 sec on my machine for N=20000.
Thanks for the tip! The order of operations really makes a big difference.
The following code:
does not work. Error message is:
It would be really good if this tool worked with objective functions which include manipulations with large sparse matrices.