How to use calllib in matlab to call a C function in the form: Void * funcName
12 views (last 30 days)
Show older comments
Robert
on 3 Apr 2013
Answered: ALBERTO VAZQUEZ
on 17 Oct 2017
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
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.
Accepted Answer
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)
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);
0 Comments
See Also
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!