%% Reuse Buffers of Different Sizes and Dimensions
% You can reuse buffers for matrices that have different sizes and shapes.
% In the Configuration Parameters dialog box, you enable this optimization
% by selecting *Reuse buffers of different sizes and dimensions*.
% This optimization conserves RAM and ROM usage and improves code execution speed. 
%
%% Example Model
% The model <matlab:rtwdemo_differentsizereuse rtwdemo_differentsizereuse> 
% contains signals of different sizes and dimensions.
%
model='rtwdemo_differentsizereuse';
open_system(model);

%% Generate Code Without Optimization
% In the Configuration Parameters dialog box, set *Reuse buffers of
% different sizes and dimension* parameter to |off| or in the MATLAB Command Window, enter:
%
set_param('rtwdemo_differentsizereuse','DifferentSizesBufferReuse','off');
%%
% Create a folder for the build and inspection process.
currentDir = pwd;
[~,cgDir] = rtwdemodir();
%%
% Turn off comments and build the model.
set_param('rtwdemo_differentsizereuse','GenerateComments','off'); 
rtwbuild('rtwdemo_differentsizereuse');
%%
% View the generated code without the optimization. The |D_Work| structure is:
%
% <<../dwork_struct_no_optim.png>>

%%
% The portion of |rtwdemo_differentsizereuse.c| is:
cfile = fullfile(cgDir,'rtwdemo_differentsizereuse_ert_rtw',...
    'rtwdemo_differentsizereuse.c');
rtwdemodbtype(cfile,'#include "rtwdemo_differentsizereuse.h"',...
    'void rtwdemo_differentsizereuse_initialize(void)',1,0);
%%
% The |D_work| structure contains eight global variables for holding the
% inputs and outputs of |Downsample|, |DeltaX|, and |DeltaY|. These
% variables have different sizes.
%% Generate Code with Optimization
% # In the Configuration Parameters dialog box, verify that *Signal
% storage reuse* is selected.
% # Set the *Reuse buffers of different sizes and dimensions* parameter to |on| 
% or in the MATLAB Command Window, enter:
set_param('rtwdemo_differentsizereuse','DifferentSizesBufferReuse','on');
%%
% Build the model.
set_param('rtwdemo_differentsizereuse','GenerateComments','off');
rtwbuild(model);
%%
% View the generated code without the optimization. The |D_Work| structure is:
%
% <<../dwork_struct_optim.png>>
%
% The portion of |rtwdemo_differentsizereuse.c| is:
cfile = fullfile(cgDir,'rtwdemo_differentsizereuse_ert_rtw',...
    'rtwdemo_differentsizereuse.c');
rtwdemodbtype(cfile,'#include "rtwdemo_differentsizereuse.h"',...
    'void rtwdemo_differentsizereuse_initialize(void)',1,0);
%%
% The |D_work| structure now contains three global variables instead of eight
% global variables for holding the inputs and outputs of |Downsample|, |DeltaX|, and |DeltaY|. 
% The generated code uses these variables to hold the differently-sized inputs and outputs.
%%
% Close the model and code generation report.
bdclose(model)
rtwdemoclean;
cd(currentDir)
%% Limitations
% * If you use a |Reusable| custom storage class to specify reuse on signals
% that have different sizes and shapes, you must select the *Reuse buffers
% of different sizes and dimensions* parameter. Otherwise, the model does not build.
% * The code generator does not replace a buffer with a lower priority
% buffer that has a smaller size.
% * The code generator does not reuse buffers that have different sizes and
% symbolic dimensions.



%% 
% Copyright 2012 The MathWorks, Inc.