Method for generating source code adapted to implementation on accelerator hardware
By generating build directives that optimize loop unrolling and array partitioning for accelerator hardware, the method enhances execution speed and resource efficiency in generating source code from block diagrams, addressing the suboptimal parallel processing of complex control algorithms.
Patent Information
- Application Number
- JP2024041729
- Authority / Receiving Office
- JP · JP
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-03-17
- Filing Date
- 2024-03-15
- Publication Date
- 2024-10-01
AI Technical Summary
Existing methods for generating source code from block diagrams do not effectively utilize the parallel processing capabilities of accelerator hardware, such as FPGAs, leading to suboptimal execution speeds for complex control algorithms.
A method is provided to automatically generate build directives, such as build pragmas, that optimize the utilization of accelerator cores by unrolling loops and partitioning arrays, allowing for parallel processing of matrix computations.
This approach significantly improves execution speed by leveraging parallelism in accelerator hardware, reducing execution time and resource consumption, and facilitating seamless migration from processor-only devices to systems with accelerator hardware.
Smart Images

Figure 2024133043000001_ABST
Abstract
Description
[Technical field]
[0001] The invention relates to generating executable code from block diagrams, in particular for programming control units. [Background technology]
[0002] Control devices are used in many different applications to record physical variables of a process and / or to influence a process using connected actuators, for example for anti-lock control in braking operations. The time constants that determine the dynamic behavior of the process often require cycle times of 1 ms or less, so the control unit must have real-time capabilities. For applications such as the control of electric motors that require cycle times in the microsecond range, the control system often includes programmable logic devices, in particular field programmable gate arrays (FPGAs). FPGAs can achieve lower latencies than processors, since their functionality is provided by circuits that are configured according to a bitstream loaded at initialization, thus allowing parallelism in hardware. The configuration bitstream is created in a synthesis toolchain for a specific FPGA device. Both FPGAs and system-on-chip devices that combine FPGAs and microcontrollers are commercially available. In recent years, accelerator cores for other applications, such as matrix calculations for neural networks, have also become more common.
[0003] To design the control unit faster, the control strategy is often developed based on a model in a computing environment such as MATLAB / Simulink. Thus, the general behavior of the process and / or the controller or the control unit can be simulated first to check whether the desired characteristics are present. In particular, the model can be a block diagram including blocks performing operations such as calculations, for example a block can calculate one output signal from multiple input signals. Generally, the block diagram is executed cyclically, all blocks are persistently held in memory, and each block is executed once per time step. In particular, at each time step, a block can apply one or more operations to input signals from a preceding block to generate an output signal for the current step. The block diagram can further include submodels for describing discrete behaviors, where multiple states and transition conditions are defined. Source code for programming the control unit can be generated from the model using a code generator. Code generators for generating production quality source code are known, for example, from the publication “Production Quality Code Generation from Simulink Block Diagrams”, Proceedings of the 1999 International Symposium on Computer Aided Control System Design, Kohala Coast, Hawaii, H. Hanselmann et al.
[0004] The generated source code can be converted into a configuration bitstream for a programmable logic device via specialized high-level synthesis tools. A synthesis tool chain for system-on-chip devices is known from the publication “Model-Based Design Automation of Hardware / Software Co-Designs for Xilinx Zynq PSoC”, Proceedings of the 2018 International Conference on ReConFigurable Computing and FPGAs (ReConfig), Cancun, Mexico, Franz-Josef Streit et al. Using the tool chain, designers can translate a given MATLAB / Simulink model into a mixed hardware and software implementation interconnected by dedicated interfaces. To use FPGAs efficiently, compiler directives, in particular compiler pragmas, are automatically inserted to specify build options. A common build option is to apply automatic loop pipelining, which increases the throughput by overlapping processing of data from multiple loop iterations in different computation stages. New data is accepted after an initiation interval of at least one clock cycle. One subsequent result is output for each initiation interval, after a latency that depends on the number of computation stages to generate the first result. However, the method described here does not take advantage of the possible speedup of FPGA configurations that involve parallel processing of multiple input values.
[0005] Accelerator hardware adapted for faster control and / or more complex control algorithms is not limited to programmable logic devices, but may also include other accelerator cores that allow faster calculations and / or parallel processing. Recently, microcontrollers have become commercially available that include accelerator cores adapted to perform vector calculations that process multiple variables in parallel, and / or to process matrix calculations, for example for neural networks. By specifying build options that are adapted to the accelerator cores available on the target microcontroller, it is possible to generate code with significantly faster execution speeds.
[0006] It would therefore be desirable to be able to automatically generate build directives, such as build pragmas, that lead to better utilization of the logic available on accelerator hardware, most notably FPGAs. Summary of the Invention [Means for solving the problem]
[0007] In an exemplary embodiment, the present invention provides a method for generating source code from one or more blocks of a block diagram, the block diagram including at least two blocks: a source block that emits matrix signals corresponding to array variables, and an operation block that receives the matrix signals and emits an output signal based on an operation applied to the matrix signals. The method includes converting the block diagram to an intermediate representation, the converting the block diagram to the intermediate representation including converting the at least two blocks, where at least one loop results from converting the operation block, identifying at least one candidate loop in the intermediate representation, where a loop body of the candidate loop includes at least one instruction that accesses the array variables, identifying at least one parallelizable loop from the at least one candidate loop, determining build options for the at least one parallelizable loop and the array variables, inserting build directives, such as build pragmas, into the intermediate representation based on the determined build options, and translating the intermediate representation into source code.
[0008] In the following, the subject matter of the present disclosure will be described in more detail based on the exemplary drawings. All features described and / or shown in this specification may be used alone or in combination in various different combinations. The features and advantages of the various embodiments will become apparent by reading the following detailed description with reference to the attached drawings. [Brief description of the drawings]
[0009] [Figure 1] FIG. 1 illustrates a preferred embodiment of a computer system. [Diagram 2] 2 is a schematic diagram of software components preferably resident in the computer system. [Diagram 3] 1 is a schematic flow chart illustrating an embodiment of the method according to the present invention for generating source code. [Figure 4]FIG. 2 illustrates an example source code having multiple loops and multiple accessed arrays. [Diagram 5] 1 is a graph illustrating relationships between loops and arrays in an example source code. DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS
[0010] Exemplary embodiments of the present invention support generating source code from block diagrams that contain build directives, such as build pragmas, that enable parallel processing and / or better utilization of accelerator cores that fit the logic circuits available on, for example, an FPGA. By unrolling loops and using parallelism in the hardware, significant improvements in execution speed are possible.
[0011] Thus, there is provided a method for generating source code from one or more blocks of a block diagram, the block diagram including at least two blocks: a source block that emits matrix signals corresponding to array variables, and an operation block that receives the matrix signals and emits an output signal based on an operation applied to the matrix signals. The method includes converting the block diagram to an intermediate representation, the converting the block diagram to the intermediate representation including converting at least two blocks, where at least one loop results from converting the operation block, identifying at least one candidate loop in the intermediate representation, where a loop body of the candidate loop includes at least one instruction that accesses the array variable, identifying at least one parallelizable loop from the at least one candidate loop, determining build options for the at least one parallelizable loop and the array variable, inserting build directives, such as build pragmas, into the intermediate representation based on the determined build options, and translating the intermediate representation into source code. Preferably, the build options include processing the loop body in parallel for at least two iterations of the parallelizable loop. Preferably, the loop body includes instructions that are executed at least once for each iteration of the loop.
[0012] A matrix signal is a signal corresponding to an array variable containing a number of identical elements, each element being preferably addressable via an index. A vector in this respect is a one-dimensional matrix. The method of the present invention is also applicable to two-dimensional matrix signals, or higher-dimensional matrix signals, such as n-dimensional matrix signals, which can also be called tensor signals if n>2. In particular, effective parallelization is possible for loops in which one index varies monotonically while the other indexes are loop-invariant, i.e., each of the other indexes remains unchanged over all iterations of the loop. A monotonically varying index may increase from one iteration to the next, and the method of the present invention is applicable to monotonically increasing as well as decreasing indexes. This implies that the array is accessed sequentially along only one dimension for the operations in the loop, and that these operations can be performed in parallel since there is no dependency between them.
[0013] Advantageously, a method according to an embodiment of the invention allows for an optimized implementation of a parallelizable loop in accelerator hardware, such as an accelerator core or FPGA. The accelerator core can be adapted for vector processing of data and / or matrix calculations. Parallelization in hardware allows for two or more iterations of a loop to be executed in parallel. Different unroll factors, i.e. the number of iterations executed in parallel, are possible based on the available resources and the complexity of the loop. Parallelization can be specified by inserting unroll directives, e.g. unroll pragmas, into the source code. Since the execution time is inversely proportional to the unroll factor, a significant increase in computation speed is possible. To maximize the increase in computation speed, a maximum unroll factor is preferably selected that is adapted to the available resources. The method also preferably determines build options for each array accessed in the parallelizable loop. Based on the unroll factor, a partition factor for the array can be determined. Partitioning of the array can be specified by inserting partition directives, such as partition pragmas, into the source code. This allows for the creation of multiple smaller arrays that can be placed in multiple memory banks and therefore accessed in parallel from multiple loop iterations. Build directives can be inserted in the form of pragmas or as any other annotation, such as predefined comments or declarations. Any specifier or token recognized by a compiler or another tool in the build tool chain can be used as a build directive to indicate optimal factors for unrolling loops and / or partitioning arrays. In particular, the implementation tool chain can generate an FPGA configuration for a particular device based on the build pragmas. Thus, automatically inserting build directives such as build pragmas in accordance with embodiments of the present invention relieves developers from time-consuming manual optimization.Block diagrams can be transformed without manually re-running build directives, facilitating iterative development of models. The generated source code also enables processor-based software-in-the-loop simulation, thus enabling a seamless transition from processor-only devices to systems that include accelerator hardware, such as programmable logic devices.
[0014] In one embodiment, identifying at least one parallelizable loop includes performing one or more of the following checks on the candidate loop: Determining that the instructions in the loop body do not introduce data dependencies between different iterations of the candidate loop or only introduce accumulator dependencies, where each loop iteration writes a value to a fixed memory location and the value written in the current iteration is calculated based on the value written in the previous iteration. The absence of data dependencies between iterations allows freedom in determining the unroll factor for the loop: if the only data dependencies between iterations are limited to accumulator dependencies, the loop can still be unrolled. Determine that for any arrays accessed within the loop body, at most one index along one dimension varies monotonically, while all other indexes along all other dimensions are loop-invariant. The array may be a one-dimensional array, where the only dimension is accessed with a monotonically varying index. In the case of a two-dimensional array, each access must be made with a first index that is loop invariant and a second index that is monotonically varying, or with a first index that is monotonically varying and a second index that is loop invariant. In the case of higher dimensional arrays, the additional indexes must be loop invariant. The term loop invariant refers to an expression that remains unchanged for each iteration of the current loop. In the case of accumulator dependencies, a single array element can be accessed with a loop invariant index. If an array is accessed only with invariant indexes, such that the same element is accessed in every iteration of the loop, there is no need to partition the array. · Determining that there are no break or return statements within the loop body. Determining that there are no address or pointer arithmetic operations. By enforcing that there are no break or return statements within the loop body and ensuring that there are no address or pointer arithmetic, thereby ensuring that unpredictable control flow does not occur, loops can be easily analyzed for optimal build options. Determining that there are no nested inner loops within the candidate loop. Determining that the maximum number of iterations for a candidate loop is fixed by an expression whose value can be determined when generating code. If there were an inner loop or a variable expression for the maximum number of iterations, it would be necessary to introduce additional checks in the source code and / or additional control circuitry in the FPGA configuration, which would reduce or completely cancel any increase in computation speed that can be achieved by parallelization. In the case of matrix operations, it is common to access arrays with monotonically changing, e.g., monotonically increasing, array indices, and in the case of matrix multiplication, accumulator dependencies are common. Therefore, the method is well suited for typical source code generated from block diagrams for model-based design of control systems.
[0015] In one embodiment, determining build options for at least one parallelizable loop includes determining an unroll factor based on a maximum number of iterations of the parallelizable loop and resources available on the accelerator hardware, particularly on a programmable logic device, and determining build options for the accessed array variable includes determining a partition factor based on an unroll factor of the parallelizable loop that accesses the array variable. Partitioning the array variable into multiple smaller arrays with subsequent elements of the array placed in different partitions ensures sufficient memory bandwidth for parallel processing of the unrolled loop. If the array variable is placed in a dual-port memory such as a block RAM in an FPGA, the partition factor is preferably determined as the unroll factor divided by two for an even unroll factor. For an odd unroll factor, the partition factor is preferably determined to be the same as the unroll factor.
[0016] In one embodiment, determining build options for the at least one parallelizable loop includes determining an unroll factor, where the unroll factor is determined by optimizing a cost-benefit ratio between increased computation speed and increased resource consumption. Unrolling a loop refers to executing the loop body in parallel for multiple iterations. The number of iterations to be executed in parallel is given by the unroll factor. If the loop body is executed in parallel for all iterations of the loop, the loop is fully unrolled and the execution speed for the loop is maximized. However, the resource consumption of fully unrolling the loop may be excessive. Therefore, the best overall result can be obtained by optimizing the ratio between computation speed and resource consumption, or the ratio between increased computation speed and increased resource consumption.
[0017] In one embodiment, the resource consumption of the loop is determined by adding up the specific resource consumption for each operation in the loop body, and the specific resource consumption is retrieved from an operation library. The operation library may be specific to a specific accelerator hardware, such as a specific programmable logic device, and allows defining properties such as resource consumption and execution speed for individual operations that can be used in the loop body of the source code. The term operation may refer to any arithmetic, logical, relational, or other mathematical operation (such as square root or trigonometric function). The operation library may be implemented as an XML file that specifies the latency and resource consumption for multiple operations, and may be specific to a specific accelerator core or a specific FPGA device, and may be specific to a specific clock period. By using different or adapted operation libraries, the method according to the present invention can be applied to different accelerator hardware, such as programmable logic devices.
[0018] In one embodiment, the latency of the loop is determined based on a specific latency for each operation in the loop body, which is retrieved from an operation library. Determining the latency of the loop allows the benefit of unrolling the loop to be estimated. If there is no data dependency between different loop iterations, the latency of the loop can be determined by adding the latency of each operation in the loop body. Furthermore, the latency of the loop can include the period required to replicate the data required to execute the loop body.
[0019] In one embodiment, the operation library is determined by implementing code containing different possible operations in a specific accelerator core or a specific programmable logic circuit, and a specific resource consumption and / or a specific latency is measured from the implemented code. Once a specific FPGA device or accelerator core is available, an operation library for this specific FPGA device or accelerator core can be automatically generated by a software module that implements different operations for the device using a high-level synthesis tool chain or a device manufacturer's compatible compiler and linker. Based on the build results, the resource consumption and latency of individual operations can be determined. By automatically implementing operations for varying data types and / or clock periods, the software module can determine the impact of these parameters on latency and resource consumption.
[0020] In one embodiment, for multiple parallelizable loops, the unroll factor of the parallelizable loops is determined by optimizing a combined cost-benefit function for the parallelizable loops, and the available resources of a particular accelerator core or a particular programmable logic device form boundary conditions to be met. The available resources on the accelerator hardware, in particular on the programmable logic device, may not allow to fully unroll each loop present in the code generated to implement the control program. Therefore, the unroll factor of all loops is preferably optimized with respect to the maximum execution speed for the entire source code of the control algorithm.
[0021] In one embodiment, the following constraints are imposed on the parallelizable loops and / or accessed array variables: The unroll factor for each parallelizable loop that accesses a particular array is the same, The partitioning factors for multiple array variables accessed in the same parallelizable loop are the same, One or more of the following may be imposed: The partition factor constraints are applied only to array variables that are accessed with loop-variant indexing in at least one loop. Imposing these constraints guarantees adequate memory bandwidth for the unrolled loops, while ensuring that the parallelization benefits are not reduced by the additional logic to implement partitioned arrays.
[0022] In one embodiment, determining build options for a plurality of parallelizable loops and array variables includes determining clusters of mutually dependent parallelizable loops and array variables, where for a first parallelizable loop in the cluster, each array variable accessed by the first parallelizable loop is added to the cluster, and for a first array variable in the cluster, each parallelizable loop that accesses the first array variable is added to the cluster, and an unroll factor for all parallelizable loops in the cluster is selected based on optimizing a combined cost-benefit ratio for the cluster, which is calculated by adding up the costs of all loops in the cluster, adding up the benefits for all loops in the cluster, and determining a ratio of both sums. Arrays accessed using only constant indices may be ignored in determining the clusters.
[0023] In one embodiment, inserting build directives, in particular build pragmas, into the modified intermediate representation based on the determined build options comprises: Inserting unroll directives, in particular unroll pragmas, for at least one parallelizable loop; and Inserting a partition directive, in particular a partition pragma, for an array variable, where the partition directive or partition pragma specifies a partitioning of the array into multiple smaller arrays, with each successive element of the array variable being placed in a different smaller array; Inserting a pipeline directive, in particular a pipeline pragma, for at least one loop, where the pipeline directive or pipeline pragma can be inserted into any loop that is not fully unrolled; Includes one or more of: Inserting an unroll directive or pragma instructs the implementation toolchain to unroll a parallelizable loop completely or by a predetermined factor, thereby exploiting the parallelism available on the programmable logic circuit. Inserting a partition directive or pragma only for arrays accessed with a single monotonically varying loop variable index. Inserting a partition directive or pragma for an array variable accessed in a parallelizable loop instructs the implementation toolchain to partition the array into multiple smaller arrays, with each successive element of the array variable being placed in a different smaller array. This ensures sufficient memory bandwidth, since iterations of the unrolled loop executed in parallel can access multiple array elements from multiple smaller arrays in different memory banks. Inserting a pipeline directive or pragma for at least one loop instructs the implementation toolchain to add circuitry to efficiently process data in each clock cycle after pipeline initialization. Because pipelining results in very little resource consumption, a pipeline directive or pipeline pragma can be applied to any loop that is not fully unrolled. This increases the execution speed for any loop that is not suitable for the large increase in computation speed that would result from loop unrolling. The build directives inserted may differ depending on the accelerator hardware. For example, a pipeline directive may only be inserted for FPGAs and not for any other accelerator cores.
[0024] In one embodiment, the intermediate representation includes at least one function body, and the method comprises: Determining the resource consumption of the function body; and Determining the number of calls to the function; and Inserting function inlining directives, in particular function inlining pragmas, into the intermediate representation based on the resource consumption being less than a predetermined resource threshold and the function being called less frequently than a predetermined call threshold; Further includes: Inserting a function inlining directive or function inlining pragma instructs the implementation toolchain to replace a function call with the entire function body at the call site. Functions are modeled as separate modules in hardware, and function calls generally require one additional cycle to route signals to the module inputs. Thus, when a small function is called only a limited number of times, the computation speed can be increased without excessive resource consumption.
[0025] The present invention further provides a method for configuring a control unit, the control unit including at least one programmable logic device or accelerator core, the method comprising: generating source code according to a method according to an embodiment of the present invention; implementing source code for execution on a programmable logic device via a high-level synthesis toolchain to generate a bitstream that relies on the built pragmas, or implementing source code for execution on an accelerator core via a specialized compiler to generate a binary that relies on the built directives; Sending the bitstream or binary to a control unit; and storing the bitstream or binary on a non-volatile memory of the control unit, and in particular Configuring a programmable logic device of a control unit using the bitstream; The present invention relates to a method comprising the steps of:
[0026] The invention further provides a non-transitory computer readable medium having stored thereon processor executable instructions for generating source code from one or more blocks of a block diagram, the block diagram including at least two blocks, a source block that emits matrix signals corresponding to array variables, and an operation block that receives the matrix signals and emits output signals based on operations applied to the matrix signals, the processor executable instructions, when executed, for: converting a block diagram to an intermediate representation, the converting the block diagram to the intermediate representation including converting at least two blocks; identifying at least one candidate loop in the intermediate representation, a loop body of the candidate loop including an instruction that accesses an array variable; identifying at least one parallelizable loop from the at least one candidate loop; determining build options for at least one parallelizable loop; determining build options for array variables; inserting build directives, such as build pragmas, into the intermediate representation based on the determined build options; and Translating the intermediate representation into source code The present invention relates to a non-transitory computer-readable medium that facilitates the
[0027] The invention further relates to a computer system including a human machine interface, a non-volatile memory and a processor, the processor being configured to implement the method according to the invention.
[0028] In the following, the invention will be explained in more detail with reference to the drawings, in which like parts are designated by the same reference numerals, in which the illustrated embodiments are highly schematic, i.e. the distances as well as the lateral and vertical dimensions are not to scale and have no derivable geometric relationship to one another unless otherwise indicated.
[0029] 1 shows an exemplary configuration of a computer system PC. The computer system has a processor CPU, which may in particular be implemented as a multi-core processor, a main memory RAM and a bus controller BC. The computer system PC is preferably configured to be operated manually directly by a user, and is connected to a monitor DIS via a graphics card GPU and to a keyboard KEY and a mouse MOU via a peripheral interface HMI. In principle, the human-machine interface of the computer system PC may also be configured as a touch interface. The computer system further comprises a non-volatile data storage HDD, which may in particular be configured as a hard disk and / or solid-state disk, and an interface NET, in particular a network interface. A control unit ES may be connected via the interface NET. In principle, one or more interfaces of any kind, in particular wired interfaces, may be provided on the computer system PC and may each be used to connect to the control unit ES. Advantageously, a network interface according to the Ethernet standard may be used, the interface NET may be wireless, in particular configured as a WLAN interface or according to a standard such as Bluetooth.
[0030] The control unit ES may be configured as a series control unit or as an evaluation board for the target platform. Advantageously, the control unit ES includes an interface NET for connecting a system on chip SOC, including a processor and a programmable logic device, and a non-volatile memory NVM, to a computer system PC. The programmable logic device includes programmable logic blocks, typically a combination of look-up tables and flip-flops, reconfigurable interconnects, block RAMs, and may also include dedicated circuits such as digital signal processing blocks. When a bitstream is written into the configuration memory of the programmable logic device, the circuit is configured accordingly. In an alternative embodiment, the control unit ES includes the processor and the programmable logic device as separate chips on a printed circuit board. The embodiments of the invention are applicable regardless of the packaging of the programmable logic device.
[0031] 2 shows a diagram of the software components preferably installed on the computer system PC. These software components use the mechanisms of the operating system OS to access, for example, the non-volatile memory HDD or to establish a connection with an external computer via the network interface NET.
[0032] The technical computing environment TCE allows the creation of models and the generation of source code from the models. In the modeling environment MOD, models of dynamic systems can be created, preferably via a graphical user interface. In particular, the models of dynamic systems can be block diagrams, which contain a number of blocks and describe the behavior and / or internal state of the dynamic system over time. At least some of these blocks are linked via signals, i.e. via directional links for data exchange, which can be of scalar or composite type. The blocks can be atomic, i.e. form a unit from the point of view of the surrounding blocks, such that at the start of a computing step all input signals must be applied and at the end of the computing step all output signals must be present. If the block diagram is hierarchical, several blocks in a lower plane can describe the configuration of one block in a higher plane. A hierarchical or composite block can contain several blocks in the lower planes, even if these blocks are atomic. In particular, a composite block can be a subsystem, which can have additional properties, such as implementation in separate functions and / or triggering of the execution of the subsystem via a dedicated signal. Specialized blocks can be placed within the subsystems to further specify properties of the subsystems. The computing environment TCE includes one or more libraries BIB from which blocks or modules can be selected for building the model. The scripting environment MAT allows instructions to be entered interactively or via batch files to perform calculations or modify the model. The computing environment TCE further includes a simulation environment SIM configured to interpret or execute block diagrams to study the behavior of the system over time.These calculations are preferably performed using high precision floating point numbers on one or more cores of the computer system's microprocessor CPU.
[0033] Preferably, source code can be generated in a programming language such as C from the created model using a code generator PCG. Additional information about the model, in particular about the block variables, is advantageously stored in the definition data pool DDT. Advantageously, the block variables are assigned a numerical range and / or a scale to aid in the calculation of the model using fixed point instructions. Desired properties of the source code, for example compliance with a standard such as MISRA, can also be set or stored in the definition data pool DDT. Advantageously, each block variable is assigned a predefined variable type and one or more desired properties, for example the permissibility of optimizations such as merging of variables, are set. The code generator PCG preferably analyzes the settings of the definition data pool DDT and takes these settings into account when generating the source code. The definition data pool DDT may have a tree structure or may be stored in the memory of the computer system as a simple file, alternatively the definition data may be stored in a dedicated database system. The definition data pool may have a program interface and / or import / export functions.
[0034] A block diagram for a control algorithm typically includes a port block that emits or receives a matrix signal containing multiple elements, and a processing block that receives one or more matrix signals, applies an operation to each element, and emits a matrix signal. An exemplary operation may be an element-wise addition of two matrix signals having the same dimension, resulting in a further matrix signal of the same dimension. To add two one-dimensional matrix signals, i.e. vectors, the code generator PCG typically generates a for loop with the number of elements as the termination threshold. Within the body of the for loop, the array variables corresponding to the matrix signals are accessed, and the iteration accesses the element specified by the index being executed. A common operation for two-dimensional matrix signals is the calculation of matrix multiplication, and the generated source code typically includes two nested for loops. In the inner loop, only one index changes per array variable, while the other index has some fixed value that does not change (until the next iteration of the outer loop). The structure of the source code generated from the block diagram is well suited for optimization according to an embodiment of the present invention.
[0035] A control program CTL and an implementation toolchain IMP are installed on the computer system. The control program allows to configure the embedded system ES, for example to write the newly created bitstream into the non-volatile memory NVM. The implementation toolchain IMP is configured to perform high-level synthesis based on the C code using pragmas from a product code generator. A common intermediate form in the implementation is a description of the desired functionality at the register transfer level (RTL) abstraction level in a hardware description language such as Verilog or VHDL. The hardware description language is adapted to model the concurrency and parallelism of the hardware. Tools for converting the RTL description into a bitstream adapted for a particular programmable logic device are available from FPGA manufacturers. An exemplary implementation toolchain includes Vivado HLS from Xilinx and a bitstream conversion tool as part of the Vivado Design Suite, also from Xilinx.
[0036] Scheduling is an important step when converting an algorithm in a high-level language such as C to an RTL description. The hardware implementation allows multiple operations to be executed in parallel if there are no data dependencies between them. Furthermore, chaining of operations can be performed to schedule a dependent operation in the same cycle as its predecessor. The number of possible chains depends on the clock period of the FPGA. In a further step, the operations are bound to resources in the FPGA fabric. For example, a multiplication operation can be bound to a DSP block. Furthermore, registers may be introduced between operations to allow proper functioning at higher clock speeds. After that, a controller implementation is generated that creates the appropriate control signals for the scheduled and bound algorithm. Finally, an RTL description of the scheduled and bound algorithm and the controller implementation is generated. The operations of the bitstream conversion tool can be adapted via build pragmas to specify build options.
[0037] The illustrated example focuses on FPGAs, but other accelerator cores may be used. Instead of a high-level synthesis toolchain, it is possible to use a compiler / linker dedicated to the instruction architecture of the accelerator core. The format of the build directives may depend on the specific compiler.
[0038] 3 is a schematic flow chart of an embodiment of a method according to the invention for generating source code, which can be fully executed by a processor of an exemplary embodiment of a computer system PC, but may also be intended for execution in a client-server environment with an operator computer and one or more servers linked via a network, in which case the computationally intensive steps can be executed on the servers.
[0039] In step S1 (reading block diagram), a block diagram is input. The block diagram comprises at least two processing blocks linked by signals and may comprise a number of further blocks. Advantageously, inputting the block diagram also comprises reading at least one block property and / or associated settings for generating code, e.g. data types of variables, from a definition data pool DDT.
[0040] In step S2 (convert to intermediate representation), the selected model is converted from one or more blocks of the block diagram into an intermediate representation, which preferably contains one or more hierarchical graphs. These hierarchical graphs may in particular be data flow graphs, control flow graphs or tree structures. In addition to the block diagram, additional information from the definition data pool DDT is advantageously also taken into account when generating the intermediate representation and incorporated into this intermediate representation. This may also include situations where elements are generated based on information in the definition data pool DDT, or where properties of elements or relevant settings are extracted from the definition data pool DDT to generate code, e.g. data types of variables.
[0041] In step S3 (optimizing the intermediate representation), the hierarchical graph can be optimized to reduce the number of variables required and / or the memory consumption, e.g. stack occupancy, and / or the number of operations or processor instructions, and / or the execution time of the source code. This optimization can include multiple intermediate steps, in which a set of hierarchical graphs can be transformed into another different set of modified hierarchical graphs by applying optimization rules. Different strategies can be applied during the optimization, e.g. constant folding or dead code elimination. Thus, multiple further optimization rules can be applied successively, resulting in a more optimized intermediate representation. The optimized intermediate representation can be iteratively changed from being structurally closer to the initial block diagram to a representation having properties closer to the source code in a language such as C or C++.
[0042] In step S4 (More Loops?), a check is performed as to whether the intermediate representation contains further candidate loops for which potential optimizations have not yet been evaluated. Only loops whose loop bodies contain accesses to array variables with loop-variable patterns should be considered as candidate loops. If there are further loops in the intermediate representation for evaluation, step S5 is then performed, otherwise execution continues to step S10.
[0043] In step S5 (cluster complete?), a check is performed as to whether a cluster of interrelated loops and array variables is complete for the currently identified further loop. If the further loop only accesses one array variable that is not accessed by the other loops, the completed cluster consists of the loop and its array variable. If the further loop accesses an array variable that is also accessed by the second loop, both the further loop and the array variable and the second loop become part of the cluster. If the further loop accesses a first array variable and a second array variable, both array variables become part of the cluster. By examining the cluster of interrelated loops and array variables, mismatches in unroll and partition factors are avoided, as will be described below in connection with FIG. 4. If the cluster is complete, step S6 is then performed, otherwise execution continues to step S4 to check further loops that have not yet been evaluated for parallelization. The further loop is preferably selected by examining loops that access array variables that are already part of the cluster.
[0044] In step S6 (Parallelizable?), a check is performed as to whether the loops in the cluster are parallelizable. This check is performed for each loop in the cluster to ensure that all loops in the cluster are parallelizable. A parallelizable loop must satisfy the following conditions: The instructions in the loop body either do not introduce data dependencies between different iterations of the candidate loop, or only introduce accumulator dependencies, where each loop iteration writes a value to a fixed memory location and the value written in the current iteration is calculated based on the value written in the previous iteration. Loops that do not have data dependencies can be referred to as non-accumulator loops, and loops that have accumulator dependencies can be referred to as accumulator loops - in principle, this kind of loop can be unrolled. For any array accessed within the loop body, at most one index of one dimension varies monotonically, while all other indexes for all other dimensions are loop-invariant. Loops with more complex access patterns are not amenable to unrolling and may prevent unrolling for the entire cluster. The maximum number of iterations of the candidate loop is fixed by an expression whose value can be determined when generating code. This can be for example a for loop with a constant as maximum iteration count. There are no break or return statements within the loop body. Otherwise, unrolling the loop would require adding an exit condition check, which would reduce the potential speedup and increase resource consumption. There is no address or pointer arithmetic, which could result in unpredictable changes in control flow and / or data flow. There are no inner loops nested within the candidate loop. If all loops in a cluster satisfy the respective conditions, then the cluster is parallelizable and step S7 is performed next, otherwise execution continues to step S9.
[0045] In step S7 (Calculate Unroll Factor), an appropriate unroll factor U for the parallelizable loops in the current cluster is calculated. The main principles for determining the unroll factor U of a parallelizable loop are as follows: The unroll factor of a parallelizable loop must be greater than or equal to the partition factor of any array accessed by that loop. If the unroll factor is larger than the partition factor, any increase in computation speed is limited by the data bandwidth of the arrays accessed in the loop body. If all arrays accessed from a loop are partitioned by a certain factor, the achievable parallelism is limited to 2 times that factor (if these arrays are located in dual-port memory). Thus, if a loop is unrolled by a factor F, arrays accessed in the loop body should be partitioned by F / 2 if F is even, and by F if F is odd. If a loop accesses multiple arrays, all arrays accessed by that loop should be partitioned by the same factor. If an array is accessed by multiple loops, all loops that access the array should be unrolled by the same factor. · The unroll factor U is preferably a factor of the number of iterations for all loops in the cluster, because the number of iterations of a parallelizable loop should be a multiple of the unroll factor U for best results. The same unroll factor U is determined for all parallelizable loops in a cluster. To determine the unroll factor, a cost-benefit function for the cluster is optimized. The details of this calculation are described later.
[0046] In step S8 (inserting unroll and partition pragmas), build directives or build pragmas are inserted into the intermediate representation based on the determined build options. An unroll directive, in particular an unroll pragma, is inserted for parallelizable loops of the cluster. Such build directives or build pragmas can be inserted within the loop body and instruct the implementation toolchain to implement multiple iterations of the loop body in hardware, resulting in corresponding resource consumption. The unroll pragmas have the form "#pragma UNROLL factor=U", where U is an integer, i.e. the unroll factor determined in step S7. A partition directive, in particular a partition pragma, is inserted for arrays of the cluster. This build pragma can be inserted after the variable definition and instructs the implementation toolchain to partition an array variable into multiple smaller arrays. The partition pragma has the form "#pragma HLS ARRAY_PARTITION variable=a cyclic factor=F dim=D", where a is the name of the array, cyclic indicates that subsequent elements should be placed in different smaller arrays, the factor F is an integer indicating the number of smaller arrays to create, and the dimension D is an integer indicating which dimension of the array variable should be partitioned. The factor F must correspond to the unroll factor U of the loop accessing the array, or may correspond to U / 2, i.e., half the unroll factor, in case of even U and dual-port memories. The dimension D is 1 for vector signals, and in case of two-dimensional arrays, D indicates the dimension of the array that is accessed with a monotonically varying index, i.e., either 1 or 2 (if the other dimension index is loop invariant). In case of multidimensional arrays, D indicates the dimension that is accessed with a monotonically varying index while the other dimensions remain loop invariant.Since accesses to the arrays now correspond to individual memory banks, the memory bandwidth allows for unrolling of parallelizable loops that access the arrays. Preferably, a pipeline pragma is inserted (described in step S9) for all parallelizable loops whose unroll factor is less than the maximum number of iterations (i.e., not fully unrolled). Execution then continues to step S4 to check further loops that have not yet been evaluated for parallelization.
[0047] In step S9 (insert pipeline pragma), a pipeline directive, in particular a pipeline pragma, is inserted for the loop that has not been unrolled. Such a build directive or build pragma can be inserted in the loop body and instructs the implementation tool chain to allow overlapping execution of successive iterations of the loop. For this purpose, the loop is partitioned into several different stages, which allows different stages of two iterations to be executed in parallel. The pipeline pragma has the form "#pragma HLS PIPELINE II=1" when the initiation interval, i.e. the number of cycles required between the initiation of two successive iterations of the computation, is one cycle. The total latency of the loop is reduced to one cycle for each stage plus the number of iterations of the loop minus one cycle when the initiation interval is 1. Execution then continues to step S4 to check further loops that have not yet been evaluated for parallelization.
[0048] In step S10 (translating the intermediate representation into source code), the optimized intermediate representation or the optimized hierarchical graph resulting from all the intermediate steps carried out is translated into a source code in a textual programming language, in particular a C code or a C++ code. In this step too, further optimizations can be carried out, in particular so that the generated instructions represent a subset of the instructions contained in principle by the language and / or so that the generated control structures represent a subset of the control structures contained in principle by the language, thereby making it possible to fulfill strictly defined language rules. Alternatively or additionally, in order to improve the readability of the source code and / or to simplify debugging, additional information, for example references between program lines and blocks of a block diagram, can be generated and incorporated into the source code, in particular in the form of comments.
[0049] 4 shows an example source code with multiple loops and multiple accessed arrays. A first loop L1 accesses arrays t1, a, and x, all of which are accessed with monotonically increasing indexes. A second loop L2 accesses arrays t2, b, and u, all of which are accessed with monotonically increasing indexes. A third loop L4 accesses arrays t4, y, and z, all of which are accessed with monotonically increasing indexes. A fourth loop L3 accesses arrays t3, t1, and t2, all of which are accessed with monotonically increasing indexes.
[0050] The loops and arrays are grouped into clusters, which consist of all interrelated loops and arrays, based on the principle that every loop that contains at least one non-invariant access to an array in a cluster becomes an element of the cluster, and every array that is accessed using a non-invariant index from a loop in the cluster. The relationships between loops and arrays, and clusters, can be visualized as an undirected graph, where each node corresponds to one loop or one array.
[0051] FIG. 5 is a graph illustrating the relationships between loops and arrays in an exemplary source code. Cluster 1 includes loop L1 and arrays t1, a, and x accessed in L1. Loop L3 also accesses t1, so loop L3 is also part of cluster 1. As a result, arrays t3 and t2 accessed in L3 are also part of cluster 1. Loop L2 also accesses t2, so loop L2 is also part of cluster 1. As a result, arrays b and u accessed in L2 are also part of cluster 1. In determining the optimal unroll factor U1 for loops in cluster 1, the cost-benefit function must consider all elements in cluster 1. Loop L4 does not access any of the arrays in cluster 1, so loop L4 is not part of this cluster. L4 and all arrays accessed in this loop are part of cluster 2. No other loops access any arrays in this cluster, so there are no further nodes in the graph for cluster 2. When determining the optimal unroll factor U2 for loop L4, the cost-benefit function only needs to consider elements of cluster 2. A constraint associated with all unroll factors is that the total resource consumption of the control algorithm must be less than the resources available on the accelerator hardware to be used, such as an FPGA device.
[0052] For each cluster that contains at least one parallelizable loop, a build option must be determined. Such a cluster is associated with a set of unroll factors. If all loops in a cluster are parallelizable loops and no complex access patterns prevent array partitioning, the unroll factor and partition factor for these loops must match, so the set of unroll factors includes common arithmetic factors of all loop iteration counts and array sizes, and the unroll factor for each loop should be a factor of the loop iteration count for best results. If at least one loop in the cluster is not parallelizable and / or at least one loop-variable access pattern prevents array partitioning, the only possible unroll factor is 2 if the array is located in a dual-port memory bank (such as a block RAM in an FPGA).
[0053] FPGA resources include configurable logic blocks (CLBs), look-up tables (LUTs), digital signal processing blocks (DSPs), and block RAMs (BRAMs). The optimization goal is to determine the unroll factor for each cluster such that the benefit to all clusters is maximized without the cost of unrolling loops within the cluster exceeding a specified resource consumption threshold. The resource consumption threshold can be limited to a predefined percentage of all available resources on the FPGA.
[0054] The cost-benefit function for a loop can be calculated based on the following parameters: maximum loop iteration count N, unroll factor U, loop body latency L, and resource usage C for one loop iteration. In addition, data transfer costs can also be considered. Resource usage needs to be considered for the specific resource type used in the loop body. Preferably, a simple additive model for loop body resource usage is used. A specific number of specific types of resources are required to realize the operations. The usage of a specific resource type can be calculated for the loop body by summing the resources required for each operation present in the loop body. In case of accumulator loops, a further parameter needs to be considered, the latency A of the accumulation operation for multiple variables with accumulator access pattern, i.e. the longest latency of any accumulation operation needs to be considered. The cost-benefit calculation requires a database of latencies and number of resources for each operation. These values depend on the FPGA device and clock frequency used and can be measured as described below. By providing a corresponding database, the cost model can be used for different FPGA devices and / or clock frequencies. Similar cost models may be used for other accelerator hardware, such as accelerator cores.
[0055] The cost of a particular unroll factor U for a given resource type r is calculated by multiplying the resource usage C in one iteration by the unroll factor C. U,r =C r·(U-1), so the additional cost is the unroll factor minus 1 multiplied by the resource usage C for each resource, since the loop body needs to be implemented once without any unrolling. This relationship applies to accumulator and non-accumulator loops. The benefit B of a particular unroll factor can be expressed in terms of the clock cycles saved by unrolling. The estimated cycles L for the original loop are calculated by summing the latency for each operation in the loop body and multiplying it by the iteration count N. O =N·L, which is true for accumulator and non-accumulator loops. If a loop is unrolled by a factor of U, the unrolled loop body is executed N / U times. For non-accumulator loops, all unrolled iterations can be executed in parallel, so the latency of the unrolled loop body is L. The resulting benefit is B=N·L·(U-1) / U. The latency of the unrolled loop body is different for each accumulator loop. Assuming that all non-accumulator parts of the loop body are executed in parallel and that U accumulation operations are scheduled in an inverted tree structure, the latency for an accumulator loop iteration can be estimated by L+log2U·A. The resulting benefit is B=N·LN / U·(L+log2U·A). Additional data transfer costs are not included in this calculation, but may need to be considered. In principle, alternatively, more complex cost estimation models may be used in determining the optimal unrolling factor, which can be determined by optimizing the benefit-cost function by integer linear programming or any other general optimization method.
[0056] By providing source code containing different operations, defining desired data types for the operands, and implementing the source code for the desired FPGA device, the operation library as a database of latencies and resource consumption for different operations can be automatically measured for different FPGA devices. A translation tool in the implementation tool chain typically calculates the resource consumption for the implemented design, since this information is also needed in the placement and routing steps. The operation library can be automatically determined by using a script to adapt the clock speed and / or data type and calling the application programming interface of the implementation tool chain. Operation libraries for other acceleration hardware, such as accelerator cores for vector calculations, can be handled similarly.
[0057] While the subject matter of the present disclosure has been illustrated and described in detail in the drawings and the foregoing description, such illustrations and descriptions should be considered as illustrative or exemplary and not restrictive. The present invention is defined by the claims, and any statements made herein characterizing the invention should also be considered as illustrative or exemplary and not restrictive. Those skilled in the art will understand that within the scope of the appended claims, changes and modifications can be made, including any combination of the features of the various different embodiments described above.
[0058] The terms used in the claims should be construed to have the broadest reasonable interpretation consistent with the foregoing description. For example, the use of the articles "a" or "the" in introducing an element should not be construed as excluding a plurality of elements. Similarly, the term "or" should be construed as inclusive, such that the term "A or B" does not exclude "A and B," unless it is clear from the context or the foregoing description that only one of A and B is intended. Furthermore, the term "at least one of A, B, and C" should be construed as one or more of the group of elements consisting of A, B, and C, and should not be construed as requiring at least one of each of the listed elements A, B, and C, regardless of whether A, B, and C are related as a category. Furthermore, references to "A, B, and / or C" or "at least one of A, B, or C" should be interpreted as including any single entity from the listed elements, e.g., A, or including any subset from the listed elements, e.g., A and B, or including the entire list of elements A, B, and C.
Claims
1. A method for generating source code from one or more blocks of a block diagram, said block diagram comprising at least two blocks: A source block that emits matrix signals corresponding to array variables; an operation block for receiving the matrix signal and emitting an output signal based on an operation applied to the matrix signal; said method comprising: - converting the block diagram to an intermediate representation, the converting the block diagram to the intermediate representation including converting the at least two blocks, and at least one loop resulting from converting the operational block; identifying at least one candidate loop in the intermediate representation, a loop body of the candidate loop including at least one instruction that accesses the array variable; - identifying at least one parallelizable loop from the at least one candidate loop; determining build options for the at least one parallelizable loop and the array variables; - inserting build directives, in particular build pragmas, into the intermediate representation based on the determined build options; - translating said intermediate representation into source code; The method includes:
2. Identifying at least one parallelizable loop includes the following checks on the candidate loop: determining that the instructions of the loop body do not introduce data dependencies between different iterations of the candidate loop or only introduce accumulator dependencies, where in the case of accumulator dependencies, each loop iteration writes a value to a fixed memory location, and the value written in the current iteration is calculated based on the value written in the previous iteration; determining that for every array accessed within the loop body, an access index in one dimension varies monotonically and every other access index is loop invariant; determining that there are no break or return statements within the loop body; determining that there are no address or pointer arithmetic operations; determining that there are no nested inner loops within the candidate loop; determining that the maximum number of iterations of the candidate loop is fixed by an equation whose value can be determined when generating code; performing one or more of the following: The method of claim 1.
3. determining build options for the at least one parallelizable loop includes determining an unroll factor based on a maximum number of iterations of the parallelizable loop, a number of elements in the array variable to be accessed, and resources available on the programmable logic device; determining build options for the accessed array variable includes determining a partition factor based on the unroll factor of the parallelizable loop that accesses the array variable. The method of claim 1.
4. determining build options for the at least one parallelizable loop includes determining an unroll factor; The unroll factor is determined by optimizing the cost-benefit ratio between increased computation speed and increased resource consumption. The method of claim 1.
5. the resource consumption for a loop is determined by adding up a particular resource consumption for each operation within the loop body; The specific resource consumption is retrieved from an operation library. The method of claim 1.
6. the operation library is determined by implementing code containing different possible operations in a particular programmable logic circuit or accelerator core; The particular resource consumption is measured from the code as implemented. The method of claim 5.
7. a latency for the loop is determined based on a particular latency for each operation within the loop body; The particular latency is retrieved from an arithmetic library. The method of claim 1.
8. the operation library is determined by implementing code containing different possible operations in a particular programmable logic circuit or accelerator core; The particular latency is measured from the code as implemented. The method of claim 7.
9. For a plurality of parallelizable loops, an unroll factor for the parallelizable loop is determined by optimizing a combined cost-benefit function for the parallelizable loop; The available resources of a particular programmable logic device or accelerator core form boundary conditions that must be met. The method of claim 1.
10. For multiple parallelizable loops and / or multiple accessed array variables, the following constraints are met: The unroll factor for each parallelizable loop that accesses a particular array is the same, The partitioning factors for multiple array variables accessed in the same parallelizable loop are the same. one or more of the following are imposed: The method of claim 1.
11. Determining build options for the plurality of parallelizable loops and array variables includes determining a cluster of interdependent parallelizable loops and array variables; for a first parallelizable loop in the cluster, each array variable accessed by the first parallelizable loop is added to the cluster; For a first array variable in the cluster, each parallelizable loop that accesses the first array variable is added to the cluster; an unroll factor for all parallelizable loops in the cluster is selected based on optimizing a combined cost-benefit ratio for the cluster, which is calculated by adding up the costs of all loops in the cluster, adding up the benefits for all loops in the cluster, and determining a ratio of the sums of both; The method of claim 1.
12. Inserting build directives, in particular build pragmas, into the modified intermediate representation based on the determined build options, - inserting an unroll directive, in particular an unroll pragma, for said at least one parallelizable loop; inserting a partition directive, in particular a partition pragma, for said array variable, said partition directive or said partition pragma specifying a partitioning of said array into a number of smaller arrays, with successive elements of said array variable being placed in different smaller arrays; Inserting a pipeline directive, in particular a pipeline pragma, for at least one loop, said pipeline directive or said pipeline pragma can be inserted into any loop that is not fully unrolled; [0033] The method of claim 1.
13. the intermediate representation includes at least one function body; The method comprises: - determining the resource consumption of the function body; - determining the number of calls to the function; inserting function inlining directives, in particular function inlining pragmas, into the intermediate representation based on the resource consumption being less than a predefined resource threshold and on the function being called less frequently than a predefined call threshold; Further comprising: The method of claim 1.
14. 1. A method for configuring a control unit, the control unit including at least one programmable logic device or accelerator core, the method comprising: - generating source code by the method of claim 1; Implementing source code for execution on the programmable logic device via a high-level synthesis toolchain to generate a bitstream that relies on built pragmas, or implementing source code for execution on the accelerator core via a specialized compiler to generate a binary that relies on built directives; - transmitting said bitstream or said binary to said control unit; storing said bitstream or said binary on a non-volatile memory of said control unit and in particular configuring said programmable logic device of said control unit with said bitstream; The method includes:
15. A non-transitory computer-readable medium having stored thereon processor-executable instructions for generating source code from one or more blocks of a block diagram, the block diagram comprising at least two blocks: A source block that emits matrix signals corresponding to array variables; an operation block for receiving the matrix signal and emitting an output signal based on an operation applied to the matrix signal; the processor-executable instructions, when executed, - converting the block diagram to an intermediate representation, the converting the block diagram to the intermediate representation comprising converting the at least two blocks; identifying at least one candidate loop in the intermediate representation, a loop body of the candidate loop including instructions that access the array variable; - identifying at least one parallelizable loop from the at least one candidate loop; determining build options for the at least one parallelizable loop; determining build options for the array variables; - inserting build directives, in particular build pragmas, into the intermediate representation based on the determined build options; - translating said intermediate representation into source code; A non-transitory computer-readable medium that facilitates the
