Speed Up Linear Algebra in Code Generated from a MATLAB Function Block - MATLAB & Simulink (original) (raw)
To improve the execution speed of code generated for certain linear algebra functions in a MATLAB Function block, specify that the code generator produce LAPACK calls. LAPACK is a software library for numerical linear algebra. See netlib.org/lapack. The code generator uses the LAPACKE C interface to LAPACK. See netlib.org/lapack/#_standard_c_language_apis_for_lapack. If you specify that you want to generate LAPACK calls, and the input arrays for the linear algebra functions meet certain criteria, the code generator produces the LAPACK calls. Otherwise, the code generator produces code for the linear algebra functions.
MathWorks® provides a collection of BLAS and LAPACK callback classes for various platforms. You can download these callback classes from this GitHub repository.
For more information on how to use these callbacks to generate code, see Speed Up of Standalone Generated Code Using Preconfigured BLAS and LAPACK Callbacks.
Specify LAPACK Library
To generate LAPACK calls, you must have access to a LAPACK callback class. A LAPACK callback class specifies the LAPACK library and LAPACKE header file for the LAPACK calls. To indicate that you want to generate LAPACK calls and that you want to use a specific LAPACK library, specify the name of the LAPACK callback class. In the Configuration Parameters dialog box, set Custom LAPACK library callback to the name of the callback class, for example, useMyLAPACK
.
Write LAPACK Callback Class
To specify the locations of a particular LAPACK library and LAPACKE header file, write a LAPACK callback class. Share the callback class with others who want to use this LAPACK library for LAPACK calls in generated code.
The callback class must derive from the abstract class coder.LAPACKCallback. Use this example callback class as a template.
classdef useMyLAPACK < coder.LAPACKCallback methods (Static) function hn = getHeaderFilename() hn = 'mylapacke_custom.h'; end function updateBuildInfo(buildInfo, buildctx) buildInfo.addIncludePaths(fullfile(pwd,'include')); libName = 'mylapack'; libPath = fullfile(pwd,'lib'); [~,linkLibExt] = buildctx.getStdLibInfo(); buildInfo.addLinkObjects([libName linkLibExt], libPath, ... '', true, true); buildInfo.addDefines('HAVE_LAPACK_CONFIG_H'); buildInfo.addDefines('LAPACK_COMPLEX_STRUCTURE'); buildInfo.addDefines('LAPACK_ILP64'); end end end
You must provide the getHeaderFilename
andupdateBuildInfo
methods. ThegetHeaderFilename
method returns the LAPACKE header file name. In the example callback class, replacemylapacke_custom.h
with the name of your LAPACKE header file. The updateBuildInfo
method provides the information required for the build process to link to the LAPACK library. Use code like the code in the template to specify the location of header files and the full path name of the LAPACK library. In the example callback class, replacemylapack
with the name of your LAPACK library.
If your compiler supports only complex data types that are represented as structures, include these lines in theupdateBuildInfo
method.
buildInfo.addDefines('HAVE_LAPACK_CONFIG_H'); buildInfo.addDefines('LAPACK_COMPLEX_STRUCTURE');
You must specify the integer type that your LAPACK library uses. Not specifying this integer type can result in incorrect behaviors or crashes. Do one of the following:
- Include these lines in the
updateBuildInfo
method.
buildInfo.addDefines('HAVE_LAPACK_CONFIG_H');
buildInfo.addDefines('LAPACK_ILP64'); - Alternatively, you can directly specify the integer type that your LAPACK library uses. For example, if the integer type is
long long
, include this line in theupdateBuildInfo
method.
buildInfo.addDefines('lapack_int=long long');
Generate LAPACK Calls by Specifying a LAPACK Callback Class
This example shows how to generate code that calls LAPACK functions in a specific LAPACK library. For this example, assume that the LAPACK callback class useMyLAPACK
specifies the LAPACK library that you want.
- Create a Simulink® model.
- Add a MATLAB Function block to the model.
- In the MATLAB Function block, add code that calls a linear algebra function. For example, add the function
mysvd
that calls the MATLAB® functionsvd
.
function s = mysvd(A)
%#codegen
s = svd(A);
end - Add a Constant block to the left of theMATLAB Function block. Set the value to
zeros(500)
. - Add an Outport block to the right of the MATLAB Function block.
- Connect the blocks.
- Set the > > > parameter to
useMyLAPACK
.
The callback class must be on the MATLAB path. - Build the model.
If the input to mysvd
is large enough, the code generator produces a LAPACK call for svd
. An example of a call to the LAPACK library function forsvd
is:
info_t = LAPACKE_dgesvd( ... LAPACK_COL_MAJOR, 'N', 'N', (lapack_int)500, ... (lapack_int)500, &A[0], (lapack_int)500, &S[0], ... NULL, (lapack_int)1, NULL,(lapack_int)1, &superb[0]);
Locate LAPACK Library in Execution Environment
The LAPACK library must be available in your execution environment. If your LAPACK library is shared, use environment variables or linker options to specify the location of the LAPACK library.
- On a Windows® platform, modify the PATH environment variable.
- On a Linux® platform, modify the LD_LIBRARY_PATH environment variable or use the
rpath
linker option. - On a macOS platform, modify the DYLD_LIBRARY_PATH environment variable or use the
rpath
linker option.
To specify the rpath
linker option, you can use the build informationaddLinkFlags
method in theupdateBuildInfo
method of your coder.LAPACKCallback class. For example, for a GCC compiler:
buildInfo.addLinkFlags(sprintf('-Wl,-rpath,"%s"',libPath));