

Thanks for your help with this problem about dynamic linked libraries.  
I thought I had it solved, but apparently not.
Below is a small piece of Pascal code that I compile into a library.

When I load the library using dyn.load  R becomes very unstable.

The Mac GUI crashes in all kinds of situations --even moving a window  
can sometimes crash it.

Running from the command line (I thought  the problem was in the GUI)  
gives me illegal floating point operations or divide by zero errors.
This is the case even when I eliminate all calls to the library, on a  
piece of code that runs fine when the library is not loaded.

Any hints about where to look to solve this problem would be much  
appreciated.

Robin Cowan

Here is the simple Pascal code:
Running on a Macintosh Intel, OS X 10.5, R 2.9.1

{$MODE DELPHI}
library LearningLib;

const num=75;
type
array1= array[1..2,1..num] of Double;
array2=array[1..num,1..num] of longint;
array3=array[1..num,1..num] of Double;
array4=array[1..num] of longint;
pA1=^array1;
pA2=^array2;
pA3=^array3;
pA4=^array4;
pD=^Double;

procedure learning(loc1:pA1;adj1:pA2;alpha:pD); cdecl;
var
i1,i2,neighs:longint;
sum1,sum2:double;
newloc: array [1..2,1..num] of double;
begin
for i1:=1 to 2 do
for i2:=1 to num do
	newloc[i1,i2]:=0;
for i1:= 1 to num do
	begin
	neighs:=0; sum1:=0; sum2:=0;
	for i2:= 1 to num do
	begin
		if (adj1^[i1,i2]=1) then
			begin
				neighs:=neighs+1;
				sum1:=sum1+loc1[1,i2];
				sum2:=sum2+loc1[2,i2];
			end;
		if(neighs>0) then
			begin
				newloc[1,i1]:=alpha^*(sum1/neighs-loc1^[1,i1]);
				newloc[2,i1]:=alpha^*(sum2/neighs-loc1^[2,i1]);
			end;
	end;
	end;
for i1:= 1 to num-1 do
	begin
		loc1^[1,i1]:=loc1^[1,i1]+newloc[1,i1];
		loc1^[2,i1]:=loc1^[2,i1]+newloc[2,i1];
	end;
end;

exports learning;
begin
end.




On Jul 3, 2009, at 4:31 PM, robin cowan wrote:

> I am trying to write a dynamic linked library for R, in Pascal.  
> (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@maastrichtuniversity.nl.







	[[alternative HTML version deleted]]

