I want to call a function in the GNU Scientific Library (GSL) from MATLAB. In particular, I want to call this method:
int gsl_linalg_SV_decomp(gsl_matrix * A, gsl_matrix * V, gsl_vector * S, gsl_vector * work)
I have the following folder structure
.
├── Makefile
├── call_gsl_svd.m
├── include
│ ├── calculate_svd_example.h
│ └── run_svd.h
├── project
└── src
├── calculate_svd_example.c
├── project.c
└── run_svd.c
I can call make and the project builds without error. I can then call ./project and a call is made to the gsl_linalg_SV_decomp function and runs fine. calculate_svd_example.c has one simple method, calculate_svd_example, that takes no input and returns no output.
I want to call this simple method from MATLAB (as a first step, anyway). My call_gsl_svd.m reads:
function [] = call_gsl_svd()
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
%#codegen
% for code generation, preinitialize the output variable
% data type, size, and complexity
% generate an include in the C code
coder.cinclude('./include/calculate_svd_example.h');
% evaluate the C function
coder.ceval('./src/calculate_svd_example');
end
I then run this command:
>> codegen call_gsl_svd calculate_svd_example.c
I get this error:
[1/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c /panfs/roc/msisoft/matlab/R2019a/extern/version/c_mexapi_version.c -o build/glnxa64/c_mexapi_version.o
[2/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c call_gsl_svd.c -o build/glnxa64/call_gsl_svd.o
[3/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c call_gsl_svd_initialize.c -o build/glnxa64/call_gsl_svd_initialize.o
[4/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c interface/_coder_call_gsl_svd_api.c -o build/glnxa64/_coder_call_gsl_svd_api.o
[5/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c call_gsl_svd_data.c -o build/glnxa64/call_gsl_svd_data.o
[6/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c call_gsl_svd_terminate.c -o build/glnxa64/call_gsl_svd_terminate.o
[7/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c interface/_coder_call_gsl_svd_mex.c -o build/glnxa64/_coder_call_gsl_svd_mex.o
[8/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c /panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src/calculate_svd_example.c -o build/glnxa64/calculate_svd_example.o
[9/10] /bin/gcc -fexceptions -fPIC -fno-omit-frame-pointer -pthread -D_GNU_SOURCE -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -std=c99 -I "." -I "/panfs/roc/groups/4/miran045/reine097/projects/connectotyping/src" -I "./interface" -I "/panfs/roc/msisoft/matlab/R2019a/extern/include" -I "." -c interface/_coder_call_gsl_svd_info.c -o build/glnxa64/_coder_call_gsl_svd_info.o
[10/10] /bin/gcc build/glnxa64/call_gsl_svd_data.o build/glnxa64/call_gsl_svd_initialize.o build/glnxa64/call_gsl_svd_terminate.o build/glnxa64/call_gsl_svd.o build/glnxa64/_coder_call_gsl_svd_info.o build/glnxa64/_coder_call_gsl_svd_api.o build/glnxa64/_coder_call_gsl_svd_mex.o build/glnxa64/calculate_svd_example.o build/glnxa64/c_mexapi_version.o -pthread -Wl,--no-undefined -Wl,-rpath-link,/panfs/roc/msisoft/matlab/R2019a/bin/glnxa64 -shared -L"/panfs/roc/msisoft/matlab/R2019a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o call_gsl_svd_mex.mexa64 -lemlrt -lcovrt -lut -lmwmathutil -Wl,--version-script,call_gsl_svd_mex.map
FAILED: call_gsl_svd_mex.mexa64
/bin/gcc build/glnxa64/call_gsl_svd_data.o build/glnxa64/call_gsl_svd_initialize.o build/glnxa64/call_gsl_svd_terminate.o build/glnxa64/call_gsl_svd.o build/glnxa64/_coder_call_gsl_svd_info.o build/glnxa64/_coder_call_gsl_svd_api.o build/glnxa64/_coder_call_gsl_svd_mex.o build/glnxa64/calculate_svd_example.o build/glnxa64/c_mexapi_version.o -pthread -Wl,--no-undefined -Wl,-rpath-link,/panfs/roc/msisoft/matlab/R2019a/bin/glnxa64 -shared -L"/panfs/roc/msisoft/matlab/R2019a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o call_gsl_svd_mex.mexa64 -lemlrt -lcovrt -lut -lmwmathutil -Wl,--version-script,call_gsl_svd_mex.map
build/glnxa64/calculate_svd_example.o: In function `calculate_svd_example':
calculate_svd_example.c:(.text+0x73): undefined reference to `gsl_matrix_view_array'
calculate_svd_example.c:(.text+0x82): undefined reference to `gsl_matrix_alloc'
calculate_svd_example.c:(.text+0x94): undefined reference to `gsl_matrix_alloc'
calculate_svd_example.c:(.text+0xa1): undefined reference to `gsl_vector_alloc'
calculate_svd_example.c:(.text+0xae): undefined reference to `gsl_vector_alloc'
calculate_svd_example.c:(.text+0xbc): undefined reference to `gsl_matrix_transpose_memcpy'
calculate_svd_example.c:(.text+0xcd): undefined reference to `gsl_linalg_SV_decomp'
calculate_svd_example.c:(.text+0xe1): undefined reference to `pretty_print'
calculate_svd_example.c:(.text+0xf5): undefined reference to `pretty_print_vector'
calculate_svd_example.c:(.text+0x109): undefined reference to `pretty_print'
calculate_svd_example.c:(.text+0x111): undefined reference to `gsl_matrix_free'
calculate_svd_example.c:(.text+0x119): undefined reference to `gsl_matrix_free'
calculate_svd_example.c:(.text+0x121): undefined reference to `gsl_vector_free'
calculate_svd_example.c:(.text+0x129): undefined reference to `gsl_vector_free'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
------------------------------------------------------------------------
??? Build error: C compiler produced errors. See the Build Log for further details.
More information
Code generation failed: View Error Report
Error using codegen
How do I do this call this simple C method? Do the .c, .h. and .m files all have to be in the same directory? How do I link with the GSL libraries?
(I have attached the source files as a ZIP file.)

댓글 수: 7

Why do you want to use that SVD-decomposition-function instead of the LAPACK-based one matlab uses?
Does the LAPACK=based one that MATLAB uses run as fast as the GSL version? If it does, I'd be happy to continue using it.
(I don't work in the field of numerical linear algebra) I've lived with the impression that LAPACK (et al.) are the state-of-the-art when it comes to linear algebra algorithms. That might be wrong. You might have some special matrices where other algorithms are faster. Perhaps you can time the SVD-algorithms on your type of matrices? Not by interfacing to matlab in the first place but to write a test-program in C...
The GSL C function is aboutI’m 200x faster than the MATLAB function.
That's not bad as such. But my idea was that you'd compare directly with the LAPACK routines also in C: LAPACK-routines for Singular Value Decomposition. If you're building an interface with an external library (which I've never done...) I'd expect you want the best bang for your effort...
How do you install SVD_LAPACK? That seems to be what I want.

댓글을 달려면 로그인하십시오.

 채택된 답변

Paul Reiners
Paul Reiners 2021년 3월 9일

0 개 추천

>> mex -v -L/home/reine097/c-libs/lib/ -R2017b run_svd.c calculate_svd_example.c -lgsl -lgslcblas -lm

댓글 수: 1

Hi, where did you obtain/find the directory -L/home/reine097/c-libs/lib/ ?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2021년 3월 2일
편집: Jan 2021년 3월 2일

0 개 추천

You can create a C++-Mex function and call the library throught it. Unfortunately all inputs must be transposed, because Matlab uses the columnwise storing in opposite to GSL. But the rest of the conversion from Matlab arrays to GSL matrices is easy.

댓글 수: 1

Hi Jan,
I added more details to my description, including attaching the source code files. If you could take another look, that would be great.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Execution Speed에 대해 자세히 알아보기

태그

질문:

2021년 3월 2일

댓글:

2023년 3월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by