Calling method in the GNU Scientific Library

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 件のコメント

Bjorn Gustavsson
Bjorn Gustavsson 2021 年 3 月 2 日
Why do you want to use that SVD-decomposition-function instead of the LAPACK-based one matlab uses?
Paul Reiners
Paul Reiners 2021 年 3 月 2 日
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.
Bjorn Gustavsson
Bjorn Gustavsson 2021 年 3 月 3 日
(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...
Paul Reiners
Paul Reiners 2021 年 3 月 9 日
The GSL C function is aboutI’m 200x faster than the MATLAB function.
Bjorn Gustavsson
Bjorn Gustavsson 2021 年 3 月 9 日
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...
Jan
Jan 2021 年 3 月 9 日
Paul Reiners
Paul Reiners 2021 年 3 月 9 日
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 件のコメント

Matthew Faulkner
Matthew Faulkner 2023 年 3 月 29 日
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 件のコメント

Paul Reiners
Paul Reiners 2021 年 3 月 9 日
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 ExchangeExecution Speed についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by