Fortran子例程被卡住(但仅在通过R调用时)

时间:2016-01-06 14:50:45

标签: r fortran

我有一个Fortran子例程,我从其他人的Fortran程序转换而来。我想通过R的.Fortran函数来调用它。当我从Fortan程序中调用它时,子程序(立即)工作,但当我尝试从R调用它时,没有任何反应(事实上,当我输入这个时,R仍在运行这个子程序)。

这是Fortran程序(也包含子程序):

       PROGRAM blep

       integer a
       real(4) b, c, d

       b = 0.9
       c = 0.1
       d = 0.99
       a = 0

       call midpss(b, c, d, a)

4        format ('Calculated sample size is   ',i6)

       print 4, a
       end







       subroutine midpss(w, x, y, numbr)
c      THIS IS TAKEN FROM "fosgate_original_working.f" AND THEN CONVERTED
          real(8) probA,probB,part1,part2,part3,part4
          real(8) totprA,totprB,factt, resp
          integer numbr
c       character  resp
1       format ('Enter proportion       ',$)
2       format ('Enter error limit      ',$)
3       format ('Enter confidence level ',$)
4       format ('Calculated sample size is   ',i6)
5       format ('Exact mid-P with ',f7.5,' 2-tail probability')
6       format ('Sorry, unable to mathmatically solve this problem.')
7       format ('Reported sample size is not accuarate.')
8       format ('Enter q to quit  ',$)
9       format ('Actual limits for distribution  ',f5.3,' - ',f5.3)
        print *, 'Exact sampleroportions'
        print *, 'Using Mid-P methods'
        print *, 'Geoff Fosgate DVM PhD'
        print *, 'College of Veterinary Medicine'
        print *, 'Texas A&M University'
        print *
10     prop1 = w
             range = x
             conlev = y
c           Convert proportions less than 0.5 for algorithm
       if (prop1 .lt. 0.5) then
        prop = 1 - prop1
        nprop = 1
       else
        prop = prop1
        nprop = 0
       end if
       slimit = max ((prop - range) , 0.0001)
       supper = min ((prop + range) , 0.9999)
c      Probabilities cannot be calculated for p=0 and p=1
       alpha = (1 - conlev)
       if (alpha .gt. 1.0) go to 10
       if (alpha .lt. 0.0) go to 10
       if (prop .gt. 1.0) go to 10
       if (prop .lt. 0.0) go to 10
       numbr = (1 / (1 - prop)) - 1
c      Define and initialize variables
c      Note names of variables based on Fortran 77 rules
c      Starting sample size is based on estimated proportion
c      Resulting sample size must be large enough to obtain this proportion
100    numbr = numbr + 1
       numx = (numbr * prop) + 0.001
c      This is the number of binomial "successes" resulting in the proportion
       if (numx .eq. numbr) go to 100
       if (numx .lt. 1) go to 100
       totprA = slimit**numbr
       totprB = supper**numbr
       do 130 loop1 = numx, (numbr - 1)
c      Must initialize variables within loop
       factt = 1.0
       probA = 0.0
       probB = 0.0
       part1 = 0.0
       part2 = 0.0
       part3 = 0.0
       part4 = 0.0
c      Start loop to calculate factorial component of binomial probability
c      Note that complete factorial calculations not necessary due to cancellations
       do 110 loop2 = (loop1 + 1) , numbr
       factt = factt * (loop2) / (numbr - (loop2 - 1))
110    continue
c      Calculate probability for this particular number of successes
c      Total probability is a running total
c      Note that real variables must have high precision and be comprised
c      of multiple bytes because factorial component can be very large
c      and exponentiated component can be very small
c      Program will fail if any component is recognized as zero or infinity
       part1 = slimit**loop1
       part2 = (1.0-slimit)**(numbr-loop1)
       part3 = supper**loop1
       part4 = (1.0-supper)**(numbr-loop1)
       if (part1 .eq. 0.0) part1 = 1.0D-307
       if (part2 .eq. 0.0) part2 = 1.0D-307
       if (part3 .eq. 0.0) part3 = 1.0D-307
       if (part4 .eq. 0.0) part4 = 1.0D-307
       if (factt .gt. 1.0D308) factt = 1.0D308
       probA = part1 * part2 * factt
       probB = part3 * part4 * factt
       if (loop1 .eq. numx)  then
        totprA = totprA + (0.5 * probA)
        totprB = totprB + (0.5 * probB)
       else
        totprA = totprA + probA
        totprB = totprB + probB
       end if
