verify correctness of the generated code
after you generate code, gpu coder™ provides you multiple options to inspect the source code and test the correctness of the generated code.
the code generation report provides an interactive interface for inspecting the generated cuda® source files, generated data types, and other code insights. for more information, see .
verify generated mex functions in the gpu coder app.
verify generated mex functions at the command line.
with the matlab® coder™ support package for nvidia® jetson and nvidia drive platforms, you can use the processor-in-the-loop (pil) execution to check the numerical behavior of the cuda code that you generate from matlab functions. for more information, see (matlab coder support package for nvidia jetson and nvidia drive platforms) and (matlab coder support package for nvidia jetson and nvidia drive platforms).
if you have embedded coder®, you can verify the numerical behavior of the generated cuda code by using software-in-the-loop (sil) execution.
note
gpu coder does not support collecting code coverage metrics during software-in-the-loop (sil) and processor-in-the-loop (pil) simulations.
verify mex functions in the gpu coder app
in the gpu coder app, after you generate a mex function, you can verify that it has the same functionality as the original matlab entry-point function. provide a test file that generates test vectors for the function under test and then calls the original matlab entry-point function. the test file can be a matlab function or script but must be in the same folder as the original entry-point function.
on the generate code page of the gpu coder app, click verify code.
type or select the test file. for example,
myfunction_test
.to run the test file without replacing calls to the original matlab function with calls to the mex function, select matlab code for the run using option. click run generated code.
to run the test file, replacing calls to the original matlab function with calls to the mex function, select generated code for the run using option. click run generated code.
compare the results of running the original matlab function with the results of running the generated cuda mex function.
verify mex functions at the command line
you can verify the generated cuda mex file at the command line by using the function. the
coder.runtest
function runs the test file by replacing calls
to the original matlab function with calls to the mex function. for example, to test
myfunction
function with myfunction_test
test file, enter the following code in the matlab command
window.
coder.runtest('myfunction_test','myfunction')
compare the results with the results of running the original matlab function. if errors occur during the run, call stack information is available for debugging. alternatively, you can use codegen
with the -test
option.
codegen myfunction -test'myfunction_test'
the test file can be a matlab function, script, or class-based unit test.
code verification through software-in-the-loop
gpu coder supports software-in-the-loop (sil) execution, which enables you to verify source code and compiled object code. during sil execution through a matlab sil interface, the software compiles and using the test vectors that you provide, runs library code on your development computer. you can reuse test vectors developed for your matlab functions to verify the numerical behavior of library code.
note
on a microsoft® windows® system, the windows firewall can potentially block a sil execution. change the windows firewall settings to allow access.
when using sil execution, make sure that the
benchmarking
option in gpu coder settings isfalse
. executing sil with benchmarking results in compilation errors.
sil execution with the gpu coder app
the software-in-the-loop (sil) execution is supported only for static and dynamic library output types. if you generated a mex function, you must change the project settings to use static library
or dynamic library
for build type and run generate code again.
to open the gpu coder app, on the matlab toolstrip apps tab, under code generation, click the app icon.
to open your project, click , and then click
open existing project
. select the project,myproject.prj
. on the generate code page, click the generate arrow .in the generate dialog box:
set build type to
static library
ordynamic library
.clear the generate code only check box.
you can leave the other settings to their default values.
to generate the cuda code, click generate. click verify code.
in the command field, specify the test file (for example,
myfunction_test.m
) that calls the original matlab functions (for example,myfunction
).to start the sil execution, click run generated code. the gpu coder app:
generates a standalone library in
codegen\lib\myfunction
.generates sil interface code in
codegen\lib\myfunction\sil
.runs the test file, replacing calls to the matlab function with calls to the generated code in the library.
displays messages from the sil execution in the test output tab.
verify that the results from the sil execution match the results from the original matlab functions.
to terminate the sil execution process, click stop sil verification. alternatively, on the test output tab, click the link that follows
to terminate execution
.
sil execution from command line
to set up and start a sil execution from the command line, you create a gpu coder configuration object for library code generation, enable config_obj.verificationmode = 'sil'
, use codegen
function to generate the library code and the sil interface, and use coder.runtest
function to run the test file for your original matlab function. the following is a build script that automates the series of commands to perform sil execution.
%% create configuration object for static library. config = coder.gpuconfig('lib'); config.generatereport = true; config.verificationmode = 'sil'; %% define argument types for entry-point 'mandelbrot_count'. args = cell(1,1); args{1} = cell(3,1); args{1}{1} = coder.typeof(0); args{1}{2} = coder.typeof(0,[1000 1000]); args{1}{3} = coder.typeof(0,[1000 1000]); %% invoke gpu coder. codegen -config config myfunction -args args{1} %% run the test file with the sil interface coder.runtest('myfunction_test', ['myfunction_sil.' mexext]); %% terminate sil execution clear myfunction_sil;
numerical differences between cpu and gpu
because of architectural differences between the cpu and gpu, numerical verification does not
always match. this scenario is specially true when using single
data type in your matlab code and performing accumulation operations on these
single
data type values. however, there are cases like the
mandelbrot example where even double
data types cause numerical
errors. one reason for this mismatch is that the gpu floating-point units use fused
floating-point multiply-add (fmad) instructions while the cpu does not use these
instructions. it is also important to note that the cuda compiler performs these instruction-level optimizations by default
impacting the accuracy of the computed results. for example, the cuda compiler fuses floating-point multiply and add instructions into a
single instruction. this floating-point multiply-add (fmad) operation executes twice
as fast compared to two single instructions but results in the loss of numerical
accuracy. you can achieve tighter control over these optimizations by using
intrinsic functions and compiler flags. to set compiler flags, see . to integrate cuda intrinsics, see call custom cuda device function from the generated code.
see also
apps
functions
codegen
| | | |
objects
- | |
related topics
- (matlab coder support package for nvidia jetson and nvidia drive platforms)
- (matlab coder support package for nvidia jetson and nvidia drive platforms)