Use Custom Cost Functions for Optimized Fixed-Point Conversion
This example shows you how to leverage custom cost functions in the Optimized Fixed-Point Conversion workflow. You compare the cost of a design when using the built-in BitWidthSum
objective function to using a custom cost function.
The objective of the custom cost function in this example is to minimize the number of DSP slices used by the model mSimpleGain
. The model demonstrates a simple multiply and accumulate operation, using Gain blocks that are mapped to DSP slices. To learn more about mapping blocks to DSP slices for FPGA targets, see Optimize Data Types for an FPGA with DSP Slices.
Optimize Data Types Using a Built-In Objective
Open the model and inspect the MAC
subsystem, which performs the multiply and accumulate operation.
open_system('mSimpleGain'); open_system('mSimpleGain/MAC');
From the Simulink® Apps tab, open the Fixed-Point Tool and select Optimized Fixed-Point Conversion.
To optimize data types for the mSimpleGain
model using a default objective function, specify these options:
Set
Allowable Wordlengths
to[17:20]
Set Objective Function to
Bit Width Sum
.Under Signal Tolerances, set Absolute Tolerance for the
MAC
subsystem topow2(-18)
.
For more details on setting optimization options in the Fixed-Point Tool, see Optimized Fixed-Point Conversion in the Fixed-Point Tool.
Click Optimize Data Types and review the optimization results.
The generated solution has optimized the data types to minimize the total bit width. The estimated cost of the model based on this metric is a total bit-width of 179.
Optimize Data Types Using Custom Cost Function
Repeat the optimization process using a custom cost function for Gain blocks that minimizes the number of DSP slices used by the model. The @dsp48_Cost
function models the cost associated with using a DSP48 slice on an FPGA. This example targets DSP48 slices because they are optimized for multiply-accumulate operations, such as the one demonstrated in the mSimpleGain
model.
To set the custom cost function, create a dictionary of cost functions at the command line. Specify @dsp48_Cost
as the cost function for Gain blocks. Assign a default cost function to the remaining blocks in the system that sets their costs to zero so that the optimization solver accounts only for Gain blocks.
costFunctions = dictionary("Gain",@dsp48_Cost);
defaultCost = @(x)0;
Before beginning the optimization, revert the model to its original state to remove any changes made by the Fixed-Point Tool by clicking Restore Original Model.
To use a custom cost function for optimization, click on Optimization Options. Under Objective Function select Custom Cost Function. Two new text boxes appear. Use the Objective Function Map text box to specify the name of the dictionary of cost functions as costFunctions
. Use the Default Objective Function text box to specify the name of the default cost function as defaultCost
.
Click Optimize Data Types and review the optimization results.
The optimization solver now reports the number of DSP slices used as the value of Cost (Custom Cost Function). The cost of the model indicates that the number of DSP slices used with the optimal solution is 4.
Creating a custom optimization for your model helps you meet your specific design requirements. In this example, you optimized the number of FPGA-specific resources rather than optimizing the more general metric of total bit width. To learn more about requirements for custom cost functions, see setCustomCost
.
Supporting Functions
DSP48 Cost Function
DSP48 slices support multipliers that can handle operands with up to 18 bits. To calculate the number of DSP48 slices used by a design, the DSP48 cost function divides the word length, x
, of the input data type of a block by 18 and rounds up.
function cost = dsp48_Cost(x) cost = ceil(x ./ 18); end