How can I speed up a nested for loop without parfor or spmd?
1 回表示 (過去 30 日間)
古いコメントを表示
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:
- Running it in a function
- Using parfor: but it doesn't work because the loop outputs are not independent of each other.
- 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!
回答 (1 件)
Prateekshya
2024 年 8 月 28 日
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:
- Pre-allocation: https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html
- Vectorization: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
- Profiling: https://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html
I hope this helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Parallel for-Loops (parfor) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!