Skip to content

call fortran in R

4 messages · Paul Roebuck, Sebastien Durand, Ales Ziberna

#
Hello,

I used a mac G5, R.2.1.1, and G77 3.4.4 and I would like to use and  
call a fortran subroutine.
The trouble is that it seems I am not able to correctly load the  
compiled code.

Here is what I have done:

In the terminal this how I compiled my fortran code:

         R CMD SHLIB  ~/Desktop/Fortan_kmeans/kmeans3.f

There is the wrapper I have paste inside de kmeans3.f file:

         c THIS IS THE WRAPPER
               subroutine wrapper(n, p, nran, mat, ishort, w, z,
             + ntran, istand, k1, k2, iassign, iseed)
               integer n,p,kmax
               real*8 mat(n,p),sx(kmax,p),sx2(kmax,p),vect(p),
             +     xbar(kmax,p),var(kmax,p),mean(p),coord(10)
               real*8 D1,Dref,SSE,SSEref,Dvec(kmax),w(p),SST
               real*8 CH,CHr(kmax),SSEr(kmax),temp
               integer list(n),howmany(kmax),no(n),nobest(kmax)
               integer listr(kmax,n),howmanyr(kmax,kmax),nnitr(kmax)
               integer ishort(p)
               double precision kmeans,
               external kmeans

               call kmeans(n, p, nran, mat, ishort, w,
             + ntran, istand, k1, k2, iassign, iseed)
               end
             c THIS IS THE BEGINNING OF THE ORIGINAL FORTRAN CODE
               subroutine kmeans(n, p, nran, mat, ishort, w,
             + ntran, istand, k1, k2, iassign, iseed, CHr, SSEr)
              ......

Once compiled, in R, I get the following:

 > dyn.load("/Users/sebas/Desktop/Fortan_kmeans/K-means3.so")
 > getLoadedDLLs()
                                                                         
   Filename
1                                                                        
     base
2  /Library/Frameworks/R.framework/Resources/library/grDevices/libs/ 
grDevices.so
3          /Library/Frameworks/R.framework/Resources/library/stats/ 
libs/stats.so
4      /Library/Frameworks/R.framework/Resources/library/methods/libs/ 
methods.so
5   /Users/sebas/Desktop/Fortan_kmeans/kmeans3.so

    Dynamic.Lookup
1           FALSE
2           FALSE
3           FALSE
4           FALSE
5            TRUE
 >
 > is.loaded(symbol.For("/Users/sebas/Desktop/Fortan_kmeans/ 
kmeans3.so"))
[1] FALSE
 > is.loaded(symbol.For("kmeans3"))
[1] FALSE
 > is.loaded(symbol.For("wrapper"))
[1] FALSE

Then whatever I write in .Fortran("",...), I always get :
Erreur in .Fortran("/Users/sebas/Desktop/Fortan_kmeans/kmeans3.so",... :
     the function name "Fortran" is absent of the allocation table
     (this warning was translated from french)


So if anyone got a clue, please let me know.

I would be more than happy to find and understand what I am doing wrong!

Cheers
S?bastien Durand
#
On Wed, 3 Aug 2005, =?iso-8859-1?Q?S=E9bastien_Durand_ wrote:

            
Shouldn't that be .Fortran("wrapper", <arglist>) or something
to that effect? I'm surprised that a null string didn't
crash the program.

Might want to look into R's registration tables as well.
That way you might get some additional debugging help.

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)
#
Hi Paul,

In fact, I used the three points just hide the 
long and exhaustive serie of parameters.
serie of parameters didn't even had the chance to 
be properly read and shall not be the cause of 
what we are seeing.
Even so I change the function name in 
.Fortran("name",....), I get the same error 
message.  It is like if it couldn't find the 
library I have dynamically loaded using dyn.load.
And I don't understand why, or where shall I look to fix such issue.

By the way you spoke about R's registration 
tables, could you tell me where should I look to 
find such info?

Thanks.

S?bastien
#
If I understand correctly, you used
.Fortran("wrapper",....)

The problem might be that the function name ("wrapper") was changed in the 
compilation of the code! See the mail bellow for clues (a previous post on 
R-help by Duncan Murdoch)
Natalie Hawkins wrote:
You need to look at the DLL to see what name it is exporting. I believe
R would be looking for "conic_".  If your Fortran compiler doesn't
append underscores, you'll get this error.

You might want to look at this page

http://www.stats.uwo.ca/faculty/murdoch/software/compilingDLLs/index.html#badname

or this one

http://www.stats.uwo.ca/faculty/murdoch/software/compilingDLLs/fortran.html

for more help.

Duncan Murdoch