From: Eli Osherovich on
I have a problem with shared libraries created from Fortran
subroutines.

Below is a simple example:

testfun.f90:
--------------------------------------------------------------
subroutine testfun (n, v, x, y)
integer, intent(in) :: n
double precision, intent(in) :: x(n), v
double precision, intent(out) :: y
double precision ddot

call dcopy(n, v, 0, x, 1)

y = ddot(n, x, 1, x, 1)

write (*,*) n, x, y
end subroutine testfun
--------------------------------------------------------------

To produce a shared library, I issue the following command:
----------------------------------------------------------------------------------
gfortran -g -shared -fPIC -lblas testfun.f90 -o testfun.so
----------------------------------------------------------------------------------

The library is created and seems to be OK. However, when I try to use
it from MATLAB I face some strange behavior: dcopy() works fine, but
ddot() is quite problematic. It can work on the first call, but any
subsequent calls return zero or even crash.

To use the library in Matlab I created an h-file
testfun.h:
-------------------------------------------------------------------------
void
testfun_(int *n, double *v, double *x, double *y);
------------------------------------------------------------------------

and now I can use

--------------------------------------------------------------------------------
loadlibrary testfun
x = rand(10,1);
y = 1;
v = 2;
n = numel(x);
[n, v, x, y] = calllib('testfun', 'testfun_', n, v, x, y);
---------------------------------------------------------------------------------
After it x's entries are all set to two, but y = 0 (why?)

Even more confusing, this behavior is inconsistent: sometimes the very
first call to calllib() works just fine, but all subsequent calls
return zero in y or even crash.

Any ideas?
Thank you.

P.S.
I use a 64 bit Linux PC.
Matlab version 7.9.0.529 (R2009b)
 | 
Pages: 1
Prev: Frame timing
Next: Report Generator Batch