matlab有趣的程序和命令

MATLAB 并行for运算

2017-12-24  本文已影响11人  SilentSummer

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;

References:

  1. Execute loop iterations in parallel - Parfor
  2. MATLAB并行:parfor
上一篇 下一篇

猜你喜欢

热点阅读