How do I stop the code and get the results?
Show older comments
I am trying to run the Matlab code for Arnoldi method. The code dose not stop because the matrix is too large,. Is there a way to get the output?
[V,H] = Arnoldi();
function [V,H]=Arnoldi(A,v1,m)
% Load the data
load('soc-Epinions1.mat');
A = Problem.A;
n=size(A,1);
m=9000;
V=zeros(n,m);
H=zeros(m,m);
v1=rand(n,1);
V(:,1)=v1/norm(v1);
for j=1:m % the Anoldi loop
z=A*V(:,j);
for i=1:j
H(i,j)=V(:,i)'*z;
z=z-H(i,j)*V(:,i);
end
H(j+1,j)=norm(z);
if H(j+1,j)==0, break, end
V(:,j+1)=z/H(j+1,j);
end
H=H(1:m,1:m);
V=V(1:n,1:m);
f=diag(V*(expm(H)-eye(m))*V')
end
6 Comments
Your calculation does
9000 * (9000+1)/2
iterations. That takes time.
Which matrix is too large?
Walter Roberson
on 5 Mar 2022
You have a double-nested loop. Your i goes 1 to j, where j is going 1:9000. That requires 1+2+3+...9000 iterations = 40504500
On my system, it takes a notable amount of time to execute, but at the end
Name Size Bytes Class Attributes
H 9000x9000 648000000 double
V 75888x9000 5463936000 double
Walter Roberson
on 5 Mar 2022
The size of the matrix A is 75888 * 75888.
Yes, but it is sparse.
Your H and V matrices would not be sparse.
You could potentially use techniques such as "tall arrays" to break up the calculation into smaller steps, but if your H + V matrices are too large for your system (such as if you only have about 8 gigabytes), then saving memory during calculations is not the problem: you would still need the roughly 6 gigabytes of memory to hold H and V no matter how you did the calculation. If your system does not have the memory available, then the problem is too large for your system.
Omar B.
on 5 Mar 2022
Walter Roberson
on 5 Mar 2022
Perhaps they just ran the code on a larger system. The code ran on my system. Not quickly, but it ran.
Answers (1)
Steven Lord
on 5 Mar 2022
1 vote
If you're out of time and need to get the results that have been computed, under certain circumstances (the main ones being the MATLAB Editor is open and you're using a sufficiently new release, plus the code has to give MATLAB a chance to recognize and process events) press the Pause button on the Editor tab of the toolstrip. save or assignin (into the base workspace) the results that you want to access and then either quit debugging (if you want to terminate the execution of the function) or continue (if you want to let it continue.)
Note that if you do this and quit debugging there's no way to "let MATLAB restart from where it was" unless your function was specifically designed to accept variables representing the state of the function's execution.
6 Comments
Walter Roberson
on 5 Mar 2022
If you are executing and you have the editor open to a function that is called by the code, you can use the editor interface to turn on a breakpoint in the function. The next time the line is reached, MATLAB will stop at the debugger and you can then edit other functions or add a "dbstop if error" or whatever is appropriate, or use dbup to get to the level where you have copies of the variables you want to save()
If you are executing and you have the editor open at all, you might be able to use the menu to open the function that is executing, so that you can inject a breakpoint into it. In my experience this does not always work: sometimes it holds off on opening the file until the next time it can process events.
Omar B.
on 6 Mar 2022
Walter Roberson
on 6 Mar 2022
f=diag(V*(expm(H)-eye(m))*V')
You do not return f from the function. You display the results of the diag, but that will be 75888 elements long and will just scroll off the display. So not a lot of point in calculating f there.
I suggest that you comment out that line, and run your code and save the V and H that result. And then I suggest that you write a new function to implement the f calculation.
V is 75888 by 9000.
H is 9000 by 9000. expm(H) is 9000 by 9000. eye(m) is eye(9000) is 9000 by 9000. So expm(H)-eye(m) is 9000 by 9000.
V*the above is then 75888 by 9000 * 9000 by 9000, giving 75888 by 9000.
You now have 75888 by 9000 * transpose(75888 by 9000) which is 75888 by 9000 * 9000 by 75888. That is 75888 by 75888 result. That is the place that your MATLAB runs out of memory.
But you have the diag() around that. You only need 75888 of the results, not 75888 by 75888 of the results.
If you have a new enough MATLAB I wonder if MATLAB would be smart enough to only calculate the diagonal elements? You did not happen to mention the release you are using.
So... implement a function to calculate f from V and H. Have it get as far as calculating V*(expm(H) - eye(m)). Then have it only calculate the diagonal elements of the *V', either by some clever rearranging (maybe) or by looping (maybe not as fast as possible but reliable and leaves room for finding some clever solution later if required.)
Omar B.
on 8 Mar 2022
syms f [5 3]
syms V [5 3]
b1 = diag(f*V')
b2 = sum(f.*conj(V),2)
b1 - b2
So... you can use
load('soc-Epinions1.mat');
A = Problem.A;
n = size(A,1);
v1 = randn(n,1);
m = 2000;
[V, H] = Arnoldi4(A, v1, m);
f = V*(expm(H)-eye(m));
b = sum(f .* conj(V),2);
Omar B.
on 9 Mar 2022
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

