Main Content

Run Parallel Language in MATLAB

The following parallel language functionality is available in MATLAB®:

You do not need Parallel Computing Toolbox™ to run code using this functionality.

Run Parallel Language in Serial

Some syntaxes for parallel language functionality have automatic parallel support. Functionality with automatic parallel support automatically uses default parallel resources if you have Parallel Computing Toolbox. If you do not have Parallel Computing Toolbox, this parallel language functionality runs in serial. For more information about automatic parallel support, see Run MATLAB Functions with Automatic Parallel Support (Parallel Computing Toolbox).

The following parallel language functionality has automatic parallel support:

To run functions in the background, use parallel language syntaxes with backgroundPool instead. Code that you write using backgroundPool can automatically scale to use more parallel resources if you have Parallel Computing Toolbox. For more information, see backgroundPool.

Serial parfor

The following syntaxes run in parallel when you have Parallel Computing Toolbox, and otherwise run in serial:

  • parfor loopvar = initval:endval; statements; end

  • parfor (loopvar = initval:endval, M); statements; end

When a parfor-loop runs in serial, the iterations run in reverse order. For more information, see parfor and parfor (Parallel Computing Toolbox).

Serial parfeval and parfevalOnAll

The following syntaxes run in parallel when you use Parallel Computing Toolbox, and otherwise run in serial:

  • parfeval(fcn,n,X1,...Xm)

  • parfevalOnAll(fcn,n,X1,...Xm)

When a Future object runs in serial, MATLAB runs the function fcn associated with it using deferred execution. The function runs when MATLAB becomes idle, blocking MATLAB until the function finishes running. Examples of functionality that causes MATLAB to be temporarily idle include:

Use Parallel Language Without a Pool

DataQueue and PollableDataQueue

When you create a DataQueue or PollableDataQueue object, the object is not directly associated with the background pool or a parallel pool. You can therefore use a DataQueue or PollableDataQueue object without any pool.

The following code updates a plot on each iteration of a parfor-loop. If you have Parallel Computing Toolbox, the parfor-loop runs using a parallel pool. If you do not have Parallel Computing Toolbox, the code runs in serial.

x = 1:1000

line = plot(x,NaN(size(x)));

q = parallel.pool.DataQueue;
afterEach(q,@(data)updatePlot(line,data));

parfor i = 1:numel(x)
    % Simulate some work   
    pause(rand)

    y(i) = x(i)^2;
    send(q,[i y(i)])
end

function updatePlot(line,data)
    line.XData(data(1)) = data(1);
    line.YData(data(1)) = data(2); 
    drawnow
end

Constant

When you create a Constant object, the object is not directly associated with the background pool or a parallel pool. You can therefore use a Constant object without any pool.

The following code creates a Constant object in your current MATLAB session. You can write to a temporary file using c.Value and fprintf.

If you have Parallel Computing Toolbox, the parfor-loop runs using a parallel pool. If you use a parallel pool, the Constant is available on each worker in the parallel pool. Otherwise, the Constant object is created only in your current MATLAB session.

c = parallel.pool.Constant(@() fopen(tempfile(pwd),'wt'),@fclose);
parfor i = 1:10
    y(i) = i^2;
    fprintf(c.Value,"%03d %f\n",i,y(i));
end

You can use the following syntaxes without Parallel Computing Toolbox:

  • parallel.pool.Constant(X)

  • parallel.pool.Constant(fcn)

  • parallel.pool.Constant(fcn,cleanupFcn)