[Rd] Troubles with the function rmultinom.c of the R's Random Number Generator
Hin-Tak Leung
hin-tak.leung at cimr.cam.ac.uk
Mon Feb 6 12:08:39 CET 2006
Sophie Ancelet wrote:
> 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?
You probably haven't managed to digest what Prof Ripley was writing:
"All arguments to functions called from C by Fortran are pointers
(or should be: yours are not)" - here is a more hand-holding kind
of answer, which is exactly the same advice, really - you need to
be doing something like this (note the pointers) instead:
>>>void F77_SUB(sarmultinom)(int *n,
>>> double** prob,
>>> int *K,
>>> int** rN){
>>>rmultinom(n, prob, K, rN);}
I'll be interested to see what breaks - do you mind sending me
both of test-multinom.f wrapper.c direct for me to have a look?
HTL
> 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 at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list