Memo for MATLAB
Parfor
.
Execute Loop Iterations in Parallel - Parfor
Main Steps
Step1. Initialization
numCores: number of cores used in loop iterations
%Close the pools, if any
try
fprintf('Closing any pools...\n');
parpool close;
catch
%ignore any errors
end
fprintf('Starting a pool of workers with %d cores\n', numCores);
parpool('local',numCores);
Step2. Execution
%matlab can't save/load inside parfor
%accumulate them and then save
parfor i = 1:N
f(i);
end
Step3. Closing
%close the pool
fprintf('Closing the pool\n');
parpool close;