c      THIS IS ERROR HANDLING. INSTEAD OF PRINTING, SET NUMBR = -1
c      *****************************************************************
       if (probA .eq. 0.0) then 
c        print 6
c        print 7
c        print *
c        go to 150
         numbr = -1
       end if
       if (probB .eq. 0.0) then
c        print 6
c        print 7
c        print *
c        go to 150
         numbr = -1
       end if
c      *****************************************************************
130    continue
140    if ((totprA + (1 - totprB)) .gt. alpha) go to 100
c      go to beginning and increase sample size by 1 if have not
c      reached specified level of confidence

c      I.E. IF INPUT PROPORTION IS LESS THAN 0.5
c      (I DONT THINK THIS IS NECESSARY -- IT JUST PRINTS THE RESULTS)
c150    if (nprop .eq. 1) then
c        print 4,numbr
c        print 9, (1-supper),(1-slimit)
c       else
c        print 4,numbr
c        print 9, slimit,supper
c       end if


c      DO WE NEED THIS PART????
c      *****************************************************************
c       if (totprA+(1-totprB) .lt. alpha) print 5,(totprA+(1-totprB))
c        print *
c        print 8
c        result = resp
c       print *
c      if (resp .ne. 'q') go to 10
c       print *
c       print *
998    return       
999    end

(很抱歉我将原始程序转换为子程序时留下的评论。)

该程序名为midpss1_prog.f,子程序名为midpss1.f

我通过执行以下操作编译并调用该程序:

C:\Users\panterasBox>gfortran midpss1_prog.f

C:\Users\panterasBox>a.exe
 Exact sampleroportions
 Using Mid-P methods
 Geoff Fosgate DVM PhD
 College of Veterinary Medicine
 Texas A&M University

Calculated sample size is       80

C:\Users\panterasBox>

这工作得很好!

当我调用子程序时,我会执行以下操作:

在命令行中,我称之为:

C:\Users\panterasBox>R CMD SHLIB midpss1.f
gfortran -m64     -O2  -mtune=core2 -c midpss1.f -o midpss1.o
gcc -m64 -shared -s -static-libgcc -o midpss1.dll tmp.def midpss1.o -Ld:/RCompil
e/r-compiling/local/local320/lib/x64 -Ld:/RCompile/r-compiling/local/local320/li
b -lgfortran -LC:/Users/panterasBox/Documents/R/R-3.2.2/bin/x64 -lR

然后,我进入R终端并执行此操作:

> setwd("C:/Users/panterasBox")
> dyn.load("midpss1.dll")
> is.loaded("midpss")
[1] TRUE
> .Fortran("midpss", w=as.numeric(0.9), x=as.numeric(0.1), y=as.numeric(0.90), numbr=as.integer(0))

最后一次调用.Fortran永远不会返回任何内容。它只是卡住了......

非常感谢任何帮助弄清楚这里发生了什么,谢谢。

1 个答案:

答案 0 :(得分:4)

R似乎是将浮点数作为双精度发送到Fortran子程序,因此我们可能需要相应地声明相应的伪参数。由于您的程序在子例程的顶部没有implicit none,因此伪参数wxy被隐式地视为单精度,使参数类型不一致R和Fortran之间(因此导致挂起)。要解决这个问题,只需明确声明它们(这里我们假设real(8)对应于R中的双精度):

      subroutine midpss(w, x, y, numbr)
          real(8) :: w, x, y                !<--- insert this line
          !! double precision :: w, x, y    !<--- or this line (but not both)

          !! No need to modify the remaining part...
          ....

然后我们获得预期的结果(在Linux x86_64上):

> .Fortran("midpss", w=as.numeric(0.9), x=as.numeric(0.1), y=as.numeric(0.99), numbr=as.integer(0))
 Exact sampleroportions
 Using Mid-P methods
 Geoff Fosgate DVM PhD
 College of Veterinary Medicine
 Texas A&M University

$w
[1] 0.9

$x
[1] 0.1

$y
[1] 0.99

$numbr
[1] 80

顺便说一下,使用implicit none(反复建议)可以避免这种问题,因为所有变量都需要明确声明,例如:

   subroutine midpss (w, x, y, numbr)
      implicit none
      real(8) :: w, x, y
      real(8) :: prop, prop1, range, conlev, slimit, supper, alpha
      integer :: loop1, loop2, numx, nprop
      ...