RunMATLABFunctions in Thread-Based Environment
Hundreds of functions in MATLAB®and other toolboxes can run in a thread-based environment. You can usebackgroundPool
orparpool("threads")
to run code in a thread-based environment.
To run a function in the background, use
parfeval
andbackgroundPool
.关于线程池的更多信息,请参阅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 useparfeval
andbackgroundPool
to run it in the background.
Use therand
function to generate a100
-by-100
matrix of random numbers in the background.
f = parfeval(backgroundPool,@rand,1,100);
For more information about running code in the background, seebackgroundPool
.
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");parfori = 1:100 A{i} = rand(100);end
关于线程池的更多信息,请参阅ThreadPool
(Parallel Computing Toolbox).
Automatically Scale Up
If you have Parallel Computing Toolbox, your code that usesbackgroundPool
automatically scales up to use more available cores.
For information about the number of cores that you can use, see theNumWorkers
property ofBackgroundPool
.
By running multiple functions in the background at the same time when you use Parallel Computing Toolbox, you can speed up the following code.
fori = 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, seeFunction List (Thread-Based Environment).
In general, functionality inGraphics,App Building,External Language Interfaces,Files and Folders, andEnvironment and Settingsis 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) |
See Also
Related Topics
- Run MATLAB Functions on a GPU(Parallel Computing Toolbox)