How to use calllib in matlab to call a C function in the form: Void * funcName

12 views (last 30 days)
I'm trying to get matlab (7.12.0 R2011a - in linux) to use the soft20 spherical harmonic C library ( http://www.cs.dartmouth.edu/~geelong/sphere/ ). In particular I am trying to replicate the test_soft_fftw_correlate2.c function in matlab so I can analyse the results at each step. This function uses the FFTW3 library and part of its initialization is a call to create a FFTW plan:
dctPlan = fftw_plan_r2r_1d( 2*bwIn, weights, workspace3, FFTW_REDFT10, FFTW_ESTIMATE ) ;
The problem is that when I use the following code:
%%Create the dctPlan
load_libsoft1('libsoft1', 'libsoft1.h')
bwIn = int32(4);
n = int32(2 * bwIn);
weightsPtr = libpointer('doublePtr', zeros(4*bwIn, 1));
workspace3Ptr = libpointer('doublePtr', zeros(n*(12 + bwIn), 1));
% Two constants defined in fftw3.h
FFTW_REDFT10 = uint32(5); % specified in enum fftw_r2r_kind_do_not_use_me...
# This is actually defined as: FFTW_ESTIMATE = U1 << 6
FFTW_ESTIMATE = uint32(1000000);
[planobj, planval] = calllib(libname, 'fftw_plan_r2r_1d', n, weightsPtr, workspace3Ptr, FFTW_REDFT10, FFTW_ESTIMATE)
A Segmentation Violation is produced and Matlab has to be restarted. The header file ( libsoft1.h ) at this stage just has these functions in it:
// This header file only contains the functions that need to be called
// void **dctPlan is used instead of fftw_plan, which is a pointer to a pointer
void FST_semi_memo(double *rdata, double *idata,
double *rcoeffs, double *icoeffs,
int bw,
double **seminaive_naive_table,
double *workspace,
int dataformat,
int cutoff,
void **dctPlan,
void **fftPlan,
double *weights );
void * fftw_plan_r2r_1d(int, double *weights, double *, int, int);
void makeweights(int bw, double *weights);
While libsoft1 is a shared dynamic library ( libsoft1.so ) made from the functions in the lib1 folder in the Soft20 library. My main question is, how should I be calling 'fftw_plan_r2r_1d' which returns a pointer so that I have access to the fftw_plan and not have matlab produce a segmentation violation. libsoft1.so, libsoft1.h and the test_soft_fftw_correlate2.c function I am trying to replicate in matlab can be found here:
  2 Comments
Philip Borghesani
Philip Borghesani on 3 Apr 2013
You gave no information from the stack dump so all is conjecture...
I suspect you problem has nothing to do with the void * return from fftw_plan_r2r_1d. If things are working you should be able to take planobj and pass it directly to another library function.
Using a library that is also used by MATLAB on Linux can be problematic try searching fftw & mex for examples and assistance.
I suggest trying to do this from a simple mex file. If that crashes the same way you have a library incompatibility.
It is possible that the crash is caused by incorrect input arguments I do not know enough about FFTW to validate your code. Again a stack trace might point in a helpful direction.
Robert
Robert on 4 Apr 2013
Sorry, I should have included the stack trace, this is what Matlab output:
------------------------------------------------------------------------
Segmentation violation detected at Thu Apr 4 13:46:04 2013
------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled
Current Visual : 0x24 (class 4, depth 16)
Default Encoding: UTF-8
GNU C Library : 2.11.3 stable
MATLAB License : 147182
MATLAB Root : /home/overflow/usr/local/matlab2011a
MATLAB Version : 7.12.0.635 (R2011a)
Operating System: Linux 2.6.32-5-amd64 #1 SMP Mon Feb 25 00:26:11 UTC 2013 x86_64
Processor ID : x86 Family 6 Model 12 Stepping 2, GenuineIntel
Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
Window System : The XFree86 Project, Inc (40300000), display :2.0
Fault Count: 1
Abnormal termination:
Segmentation violation
Register State (from fault):
RAX = 0000000000000001 RBX = 0000000000000001
RCX = 00000000028d0970 RDX = 0000000000000001
RSP = 00007f0e795d2cf8 RBP = 00007f0e795d2dd0
RSI = 00007f0e795d2dd0 RDI = 0000000000000004
R8 = 0000000000000000 R9 = 0000000000000001
R10 = 00007f0e5520e300 R11 = 0000000000000000
R12 = 0000000000000001 R13 = 0000000000000000
R14 = 0000000000000001 R15 = 0000000000000000
RIP = 0000000000000000 EFL = 0000000000010202
CS = 0033 FS = 0000 GS = 0000
Stack Trace (from fault):
[ 0] 0x00007f0e89859f49 /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwfl.so+00474953 fl::sysdep::linux::unwind_stack(void const**, unsigned long, unsigned long, fl::diag::thread_context const&)+000009
[ 1] 0x00007f0e8980fdc1 /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwfl.so+00171457 fl::diag::stacktrace_base::capture(fl::diag::thread_context const&, unsigned long)+000161
[ 2] 0x00007f0e898190dd /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwfl.so+00209117
[ 3] 0x00007f0e89819364 /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwfl.so+00209764 fl::diag::terminate_log(char const*, fl::diag::thread_context const&, bool)+000100
[ 4] 0x00007f0e88885be5 /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so+00420837 fl::diag::terminate_log(char const*, ucontext const*, bool)+000085
[ 5] 0x00007f0e88882b54 /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so+00408404
[ 6] 0x00007f0e8888403d /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so+00413757
[ 7] 0x00007f0e88884197 /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so+00414103
[ 8] 0x00007f0e88884b1d /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so+00416541
[ 9] 0x00007f0e88885435 /home/overflow/usr/local/matlab2011a/bin/glnxa64/../../bin/glnxa64/libmwmcr.so+00418869
[ 10] 0x00007f0e873dfff0 /lib/libpthread.so.0+00061424
If this problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/
A technical support engineer might contact you with further information.
Thank you for your help.** This crash report has been saved to disk as /home/rll6/matlab_crash_dump.10250-1 **
Caught MathWorks::System::FatalException

Sign in to comment.

Accepted Answer

Philip Borghesani
Philip Borghesani on 4 Apr 2013
The stack trace suggests that the problem came from pthreads I suspect the libfftw you are using expects a different version of pthreads than that used with MATLAB and is therefor incompatible with MATLAB. Your libraries probably need to be rebuild using the shared libraries shipped with MATLAB.
Does the crash occur on the loadlibrary call or the calllib call?

More Answers (2)

Robert
Robert on 8 Apr 2013
After testing different situations it was determined that the segmentation violation arises when fftw3 is called through matlab similar to how the soft20 library was using it. Fortunately, soft20 also provides a library that does not require fftw, allowing soft20 to be used through matlab without causing the segmentation violation.

ALBERTO VAZQUEZ
ALBERTO VAZQUEZ on 17 Oct 2017
Estoy tratando de usar calllib para usar funciones desde Refprop, sin embargo al principio me pedia un compliador, posteriormente me ha marcado el error de esta funcion
refpropm ('T', 'P', 101,325, 'Q', 0, 'WATER') utilizo esta funcion para comprobar, pero me aparece esto:
Warning: Input should be a character array or cell array of character arrays. > In strcat (line 83) In refpropm (line 281) Error using calllib Method was not found.
Error in refpropm (line 295) [nc, ~,~,~,ierr,errTxt] = calllib(libName,'SETUPdll',numComponents,path,hmix,href,0,herr,10000,255,3,255);

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!