Main Content

Write Portable Parallel Code

You can write portable parallel code that automatically uses parallel resources if you use Parallel Computing Toolbox™, and that will still run if you do not have Parallel Computing Toolbox.

This topic covers how to:

  • Write portable parallel code that runs in serial without Parallel Computing Toolbox.

  • Write code that runs in the background without Parallel Computing Toolbox and uses more parallel resources if you have Parallel Computing Toolbox.

  • Write custom portable parallel code that runs in the background without Parallel Computing Toolbox and uses more parallel resources if you have Parallel Computing Toolbox.

Run Parallel Code in Serial Without Parallel Computing Toolbox

You can run the following parallel language features in serial without Parallel Computing Toolbox:

To write portable parallel code designed to use parallel pools or clusters if you have Parallel Computing Toolbox, use parallel language syntaxes with automatic parallel support. These syntaxes run in serial if you do not have Parallel Computing Toolbox.

To write portable parallel code that automatically runs in serial if you do not have Parallel Computing Toolbox, do not specify a pool argument for these language features.

As a best practice, specify the pool argument for Parallel Computing Toolbox functionality only if you need to specify an environment to run your code. If you do not specify a pool argument for parallel functionality, the functionality runs:

  • In serial if one of the following applies:

    • You do not have Parallel Computing Toolbox

    • You do not have a parallel pool currently open and you do not have automatic pool creation enabled

  • On a parallel pool if you have Parallel Computing Toolbox and if one of the following applies:

    • You have a parallel pool currently open

    • You have automatic pool creation enabled

If you do not have a parallel pool open and automatic pool creation is enabled, you open a pool using the default cluster profile. For more information on setting your default cluster profile, see Discover Clusters and Use Cluster Profiles.

Use parfeval without a pool to asynchronously run magic(3) and return one output. The function runs in serial if you do not have Parallel Computing Toolbox.

f = parfeval(@magic,1,3)

Use a parfor-loop without a pool to run magic with different matrix sizes. The loop runs in serial if you do not have Parallel Computing Toolbox.

parfor i = 1:10
    A{i} = magic(i);
end

For information about parallel language syntaxes that run in serial without Parallel Computing Toolbox, see Run Parallel Language in Serial.

Automatically Scale Up with backgroundPool

If you have Parallel Computing Toolbox, your code that uses backgroundPool automatically scales up to use more available cores.

For more information about your calculations in the background automatically scaling up, see Run MATLAB Functions in Thread-Based Environment.

Run parfor-loop on the Background Pool

You can use parforOptions to run a parfor-loop on the background pool.

Note

When you run a parfor-loop using the background pool, MATLAB® suspends execution until the loop is finished. As the code still runs in the background, you can use only functionality that is supported in a thread-based environment.

When you run multiple functions in the background using parfeval and backgroundPool, your code scales up to use more available cores. Use parfeval to run rand in the background 20 times.

for i = 1:20   
    f(i) = parfeval(backgroundPool,@rand,1);
end

To run a parfor-loop in the background, specify backgroundPool as the pool argument for parforOptions, then use the result as the opts arguments for parfor.

parfor (loopVal = initVal:endVal, parforOptions(backgroundPool))
    statements
end

Write Custom Portable Parallel Code

If you write portable parallel code that can automatically use parallel resources if you have Parallel Computing Toolbox, you create portable parallel code with the following limitations:

  • You are unable to automatically start a ThreadPool to run your parallel code

  • Your code runs in serial if you do not have Parallel Computing Toolbox

The selectPool function below returns either the background pool or a parallel pool. You can use selectPool as the pool argument with parallel language features such as parfeval and parforOptions. If you have Parallel Computing Toolbox and have automatic parallel pool creation enabled, the function returns a parallel pool. Otherwise, it returns the background pool.

function pool = selectPool
    if canUseParallelPool
        pool = gcp;
    else
        pool = backgroundPool;
    end
end