[Rd] using a "third party" DLL in my package

Seija Sirkiä seija.sirkia at metla.fi
Wed May 20 13:32:02 CEST 2009


Hello again,

thank you for the comments, especially this one:

Prof Brian Ripley wrote:

 > My concern would be that there are different cases that fail under
 > Fortran compiler X and you are just sweeping the problem under the
 > carpet.

It inspired us to go back to search the cause, and we've made some 
progress: it's not the compiler, it's the compiler options. Simple, but 
it took a while to figure that out since my experience in these things 
is limited. When I build the package with default options using INSTALL 
--build the dll is built with option -O3 as per R's Makeconfig file. If 
I build the dll by hand, using gfortran with no additional options and 
dyn.load it, everything works, and also with -O and -Os. (No, I don't 
fully understand what the differences between all these are, but that's 
another question).

I'm looking at chapter 5.5 in Writing R Extensions and also 6.3.3 in R 
Installation and Administration but I can't figure out how to tell 
"inside" my package that it is not to be built -O3 but with, say, -O. I 
can see how to add flags in the package (and as far as I can tell, if 
there are several optimization level flags the last in line is used and 
that's the wrong one from my point of view), and also how to override 
flags but only on my computer. Am I blind or am I again attempting 
something I shouldn't?

Best regards,
Seija S.


Prof Brian Ripley wrote:
> On Thu, 14 May 2009, Seija Sirkiä wrote:
> 
>> Hello all,
>>
>> it seems my efforts in reading the manuals and help files aren't 
>> enough so here I am. The question is, how would I go about linking a 
>> pre-compiled DLL in to my package? I have previously successfully 
>> built packages with Fortran and C source code, but now I'd like to 
>> take this ready made DLL and call its routines from R.
>>
>> My collegue was brave enough to simply try and put the DLL in src 
>> folder of the package and proceed with building and installing the 
>> same way as if it had been source code. And it works, on my computer 
>> and his. Clearly this isn't exactly portable, and while that's ok for 
>> the use we have in mind (we both work on Windows XP) I have a nagging 
>> feeling this is somehow criminal or unwise at least.
> 
> Unwise, since it is unsupported and might stop working at any time. 
> Actually, you don't mention the version of R you used and (without 
> checking) I think this may not work in all recent versions of R.
> 
> What you can so is to have an 'inst/libs' directory in your package 
> sources, and put the DLL you want to use there.  Another approach (used 
> in a few CRAN packages) is to use 'configure.win' to copy the DLL to a 
> 'libs' directory in the installed package.  A third approach is to have 
> a 'src/Makefile.win' that creates the DLL you want (possibly by just 
> checking it is present in 'src', but also possiby by calling the 
> mysterious Fortran compiler X you obliquely mention below).
> 
>> Why I want to do this might explain it a bit further but in fact the 
>> background contains another problem and I welcome anyone to give hints 
>> about that too.
>>
>> So, we have some Fortran code from way back which deals with fitting 
>> taper curves for tree boles, and some other functions related to that. 
>> We wanted to make these available for use in R and I made a package 
>> with simple wrapper functions for the .Fortran calls, and built it 
>> with the help of the compiler in Rtools. All was fine until my 
>> collegue managed to bump in to a combination of parameters (tree 
>> species, height and breast height diameter) with which the computation 
>> freezes. We traced it to a certain very simple iteration in the 
>> Fortran code that finds the height at which the tree has a given 
>> diameter. I could give more details on that, but the point is that the 
>> very same computation goes through just fine when executed "fully" in 
>> Fortran, with the routines and an interface compiled with another 
>> Fortran compiler. And also when that compiler is used to make a DLL 
>> and that DLL is used within the R package in the way I described above.
>>
>> In summary, what we seem to have here is a compiler dependent 
>> convergence problem. Possible solutions are to figure out what's wrong 
>> with the computation - and I've pretty much exhausted my skills on 
>> that - or to figure out how to use a working DLL, as was my first 
>> question.
> 
> My concern would be that there are different cases that fail under 
> Fortran compiler X and you are just sweeping the problem under the carpet.
> 
>>
>> Great big thanks in advance to anyone with advice!
>>
>> Seija Sirkiä,
>> senior researcher (statistics), Finnish forestry research institute
>



More information about the R-devel mailing list