Please speed up my code

How can I speed up my code?
I know I can use parallel computing on while loop.
And I used gpuArray (quadro M6000) but runtime increased
I want to use CPU parallel computing or GPU
please modify my code which can run in the parallel computing tool
clear all
tic;
N = 1E5;
phi = linspace(0, 2 * pi, N);
V0 = 500;
Te = 3;
f =13.56E6;
w = f * 2 * pi;
tau = phi./w;
t = tau;
e=1.6E-19;
mi=40*1.6726E-27;
ni = 1E9*1E6;
eps0=8.8541878176E-12;
s0 = sqrt(eps0 * V0/ni/e/2);
% x = linspace(2 * s0,2 * s0, N);
x = 2 * s0 * ones(1,N);
sum_x = sum(x);
dt = 1E-10;
uB=sqrt(e*Te/mi);
% v = linspace(uB,uB,N);
v = -uB * ones(1, N);
le_i = [];
while sum_x > 0
s = s0 * (1 - sin(w * t));
ids1 = x >= s;
x_noE = x(ids1);
vx_no = v(ids1);
t_no = t(ids1);
ids2 = x < s;
x_E = x(ids2);
s_E = s(ids2);
t_E = t(ids2);
E = e * ni/eps0 * (x_E-s_E);
vx_E = v(ids2) + e*E/mi * dt;
v = [vx_no, vx_E];
x = [x_noE, x_E];
t = [t_no, t_E];
e_i = 0.5 * mi * v.^2;
x = x + v * dt;
t = t + dt;
idx = x<=0;
le_i = [le_i e_i(idx)./e];
idx = x>0;
x = x(idx);
t = t(idx);
if mod(1000*(N-length(x))/N,1) == 0
fprintf('Getting data... (%.1f %%)\n',100 * ((N-length(x)) / N));
end
sum_x = sum(x);
end

8 commentaires

Sargondjani
Sargondjani le 10 Mar 2021
Please format your code (and maybe make your code a bit denser, because it is quite long).
Anyway, it seems there is plenty of room for speed improvement. Just need to have a clearer picture of what your are doing.
darova
darova le 10 Mar 2021
You have Euler formula inside while loop. It can't be vectorized
Jan
Jan le 10 Mar 2021
The first point to improve the speed is to omit clear all. This removes all loaded functions from the memory and Matlab has to relaod them from the disk. This has no advantages but it is a waste of time only.
Raymond Norris
Raymond Norris le 10 Mar 2021
A couple of small points, most of these won't help with any signifcance. But first, I would suggest posting how long it takes to run and how long you'd like it to take.
  1. Only measure what can be improved. If you can't speed up the top, drop the call to tic to just what can (the while loop?)
  2. Preallocate le_i (size 1xN) and change le_i = [le_i ...
  3. Use a function
  4. Remove the fprintf. This saved 33% of the time. If you need it, descreas how often it prints.
At first glance, the while loop stops based on a condition and iteration I is dependent on iteration I-1. Don't see how you'll parallelize this.
inho seong
inho seong le 11 Mar 2021
I just want to obtain ion energy distribution. In the detial, N particles start to same point x = 2s0 with different phases. Here, meaning of different phases is that each particle can be accelerated by e-field determined by each phase or not.
They traversing x will reach the x = 0 point. At that time, this code will be done and I will obtain each energies of particles on the x=0 point.
I used the profiler provided by matlab which show me the runtime of each line. So as based on its result, I modified the code to speed up.
But I didn't know how to modify the le_i line to preallocate so I can't do it because its size is changed continuously during while loop.
The reason why I use fprintf is that I want to see when this code finish, directly. I am supposed to remove this line when I finally speed up.
Lastly, thanks for your comment about using function. I will try to functionalize my code.
I hope that my code will be Parallelizing. I mean that if there is a something one replaced with while loop, I want to modify my code using something that.
Please give me any idea about parallel tool
And I'm sorry about my immature english. I want you to understand about it
Thank you
Jan
Jan le 11 Mar 2021
I do not see a way to parallelize your code, because each iteration is based on the results of the former one.
inho seong
inho seong le 12 Mar 2021
I've been appreciating everyone, always.
If you have any idea to speed up like parallelization, please feel free to contact me!
Jan
Jan le 13 Mar 2021
Create your own copy of ttest, e.g. as "myttest.m" and omit all stuff, which is not needed by your call of this function.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Commenté :

Jan
le 13 Mar 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by