Use Fortran subroutine in R? Error: Return type mismatch -
i'm trying learn how use fortran code inside r. able follow this tutorial. now, i'm trying use base plus this fortran program calculate pi. create file fpi.f90 code:
subroutine pi(avepi, darts, rounds) double precision, intent(out) :: avepi integer, intent(in) :: darts, rounds integer :: master, rank, i, n integer, allocatable :: seed(:) double precision :: pi_est, homepi, pirecv, pisum ! set 0 in sequential run rank = 0 ! initialize random number generator ! make sure seed different each task call random_seed() call random_seed(size = n) allocate(seed(n)) seed = 12 + rank*11 call random_seed(put=seed(1:n)) deallocate(seed) avepi = 0 = 0, rounds-1 pi_est = dboard(darts) ! calculate average value of pi on iterations avepi = ((avepi*i) + pi_est)/(i + 1) end end subroutine pi double precision function dboard(darts) integer, intent(in) :: darts double precision :: x_coord, y_coord integer :: score, n score = 0 n = 1, darts call random_number(x_coord) call random_number(y_coord) if ((x_coord**2 + y_coord**2) <= 1.0d0) score = score + 1 end if end dboard = 4.0d0*score/darts end function when
$ r cmd shlib ./fortran/fpi.f90 gfortran -fpic -g -o2 -fstack-protector-strong -c fortran/fpi.f90 -o fortran/fpi.o fortran/fpi.f90:22.15: pi_est = dboard(darts) 1 error: return type mismatch of function 'dboard' @ (1) (real(4)/real(8)) /usr/lib/r/etc/makeconf:161: recipe target 'fortran/fpi.o' failed make: *** [fortran/fpi.o] error 1 what doing wrong?
after adding double precision :: dboard pi different error.
r cmd shlib ./fortran/fpi.f90 gfortran -shared -l/usr/lib/r/lib -wl,-z,relro -o fortran/fpi.so ./fortran/fpi.o -l/usr/lib/r/lib -lr /usr/bin/ld: ./fortran/fpi.o: relocation r_x86_64_32 against `.rodata' can not used when making shared object; recompile -fpic ./fortran/fpi.o: error adding symbols: bad value collect2: error: ld returned 1 exit status /usr/share/r/share/make/shlib.mk:6: recipe target 'fortran/fpi.so' failed make: *** [fortran/fpi.so] error 1
you not use implicit none. bad! due implicit typing dboard thought default real inside pi.
declare double precision, or if possible r, use modules. interface block can used declare dboard inside pi.
Comments
Post a Comment