# Why is my parfor loop running slower than a regular for loop

6 ビュー (過去 30 日間)
tiwwexx 2023 年 12 月 19 日

Hello all, I have a 3D array that takes an SVD of the first two dimensions for the size of the 3rd dimension. This seems like it would be extreemly parallelizable to me so I tried to run it in a parfor loop but for some reason the computation is like 10x slower running the parfor vs the standard for loop. Any help will be much appriciated!!
im_vec = rand(4,4,1000);
U = zeros(size(im_vec,1),size(im_vec,1),size(im_vec,3));
sig = zeros(size(im_vec));
V = zeros(size(im_vec,2),size(im_vec,2),size(im_vec,3));
%%% For loop time %%%
tic
for n = 1:size(im_vec,3)
[U(:,:,n), sig(:,:,n),V(:,:,n)] = svd(im_vec(:,:,n));
end
toc
%%% Parfor loop time %%%
p = gcp('nocreate');
if isempty(p)
parpool;
end
tic
parfor n = 1:size(im_vec,3)
[U(:,:,n), sig(:,:,n),V(:,:,n)] = svd(im_vec(:,:,n));
end
toc
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Walter Roberson 2023 年 12 月 19 日
On my system, if I expand the work by a factor of 100, then the regular for loop takes about 0.58 seconds and the parfor loop takes about 0.22 seconds the first time (and about 0.15 seconds after that.)

サインインしてコメントする。

### 回答 (1 件)

Christine Tobler 2023 年 12 月 19 日
The problem is likely that the cost of each iteration is still too small to warrant the start-up time of the parfor loop.
You could take a look at the pagesvd function, which applies the SVD to each page A(:, :, i) of the input array, just like what you're doing here. It uses threading on a lower level when the size of the array means we expect the threading to be worthwhile.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by