Speed Up of Standalone Generated Code Using Preconfigured BLAS and LAPACK Callbacks - MATLAB & Simulink (original) (raw)

Main Content

To improve the execution speed of standalone code that performs vector and matrix operations or linear algebra functions, you can use BLAS and LAPACK libraries withMATLAB® Coder™. You can integrate these libraries in the code generation process using the coder configuration options CustomBLASCallback andCustomLAPACKCallback. This example shows how use the BLAS and LAPACK libraries present in this GitHub repository and generate standalone code.

Add GitHub Repository to MATLAB Path

MathWorks provides a collection of callback libraries for different platforms in the GitHub repository.

To clone the GitHub® repository use this command at the system command prompt.

git clone https://github.com/mathworks/MATLAB-Coder-integration-for-BLAS-LAPACK

Alternatively, download this ZIP file and unzip it at your desired location.

To use the callback classes with MATLAB Coder, add the folder containing the callback classes to the MATLAB path using addpath function. Specify the path where you downloaded the repository.

addpath("C:\Users\username\Downloads\MATLAB-Coder-integration-for-BLAS-LAPACK-main");

Specify Callback Classes for Code Generation

To use the predefined callback classes from the GitHub repository, create a coder configuration object and assign the configuration options CustomBLASCallback andCustomLAPACKCallback with the appropriate class names. Choose the callback class based on the BLAS and LAPACK library installed on your system. These callback classes will operate without additional modifications if the library installation paths align with those specified in the callback classes. For more information, see README included in the repository.

For example, to create a configuration using the Intel® oneMKL BLAS and LAPACK callback classes on Windows®, use these commands.

cfg1 = coder.config("lib"); cfg1.CustomBLASCallback = "oneMKL.windows.OpenMPBLASCallback"; cfg1.CustomLAPACKCallback = "oneMKL.windows.OpenMPLAPACKCallback";

Generate Code with BLAS and LAPACK Callbacks

This example shows how to use the callback classes from the repository for code generation using the MATLAB function conv2Wrapper.m. This function performs a two-dimensional convolution on the input matrices and returns the result in the out matrix.

function out = conv2Wrapper(in, ft) out = conv2(in, ft); end

After specifying the callback classes, define the inputs and generate the code in the MATLAB Command Window.

rng(11, 'twister'); A = rand(2000, 2000); B = rand(30, 30);

codegen conv2Wrapper -args {A,B} -config cfg1

Code Generation Successful

Compare Performance

Compare the performance of the generated code using coder.timeit with and without using callback classes.

Run these commands to get the run times of the code generated withcfg1 and cfg2. The output shows the results of running the code on a 6-core, 12-thread Intel Xeon® W-2133 CPU running Windows 11.

%% Time the configurations cfg2 = coder.config('lib');

tWoCB = coder.timeit('conv2Wrapper', 1, {A, B},'CoderConfig', cfg2, 'CompileArgs', {coder.typeof(1, [Inf, Inf]), coder.typeof(1, [Inf, Inf])}); tCB = coder.timeit('conv2Wrapper', 1, {A, B},'CoderConfig', cfg1, 'CompileArgs', {coder.typeof(1, [Inf, Inf]), coder.typeof(1, [Inf, Inf])});

f = @() conv2(A, B); tML = timeit(f);

fprintf("Time taken to run MATLAB code: %f\n", tML); fprintf("Time taken to run generated code without callbacks: %f\n", tWoCB); fprintf("Time taken to run generated code with MKL callbacks: %f\n", tCB);

Statistical Overview: mean = 3.05e-01 s max = 3.17e-01 s sd = 6.19e-03 s median = 3.03e-01 s min = 2.95e-01 s 90th = 3.13e-01 s

Statistical Overview: mean = 1.18e-01 s max = 1.41e-01 s sd = 8.28e-03 s median = 1.17e-01 s min = 1.11e-01 s 90th = 1.29e-01 s

Time taken to run MATLAB code: 0.154085 Time taken to run generated code without callbacks: 0.303492 Time taken to run generated code with MKL callbacks: 0.116558

The code generated with callback classes performs fastest when compared with MATLAB code and code generated without callbacks.

See Also

coder.LAPACKCallback | coder.BLASCallback

Topics