From: Eli on
I am trying to use some fortran code from netlib in my matlab programs.

Unfortunately, it does not work for some reason. Below is a small demonstration of such a behavior:

testfun.f90:
------------------------------------------------------
subroutine testfun (n, v, x, y)
integer, intent(in) :: n, v
double precision, intent(in) :: x(n)
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
------------------------------------------------------
There is an H-file to accompany this fortran code:

testfun.h:
----------------
void
testfun_(int *n, double *v, double *x, double *y);
--------------------------------------------------------------------------------

To create a shared library I compile the fortran code as follows:
-------------------
gfortran -g -shared -fPIC -lblas testfun.f90 -o testfun.so
--------------------------------------------------------------------------------------

Then, from the Matlab, I perform:
>> loadlibrary testfun
>> x = rand(10,1)

x =

0.8147
0.9058
0.1270
0.9134
0.6324
0.0975
0.2785
0.5469
0.9575
0.9649

>> v = 2

v =

2

>> y = 1

y =

1

>> n = numel(x)

n =

10
>> [n, v, x, y] = calllib('testfun', 'testfun_', n, v, x, y)
10 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 0.00000000000000

n =

10


v =

2


x =

2
2
2
2
2
2
2
2
2
2


y =

0

>>

As you can see dcopy() works fine, but ddot() doesn't: I receive 0 in y. This behavior is very strange because sometimes, I do receive correct value in y on the first call to callllib() but any subsequent calls always produce zero or crash.

It does not matter what BLAS library I use: I tried all available libraries including Matlab's one mwblas.

This is Matlab 7.9.0.529 (R2009b) on a Linux 64 bin machine (intel).

Thank you.
From: Eli on
"Eli " <eli.osherovich(a)gmail.com> wrote in message <i1mrut$1k7$1(a)fred.mathworks.com>...
> I am trying to use some fortran code from netlib in my matlab programs.
>
> Unfortunately, it does not work for some reason. Below is a small demonstration of such a behavior:
>
> testfun.f90:
> ------------------------------------------------------
> subroutine testfun (n, v, x, y)
> integer, intent(in) :: n, v
> double precision, intent(in) :: x(n)
> 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
> ------------------------------------------------------
> There is an H-file to accompany this fortran code:
>
> testfun.h:
> ----------------
> void
> testfun_(int *n, double *v, double *x, double *y);
> --------------------------------------------------------------------------------
>
> To create a shared library I compile the fortran code as follows:
> -------------------
> gfortran -g -shared -fPIC -lblas testfun.f90 -o testfun.so
> --------------------------------------------------------------------------------------
>
> Then, from the Matlab, I perform:
> >> loadlibrary testfun
> >> x = rand(10,1)
>
> x =
>
> 0.8147
> 0.9058
> 0.1270
> 0.9134
> 0.6324
> 0.0975
> 0.2785
> 0.5469
> 0.9575
> 0.9649
>
> >> v = 2
>
> v =
>
> 2
>
> >> y = 1
>
> y =
>
> 1
>
> >> n = numel(x)
>
> n =
>
> 10
> >> [n, v, x, y] = calllib('testfun', 'testfun_', n, v, x, y)
> 10 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 2.00000000000000 0.00000000000000
>
> n =
>
> 10
>
>
> v =
>
> 2
>
>
> x =
>
> 2
> 2
> 2
> 2
> 2
> 2
> 2
> 2
> 2
> 2
>
>
> y =
>
> 0
>
> >>
>
> As you can see dcopy() works fine, but ddot() doesn't: I receive 0 in y. This behavior is very strange because sometimes, I do receive correct value in y on the first call to callllib() but any subsequent calls always produce zero or crash.
>
> It does not matter what BLAS library I use: I tried all available libraries including Matlab's one mwblas.
>
> This is Matlab 7.9.0.529 (R2009b) on a Linux 64 bin machine (intel).
>
> Thank you.

there is a small typo in the Fortran code: v should be declared as double precision and not as an integer.