From: gmail-unlp on
On Jul 15, 11:40 am, Eli Osherovich <eli.osherov...(a)gmail.com> wrote:
> 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?

I don't think so, since it works with Fortran as well as C. Reading
again the example and the problem on ddot(), I realize ddot() is used
from blas, as well as dcopy(), are you sure Matlab is able to get blas
too? Somethings you can check:
1) Call ddot() and dcopy() directly from Matlab code. If this works,
Matlab is able to reach the blas library.
2) Don't call ddot() dcopy() from testfun(), and replace them with
your code (or not code at all, just to check behavior). If this work,
the problem is how to reach the blas from your .so function called
from Matlab.
3) I'm running out of ideas...

Fernando.
From: Eli Osherovich on

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

Just wanted to be sure that I create the shared library correctly.

> 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.

It seems to be fine (at least dcopy() works as expected)

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

I try to ask there.


From: gmail-unlp on
> 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?

I don't think so, since it works with Fortran as well as C. Reading
again the example and the problem on ddot(), I realize ddot() is used
from blas, as well as dcopy(), are you sure Matlab is able to get
blas
too? Somethings you can check:
1) Barron and dpb suggestions. I think you will not need to see 2) and
3) below
2) Call ddot() and dcopy() directly from Matlab code. If this works,
Matlab is able to reach the blas library.
3) Don't call ddot() dcopy() from testfun(), and replace them with
your code (or not code at all, just to check behavior). If this work,
the problem is how to reach the blas from your .so function called
from Matlab.

Fernando.
From: Eli Osherovich on
On Jul 15, 5:46 pm, Barron <barron.j.bic...(a)gmail.com> wrote:
> 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

Barron,

Is there any difference?
I think these two ways are the same.
Anyway, just to be sure, I tried this one too, with no success,
unfortunately :(

From: Barron on
On Jul 15, 10:20 am, Eli Osherovich <eli.osherov...(a)gmail.com> wrote:
> On Jul 15, 5:46 pm, Barron <barron.j.bic...(a)gmail.com> wrote:
>
>
>
>
>
> > 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
>
> Barron,
>
> Is there any difference?
> I think these two ways are the same.
> Anyway, just to be sure, I tried this one too, with no success,
> unfortunately :(

Actually, I don't think there is a difference. I just quickly scanned
some similarly intentioned code that I had on hand for differences and
that jumped out at me. I should have thought through it better. Since
I do have MATLAB and some experience calling Fortran libs from it,
I'll try to find some time today to compile your code and examine it
more thoroughly. Sorry for the misdirection.

--Barron