main content

generate compiled c code function including logging instrumentation -凯发k8网页登录

generate compiled c code function including logging instrumentation

description

example

buildinstrumentedmex fcn -options translates the matlab® file fcn.m to a mex function and enables instrumentation for logging minimum and maximum values of all named and intermediate variables. optionally, you can enable instrumentation for log2 histograms of all named, intermediate and expression values. the general syntax and options of buildinstrumentedmex and are the same, except buildintstrumentedmex has no fi object restrictions and supports the '-coder' option.

note

like the function, the buildinstrumentedmex function generates a mex function. to generate c code, use the codegen (matlab coder) function.

example

buildinstrumentedmex fcn_1... fcn_n -options -coder translates the matlab functions fcn_1 through fcn_n to a mex function and enables instrumentation for logging minimum and maximum values of all named and intermediate variables. generating a mex function for multiple entry-point functions requires the '-coder' option.

note

generating a mex function for multiple entry-point functions using the buildinstrumentedmex function requires a matlab coder™ license.

examples

this example shows how to create an instrumented mex function, run a test bench, then view logged results.

define prototype input arguments.

n = 128;
x = complex(zeros(n,1));
w = fi_radix2twiddles(n);

generate an instrumented mex function. use the -o option to specify the mex function name. use the -histogram option to compute histograms.

if you have a matlab® coder™ license, you can also add the -coder option. in this case,

buildinstrumentedmex testfft -o testfft_instrumented -args {x,coder.constant(w)} -histogram

if you have a matlab® coder™ license, you can also add the -coder option. for example,

buildinstrumentedmex testfft -coder -o testfft_instrumented -args {x,w}

like the fiaccel function, the buildinstrumentedmex function generates a mex function. to generate c code, use the matlab® coder™ codegen function.

run a test file to record instrumentation results. use the showinstrumentedmex function to open the report. to view the simulation minimum and maximum values and whole number status, pause over a variable in the report. you can also see proposed data types for double precision numbers in the table.

for i=1:20
   y = testfft_instrumented(randn(size(x)),w);
end
showinstrumentationresults testfft_instrumented

instrumentation-report-tb1.png

close the histogram display, then use the clearinstrumentationresults function to clear the results log.

clearinstrumentationresults testfft_instrumented

run a different test bench, then view the new instrumentation results.

for i=1:20
   y = testfft_instrumented(cast(rand(size(x))-0.5,'like',x),w);
end
showinstrumentationresults testfft_instrumented

instrumentation-report-tb2.png

to view the histogram for a variable, click the histogram icon in the variables tab.

numeric-type-scope-x-tb2.png

close the histogram display, then use the clearinstrumentationresults function to clear the results log.

clearinstrumentationresults testfft_instrumented

clear the mex function.

clear testfft_instrumented

this example shows how to create an instrumented mex function for multiple entry point functions. generating a mex function for multiple entry point functions using the buildinstrumentedmex function requires using the '-coder' option and a matlab® coder™ license.

section 1 title

this example uses the two entry-point functions, ep1 and ep2.

function y1 = ep1(u) %#codegen
y1 = u;
end
function y2 = ep2(u,v) %#codegen
y2 = u   v;
end

generate an instrumented mex function for the two entry-point functions. use the -o option to specify the name of the mex function. use the -histogram option to compute histograms. use the -coder option to enable generating multiple entry points with the buildinstrumentedmex function.

u = 1:100;
v = 5:104;
buildinstrumentedmex -o sharedmex ...
ep1 -args {u} ... % entry point 1
ep2 -args {u,v} ... % entry point 2
-histogram -coder
code generation successful.

use the generated mex function to call the first entry-point function.

y1 = sharedmex('ep1',u);

use the generated mex function to call the second entry-point function.

y2 = sharedmex('ep2',u,v);

show the instrumentation results.

showinstrumentationresults sharedmex

report-multiple-entry-point-functions.png

input arguments

matlab entry-point functions to be instrumented, specified as a function existing in the current working folder or on the path. the entry-point functions must be suitable for code generation. for more information, see (matlab coder).

