What happens when I pass an array to a dynamically linked C routine? Is only its reference copied when an array is passed and returned? Or, is its whole content copied? In R extension manual, I found the following description. But, I can't know exactly which is true. "There can be up to 65 further arguments giving R objects to be passed to compiled code. Normally these are copied before being passed in, and copied again to an R list object when the compiled code returns. If the arguments are given names, these are used as names for the components in the returned list object (but not passed to the compiled code)." Thanks in advance. Daehyok Shin
I thought that description is rather clear. If `x' is a vector of doubles, then result <- .C("myCfunc", x = x) makes a copy of `x' to be passed to `myfunc'. The copy in R is not touched. When `myfunc' returns, another copy is made and placed into result[["x"]]. There's the `DUP' argument in .C/.Fortran that controls whether the copying is done. See ?.C for detail. HTH, Andy> Shin, Daehyok > > What happens when I pass an array to a dynamically linked C routine? > Is only its reference copied when an array is passed and returned? > Or, is its whole content copied? > > In R extension manual, I found the following description. > But, I can't know exactly which is true. > > "There can be up to 65 further arguments giving R objects to > be passed to > compiled code. > Normally these are copied before being passed in, and copied > again to an R > list object when > the compiled code returns. If the arguments are given names, > these are used > as names for > the components in the returned list object (but not passed to > the compiled > code)." > > Thanks in advance. > > Daehyok Shin > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Thanks, Andy. Let me ask a little more about the DUP argument. In the manual, I can see that only reference copy happens when passing an array, if DUP=FALSE. Then, what happens when returning it in a list, if DUP=FALSE? Another reference copy, or deep copy? I am trying to implement a dynamic modeling system in R, where some C functions should be called more than 1000 times with arguments of huge arrays. So, the issue is important for better performance. Thanks again, Daehyok Shin
My understanding is that if DUP=FALSE, no copying is done in either direction. If you want to avoid copying, the .Call interface is probably more suitable, as you pass the actual R objects to the C function. I'm not familiar with that, though. Andy> From: Shin, Daehyok [mailto:sdhyok at email.unc.edu] > > Thanks, Andy. > Let me ask a little more about the DUP argument. > In the manual, I can see that only reference copy happens > when passing an > array, > if DUP=FALSE. Then, what happens when returning it in a list, > if DUP=FALSE? > Another reference copy, or deep copy? > > I am trying to implement a dynamic modeling system in R, > where some C functions should be called more than 1000 times > with arguments > of huge arrays. > So, the issue is important for better performance. > Thanks again, > > Daehyok Shin > > >
For me it's just a matter of getting my hands dirty, I guess. For whatever reason I seem to have a mental block about the PROTECT/UNPROTECT business... Best, Andy> From: Duncan Murdoch [mailto:dmurdoch at pair.com] > > On Sun, 11 Apr 2004 21:25:13 -0400, you wrote: > > >My understanding is that if DUP=FALSE, no copying is done in either > >direction. > > > >If you want to avoid copying, the .Call interface is > probably more suitable, > >as you pass the actual R objects to the C function. I'm not > familiar with > >that, though. > > If you're using C, .Call (or .External) is not really very hard to > use. You want to take a look at the examples in the Writing > Extensions manual, and probably in one or two simple packages. > > The big limitation with .Call is that it is really only practical with > C or C++ (because you definitely need to use the R header files), > whereas .C and .Fortran make calls that just about any language can > handle. > > Duncan Murdoch > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}