Improving performance of a complex for loop

2 visualizaciones (últimos 30 días)
Rafi el 21 de Feb. de 2020
Comentada: Rafi el 10 de Mzo. de 2020
I have a Matrix of size (300000X4). This is getting generated from a simulator that is running 200 particles for 1500 timesteps, hence producing 300000 rows of data. So, each of these 200 rows represents one timestep worth of data for one particle. The 4 columns are position and velocity components (Px, Py, Vx, Vy).
Now I am trying to create a new matrix that will be of dimension (3000000,10) where the columns will be the velocity values of the 5 nearest neighbors of a particle (at a particular timestep). The number of columns is 10 because I am going for 5 enighbours (i.e., Vx and Vy components of 5 nearest neighbors).
Currently, I can do it with for loops (pseudocode below), but I was wondering if it can be vectorized to speed up the computation. Any help or pointers (vectorizing or improving the approach) are much appreciated.
ROWS = 300000; %no of data rows
PARTICLES = 200; %no of particles
myData = rand(ROWS, PARTICLES); %matrix that holds my initial data from simulator, using dummy here
neighbors = 5;
neighborVelocity = zeros(ROWS,neighbors*2); %Allocating space for the final matrix to be generated
for t = 1:1500 %for each timestep
start = (t-1)*PARTICLES+1;
finish = (start-1)+PARTICLES;
currData = myData(start:finish,:); %Data for current timestep
temp = zeros(PARTICLES,neighbors*2); %For saving the processed data for current timestep
for m = 1:200 %for each particle
particle = currData(m,1:2);
allParticlePos = currData(:,1:2);
particleVelocity = currData(m,3:4);
allParticleVel = currData(:,3:4);
[Idx,~] = knnsearch(allParticlePos,particle,'K',neighbors);
nearest = allParticleVel(Idx,:); %The Velocities of the nearest neighbors are stored
%Some reshaping to save the nearest velocities in a intended format
nearest = reshape(nearest.',1,[]);
temp(m,:) = nearest;
neighborVelocity(start:finish,:) = temp; %Storing the particula timestep data
  2 comentarios
Rafi el 24 de Feb. de 2020
Hi Walter, can you please elaborate on "all items simultaneiously"? This might help me improve the loop. Thanks!

Iniciar sesión para comentar.

Respuesta aceptada

Pravin Jagtap
Pravin Jagtap el 24 de Feb. de 2020
Editada: Pravin Jagtap el 24 de Feb. de 2020
Hello Rafi,
From your code and data structure used it is clear that there are two for loops:
  • Outside for loops iterates over the time which is not vectorizable because of data dependency involved
  • Inside for loop iterates over 200 particles and from your explanation, the nature of computations for each of those 200 particles is the same. If it is so you can consider rewriting code in such a way that it can be vectorized or ‘parfor’ can be used from parallel computing toolbox. But you must make sure that operations(computations) involved in the inside for loop are independent.
I would recommend the following to improve the performance:
  • Focus on optimizing the bottlenecks (‘knnsearch’ is the bottleneck in your code)
  • Consider the vectorization/parallel computing toolbox or GPU coder toolbox for accelerating the code considering the data dependency involved and the availability of hardware.
  1 comentario
Rafi el 10 de Mzo. de 2020
Hi Pravin, thanks for the advice regarding checking bottlenecks. The execution time has significantly improved as I changed the knnsearch function to a custom one.

Iniciar sesión para comentar.

Más respuestas (0)


Más información sobre MATLAB Compiler en Help Center y File Exchange.




Community Treasure Hunt

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

Start Hunting!

Translated by