Is there a faster method than parfor?

10 ビュー (過去 30 日間)
DB
DB 2024 年 12 月 19 日
回答済み: Edric Ellis 2024 年 12 月 23 日
Hello,
I used parfor in attempt to speed up my code and it didn't. I'm wondering if anyone has a better idea to speed this process up using different methods(spmd,parfeval)?
Rvol is a 3D array (1280,1280,560). I need to do calculations and update the values in it using the Pointers for location to update and Weights for values. However, I need the result from previous iteration to go to the next iteration. I made sure on each iteration, the pages that need to be calculated in parallel are isolated from each workers.
Here's my simplified code:
for iter = 1:5
parfor workers = 1:4
Views = (workers - 1) * 5 + iter;
pStart(workers) = pageInfo{Views,1}(1);
pEnd(workers) = pageInfo{Views,1}(end);
tempResult(:,:,:,workers) = modifyArray(Rvol(:,:,pStart(workers):pEnd(workers)),Pointers,Weights);
end
for i = 1:4
Views = (i - 1) * 5 + iter;
pStart(workers) = pageInfo{Views,1}(1);
pEnd(workers) = pageInfo{Views,1}(end);
Rvol(:,:,pStart:pEnd) = tempResult(:,:,:,i);
end
end
Using parfor took about 1560 seconds whereas using regular for loop took 1357 seconds. Any great ideas to make the code above way faster? I'm still learning about spmd and parfeval. I wish I can use gpuArray, but I think the communication between cpu and gpu between each iteration would take longer. Thanks.
  4 件のコメント
DB
DB 2024 年 12 月 19 日
@Walter Roberson Thanks for pointing my error (clumsy copy, paste,simplify the code), therefore helped me to even simplified it more. Still need to figure out to make it faster though.
Walter Roberson
Walter Roberson 2024 年 12 月 19 日
You could speed up the code marginally by making pageInfo into a iters by workers 3D cell instead of a 2D cell.
pStart = pageInfo{iter, workers, 1}(1);
pEnd = pageInfo{iter, workers, 1}(end);
That would allow pageInfo to be sliced instead of broadcast.
I would not imagine that it would speed up the code much but it would speed it up.

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

回答 (1 件)

Edric Ellis
Edric Ellis 2024 年 12 月 23 日
There's quite a bit going on here, and without seeing the details of modifyArray, it's hard to know exactly what to suggest to make improvements. Here's a few thoughts though:
  1. If modifyArray is already intrinsically multi-threaded by MATLAB itself, parfor using a local pool will not improve performance. (You can get a feel for this by watching MATLAB %CPU usage while it is running.)
  2. You are sending all of Rvol to every worker each time you start a new parfor loop (it is a "broadcast" variable). This might end up slowing you down. If it is compatible with your code, parpool("Threads") can really help here.
  3. It is possible that you might be able to do somewhat better using spmd. This allows you to have each worker modify their own portion of Rvol and then use something like spmdSendReceive to exchange results so that each worker has an updated view of Rvol.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by