我怎么跟OpenBLAS例程生成代码吗in MATLAB Coder on Linux?

17 views (last 30 days)
I am running MATLAB on Linux, and I have a MATLAB function, simplefn, shown below:
功能[outputArg1] = simplefn(inputArg1,inputArg2)
outputArg1 = inputArg1 * inputArg2;
end
当我为此功能生成代码时,我将代码如下所示:
空白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支万博1manbetx持团队
编辑:Mathworks支万博1manbetx持团队 2021年1月13日
To generate code for 'simplefn' using OpenBLAS routines, first define a MATLAB class openblascallback.m as shown below (adapted from the example in the documentation below). Note that the library shared object and header locations, as well as the header name, depend on the Operating System, and specific BLAS library or package installed:
classdefopenblascallback < coder.BLASCallback
方法(静止的)
功能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
功能标题=getHeaderFilename()
标题='cblas.h';
end
功能inttypename = getBlasintTypename()
inttypename ='blasint';
end
end
end
然后创建以下代码生成脚本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=代码r.config('lib','ecoder',false);
cfg.GenerateReport=真的;
CFG. -Toolchain='GNU GCC/G ++ |Gmake(64位Linux)';
cfg.CustomBLASCallback=“ OpenBlascallback”;
%%调用MATLAB编码器。
代码gen-configcfgsimplefn-args{zeros(1000),zeros(1000)}-报告
Running simplefn_script.m should successfully generate code with OpenBLAS routines. The simplefn.c now contains:
空白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);
}
2 Comments
瑞安·利文斯顿
瑞安·利文斯顿 on 10 Jan 2021
Using BLAS via the CustomBLASCallback 属性不需要嵌入式编码器。您可以更改脚本以适应:
cfg = coder.config('lib','ecoder',错误的);
cfg.GenerateReport = true;
cfg.toolchain ='GNU GCC/G ++ |Gmake(64位Linux)';
cfg.CustomBLASCallback =“ OpenBlascallback”;
查看编码器配置设置是否需要嵌入式编码器的一种简单方法是查看它是否存在于 Coder.CodeConfig class
或仅在 代码r.EmbeddedCodeConfig class
CustomBLASCallback is in the former, so it does not require Embedded Coder.

Sign in to comment.

更多的Answers (0)