Run MATLAB Functions in Thread-Based Environment
Hundreds of functions in MATLAB® and other toolboxes can run in a thread-based environment. You can use
backgroundPool
or parpool("threads")
to run code in
a thread-based environment.
To run a function in the background, use
parfeval
andbackgroundPool
.For more information about thread pools, see Choose Between Thread-Based and Process-Based Environments (Parallel Computing Toolbox).
Run Functions in the Background
If a function is supported in a thread-based environment, you can use
parfeval
and backgroundPool
to run it in the
background.
Use the rand
function to generate a
100
-by-100
matrix of random numbers in the
background.
f = parfeval(backgroundPool,@rand,1,100);
For more information about running code in the background, see backgroundPool
.
Run Functions on a Thread Pool
If a function is supported in a thread-based environment, you can run it on a thread pool if you have Parallel Computing Toolbox™.
parpool("threads"); parfor i = 1:100 A{i} = rand(100); end
For more information about thread pools, see ThreadPool
(Parallel Computing Toolbox).
Automatically Scale Up
If you have Parallel Computing Toolbox, your code that uses backgroundPool
automatically scales
up to use more available cores.
For information about the number of cores that you can use, see the
NumWorkers
property of BackgroundPool
.
By running multiple functions in the background at the same time when you use Parallel Computing Toolbox, you can speed up the following code.
for i = 1:100 f(i) = parfeval(backgroundPool,@rand,1,100); end
Check Thread Supported Functions
If a MATLAB function has thread support, you can consult additional thread usage information on its function page. See "Thread-Based Environment" in the Extended Capabilities section at the end of the function page.
Tip
For a filtered list of MATLAB functions that have thread support, see Function List (Thread-Based Environment).
In general, functionality in Graphics, App Building, External Language Interfaces, Files and Folders, and Environment and Settings is not supported.
MATLAB and several toolboxes include functions with built-in thread support. To view lists of all functions in MATLAB and these toolboxes that have thread support, use the links in the following table. Functions in the lists with warning indicators have limitations or usage notes specific to running the function on threads. You can check the usage notes and limitations in the Extended Capabilities section of the function reference page. For information about updates to individual thread-supported functions, see the release notes.
Product | List of Functions Supported on Threads |
---|---|
MATLAB | Functions with thread support |
Image Processing Toolbox™ | Functions with thread support (Image Processing Toolbox) |
Signal Processing Toolbox™ | Functions with thread support (Signal Processing Toolbox) |
Statistics and Machine Learning Toolbox™ | Functions with thread support (Statistics and Machine Learning Toolbox) |
See Also
Related Topics
- Run MATLAB Functions on a GPU (Parallel Computing Toolbox)