Speed up a running code
3 views (last 30 days)
Show older comments
I have a code on frequency calculations to train my machine learning algo.
The code has been running for 3 days already.
Is there there any way to speed up the code without actually stopping or disturbing it's compilation.
I have a deadline to meet and I cannot afford to start the code allover again.
Please help.
My laptop runs on Nvidia 920M 4gb and I have 16 gigs of RAM and 2TB of storage.
clear all
ulti=zeros(0,0);
inde=1;
for location=[50 100 150 200 250 300 350 400 450 500 550 600 650 700 750]
for fac1=0.01:0.01:0.9
loc=location; %damage location
Ne=800;
fac=ones(Ne,1);
fac(loc(1))=fac1;
%fac(loc(2))=.9;
%fac(loc(3))=.7; %damage location
syms xi
deltat=2.0000e-06;
L=0.8;
beta=1/4;
E=70*10^9;
b=0.02495;
d=0.0028;
rho=2700;
I=b*d^3/12;
A=b*d;
le=L/Ne;
S1=5;
S2=14;
for i=1:Ne
C(i,:)=[(i-1)*2+1 (i-1)*2+2 (i-1)*2+3 (i-1)*2+4];
end
N=[(1-xi)^2*(2+xi)/4 le/8*(1-xi)^2*(1+xi) (1+xi)^2*(2-xi)/4 le/8*(1+xi)^2*(xi-1)];
for i=1:4
for j=1:4
Me(i,j)=rho*A*double(double(int(N(i)*N(j)*le/2,xi,-1,1)));
end
end
Ke=E*I*[12/le^3 6/le^2 -12/le^3 6/le^2;6/le^2 4/le -6/le^2 2/le;-12/le^3 -6/le^2 12/le^3 -6/le^2; 6/le^2 2/le -6/le^2 4/le];
K=zeros(2*(Ne+1),2*(Ne+1));
M=zeros(2*(Ne+1),2*(Ne+1));
for e=1:Ne
for j=1:4
for k=1:4
K(C(e,j),C(e,k))= K(C(e,j),C(e,k))+fac(e)*Ke(j,k);
M(C(e,j),C(e,k))= M(C(e,j),C(e,k))+Me(j,k);
end
end
end
Kg=K(3:2*(Ne+1),3:2*(Ne+1));
Mg=M(3:2*(Ne+1),3:2*(Ne+1));
Ft=zeros(2*Ne,1);
Cg=0.00000001*Kg;
x(:,1)=zeros(2*Ne,1);
v(:,1)=zeros(2*Ne,1);
a(:,1)=zeros(2*Ne,1);
% Lod=load('Load.txt');
% sz=size(Lod);
[psi,xval] = morlet(-4,4,90);
for i=2:900
t=(i-1)*deltat;
i
% Ft=F'*0;
T=deltat*50;
if t<=T
% %
Ft(end)=(sin(pi*t/T))^2*sin(4*pi*t/T);
% Ft(end)=exp(-(25*deltat*100000)^2/2)*cos(5*deltat)
Ft(end-4)=-Ft(end);
end
% if i<=90
% Ft(end)=psi(i);
% end
% %Ft=F';
% end
% Ft=F';
KK=Mg+deltat/2*Cg+beta*deltat^2*Kg;
FF=Ft-Cg*(v(:,i-1)+deltat/2*a(:,i-1))-Kg*(x(:,i-1)+deltat*v(:,i-1)+(1-2*beta)/2*deltat^2*a(:,i-1));
a(:,i)=inv(KK)*FF;
v(:,i)=v(:,i-1)+deltat/2*(a(:,i)+a(:,i-1));
abeta=(1-2*beta)*a(:,i-1)+2*beta*a(:,i);
x(:,i)=x(:,i-1)+deltat*v(:,i-1)+abeta*deltat^2/2;
end
S1A=a(2*(S1-1)+1,:)';
S2A=a(2*(S2-1)+1,:)';
%Sloc1=800; % sensor location
Sloc2=330;
%ss=x(2*(Sloc1+1),:)-x(2*Sloc1,:);
ss1=x(2*(Sloc2+1),:)-x(2*Sloc2,:);
ss1(length(ss1)+1)=loc;
ss1(length(ss1)+1)=fac1;
ulti(inde,:)=[ss1];
inde=inde+1;
%
%
% save 'ssh.txt' ss -ASCII
% save 'ssh1.txt' ss1 -ASCII
% save 'S1.txt' S1A -ASCII
% save 'S2.txt' S1A -ASCII
end
end
writematrix(ulti,'DATASET.csv',"Delimiter",',')
0 Comments
Answers (1)
Mohith Kulkarni
on 25 Nov 2020
The following provides information on tools within MATLAB that can help you optimize the performance of your code.
1. The first step is to analyze the performance of your MATLAB code in its current state. The following is a link to the documentation regarding this topic:
In particular, the MATLAB Profiler measures where a program spends time and generates a summary. By using the Profiler, you can determine which commands and which lines of code are taking the longest to execute, and therefore determine where you can focus most of your optimization efforts. To read about how to use the MATLAB Profiler to improve performance, please see the following link:
2. There is a section in the documentation that discusses best practices for writing highly efficient code, including when and how to vectorize, how to preallocate memory for arrays,
3. Multithreading comes default enabled in the most recent version of MATLAB. Common mathematical operations are programmed to make use of multithreading. For a list of the affected functions, see the Related Solution at the bottom of this page.
To find out if the Parallel Computing Toolbox can help make best use of a multiple core desktop or a computing cluster, navigate to the following link:
0 Comments
See Also
Categories
Find more on Parallel for-Loops (parfor) 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!