Main Content

coder.BLASCallback class

Package: coder

Abstract class for specifying the BLAS library and CBLAS header and data type information for BLAS calls in generated code

Description

coder.BLASCallback is an abstract class for defining a BLAS callback class. A BLAS callback class specifies the BLAS library and CBLAS header and data type information to use for BLAS calls in code generated from MATLAB® code. If you use MATLAB Coder™ to generate standalone code or generate code for the MATLAB Function block, for certain vector and matrix function calls, you can generate BLAS calls. To generate BLAS calls, set the appropriate configuration parameter to the name of the BLAS callback class.

  • For code generation by using the MATLAB Coder codegen command, set CustomBLASCallback.

  • For code generation by using the MATLAB Coder app, set Custom BLAS library callback.

  • For code generation for a MATLAB Function block by using Simulink® Coder, set Custom BLAS library callback.

To define a BLAS callback class with the name useMyBLAS, make the following line the first line of your class definition file.

classdef useMyBLAS < coder.BLASCallback
You must define the updateBuildInfo, getHeaderFileName, and getBLASIntTypeName methods. The other methods, getBLASDoubleComplexTypeName, getBLASSingleComplexTypeName, and useEnumNameRatherThanTypedef, are already implemented in coder.BLASCallback. In certain situations, you must override these methods with your own definitions when you define your callback class. All methods are static and are not compiled.

Class Attributes

Abstract
true

For information on class attributes, see Class Attributes.

Methods

expand all

Examples

collapse all

This example is an implementation of the BLAS callback class mklcallback for linking to the Intel MKL BLAS library on a Windows® platform. mklcallback does not include explicit implementations of getBLASDoubleComplexTypeName, getBLASSingleComplexTypeName, and useEnumNameRatherThanTypedef. It inherits these methods from coder.BLASCallback.

classdef mklcallback < coder.BLASCallback
    methods (Static)
        function updateBuildInfo(buildInfo, ~)
            libPath = fullfile(pwd,'mkl','WIN','lib','intel64');
            libPriority = '';
            libPreCompiled = true;
            libLinkOnly = true;
            libs = {'mkl_intel_ilp64.lib' 'mkl_intel_thread.lib' 'mkl_core.lib'};
            buildInfo.addLinkObjects(libs, libPath, libPriority, libPreCompiled, libLinkOnly);
            buildInfo.addLinkObjects('libiomp5md.lib',fullfile(matlabroot,'bin','win64'), ...
                libPriority, libPreCompiled, libLinkOnly);
            buildInfo.addIncludePaths(fullfile(pwd,'mkl','WIN','include'));
            buildInfo.addDefines('-DMKL_ILP64');
        end
        function headerName = getHeaderFilename()
            headerName = 'mkl_cblas.h';
        end
        function intTypeName = getBLASIntTypeName()
            intTypeName = 'MKL_INT';
        end
    end
end

Use this example class as a template for writing your own BLAS callback class.

If you are using a different BLAS library, replace 'mkl_cblas.h' with the name of your CBLAS header file.

If you are using a different BLAS library, replace 'MKL_INT' with the name of your CBLAS integer data type.

To update the build information in updateBuildInfo with the name and location of your BLAS library, use the build information addLinkObjects method. If you use the Intel MKL BLAS library, use the link line advisor to see which libraries and compiler options are recommended for your use case.

To update the build information in updateBuildInfo with the location of the CBLAS header files, use the build information addIncludePaths method.

To add preprocessor macro definitions to the build information in updateBuildInfo, use the build information addDefines method.

To specify the compiler options in updateBuildInfo, use the build information addCompileFlags method.

To specify the linker options in updateBuildInfo, use the build information addLinkFlags method.

The getBLASDoubleComplexTypeName method returns the type used for double-precision complex variables in the generated code. If your BLAS library takes a type other than double* and void* for double-precision complex array arguments, include this method in your callback class definition.

function doubleComplexTypeName = getBLASDoubleComplexTypeName()
doubleComplexTypeName = 'my_double_complex_type';
end

Replace my_double_complex_type with the type that your BLAS library takes for double-precision complex array arguments.

The getBLASSingleComplexTypeName method returns the type used for single-precision complex variables in the generated code. If your BLAS library takes a type other than float* and void* for single-precision complex array arguments, include this method in your callback class definition.

function singleComplexTypeName = getBLASSingleComplexTypeName()
doubleComplexTypeName = 'my_single_complex_type';
end

Replace my_single_complex_type with the type that your BLAS library takes for single-precision complex array arguments.

The useEnumNameRatherThanTypedef method returns false by default. If types for enumerations in your BLAS library include the enum keyword, redefine this method to return true in your callback class definition.

function p = useEnumNameRatherThanTypedef()
p = true;
end

This example is an implementation of the BLAS callback class openblascallback for linking to the OpenBLAS library on a Linux® platform. openblascallback does not have explicit implementations of getBLASDoubleComplexTypeName, getBLASSingleComplexTypeName, and useEnumNameRatherThanTypedef. It inherits these methods from coder.BLASCallback.

classdef openblascallback < coder.BLASCallback
    methods (Static)
        function updateBuildInfo(buildInfo, buildctx)
            libPriority = '';
            libPreCompiled = true;
            libLinkOnly = true;
            libName = 'libopenblas.a';          
            libPath = fullfile(pwd,'openblas');
            incPath = fullfile(pwd,'openblas');
            buildInfo.addLinkFlags('-lpthread');
            buildInfo.addLinkObjects(libName, libPath, ...
                libPriority, libPreCompiled, libLinkOnly);
            buildInfo.addIncludePaths(incPath);
        end
        function headerName = getHeaderFilename()
            headerName = 'cblas.h';
        end
        function intTypeName = getBLASIntTypeName()
            intTypeName = 'blasint';
        end
    end
end

If you generate C++ code that includes calls to OpenBLAS library functions, compiling it with the -pedantic option produces warnings. To disable the -pedantic compiler option, in the updateBuildInfo method, include these lines:

if buildctx.getTargetLang() == 'C++'
    buildInfo.addCompileFlags('-Wno-pedantic');
end

OpenBLAS does not support the C89/C90 standard.

Introduced in R2018b