Documentation

parfor

Parallelfor-loop

Syntax

parfor LoopVar = InitVal:EndVal; Statements; end
parfor (LoopVar = InitVal:EndVal, NumThreads); Statements; end

Description

example

parforLoopVar=InitVal:EndVal;Statements; endcreates a loop in a generated MEX function or in C/C++ code that runs in parallel on shared-memory multicore platforms.

Theparfor-loop executes theStatementsfor values ofLoopVarbetweenInitValandEndval.LoopVarspecifies a vector of integer values increasing by 1.

example

parfor (LoopVar=InitVal:EndValNumThreads);Statements; enduses a maximum ofNumThreadsthreads when creating a parallelfor-loop.

Examples

collapse all

Generate a MEX function for aparfor-loop to execute on the maximum number of cores available.

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

functiona = test_parfor%#codegena = ones(10,256); r = rand(10,256);parfori = 1:10 a(i,:) = real(fft(r(i)));end

Generate a MEX function fortest_parfor. At the MATLAB command line, enter:

codegentest_parfor

codegengenerates a MEX function,test_parfor_mex,in the current folder.

Run the MEX function. At the MATLAB command line, enter:

test_parfor_mex

The MEX function runs using the available cores.

Specify the maximum number of threads when generating a MEX function for aparfor-loop.

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

functiony = specify_num_threads(u)%#codegeny = ones(1,100);% u specifies maximum number of threadsparfor(i = 1:100,u) y(i) = i;endend

Generate a MEX function forspecify_num_threads. Use-args 0to specify the type of the input. At the MATLAB command line, enter:

% -args 0 specifies that input u is a scalar double% u is typecast to an integer by the code generatorcodegen-reportspecify_num_threads-args0

codegengenerates a MEX function,specify_num_threads_mex,in the current folder.

Run the MEX function, specifying that it run in parallel on at most four threads. At the MATLAB command line, enter:

specify_num_threads_mex(4)

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.

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

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

functiona = test_parfor%#codegena = ones(10,256); r = rand(10,256);parfori = 1:10 a(i,:) = real(fft(r(i)));end

Generate a MEX function fortest_parfor. Disable the use of OpenMP so thatcodegendoes not generate a MEX function that can run on multiple threads.

codegen-Odisable:OpenMPtest_parfor

codegengenerates a MEX function,test_parfor_mex,in the current folder.

Run the MEX function.

test_parfor_mex

The MEX function runs on a single thread.

If you disable parallelization,MATLAB Coder™treatsparfor-loops asfor循环。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

Loop index variable whose initial value isInitValand final value isEndVal.

Initial value for loop index variable,Loopvar. WithEndVal,specifies theparforrange vector, which must be of the formM:N.

Final value for loop index variable,LoopVar. WithInitVal,specifies theparforrange vector, which must be of the formM:N.

The series of MATLAB commands to execute in theparfor-loop.

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

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

Maximum number of threads to use. If you specify the upper limit,MATLAB Coderuses no more than this number, even if additional cores are available. If you request more threads than the number of available cores,MATLAB Coderuses 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 orNumThreadsis 0),MATLAB Coderexecutes the loop in a serial manner.

Limitations

  • You must use a compiler that supports the Open Multiprocessing (OpenMP) application interface. SeeSupported Compilers. If you use a compiler that does not support OpenMP,MATLAB Codertreats theparfor-loops asfor循环。在生成的墨西哥人函数或C / c++鳕鱼e, the loop iterations run on a single thread.

  • The OpenMP application interface is not compatible with JIT MEX compilation. SeeJIT Compilation Does Not Support OpenMP.

  • Do not use the following constructs insideparfor-loops:

    • You cannot call extrinsic functions usingcoder.extrinsicin the body of aparfor-loop.

    • You cannot write to a global variable inside aparfor-loop.

    • MATLAB Coderdoes not support the use ofcoder.cevalin reductions. For example, you cannot generate code for the followingparfor-loop:

      parfori = 1:4 y = coder.ceval(“myCFcn”,y,i);end
      Instead, write a local function that calls the C code usingcoder.cevaland call this function in theparfor-loop. For example:
      parfori = 1:4 y = callMyCFcn(y,i);endfunctiony = callMyCFcn(y,i) y = coder.ceval('mCyFcn',y , i);end

    • You cannot usevararginorvarargoutinparfor循环。

  • The type of the loop index must be representable by an integer type on the target hardware. Use a type that does not require a multiword type in the generated code.

  • parforfor standalone code generation requires the toolchain approach for building executables or libraries. Do not change settings that cause the code generator to use the template makefile approach. SeeProject or Configuration is Using the Template Makefile.

For a comprehensive list of restrictions, seeparfor Restrictions.

Tips

  • Use aparfor-loop when:

    • You need many loop iterations of a simple calculation.parfordivides the loop iterations into groups so that each thread can execute one group of iterations.

    • You have loop iterations that take a long time to execute.

  • Do not use aparfor-loop when an iteration in your loop depends on the results of other iterations.

    Reductions are one exception to this rule. Areduction variableaccumulates a value that depends on all the iterations together, but is independent of the iteration order.

Introduced in R2012b

Was this topic helpful?