How can I speed up a nested for loop without parfor or spmd?

1 visualizzazione (ultimi 30 giorni)
I'm trying to run a nested for loop (shared below) on a data set that is 128x128x5263 and is complex. It's taking too long, over 25 minutes.
These are the things I tried:
  1. Running it in a function
  2. Using parfor: but it doesn't work because the loop outputs are not independent of each other.
  3. Using spmd, but that did not help with the speed.
Is there another way I can do this?
Code below:
NOTE: k_data_shift is a 128x128x5263 complex data-set.
k_d1=zeros(size(k_data_shift,1),size(k_data_shift,2),size(k_data_shift,3));
k_d1(:,ss(1),1)=k_data_shift(:,ss(1),1);
%
for j=2:num_frames;
k_d1(:,:,j)=k_d1(:,:,j)+k_d1(:,:,j-1);
for i=1:num_frames;
k_d1(:,ss(i),i)=k_data_shift(:,ss(i),i);
end
end
Thanks!

Risposte (1)

Prateekshya
Prateekshya il 28 Ago 2024
Hello Radhika,
Apart from the things you have already tried, you can use methods like pre-allocation, vectorization and profiling to optimize your code further. Here is a sample code for the same:
% Assuming num_frames is defined and ss is an index array
num_frames = size(k_data_shift, 3);
k_d1 = zeros(size(k_data_shift));
% Initialize the first frame
k_d1(:, ss(1), 1) = k_data_shift(:, ss(1), 1);
% Iterate over frames
for j = 2:num_frames
% Accumulate the sum from the previous frame
k_d1(:, :, j) = k_d1(:, :, j-1);
% Update specific slices
% Vectorize the inner loop if possible
k_d1(:, ss(1:num_frames), j) = k_data_shift(:, ss(1:num_frames), j);
end
You can find more details on the above-mentioned techniques in the below links:
I hope this helps!

Categorie

Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by