%% Run parfor on a Parallel Pool and Control Options
% You can use parforOptions to run parfor on the workers of a parallel pool. Use this approach when you want to reserve a fixed number of workers for the parfor-loop. You can also have finer control on how parfor divides iterations for workers.
% Create a parallel pool using the parpool function. By default, parpool uses your default cluster profile. Check your default profile on the MATLAB Home tab, in Parallel > Select a Default Cluster. Create a set of parfor options with the parallel pool object, and specify options. For example, specify subranges of fixed size 2 as the partitioning method.

p = parpool;
opts = parforOptions(p,'RangePartitionMethod','fixed','SubrangeSize',2);

% Pass the parfor options as the second input argument to the parfor function. parfor runs the loop body on the parallel pool and divides iterations according to opts.

values = [3 3 3 3 3 3];
parfor (i=1:numel(values),opts)
    out(i) = norm(pinv(rand(values(i)*1e3)));
end

% Copyright 2018 The MathWorks, Inc.