Speed up: Parfor loop vs Vectorization

I'm trying to speed up my code, which is bottlenecking here:
tic
FD = zeros(28, Rotations);
parfor i = 1:Rotations
CodePR = rotate (CodeP, i*(Rotations - 1)*angle, [0 0]);
for j = 1:28
FD(j, i) = (area(intersect(intersect(CodePR, MaskP), WholeVD(j))))/(AreaFD(j));
end
end
A = FD;
toc
I came up with that solution:
tic
FD = zeros(28, Rotations);
WholeVD = transpose(WholeVD);
AreaFD = transpose(AreaFD);
parfor i = 1:Rotations
CodePR = rotate (CodeP, i*(Rotations - 1)*angle, [0 0]);
FD(:,i) = area(intersect(intersect(CodePR, MaskP), WholeVD))./AreaFD;
end
B = FD;
toc
which is like 20 times (for my rig at least). The bad thing is that with actual dataset it consumes too much RAM (and eventually crashes matlab) so I can't evaluate that. Getting rid or PARFOR helps with memory problem but effectivly slower (by amount of CPU cores). Is there any walk arounds to reduce memory usage so I could utilize more threads?
The whole code is in attachment (fold everything, described part is not foldable). Thanks.

 採用された回答

Mohammad Sami
Mohammad Sami 2020 年 8 月 21 日

2 投票

R2020a introduced Threads based parallel pool. This may reduce the memory issues.
You can create a threads based parpool before using parfor.
pool = parpool("threads");

3 件のコメント

max fire
max fire 2020 年 8 月 21 日
Thanks, seems a legit way to improve memory usage. I have to update to 2020a to check this, tho.
Raymond Norris
Raymond Norris 2020 年 8 月 21 日
Running your code (which I'm asusming isn't the entire number of iteration) ran under "threads", so I think that's a good approach to potentially solving your parfor memory issues. Both "local" and "threads" ran in the same amount of time.
max fire
max fire 2020 年 8 月 21 日
Oh, that's nice of you, Raymond, thanks.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2020 年 8 月 20 日

コメント済み:

2020 年 8 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by