Time Parallel FOR (PARFOR) Loops
Sean's pick this week is Par Tic Toc by Sarah Wait Zaranek.
I run a fair number of computationally expensive operations and often use parallel for (parfor) loops to speed them up. This utility interested me mainly because it shows how work is distributed across the workers and provides worker utilization rates.
First, I will make sure a parallel pool is open using gcp() or "Get Current Pool". This opens the headless MATLAB processes that a parfor loop distributes iterations to.
pool = gcp
pool = Pool with properties: Connected: true NumWorkers: 2 Cluster: local AttachedFiles: {} IdleTimeout: 90 minute(s) (89 minutes remaining) SpmdEnabled: true
We'll do a simple example with a loop that has a random pause. The pause is a proxy for an operation that takes a varying and unpredicable amount of time.
n = 50; p = Par(n); % Build the Par timer parfor ii = 1:n Par.tic; % start timer pause(randi(5)) % Pause for up to five seconds p(ii) = Par.toc; % measure elapsed time end stop(p); % all done plot(p);
The parfor loop has done a good job distributing the calculations between the two workers. The more highly utilized worker only has one additional iteration that the other.
There are a few other neat tricks in here as well that I found from looking through the code.
- If you right click on the axes with each iteration's time, it gives you context menu to select the scale.
- Since the Par class inherits from handle, you get those visible methods. Sarah hides those from methods() by making their signature hidden.
- Sarah has also provided a full set of documentation on the capabilities of this utiltiy as well as examples.
Comments
Give it a try and let us know what you think here or leave a comment for Sarah.
- カテゴリ:
- Picks
コメント
コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。