How to optimize multiplications with hdl coder
Show older comments
Hello community
I'm using Simulink to generate VHDL code. The system runs on 2.5 MHz sample rate and is supposed to run on a target architecture with 100 MHz.
The model uses quite a lot of constant multiplications, too many to fit on a FPGA, i.e. the available DSP blocks are not sufficient.
Since the FPGA runs on a much faster clock than the Simulink model, I want to use the 40 clock cycles to optimize the system by implementing the multiplications based on the shift and add algorithm or by multiplexing between the hardware multipliers.
1) Is it possible to automatically implement the shift and add approach with the hdl coder? If yes, how?
2) Is it possible to automatically implement the multiplexing approach with the hdl coder? If yes, how?
3) Is it possible to automatically approximate a constant with the nearest fixpoint representation and then implement the multiplication as a shift operation only? If yes, how?
1 Comment
Kiran Kintali
on 21 Jul 2020
Can you share your design? What are the sizes of multipliers you have in mind?
ConstMultiplierOptimization (CSD/FCSD) would be a good choice if you have Gain blocks (multiplication by constants) and you do not want to use hard multipliers on the FPGA.
Answers (1)
Kiran Kintali
on 21 Jul 2020
1 vote
ConstMultiplierOptimization
The ConstMultiplierOptimization implementation parameter lets you specify use of canonical signed digit (CSD) or factored CSD optimizations for processing coefficient multiplier operations in the generated code.
The following table shows the ConstMultiplierOptimization parameter values.
ConstMultiplierOptimization SettingDescription
'none'
(Default)By default, HDL Coder does not perform CSD or FCSD optimizations. Code generated for the Gain block retains multiplier operations.
'CSD'When you specify this option, the generated code decreases the area used by the model while maintaining or increasing clock speed, using canonical signed digit (CSD) techniques. CSD replaces multiplier operations with add and subtract operations. CSD minimizes the number of addition operations required for constant multiplication by representing binary numbers with a minimum count of nonzero digits.
'FCSD'This option uses factored CSD (FCSD) techniques, which replace multiplier operations with shift and add/subtract operations on certain factors of the operands. These factors are generally prime but can also be a number close to a power of 2, which favors area reduction. This option lets you achieve a greater area reduction than CSD, at the cost of decreasing clock speed.
'auto'
When you specify this option, HDL Coder chooses between the CSD or FCSD optimizations. The coder chooses the optimization that yields the most area-efficient implementation, based on the number of adders required. When you specify 'auto', the coder does not use multipliers, unless conditions are such that CSD or FCSD optimizations are not possible (for example, if the design uses floating-point arithmetic).
The ConstMultiplierOptimization parameter is available for the following blocks:
- Gain
- Stateflow® chart
- Truth Table
- MATLAB Function
- MATLAB System
9 Comments
Dominik Hiltbrunner
on 21 Jul 2020
Kiran Kintali
on 21 Jul 2020
hdlset_param('modle_name', 'MultiplierPartitioningThreshold', 18);
If you want to split large multiplier into smaller chunks you can consider using this global optimization option.
MultiplierPartitioningThreshold
Multiplier partitioning bit width threshold
N must be an integer greater than or equal to 2.
The maximum bit width for a multiplier. If a multiplier has a bit width greater than or equal to MultiplierPartitioningThreshold, HDL Coder™ splits the multiplier into smaller multipliers.
To improve your hardware mapping results, set MultiplierPartitioningThreshold to the bit width of the DSP or multiplier hardware on your target device.
Kiran Kintali
on 21 Jul 2020
In addition if you would like to make a multiplier with logic elements (shift-add) architecture and not multiplier resource on the FPGA currently you need to build a custom mutlplier.
HDLCoder team is automating this in the near release as a multiplier block specific option.
See attached example for additional details. If you have trouble opening the model
slprivate('showprefs')
Uncheck Do not load models created with a newer version of Simulink or use the command below.
>> set_param(0,'ErrorIfLoadNewModel', 'off')
Dominik Hiltbrunner
on 22 Jul 2020
Kiran Kintali
on 22 Jul 2020
#1 please share your project. if splitting fails you should have gotten a message. we can track this as a bug and provide resolution. By Minimum bitwidth you are referring to the threshold for split here.
#2 The link you provided is MATLAB to HDL workflow. For Simulink the resource sharing options are at subsystem level. You need to mark the sharable subsystems as atomic and make sure they have identical contents (types, sizes, complexity, rates etc.,). For MATLAB to HDL the settings are at global level in the optimization pane or on the MATLAB config object. Resource sharing factor
#3 There is no global setting. We will take this as input for future improvement for HDL Coder optimization settings. For now you can use a script that does find_system on the DUT to find all gain blocks and run hdlset_param on them. See hdlsaveparams for example on the syntax of block settings serialized to text in MATLAB.
Dominik Hiltbrunner
on 22 Jul 2020
Edited: Dominik Hiltbrunner
on 22 Jul 2020
Kiran Kintali
on 23 Jul 2020
Edited: Kiran Kintali
on 24 Jul 2020
There are missing initialization variables in the model without with the model would not initilizae and compile.
It looks like I need simin, fs_sim and other init variables to ctrl-d the model.
Without being able to compile i cannot generate HDL code from the model.
Dominik Hiltbrunner
on 24 Jul 2020
Dominik Hiltbrunner
on 24 Jul 2020
Categories
Find more on Speed and Area Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!