[Rd] Dynamic libraries
Duncan Murdoch
murdoch at stats.uwo.ca
Sat Jul 4 01:53:26 CEST 2009
On 03/07/2009 4:37 PM, robin cowan wrote:
> I am trying to write a dynamic linked library for R, in Pascal.
You need to tell us what Pascal compiler you're using. You may need to
ask your compiler provider how to compile something to interface with R,
or tell us all sorts of details that you may not know. Such as:
- what is "integer"?
- what is "cdecl"?
Those have obvious answers; but it really depends on your compiler
writer to implement them in the obvious way or some other. And then
there's:
- what registers does your compiler preserve across calls?
That's usually much harder to discover, but really crucial for stable
performance of the caller.
Duncan Murdoch
(This
> is to speed up the execution of a simulation that I am running in R.)
> I know Pascal might not be the perfect language for this (C or Fortran
> being more natural), but from what I have read I think it should
> work. Though I should point out that I am a neophyte when it comes to
> DLLs.
>
> From R I want to hand a function in the library a two-dimensional
> matrix, operate on the matrix, and hand back a 2-d matrix.
>
> I have no problem creating the library and loading it using dyn.load()
>
> I have written an R wrapper for the function.
>
> When I do something simple using scalars (integers) everything works
> fine, or so it seems at least.
> However, when I try to use a vector as an argument to the function,
> two things happen:
> R becomes very unstable;
> the function only operates on the first 3 elements of the vector (say
> if the vector has 5 elements).
>
> Anyone have experience with this sort of thing?
>
> Thanks,
> Robin Cowan
>
> Sample below:
>
> This one works fine:
>
> library Test1Lib;
>
> type
> pA=^integer;
>
> procedure simple(x:pA); cdecl;
> var i1,i2:integer;
>
> begin
> x^:=x^*2;
> end;
>
> exports simple;
> begin
> end.
>
>
> This one does not:
>
> library Test2Lib;
>
> type
> array1=array[1..5] of integer;
> pA=^array1;
> procedure simpleArray(x:pA); cdecl;
> var i1:integer;
>
> begin
> for i1:=1 to 5 do
> x^[i1]:=x^[i1]*2;
> end;
>
> exports simpleArray;
> begin
> end.
>
>
> Here is the wrapper I use:
> MySimple <- function(x)
> {
> ans <- .C("simple",as.integer(x)) # or simpleArray in the second case
> ans[[1]]
> }
>
> Here is what I get:
>
> > x<-c(1,2,3,4,5)
> > MySimple(x)
> [1] 2 4 6 4 5
>
>
>
> The University of Maastricht has changed its name and mail servers. My
> email address is now r.cowan at maastrichtuniversity.nl.
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list