trace between generated cuda code and matlab source code
this example shows how to trace (highlight sections) between matlab® source code and the generated cuda® code. tracing between source code and generated code helps you to:
understand how the code generator maps your algorithm to gpu kernels.
debug issues in the generated code.
evaluate the quality of the generated code.
you can trace by using one of these methods:
configure gpu coder™ to generate code that includes the matlab source code as comments. in the comments, a traceability tag immediately precedes each line of source code. the traceability tag provides details about the location of the source code. if you have embedded coder®, in the code generation report, the traceability tags link to the corresponding matlab source code.
with embedded coder, produce a code generation report that includes interactive traceability. interactive tracing in the report helps you to visualize the mapping between the matlab source code and the generated c/c code. see (embedded coder).
generate traceability tags
create the matlab source code
to illustrate traceability tags, this example uses an implementation of the mandelbrot set by using standard matlab commands running on the cpu. this implementation is based on the code provided in the e-book by cleve moler.
the mandelbrot set is the region in the complex plane consisting of the values z0 for which the trajectories defined by this equation remain bounded at k→∞.
create a matlab function called mandelbrot_count.m
with the following
lines of code. this code is a vectorized matlab implementation of the mandelbrot set. for every point
(xgrid,ygrid)
in the grid, it calculates the iteration index
count
at which the trajectory defined by the equation reaches a
distance of 2
from the origin. it then returns the natural logarithm of
count
, which is used generate the color coded plot of the mandelbrot
set.
function count = mandelbrot_count(maxiterations,xgrid,ygrid) % add kernelfun pragma to trigger kernel creation coder.gpu.kernelfun; % mandelbrot computation z0 = xgrid 1i*ygrid; count = ones(size(z0)); z = z0; for n = 0:maxiterations z = z.*z z0; inside = abs(z)<=2; count = count inside; end count = log(count);
create test vectors
create test vectors for the entry-point function by using the following lines of code.
the script generates a 1000 x 1000 grid of real parts (x) and imaginary
parts (y) between the limits specified by xlim
and
ylim
. you can use these inputs to validate the
mandelbrot_count
entry-point function and plots the resulting
mandelbrot set.
maxiterations = 500; gridsize = 1000; xlim = [-0.748766713922161,-0.748766707771757]; ylim = [0.123640844894862,0.123640851045266]; x = linspace(xlim(1),xlim(2),gridsize); y = linspace(ylim(1),ylim(2),gridsize); [xgrid,ygrid] = meshgrid(x,y);
generate traceability tags
to produce traceability tags in the generated code, enable generation of matlab source code as comments.
in the gpu coder app, set matlab source code as comments to
yes
.in a code generation configuration object, create a object and set the
matlabsourcecomments
property totrue
.cfg = coder.gpuconfig('dll','ecoder',true); cfg.generatereport = true; cfg.matlabsourcecomments = true; cfg.gpuconfig.compilerflags = '--fmad=false'; codegen -config cfg -args {maxiterations,xgrid,ygrid} mandelbrot_count
note
the
--fmad=false
flag when passed to thenvcc
, instructs the compiler to disable floating-point multiply-add (fmad) optimization. this option is set to prevent numerical mismatch in the generated code because of architectural differences in the cpu and the gpu. for more information, see numerical differences between cpu and gpu.
access the report
to open the code generation report, click view report.
the code generation report is named report.mldatx
. it is located in
the html
subfolder of the code generation output folder. if you have
matlab r2018a or later, you can open the report.mldatx
file by
double-clicking it.
in the matlab source pane, select
mandelbrot_count.m
. you see the matlab source code in the code pane.
the green gpu marker next to mandelbrot_count
function indicates that the generated code has both cpu and gpu sections. the green
vertical bar indicates the lines of code that are mapped to the gpu. to see information
about the type of a variable or expression and the name of the corresponding gpu
kernel function, pause over the variable or expression. when you select
highlighted code by clicking it, the code becomes blue and you can see the information
even when you move your pointer away from the selection. the code remains selected until
you press esc
or select different
code.
to view the cuda code generated for the mandelbrot_count.m
entry-point
function, select mandelbrot_count.cu
from the generated
code pane.
format of traceability tags
in the generated code, traceability tags appear immediately before the matlab source code in the comment. the format of the tag is:
.
for example, this comment indicates that the code z0 = xgrid
1i*ygrid;
appears at line 5
in the source file
mandelbrot_count.m
.
/* 'mandelbrot_count:5' z0 = xgrid 1i*ygrid;
traceability tag limitations
you cannot include matlab source code as comments for:
mathworks® toolbox functions
p-code
the appearance or location of comments can vary:
even if the implementation code is eliminated, for example, due to constant folding, comments can still appear in the generated code.
if a complete function or code block is eliminated, comments can be eliminated from the generated code.
for certain optimizations, the comments can be separated from the generated code.
even if you do not choose to include source code comments in the generated code, the generated code includes legally required comments from the matlab source code.
functions with multiple outputs do not get highlighted.
calls to
coder
functions such as will not be highlightedcode that gets mapped to library calls such as cudnn, cublas and cufft will not be highlighted. as a result, functions that are completely mapped to gpu may be tagged incorrectly.
see also
codegen
| | |