Re: [Rd] Troubles with Fortran and C (was with the function rmultinom.c of the R's Random Number Generator)

From: Sophie Ancelet <Ancelet_at_engref.fr>
Date: Mon 06 Feb 2006 - 09:50:36 GMT

Thank you for the answer. However, I sought in Doc. Writing R extensions, in particular in the paragraph 5.6 "Calling C from FORTRAN and vice versa" (page 67) but I did not find anything which could help me to correct my code. Indeed, rmultinom.c is a particular function since arrays are passed in arguments. Has somebody ever written a wrapper for this function?

Thanks in advance,
Sophie.

At 17:06 20/01/06 +0000, Prof Brian Ripley wrote:
>All arguments to functions called from C by Fortran are pointers
>(or should be: yours are not). The error is within your own code.
>
>You don't want to call rndstart and rndend around every call, only before
>the first and after the last.
>
>This is not the list for advice om mixed Fortran/C programming, though.

>On Fri, 20 Jan 2006, Sophie Ancelet wrote:
>
>>
>> Hi,
>>
>> I'm simulating a Markov chain in Fortran interfaced with R-2.2.1 in order
>> to generate data according to a Markov Random Field called the Potts model.
>>
>> R Version:
>> platform i686-pc-linux-gnu
>> arch i686
>> os linux-gnu
>> system i686, linux-gnu
>> status
>> major 2
>> minor 2.1
>> year 2005
>> month 12
>> day 20
>> svn rev 36812
>>
>>
>>
>>
>> Each loop of my Fortran calls the function rmultinom.c of the R's Random
>> Number Generator through the wrapper:
>>
>> #include <R.h>
>> #include <Rmath.h>
>> void F77_SUB(sarmultinom)(int n,
>> double* prob,
>> int K,
>> int* rN){
>> rmultinom(n, prob, K, rN);}
>>
>>
>>
>> My fortran program is:
>>
>> subroutine testsarmultinom(n,prob,K,rN)
>> implicit none
>> integer n,K,rN(K)
>> double precision prob(K)
>>
>> call rndstart()
>> call sarmultinom(n,prob,K,rN)
>> call rndend()
>> end
>>
>>
>> In order to understand better how the function rmultinom.c works, I have
>> written an R code which calls this fortran subroutine as follows:
>>
>> system("R CMD SHLIB test-multinom.f wrapper.c")
>> dyn.load("~/Package/test/test-multinom.so")
>>
>> n=1
>> prob=c(0.6,0.1,0.3)
>> K=3
>> rN=c(1,0,0)
>> res<- .Fortran("testsarmultinom",
>> as.integer(n),
>> as.double(prob),
>> as.integer(K),
>> as.integer(rN))
>>
>>
>> Unfortunately, I have some trouble with the results. First, this command
>> always returns 0 values. In other words, I always get:
>>
>>> res[[4]]
>> [1] 0 0 0
>>
>>
>> Moreover, if I run this R code a second time, an error message appears:
>> Segmentation fault.
>>
>> Has somebody ever used rmultinom.c and encountered these problems? My code
>> must be wrong but I don't know where. In this case, what is the correct way
>> to call the C function rmultinom.c?
>>
>> Thanks in advance,
>>
>> Sophie.



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon Feb 06 21:14:26 2006

This archive was generated by hypermail 2.1.8 : Mon 06 Feb 2006 - 15:33:21 GMT