Documentation Center

  • Trial Software
  • Product Updates

parfeval

Execute function asynchronously on parallel pool worker

Syntax

F = parfeval(p,fcn,numout,in1,in2,...)
F = parfeval(fcn,numout,in1,in2,...)

Description

F = parfeval(p,fcn,numout,in1,in2,...) requests asynchronous execution of the function fcn on a worker contained in the parallel pool p, expecting numout output arguments and supplying as input arguments in1,in2,.... The asynchronous evaluation of fcn does not block MATLAB. F is a parallel.FevalFuture object, from which the results can be obtained when the worker has completed evaluating fcn. The evaluation of fcn always proceeds unless you explicitly cancel execution by calling cancel(F). To request multiple function evaluations, you must call parfeval multiple times. (However, parfevalOnAll can run the same function on all workers.)

F = parfeval(fcn,numout,in1,in2,...) requests asynchronous execution on the current parallel pool. If no pool exists, it starts a new parallel pool, unless your parallel preferences disable automatic creation of pools.

Examples

Submit a single request to the parallel pool and retrieve the outputs.

p = gcp(); % get the current parallel pool
f = parfeval(p,@magic,1,10);
value = fetchOutputs(f); % Blocks until complete

Submit a vector of multiple future requests in a for-loop and retrieve the individual future outputs as they become available.

p = gcp();
% To request multiple evaluations, use a loop.
for idx = 1:10
  f(idx) = parfeval(p,@magic,1,idx); % Square size determined by idx
end
% Collect the results as they become available.
magicResults = cell(1,10);
for idx = 1:10
  % fetchNext blocks until next results are available.
  [completedIdx,value] = fetchNext(f);
  magicResults{completedIdx} = value;
  fprintf('Got result with index: %d.\n', completedIdx);
end

See Also

| | | | | |

Was this topic helpful?