parfor - Parallel for-loop - MATLAB (original) (raw)

Syntax

Description

parfor [LoopVar](#btz9%5Fxj-1-LoopVar) = [InitVal](#btz9%5Fxj-1-InitVal):[EndVal](#btz9%5Fxj-1-EndVal); [Statements](#btz9%5Fxj-1-Statements); end creates a loop in a generated MEX function or in C/C++ code that runs in parallel on shared-memory multicore platforms.

The parfor-loop executes the Statements for values of LoopVar between InitVal and Endval. LoopVar specifies a vector of integer values increasing by 1.

example

parfor ([LoopVar](#btz9%5Fxj-1-LoopVar) = [InitVal](#btz9%5Fxj-1-InitVal):[EndVal](#btz9%5Fxj-1-EndVal), [NumThreads](#btz9%5Fxj-1-NumThreads)); [Statements](#btz9%5Fxj-1-Statements); end uses a maximum of NumThreads threads when creating a parallel for-loop.

example

Examples

collapse all

Generate Code with Parallel for-Loops

Generate a MEX function and a standalone C library for a MATLAB® function that contains a parfor-loop. Instruct the code generator to use the maximum number of available cores.

Define a MATLAB function, test_parfor, that calls the fast Fourier transform function, fft, in a parfor-loop. Because the loop iterations run in parallel, this evaluation can be completed much faster than an analogousfor-loop.

function a = test_parfor %#codegen a = ones(10,256);
r = rand(10,256); parfor i = 1:10 a(i,:) = real(fft(r(i))); end end

Generate a MEX function for test_parfor at the command line. Do not specify a maximum number of threads.

Run the MEX function at the command line. The MEX function uses the available cores.

Generate a standalone C library for test_parfor. Do not specify a maximum number of threads. The generated C code executes the loop iterations in parallel on the available cores.

codegen -config:lib test_parfor

Specify Maximum Number of Threads for parfor

Specify the maximum number of threads when generating code for aparfor-loop.

Write a MATLAB function, specify_num_threads, that uses the input argument u to specify the maximum number of threads in theparfor-loop.

function y = specify_num_threads(u) %#codegen y = ones(1,100); parfor (i = 1:100,u) y(i) = i; end end

Generate a MEX function for specify_num_threads. Use-args 0 to specify that u is a scalar double.

codegen specify_num_threads -args 0

Run the generated MEX function, specifying that the function run in parallel on at most four threads. The generated MEX function runs on up to four cores. If fewer than four cores are available, the MEX function runs on the maximum number of cores available at the time of the call.

specify_num_threads_mex(4)

Generate C code for test_parfor. Use -args 0 to specify that u is a scalar double. In the generated code, the iterations of the parfor-loop run on at most u cores. If fewer cores are available, the iterations run on the cores available at the time of the call.

codegen -config:lib test_parfor -args 0

Generate MEX for parfor Without Parallelization

Disable parallelization before generating a MEX function for a parfor-loop.

Write a MATLAB function, test_parfor, that calls the fast Fourier transform function, fft, in a parfor-loop.

function a = test_parfor %#codegen a = ones(10,256);
r = rand(10,256); parfor i = 1:10 a(i,:) = real(fft(r(i))); end end

Generate a MEX function for test_parfor. Disable the use of OpenMP so that codegen does not generate a MEX function that can run on multiple threads.

codegen -O disable:OpenMP test_parfor

codegen generates a MEX function, test_parfor_mex, in the current folder.

Run the MEX function.

The MEX function runs on a single thread.

If you disable parallelization, MATLAB Coder™ treats parfor-loops as for-loops. The software generates a MEX function that runs on a single thread. Disable parallelization to compare performance of the serial and parallel versions of the generated MEX function or C/C++ code. You can also disable parallelization to debug issues with the parallel version.

Input Arguments

collapse all

LoopVar — Loop index

integer

Loop index variable whose initial value is InitVal and final value is EndVal.

InitVal — Initial value of loop index

integer

Initial value for loop index variable, Loopvar. With EndVal, specifies the parfor range vector, which must be of the form M:N.

EndVal — Final value of loop index

integer

Final value for loop index variable, LoopVar. With InitVal, specifies the parfor range vector, which must be of the form M:N.

Statements — Loop body

text

The series of MATLAB commands to execute in the parfor-loop.

If you put more than one statement on the same line, separate the statements with semicolons. For example:

parfor i = 1:10 arr(i) = rand(); arr(i) = 2*arr(i)-1; end

NumThreads — Maximum number of threads running in parallel

number of available cores (default) | nonnegative integer

Maximum number of threads to use. If you specify the upper limit, MATLAB Coder uses no more than this number, even if additional cores are available. If you request more threads than the number of available cores, MATLAB Coder uses the maximum number of cores available at the time of the call. If the loop iterations are fewer than the threads, some threads perform no work.

If the parfor-loop cannot run on multiple threads (for example, if only one core is available or NumThreads is 0), MATLAB Coder executes the loop in a serial manner.

Limitations

For a comprehensive list of restrictions, see parfor Restrictions.

Tips

Version History

Introduced in R2012b

See Also

Functions

Topics