Re: [Rd] Calling FORTRAN function from R issue?

From: Berend Hasselman <bhh_at_xs4all.nl>
Date: Tue, 06 Mar 2012 11:19:07 +0100

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.



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 06 Mar 2012 - 10:21:44 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 06 Mar 2012 - 12:10:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive