From: Eli Osherovich on
I have a problem with a simple shared library created from a Fortran
subroutine.
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.
From: gmail-unlp on
On Jul 15, 10:58 am, Eli Osherovich <eli.osherov...(a)gmail.com> wrote:
> I have a problem with a simple shared library created from a Fortran
> subroutine.
> 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.

Just guessing:
1) Call the function from a Fortran progran. If this works, the
problem is not in the Fortran shared library but in the Fortran-Matlab
interoperation.
2) if the problem is about Fortran-Matlab interop., well, I've not
used that in Matlab, I only found http://www.mathworks.com/access/helpdesk/help/techdoc/ref/calllib.html
maybe in a Matlab forum?

Hope this helps,

Fernando.

From: Eli Osherovich on
On Jul 15, 5:26 pm, gmail-unlp <ftine...(a)gmail.com> wrote:
> On Jul 15, 10:58 am, Eli Osherovich <eli.osherov...(a)gmail.com> wrote:
>
>
>
> > I have a problem with a simple shared library created from a Fortran
> > subroutine.
> > 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.
>
> Just guessing:
> 1) Call the function from a Fortran progran. If this works, the
> problem is not in the Fortran shared library but in the Fortran-Matlab
> interoperation.
> 2) if the problem is about Fortran-Matlab interop., well, I've not
> used that in Matlab, I only foundhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/calllib.html
> maybe in a Matlab forum?
>
> Hope this helps,
>
> Fernando.

Fernando,

The function seems to work when I call it from another Fortran or C
program. But still, I cannot understand the problem with ddot().
Matlab does call the function but the result is zero for some reason.
Maybe the way I create the shared library is not correct?

From: Barron on
On Jul 15, 8:58 am, Eli Osherovich <eli.osherov...(a)gmail.com> wrote:

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

I think this should be:

void
testfun_(int *n, double *v, double x[], double *y);

Or at least that's how I have mined defined in a similar "calling
Fortran lib from MATLAB" situation.

--Barron
From: dpb on
Eli Osherovich wrote:
> On Jul 15, 5:26 pm, gmail-unlp <ftine...(a)gmail.com> wrote:
....

> The function seems to work when I call it from another Fortran or C
> program. But still, I cannot understand the problem with ddot().
> Matlab does call the function but the result is zero for some reason.
> Maybe the way I create the shared library is not correct?

As noted, this likely isn't actually a Fortran question/problem but has
to do w/ the Matlab interface to shared library function.

Check carefully the ML doc's on passing ML data since you have an array
in particular. The version of ML I have predates the feature so can't
test here but that would be my first supposition.

If that suggestion doesn't bring joy, post on the Matlab newsgroup
instead comp.soft-sys.matlab

--