How do I generate code with OpenBLAS routines in MATLAB Coder on Linux?
조회 수: 7 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2020년 12월 9일
편집: MathWorks Support Team
2025년 2월 3일
I am running MATLAB on Linux, and I have a MATLAB function, simplefn, shown below:
function [outputArg1] = simplefn(inputArg1,inputArg2)
outputArg1 = inputArg1 * inputArg2;
end
When I generate code for this function, I see code as follows in simplefn.c:
void simplefn(const double inputArg1[1000000], const double inputArg2[1000000],
double outputArg1[1000000])
{
double d;
int i;
int i1;
int i2;
for (i = 0; i < 1000; i++) {
for (i1 = 0; i1 < 1000; i1++) {
d = 0.0;
for (i2 = 0; i2 < 1000; i2++) {
d += inputArg1[i + 1000 * i2] * inputArg2[i2 + 1000 * i1];
}
outputArg1[i + 1000 * i1] = d;
}
}
}
However, I want the generated code to use OpenBLAS routines. How do I accomplish this?
채택된 답변
MathWorks Support Team
2025년 1월 24일
편집: MathWorks Support Team
2025년 2월 3일
To generate code for 'simplefn' using OpenBLAS routines, first define a MATLAB class openblascallback.m as shown below. This example is adapted from the documentation. Note that the library shared object and header locations, as well as the header name, depend on the Operating System and the specific BLAS library or package installed. For detailed instructions, you can access the release-specific documentation by executing the following command in the MATLAB R2020b command window:
>> web(fullfile(docroot, 'coder/ref/coder.blascallback-class.html'))
classdef openblascallback < coder.BLASCallback
methods (Static)
function updateBuildInfo(buildInfo, buildctx)
libPriority = '';
libPreCompiled = true;
libLinkOnly = true;
libName = 'libopenblas.so';
libPath = fullfile('/usr/lib/x86_64-linux-gnu');
incPath = fullfile('/usr/include/x86_64-linux-gnu');
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
Then create the following Code Generation script simplefn_script.m:
% SIMPLEFN_SCRIPT Generate static library simplefn from simplefn.
%
% Script generated from project 'simplefn.prj' on 04-Dec-2020.
%
% See also CODER, CODER.CONFIG, CODER.TYPEOF, CODEGEN.
%% Create configuration object of class 'coder.EmbeddedCodeConfig'.
cfg = coder.config('lib', 'ecoder', false);
cfg.GenerateReport = true;
cfg.Toolchain = 'GNU gcc/g++ | gmake (64-bit Linux)';
cfg.CustomBLASCallback = 'openblascallback';
%% Invoke MATLAB Coder.
codegen -config cfg simplefn -args {zeros(1000),zeros(1000)} -report
Running simplefn_script.m should successfully generate code with OpenBLAS routines. The simplefn.c now contains:
void simplefn(const double inputArg1[1000000], const double inputArg2[1000000],
double outputArg1[1000000])
{
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, (blasint)1000, (blasint)
1000, (blasint)1000, 1.0, &inputArg1[0], (blasint)1000,
&inputArg2[0], (blasint)1000, 0.0, &outputArg1[0], (blasint)1000);
}
Please follow the link below to search for the required information regarding the current release:
댓글 수: 2
Ryan Livingston
2021년 1월 10일
편집: Ryan Livingston
2021년 1월 10일
Using BLAS via the CustomBLASCallback property does not require Embedded Coder. You can change the script to accommodate that:
cfg = coder.config('lib','ecoder',false);
cfg.GenerateReport = true;
cfg.Toolchain = 'GNU gcc/g++ | gmake (64-bit Linux)';
cfg.CustomBLASCallback = 'openblascallback';
A simple way to see if a Coder config setting requires Embedded Coder is to see if it present in the coder.CodeConfig class
or only in the coder.EmbeddedCodeConfig class
CustomBLASCallback is in the former, so it does not require Embedded Coder.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Execution Speed에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!