www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/ConvertDataCopiesToPointerAssignmentsExample.m
%% Convert Data Copies to Pointer Assignments % The code generator optimizes generated code for vector signal assignments % by trying to replace |for| loop controlled element assignments and |memcpy| % function calls with pointer assignments. Pointer assignments avoid % expensive data copies. Therefore, they use less stack space and offer % faster execution speed than |for| loop controlled element assignments and % |memcpy| function calls. If you assign large data sets to vector signals, % this optimization can result in significant improvements to code % efficiency. %% Configure Model to Optimize Generated Code for Vector Signal Assignments % To apply this optimization: % % # Verify that your target supports the |memcpy| function. % # Determine whether your model uses vector signal assignments (such as % |Y=expression|) to move large amounts of data. For example, your model % could use a Selector block to select input elements from a vector, % matrix, or multidimension signal. % # On the *Optimization > Signals and Parameters* pane, the *Use memcpy for % vector assignment parameter*, which is on by default, enables the % associated *Memcpy threshold (bytes)* parameter. % # Examine the setting of *Memcpy threshold (bytes)*. By default, it % specifies 64 bytes as the minimum array size for which |memcpy| function % calls or pointer assignments can replace |for| loops in the generated code. % Based on the array sizes in your application's vector signal assignments, % and target environment considerations on the threshold selection, accept % the default value or specify another array size. % %% Example Model % Consider the following model named <matlab: rtwdemo_pointer_conversion>. % This model uses a Switch block to assign data to a vector signal. This % signal then feeds into a Bus Selector block. %% model='rtwdemo_pointer_conversion'; load_system(model); open_system(model); set_param(model, 'EnableMemcpy','off'); %% Generate Code without Optimization % % # In the Configuration Parameters dialog box, on the *All Parameters* % tab, clear the *Use memcpy for vector assignment* parameter. % # Create a temporary folder for the build and inspection process. % # Press *Ctrl+B* to generate code. %% currentDir = pwd; [~,cgDir] = rtwdemodir(); rtwbuild(model); %% % View the generated code without the optimization. Here is a portion of % |rtwdemo_pointer_conversion.c| . cfile = fullfile(cgDir,'rtwdemo_pointer_conversion_ert_rtw','rtwdemo_pointer_conversion.c'); rtwdemodbtype(cfile,'/* Model step','/* Model initialize',1, 0); %% % Without the optimization, the generated code contains |for| loop controlled % element assignments. %% Enable Optimization and Generate Code % % # In the Configuration Parameter dialog box, on the *All Parameters* tab, % select the *Use memcpy for vector assignment* parameter. % # Generate code. % set_param(model, 'EnableMemcpy','on') rtwbuild(model); %% % View the generated code without the optimization. Here is a portion of % |rtwdemo_pointer_conversion.c|. cfile = fullfile(cgDir,'rtwdemo_pointer_conversion_ert_rtw','rtwdemo_pointer_conversion.c'); rtwdemodbtype(cfile,'/* Model step','/* Model initialize',1, 0); %% % Because the setting of the *Memcpy threshold (bytes)* parameter is below the % array sizes in the generated code, the optimized code contains pointer % assignments for the vector signal assignments. bdclose(model) rtwdemoclean; cd(currentDir)