optimize fixed-point data types for a system
data type optimization seeks to minimize an objective function, such as the total bit-width or an estimated count of operators in generated code for a specified system, while maintaining original system behavior within a specified tolerance. during the optimization, the software establishes a baseline by simulating the original model. it then constructs different fixed-point versions of your model and runs simulations to determine the behavior using the new data types. the optimization selects the model that minimizes the objective function while also meeting the specified behavioral constraints.
the model containing the system you want to optimize must have the following characteristics:
all blocks in the model must support fixed-point data types.
the design ranges specified on blocks in the model must be consistent with the simulation ranges.
if the model contains a matlab function block, it must use matlab® language features supported for fixed-point conversion. for more information, see .
the model must have finite simulation stop time.
during the optimization process, the software makes changes to several settings and model configuration parameters. these purpose of these changes include suppressing diagnostics, enabling logging with the simulation data inspector, reducing the memory consumed by the result, ensuring validity of the model, accelerating the optimization process, and turning off data type override. for more information, see . you can restore these diagnostics after the optimization is complete.
best practices for optimizing data types
define constraints
to determine if the behavior of a new fixed-point implementation is acceptable, the optimization requires well-defined behavioral constraints. to define a constraint, use the method of the object, or use one or more blocks in your model. for more information, see .
minimize locked data types
when the lock data types against changes by the fixed-point tools setting of a block within the system you want to optimize is enabled, it minimizes the freedom of the optimization process to find new solutions.
model management and exploration
the fxpopt
function returns an object containing a
series of fixed-point implementations called solutions. if the optimization process finds a
fixed-point implementation that meets the specified behavioral constraints, the solutions
are sorted by cost, giving the best solution with the smallest cost (bit width or operator
counts) as the first element of the array.
in cases where the optimization is not able to find a fixed-point implementation meeting the behavioral constraints, the solutions are ordered by maximum absolute difference from the baseline model, with the smallest difference as the first element.
explore the best found solution using the method of the
optimizationresult
object. you can also explore any of the other found
solutions in the same manner. do not save and close the model until you select the solution
you want to keep. closing or saving the model inhibits further exploration of different
solutions.
optimize fixed-point data types
this example shows how to optimize the data types used by a system based on specified tolerances.
to begin, open the system for which you want to optimize the data types.
model = 'ex_auto_gain_controller'; sud = 'ex_auto_gain_controller/sud'; open_system(model)
create an fxpoptimizationoptions
object to define constraints and tolerances to meet your design goals. set the useparallel
property of the fxpoptimizationoptions
object to true
to run iterations of the optimization in parallel. you can also specify word lengths to allow in your design through the allowablewordlengths
property.
opt = fxpoptimizationoptions('allowablewordlengths', 10:24, 'useparallel', true)
opt = fxpoptimizationoptions with properties: maxiterations: 50 maxtime: 600 patience: 10 verbosity: high allowablewordlengths: [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24] useparallel: 1 advanced options advancedoptions: [1×1 struct]
use the addtolerance
method to define tolerances for the differences between the original behavior of the system, and the behavior using the optimized fixed-point data types.
tol = 10e-2; addtolerance(opt, [model '/output_signal'], 1, 'abstol', tol);
use the fxpopt
function to run the optimization. the software analyzes ranges of objects in your system under design and the constraints specified in the fxpoptimizationoptions
object to apply heterogeneous data types to your system while minimizing total bit width.
result = fxpopt(model, sud, opt);
starting parallel pool (parpool) using the 'local' profile ... connected to the parallel pool (number of workers: 4). preprocessing modeling the optimization problem - constructing decision variables running the optimization solver analyzing and transferring files to the workers ...done. - evaluating new solution: cost 180, does not meet the tolerances. - evaluating new solution: cost 198, does not meet the tolerances. - evaluating new solution: cost 216, does not meet the tolerances. - evaluating new solution: cost 234, does not meet the tolerances. - evaluating new solution: cost 252, does not meet the tolerances. - evaluating new solution: cost 270, does not meet the tolerances. - evaluating new solution: cost 288, does not meet the tolerances. - evaluating new solution: cost 306, meets the tolerances. - evaluating new solution: cost 324, meets the tolerances. - evaluating new solution: cost 342, meets the tolerances. - evaluating new solution: cost 360, meets the tolerances. - evaluating new solution: cost 378, meets the tolerances. - evaluating new solution: cost 396, meets the tolerances. - evaluating new solution: cost 414, meets the tolerances. - evaluating new solution: cost 432, meets the tolerances. - updated best found solution, cost: 306 - evaluating new solution: cost 304, meets the tolerances. - evaluating new solution: cost 304, meets the tolerances. - evaluating new solution: cost 301, meets the tolerances. - evaluating new solution: cost 305, does not meet the tolerances. - evaluating new solution: cost 305, meets the tolerances. - evaluating new solution: cost 301, meets the tolerances. - evaluating new solution: cost 299, meets the tolerances. - evaluating new solution: cost 299, meets the tolerances. - evaluating new solution: cost 296, meets the tolerances. - evaluating new solution: cost 299, meets the tolerances. - evaluating new solution: cost 291, meets the tolerances. - evaluating new solution: cost 296, does not meet the tolerances. - evaluating new solution: cost 299, meets the tolerances. - evaluating new solution: cost 300, meets the tolerances. - evaluating new solution: cost 296, does not meet the tolerances. - evaluating new solution: cost 301, meets the tolerances. - evaluating new solution: cost 303, meets the tolerances. - evaluating new solution: cost 299, meets the tolerances. - evaluating new solution: cost 304, does not meet the tolerances. - evaluating new solution: cost 300, meets the tolerances. - updated best found solution, cost: 304 - updated best found solution, cost: 301 - updated best found solution, cost: 299 - updated best found solution, cost: 296 - updated best found solution, cost: 291 - evaluating new solution: cost 280, meets the tolerances. - evaluating new solution: cost 287, meets the tolerances. - evaluating new solution: cost 288, does not meet the tolerances. - evaluating new solution: cost 287, does not meet the tolerances. - evaluating new solution: cost 283, meets the tolerances. - evaluating new solution: cost 283, does not meet the tolerances. - evaluating new solution: cost 262, does not meet the tolerances. - evaluating new solution: cost 283, does not meet the tolerances. - evaluating new solution: cost 282, does not meet the tolerances. - evaluating new solution: cost 288, meets the tolerances. - evaluating new solution: cost 289, meets the tolerances. - evaluating new solution: cost 288, meets the tolerances. - evaluating new solution: cost 290, meets the tolerances. - evaluating new solution: cost 281, does not meet the tolerances. - evaluating new solution: cost 286, does not meet the tolerances. - evaluating new solution: cost 287, meets the tolerances. - evaluating new solution: cost 284, meets the tolerances. - evaluating new solution: cost 282, meets the tolerances. - evaluating new solution: cost 285, does not meet the tolerances. - evaluating new solution: cost 277, meets the tolerances. - updated best found solution, cost: 280 - updated best found solution, cost: 277 - evaluating new solution: cost 272, meets the tolerances. - evaluating new solution: cost 266, meets the tolerances. - evaluating new solution: cost 269, meets the tolerances. - evaluating new solution: cost 271, does not meet the tolerances. - evaluating new solution: cost 274, meets the tolerances. - evaluating new solution: cost 275, meets the tolerances. - evaluating new solution: cost 274, does not meet the tolerances. - evaluating new solution: cost 275, meets the tolerances. - evaluating new solution: cost 276, does not meet the tolerances. - evaluating new solution: cost 271, meets the tolerances. - evaluating new solution: cost 267, meets the tolerances. - evaluating new solution: cost 270, meets the tolerances. - evaluating new solution: cost 272, meets the tolerances. - evaluating new solution: cost 264, does not meet the tolerances. - evaluating new solution: cost 265, does not meet the tolerances. - evaluating new solution: cost 269, meets the tolerances. - evaluating new solution: cost 270, meets the tolerances. - evaluating new solution: cost 269, meets the tolerances. - evaluating new solution: cost 276, meets the tolerances. - evaluating new solution: cost 274, meets the tolerances. - updated best found solution, cost: 272 - updated best found solution, cost: 266 optimization has finished. - neighborhood search complete. - maximum number of iterations completed. fixed-point implementation that met the tolerances found. - total cost: 266 - maximum absolute difference: 0.087035 - use the explore method of the result to explore the implementation.
use the explore
method of the optimizationresult
object, result
, to launch simulation data inspector and explore the design containing the smallest total number of bits while maintaining the numeric tolerances specified in the opt
object.
explore(result);
you can revert your model back to its original state using the revert
method of the optimizationresult
object.
revert(result);