[Rd] call fortran in R

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 4 16:51:09 CEST 2005


On Thu, 4 Aug 2005, Duncan Murdoch wrote:

> Sébastien Durand wrote:
>> At the beginning I was not using symbol.For(), and whatever I add a
>> "_" or not
>>
>> e.g.:
>>
>> .Fortran("wrapper",...
>>
>> I get the same error
>
> You could look at symbol.For("wrapper"), and see if the name it prints
> is exported from your .so.  I don't know how to see the exports of a
> .so, I use Windows.

On a Unix-alike, nm -g foo.so.  (This looks like MacOS X, although that 
was never stated.)

> Alternatively (and probably better overall), you could get your DLL to
> register its exports with R.  This is explained in the "Writing R
> Extensions" manual.  It should help to prevent any odd naming
> conventions of your compiler from causing trouble, and you'll only have
> to worry about odd parameter passing conventions (and how to create the
> C language registration function).
>
> Duncan Murdoch
>>
>> Sebastien
>>
>>
>>
>>
>>> You want to just say:
>>>
>>> .Fortran("wrapper", ...
>>>
>>> not
>>>
>>> .Fortran(symbol.For("wrapper"), ...
>>>
>>> Patrick Burns
>>> patrick at burns-stat.com
>>> +44 (0)20 8525 0696
>>> http://www.burns-stat.com
>>> (home of S Poetry and "A Guide for the Unwilling S User")
>>>
>>> Sébastien Durand wrote:
>>>
>>>
>>>
>>>> Thanks for your help,
>>>>
>>>> I had read those web pages already, and the post you referred to
>>>> already.
>>>>
>>>> So I will try to give more details to what I have done till now
>>>>
>>>> First I must mention again that I am using a mac under tiger.  So
>>>> I  am using .so files.
>>>>
>>>> My fortran file is called kmeans.f, and my subroutine or wrapper
>>>> I  named  it "wrapper"
>>>>
>>>> There is my first line of code
>>>>
>>>>    subroutine wrapper(n, p, nran, mat, ishort, w, z, ntran,
>>>> istand,  k1, k2, iassign, iseed)
>>>>
>>>> In the terminal:
>>>>    R CMD SHLIB  ~/Desktop/Fortan_kmeans/kmeans3.fg77
>>>>        -fno-common  -g -O2 -c /Users/sebas/Desktop/Fortan_kmeans/
>>>> kmeans3.f -o /Users/sebas/Desktop/Fortan_kmeans/kmeans3.o
>>>>        gcc-3.3 -bundle -flat_namespace -undefined suppress -L/
>>>> usr/ local/lib -o
>>>>        /Users/sebas/Desktop/Fortan_kmeans/kmeans3.so /Users/
>>>> sebas/ Desktop/Fortan_kmeans/kmeans3.o
>>>>        -L/usr/local/lib/gcc/powerpc-apple-darwin6.8/3.4.2 -lg2c -
>>>> lSystem -framework R
>>>>
>>>> The files append to compile without any warnings.
>>>>
>>>> In R:
>>>>
>>>>
>>>>> dyn.load("/Users/sebas/Desktop/Fortan_kmeans/kmeans3.so")
>>>>> getLoadedDLLs()
>>>>
>>>> Filename
>>>> base
>>>>               base
>>>> grDevices /Library/Frameworks/R.framework/Resources/library/
>>>> grDevices/ libs/grDevices.so
>>>> stats             /Library/Frameworks/R.framework/Resources/
>>>> library/ stats/libs/stats.so
>>>> methods       /Library/Frameworks/R.framework/Resources/library/
>>>> methods/libs/methods.so
>>>> kmeans3                                   /Users/sebas/Desktop/
>>>> Fortan_kmeans/kmeans3.so
>>>>          Dynamic.Lookup
>>>> base               FALSE
>>>> grDevices          FALSE
>>>> stats              FALSE
>>>> methods            FALSE
>>>> kmeans3             TRUE
>>>>
>>>>
>>>> Dyn.load seems to work since if I type in getLoadedDLLs()  I can
>>>> see  the link.
>>>>
>>>>
>>>>> zozo=.Fortran(symbol.For("wrapper"),n=as.integer(n),
>>>>
>>>> p=as.integer (p), nran=as.integer(nran), mat=as.matrix(mat),
>>>> ishort=as.vector (ishort), w=as.vector(w),ntran=as.integer(ntran),
>>>> istand=as.integer (istand), k1=as.integer(k1), k2=as.integer(k2),
>>>> iassign=as.integer (iassign), iseed=as.integer(iseed))
>>>> Erreur dans .Fortran(symbol.For("wrapper"), n = as.integer(n), p
>>>> =  as.integer(p),  :
>>>>    nom de fonction "Fortran" absent de la table d'allocation
>>>>
>>>> Even so I am using in the name parameter of the .Fortran
>>>> function,  "wrapper", "kmeans3", "wrapper_" or "kmeans3_"
>>>>
>>>> I always get the same error call telling me that the fonction
>>>> "Fortran" is not in the allocation table
>>>>
>>>> That is where I am at now, it has been 4 days I am digging into
>>>> docs  but I haven't been able to find what I am doing wrong.
>>>>
>>>> R is so great, I got tons of Fortran lines and subroutine to
>>>> implement in R but I guess I am missing a little thing to make it
>>>> work and now I really don't have a cue how to solve that problem.
>>>>
>>>> Cheers
>>>>
>>>> I will be anxiously waiting for any reply
>>>>
>>>> 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:
>>>>>
>>>>>
>>>>>
>>>>>> Using R 2.0.1 on Windows XP, I am getting an error
>>>>>> msg:
>>>>>>
>>>>>> Error in .Fortran("conic", nxy = nxy, npt = npt, CP =
>>>>>> cp, EP1 = ep1, EP2 = ep2,  :
>>>>>>
>>>>>> Fortran function name not in load table
>>>>>>
>>>>>> I am wondering if there is a way to see what function
>>>>>> names are in the load table?  Maybe the function name
>>>>>> has been altered?
>>>>>>
>>>>>>
>>>>>
>>>>> 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
>>>>>
>>>>>
>>>>
>>>>    [[alternative HTML version deleted]]
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> ---
>>>>
>>>> ______________________________________________
>>>> R-devel at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>>
>>>>
>>>
>>>
>>
>> 	[[alternative HTML version deleted]]
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-devel mailing list