Main Content

使用并行循环(Parcol)的算法加速度

循环(Parcol)在生成的代码中

To potentially accelerate execution, you can generate MEX functions or C/C++ code from MATLAB®Code that contains parallel for-loops (par-loops).

一种par- 就像标准的matlab一样为了-loop, executes a series of statements (the loop body) over a range of values. Unlike the为了- 但是,迭代的迭代par-loop可以在目标硬件上的多个核心上并行运行。

Running the iterations in parallel might significantly improve execution speed of the generated code. For more information, seeHow parfor-Loops Improve Execution Speed

笔记

并行执行仅在生成的MEX函数或C / C ++代码中发生;不是原始的matlab代码。要加速您的MATLAB代码,请从中生成MEX功能par-loop. Then, call the MEX function from your code. For more information, seeWorkflow for Accelerating MATLAB Algorithms

To useparin your MATLAB code, you require a Parallel Computing Toolbox™ license.

Matlab Coder™软件使用打开的多处理(OpenMP)应用程序接口来支持共享内存,多核代码生成。金宝app如果要分布式并行性,请使用并行计算工具箱产品。默认情况下,Matlab编码器uses up to as many cores as it finds available. If you specify the number of threads to use,Matlab编码器uses at most that number of cores for the threads, even if additional cores are available. For more information, seepar

Because the loop body can execute in parallel on multiple threads, it must conform to certain restrictions. IfMatlab编码器软件检测不符合符合的循环parspecifications, it produces an error. For more information, seeparRestrictions

How parfor-Loops Improve Execution Speed

一种par-loop might provide better execution speed than its analogous为了- 因此,由于多个线程可以在同一循环上同时计算。

每次执行一个身体par-loop称为迭代。线程以任意顺序的迭代术语评估迭代,彼此独立。因为每次迭代都是独立的,因为它们不必同步。如果线程数等于循环迭代的数量,则每个线程执行循环的一次迭代。如果迭代的迭代,则某些线程执行多于一个循环迭代。

例如,当100个迭代循环运行20个线程时,每个线程同时执行循环的五个迭代。如果由于大量的迭代或冗长的单个迭代而运行,因此您的循环需要很长时间,您可以使用多个线程显着减少运行时间。在此示例中,由于并行化开销(例如线程创建和删除),您可能不会获得20倍的速度提高。

When to Use parfor-Loops

Useparwhen you have:

  • Many iterations of a simple calculation.par将循环迭代划分为组,以便每个线程执行一组迭代。

  • 一种loop iteration that takes a long time to execute.parexecutes the iterations simultaneously on different threads. Although this simultaneous execution does not reduce the time spent on an individual iteration, it might significantly reduce overall time spent on the loop.

什么时候不使用循环循环

不使用parwhen:

  • 一种n iteration of your loop depends on other iterations. Running the iterations in parallel can lead to erroneous results.

    To help you avoid usingpar当循环的迭代取决于其他迭代时,Matlab编码器specifies a rigid classification of variables. For more information, see在循环中的变量分类。如果Matlab编码器检测不符合符合的环路parspecifications, it does not generate code and produces an error.

    Reductions are an exception to the rule that loop iterations must be independent. A减少变量accumulates a value that depends on all the iterations together, but is independent of the iteration order. For more information, seeReduction Variables

  • 只有几个迭代执行一些简单的计算。

    笔记

    对于少量循环迭代,您可能不会加速由于并行化开销导致的执行。此类开销包括用于线程创建的时间,线程之间的数据同步和线程删除。

Paret-Loop语法

  • 为一个par-loop,使用此语法:

    Parcon i = ingval:endval parfor(i = ingval:endval)

  • 要指定最大线程数,请使用此语法:

    parcol(i = ingval:endval,numthreads)

有关更多信息,请参阅par

parRestrictions

  • parloop does not support the syntax:

    parcol(i = ingval:步骤:endval)parcon i = ingval:步骤:endval

  • 你必须使用一个编译器,它支持金宝appOpen Multiprocessing (OpenMP) application interface. See金宝app支持的编译器。如果you use a compiler that does not support OpenMP,Matlab编码器对待par- 乐队为了-loops. In the generated MEX function or C/C++ code, the loop iterations run on a single thread.

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

  • 环索引的类型必须通过目标硬件上的整数类型表示。使用生成的代码中不需要多字类型的类型。

  • par为了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

  • 不使用the following constructs in the body of apar环形:

    • Nested parfor-loops

    • 休息和退货陈述

    • Global variables

    • Reductions onMATLAB课堂

    • Reductions onChar变量

    • Reductions using external C code

    • 外在功能呼叫

    • 内联功能

    • Unrolling loops

    • varargin./varargout