Code generation with Lapacke Class doesn't generate Laplack function
Show older comments
Hello,
I try to create C/C++ code with a Lapack function. I followed this instruction to setup my main-function like:
function i = main()
rng(4);
A = rand(2600,2600);
tic
s = svd(A);
toc
tic
[X_mat,lambda] = eig(A);
toc
fprintf('%f : svd\n', s(1));
fprintf('%f : eigX\n', X_mat(1));
fprintf('%f : eigL\n', lambda(1));
i = int32(0); %return 0
end
Then creating the respective Class file:
classdef LapackClass < coder.LAPACKCallback
methods (Static)
function hn = getHeaderFilename()
hn = 'lapacke.h';
end
function updateBuildInfo(buildInfo, buildctx)
buildInfo.addIncludePaths(fullfile(pwd,'include'));
libName = 'liblapack';
libPath = fullfile(pwd,'lib');
[~,linkLibExt] = buildctx.getStdLibInfo();
buildInfo.addLinkObjects([libName linkLibExt], libPath, '', true, true);
buildInfo.addDefines('HAVE_LAPACK_CONFIG_H');
buildInfo.addDefines('LAPACK_COMPLEX_STRUCTURE');
buildInfo.addDefines('LAPACK_ILP64');
end
end
end
Then putting the header files und lib files in respective folders of the projekt folder like:

and finally setting the Custom callback in the coders advanced settings like:

I figured this is the equivament of the 3) Specify the LAPACK callback class useMyLAPACK in the instructions,
I then hit Generate, but in the resulting main.cpp, there is no different function call to the Lapack lib and in the make-file I couldn't find any trace of that either.
...
double s[2600];
state[624] = 624U;
coder::b_rand(A);
coder::tic();
coder::svd(A, s);
coder::toc();
...
Can someone hint me to what I need to change to make Matlab generate the code with Lapack call please?
2 Comments
Ryan Livingston
on 17 Oct 2022
Edited: Ryan Livingston
on 17 Oct 2022
The steps you've followed should produce LAPACK calls. Can you check the contents of the coder::svd and coder::eig functions to see if there are references to any LAPACKE_*functions? There should also be a reference to lapacke.h in those files. Note there generally will not be a change in main.cpp. The changes will be in the implementations of svd and eig.
I tried the code you showed and see the following
svd.cpp
// Include Files
#include "svd.h"
#include "rt_nonfinite.h"
#include "lapacke.h"
#include <algorithm>
// Function Definitions
//
// Arguments : const double A[6760000]
// double U[2600]
// Return Type : void
//
namespace coder {
void svd(const double A[6760000], double U[2600])
{
static double b_A[6760000];
lapack_int info_t;
double superb[2599];
std::copy(&A[0], &A[6760000], &b_A[0]);
info_t = LAPACKE_dgesvd(LAPACK_COL_MAJOR, 'N', 'N', (lapack_int)2600,
(lapack_int)2600, &b_A[0], (lapack_int)2600, &U[0],
nullptr, (lapack_int)1, nullptr, (lapack_int)1,
&superb[0]);
xgeev.cpp
// Include Files
#include "xgeev.h"
#include "rt_nonfinite.h"
#include "lapacke.h"
#include <algorithm>
// Function Definitions
//
// Arguments : const double A[6760000]
// int *info
// creal_T W[2600]
// creal_T VR[6760000]
// Return Type : void
//
namespace coder {
namespace internal {
namespace lapack {
void xgeev(const double A[6760000], int *info, creal_T W[2600],
creal_T VR[6760000])
{
static double b_A[6760000];
static double vright[6760000];
lapack_int ihi_t;
lapack_int ilo_t;
lapack_int info_t;
double scale[2600];
double wimag[2600];
double wreal[2600];
double abnrm;
double rconde;
double rcondv;
double vleft;
std::copy(&A[0], &A[6760000], &b_A[0]);
info_t = LAPACKE_dgeevx(
LAPACK_COL_MAJOR, 'B', 'N', 'V', 'N', (lapack_int)2600, &b_A[0],
(lapack_int)2600, &wreal[0], &wimag[0], &vleft, (lapack_int)1, &vright[0],
(lapack_int)2600, &ilo_t, &ihi_t, &scale[0], &abnrm, &rconde, &rcondv);
Some debugging steps:
- Use the MATLAB find in files tool to do a case insensitive search for "lapacke". That should show some calls like what I pasted above.
- Generate code with and without specifying LapackClass and compare the codegen folders using the MATLAB comparison tool. Specifically pay attention to main_rtw.mk, svd.cpp, eig.cpp, xgeev.cpp to see if any changes show up. You should see your library in main_rtw.mk, an include of lapacke.h in the C++ files, and references to LAPACKE_*functions.
- If that doesn't show anything, put a breakpoint in your updateBuildInfo in LapackClass.m in the MATLAB editor and run codegen / click the generate button. If this breakpoint triggers, your class is being used.
- If the breakpoint isn't being hit, then this sounds like a possible issue in MATLAB Coder. We'd love if you could reach out to MathWorks technical support to share reproduction steps so we can investigate further.
Andreas Scholz
on 7 Dec 2022
Answers (0)
Categories
Find more on Algorithm Design Basics 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!