Skip to content

Optimize BlockInitialization MATLAB function to reduce model compilation time  #189

@traversaro

Description

@traversaro

Summary

It seems that the BlockInitialization MATLAB function takes a lot of time to execute in some models, impacting a lot the time necessary to start a simulation.

Motivation

Profiling in some Simulink models that use WB-Toolbox revealed that during Simulink model compilation ( https://de.mathworks.com/help/simulink/ug/simulating-dynamic-systems.html#f7-8245 ) . In particular, it seems that the part that costs more in term of time is the following loop:

% Look a config block from the block's scope going up in the tree
analyzedScope = blockScopeName;
configBlock = char;
while ~isempty(analyzedScope)
rule = strcat(strrep(analyzedScope,'/','\/'),'\/\w+\/ImConfig');
idx = cellfun(@(x) ~isequal(regexp(x,rule),[]), blocks_system);
if (sum(idx) > 1)
error('[%s::Initialization] Found more than one configuration', char(currentBlock));
elseif (sum(idx) == 1)
configBlock = blocks_system{idx};
configBlock = fileparts(configBlock);
break;
end
analyzedScope = fileparts(analyzedScope);
end
, that takes ~100 ms for each iteration, and perform an interation for each nesting level of the block, arriving to a time ~1/2 seconds for really nested models, and a total compilation time of ~1 minute for a model with a lot of WB-Toolbox blocks.

Additional context

Further profiling to understand which line of

% Look a config block from the block's scope going up in the tree
analyzedScope = blockScopeName;
configBlock = char;
while ~isempty(analyzedScope)
rule = strcat(strrep(analyzedScope,'/','\/'),'\/\w+\/ImConfig');
idx = cellfun(@(x) ~isequal(regexp(x,rule),[]), blocks_system);
if (sum(idx) > 1)
error('[%s::Initialization] Found more than one configuration', char(currentBlock));
elseif (sum(idx) == 1)
configBlock = blocks_system{idx};
configBlock = fileparts(configBlock);
break;
end
analyzedScope = fileparts(analyzedScope);
end
takes so much time may be needed.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions