You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Code generation from matlab function using my own BLAS function
2 views (last 30 days)
Show older comments
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
Ryan Livingston
on 26 Mar 2021
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?
anamika kumari
on 26 Mar 2021
Edited: anamika kumari
on 26 Mar 2021
@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
Ryan Livingston
on 26 Mar 2021
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
anamika kumari
on 30 Mar 2021
@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
Ryan Livingston
on 30 Mar 2021
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.
anamika kumari
on 31 Mar 2021
Edited: anamika kumari
on 31 Mar 2021
@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.
Ryan Livingston
on 1 Apr 2021
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.
anamika kumari
on 5 Apr 2021
@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?
Ryan Livingston
on 5 Apr 2021
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.
anamika kumari
on 6 Apr 2021
@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.
anamika kumari
on 20 Apr 2021
Edited: anamika kumari
on 21 Apr 2021
@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?
Answers (0)
See Also
Categories
Find more on Get Started with GPU Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)