Code generation from matlab function using my own BLAS function

codegen is by default replacing multiplication with cblas_sgemm though the header file have fun definition with different name.
What is the correct approach. I want to replace maths operators with BLAS?
classdef ifxcallback < coder.BLASCallback
methods (Static)
function updateBuildInfo(buildInfo, ~)
buildInfo.addSourceFiles(fullfile(pwd,'BLAS','src.c'))
buildInfo.addIncludePaths(fullfile(pwd,'BLAS'));
end
function headerName = getHeaderFilename()
headerName = 'inc.h';
end
function intTypeName = getBLASIntTypeName()
intTypeName = 'int';
end
end
end
function p = useEnumNameRatherThanTypedef()
p = true;
end

12 Comments

MATLAB Coder expects to call a BLAS library with a CBLAS interface. Which BLAS library are you using and what is the name of the cblas_sgemm analog in that library?
@Ryan LivingstonI have created dummy BLAS Library and the name analogus to cblas_sgemm is cblas_sgemm_f32.
classdef ifxBLAS < coder.BLASCallback
methods (Static)
function updateBuildInfo(buildInfo, ~)
libPath = fullfile(pwd,'BLAS');
libPriority = '';
libPreCompiled = true;
libLinkOnly = true;
libs = {'src.lib'};
buildInfo.addLinkObjects(libs, libPath, libPriority, libPreCompiled, libLinkOnly);
buildInfo.addSourceFiles(fullfile(pwd,'BLAS','src.c'))
buildInfo.addIncludePaths(fullfile(pwd,'BLAS'));
end
function headerName = getHeaderFilename()
headerName = 'inc.h';
end
function intTypeName = getBLASIntTypeName()
intTypeName = 'int';
end
end
end
Ah. Coder isn't going to find the updated name as it is expecting to find a standard CBLAS library which doesn't contain cblas_sgemm_f32. Can you update the name in your library to be cblas_sgemm or add a wrapper to provide the standard CBLAS interface:
http://www.netlib.org/blas/cblas.h
@Ryan Livingston My library have definition for matrix-vector multiplication(cblas-sgemv) but my matlab function of matrix-vector multiplication is getting encoded to cblas-sgemm.
Can you guide me with this?
function E = func(A,B,C) %A,B are matrices and C is a vector
%Matrix mul
D = A*B;
E = D*C;
end
MATLAB Coder may decide to use either cblas_sgemv or cblas_sgemm (I think it currently favors _sgemm) so the BLAS library would need to have both routines available.
@Ryan Livingston Hi Ryan. Can you provide the list of BLAS functions used by the code generator? we need this list for development of a BLAS library, we will prioritize development of those functions used by matlab coder and develop the rest later.
We don't have an explicit list anywhere. The code that produces the BLAS calls is all under:
fullfile(matlabroot,'toolbox','eml','eml','+coder','+internal','+blas')
You'll see a bunch of MATLAB functions named similar to BLAS functions. Inside each you'll find the calls being used:
% Select BLAS function.
if isreal(C)
if isa(C, 'single')
fun = 'sgemm';
else
fun = 'dgemm';
end
else
if isa(C, 'single')
fun = 'cgemm';
else
fun = 'zgemm';
end
end
So using that, you should be able to extract the list.
Can I ask why you're developing the BLAS library and what hardware/target it's for? My expectation was that most hardware would have an existing BLAS library so I'm interested to hear what your team is working on.
@Ryan Livingston Thank you Ryan for the clarification.
We are developing library for a vector processor heavily optimized for performance using special language extensions supported by processor compiler.
Can you please confirm that the BLAS functions listed under fullfile(matlabroot,'toolbox','eml','eml','+coder','+internal','+blas') are the only ones that are today called by the matlab coder? And when support for a new BLAS function is introduced, one may expect to find corresponding .m file of the new function in this very folder?
Good to know. Thanks for the info.
Those are the BLAS functions being called via the CustomBLASCallback interface as of right now. I cannot confirm that if a new one is introduced it will also appear there. Being an internal directory, we assume that we're free to refactor at will. So that code may be moved, split, etc. in future releases.
If it would be helpful for you to have a curated list, I can put in an internal request to see if the teams would consider documenting such a list in the future. I can't promise it will be agreed to, but we could at least ask.
@Ryan Livingston Thank you very much for the proposal to raise an internal request. I, like many others are certain to find the curated list extraordinarily helpful. It helps prioritizing development activities.
Thanks a lot, again.
@Ryan Livingston Assuming that a sequence of mathematical operation have been described using simulink operators (e.g. multiply, add, transpose), it should be possible to generate either a BLAS call or perhaps a call to a function defined in the code replacement library. In a specific case such as matrix multiply, it should be possible for code generator to either generate cblas_sgemm() or crl_matrix_multiply(). Given availability of two alternative definitions, is it possible to prioritize one code replacement style over other?
For e.g. can i get code for operator matrix multiplication mapped to BLAS function while getting the code for the matrix transpose operator mapped to function defined in code replacement library?

Sign in to comment.

Answers (0)

Products

Release

R2020b

Asked:

on 25 Mar 2021

Edited:

on 21 Apr 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!