From bhh at xs4all.nl  Tue Mar  6 11:19:07 2012
From: bhh at xs4all.nl (Berend Hasselman)
Date: Tue, 6 Mar 2012 11:19:07 +0100
Subject: [Rd] Calling FORTRAN function from R issue?
In-Reply-To: <CADUbQ5ghmCd-3K4hx=FrzhA8YwfohhUp0-g6rJHHU0s-uEfN+A@mail.gmail.com>
References: <CADUbQ5ghmCd-3K4hx=FrzhA8YwfohhUp0-g6rJHHU0s-uEfN+A@mail.gmail.com>
Message-ID: <B1B70BE3-2D8A-495B-8893-CC67026E1E9F@xs4all.nl>


On 06-03-2012, at 01:21, Dominick Samperi wrote:

> Hello,
> 
> I am trying to call the BLAS Level1 function zdotc from R via
> a .C call like this:
> 
> #include "R.h"
> #include "R_ext/BLAS.h"
> 
> void testzdotc() {
>    Rcomplex zx[3], zy[3], ret_val;
> 
>    zx[0].r = 1.0; zx[0].i = 0.0;
>    zx[1].r = 2.0; zx[0].i = 0.0;
>    zx[2].r = 3.0; zx[0].i = 0.0;
> 
>    zy[0].r = 1.0; zy[0].i = 0.0;
>    zy[1].r = 2.0; zy[0].i = 0.0;
>    zy[2].r = 3.0; zy[0].i = 0.0;
> 
>    int n=3, incx=1, incy=1;
>    F77_CALL(zdotc)(&ret_val, &n, zx, &incx, zy, &incy);
>    Rprintf("ret_val = %f, %f\n", ret_val.r, ret_val.i);
> }
> 
> This does not work. When I run '.C('testzdotc')' there is
> typically a delay for a second or so, then I get: 0.0, 0.0
> instead of the correct ans: 14.0, 0.0.

I tried calling zdotc  through an intermediate Fortran routine hoping it would solve your problem.

Above C routine changed to

<code>
#include "R.h"

void F77_NAME(callzdotc)(Rcomplex *, int *, Rcomplex *, int *, Rcomplex *, int *);

void testzdotc() {
   Rcomplex zx[3], zy[3], ret_val;

   zx[0].r = 1.0; zx[0].i = 0.0;
   zx[1].r = 2.0; zx[0].i = 0.0;
   zx[2].r = 3.0; zx[0].i = 0.0;

   zy[0].r = 1.0; zy[0].i = 0.0;
   zy[1].r = 2.0; zy[0].i = 0.0;
   zy[2].r = 3.0; zy[0].i = 0.0;

   int n=3, incx=1, incy=1;
   F77_CALL(callzdotc)(&ret_val, &n, zx, &incx, zy, &incy);
   Rprintf("ret_val = %f, %f\n", ret_val.r, ret_val.i);
}
</code>

The fortran subroutine is

<code>
      subroutine callzdotc(retval,n, zx, incx, zy, incy)
      integer n, incx, incy
      double complex retval, zx(*), zy(*)
      external double complex zdotc

      retval = zdotc(n, zx, incx, zy, incy)

      return
      end
</code>

Made a shared object with

R CMD SHLIB --output=dozdot.so callzdotc.f czdot.c 

and ran

dyn.load("dozdot.so")
.C("testzdotc")

with the result 0.0, 0.0

I would've expected this to give the correct result.

Berend

Mac OS X 10.6.8
R2.14.2
Using reference Rblas.



[Rd] Calling FORTRAN function from R issue?
Berend Hasselman