choice of compiler options. buildinstrumentedmex gives precedence to individual command-line options over options specified using a configuration object. if command-line options conflict, the rightmost option prevails.

-args example_inputs

define the size, class, and complexity of all matlab function inputs. use the values in example_inputs to define these properties. example_inputs must be a cell array that specifies the same number and order of inputs as the matlab function.

-coder

use matlab coder software to compile the mex file, instead of the default fixed-point designer™ function. this option removes fiaccel restrictions and allows for full code generation support. you must have a matlab coder license to use this option.

-config config_object

specify mex generation parameters, based on config_object, defined as a matlab variable using coder.mexconfig. for example:

cfg = coder.mexconfig;
-d out_folder

store generated files in the absolute or relative path specified by out_folder. if the folder specified by out_folder does not exist, buildinstrumentedmex creates it for you.

if you do not specify the folder location, buildinstrumentedmex generates files in the default folder:

fiaccel/mex/fcn. 

fcn is the name of the matlab function specified at the command line.

the function does not support the following characters in folder names: asterisk (*), question-mark (?), dollar ($), and pound (#).

-g

compiles the mex function in debug mode, with optimization turned off. if not specified, buildinstrumentedmex generates the mex function in optimized mode.

-global global_values

specify initial values for global variables in matlab file. use the values in cell array global_values to initialize global variables in the function you compile. the cell array should provide the name and initial value of each global variable. you must initialize global variables before compiling with buildinstrumentedmex. if you do not provide initial values for global variables using the -global option, buildinstrumentedmex checks for the variable in the matlab global workspace. if you do not supply an initial value, buildinstrumentedmex generates an error.

the generated mex code and matlab each have their own copies of global data. to ensure consistency, you must synchronize their global data whenever the two interact. if you do not synchronize the data, their global variables might differ.

-histogramcompute the log2 histogram for all named, intermediate and expression values. a histogram column appears in the code generation report table.
-i include_path

add include_path to the beginning of the code generation path.

buildinstrumentedmex searches the code generation path first when converting matlab code to mex code.

-launchreport

generate and open a code generation report. if you do not specify this option, buildinstrumentedmex generates a report only if error or warning messages occur or you specify the -report option.

-o output_file_name

generate the mex function with the base name output_file_name plus a platform-specific extension.

output_file_name can be a file name or include an existing path.

if you do not specify an output file name, the base name is fcn_mex, which allows you to run the original matlab function and the mex function and compare the results.

-o optimization_option

optimize generated mex code, based on the value of optimization_option:

  • enable:inline — enable function inlining

  • disable:inline — disable function inlining

if not specified, buildinstrumentedmex uses inlining for optimization.

-report

generate a code generation report. if you do not specify this option, buildinstrumentedmex generates a report only if error or warning messages occur or you specify the -launchreport option.

tips

  • you cannot instrument matlab functions provided with the software. if your top-level function is such a matlab function, nothing is logged. you also cannot instrument scripts.

  • instrumentation results are accumulated every time the instrumented mex function is called. use clearinstrumentationresults to clear previous results in the log.

  • some coding patterns pass a significant amount of data, but only use a small portion of that data. in such cases, you may see degraded performance when using buildinstrumentedmex. in the following pattern, subfun only uses one element of input array, a. for normal execution, the amount of time to execute subfun once remains constant regardless of the size of a. the function topfun calls subfun n times, and thus the total time to execute topfun is proportional to n. when instrumented, however, the time to execute subfun once becomes proportional to n^2. this change occurs because the minimum and maximum data are calculated over the entire array. when a is large, the calculations can lead to significant performance degradation. therefore, whenever possible, you should pass only the data that the function actually needs.

    function a = topfun(a)
        n = numel(a);
        for i=1:n
            a(i) = subfun(a,i);
        end
    end
    function b = subfun(a,i)
        b = 0.5 * a(i);
    end
      
    function a = topfun(a)
        n = numel(a);
        for i=1:n
            a(i) = subfun(a(i));   
        end  
    end
    function b = subfun(a)
        b = 0.5 * a;
    end
    

version history

introduced in r2011b
网站地图