[R-pkg-devel] Managing RNG in C code

2015-06-04 Thread Guillaume Chapron
Hello,

I am working on a package where I am passing some computations from R to C and 
back. The C code runs many stochastic trajectories of a population model (with 
the RNG from R through multiple calls to GetRNGstate and PutRNGstate). I would 
like that each trajectory has its own seed. 

My problem is that the guidelines to develop R packages write in the section 
'6.3 Random number generation' that "The random number generator is private to 
R; there is no way to select the kind of RNG or set the seed except by 
evaluating calls to the R functions."

The only way I see to get around this is to set the seed in R, run 1 trajectory 
in C, get results back in R, increment the seed in R, run a 2nd trajectory in C 
with this new seed, add the new results to the previous one in R, and repeat 
this e.g. 1000 times. Does this sound fine or should I be concerned that 
calling the C library so many times may slow down the computation (even if the 
library is only loaded 1 time at the beginning), e.g. is there a substantial 
overhead in interfacing from R to C and back?

Alternatively, is there a hack to get access by force to the private RNG in R 
from C and set its seed? This way, I could call the C code only 1 time and run 
the 1000 trajectories within C, updating the seed in R for each new trajectory 
(this is what I did when I previously used the GSL).

Thanks for any advice on this!

Guillaume
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


[R-pkg-devel] Multi-threaded C with R's RNG

2015-11-27 Thread Guillaume Chapron
Hello,

Has anyone written a package with a C code that uses R's RNG but in a 
multi-threaded way (e.g. to parallelize Monte Carlo simulations)? I do not have 
the knowledge to figure out myself how to solved this and would need to see an 
example.

Thanks!

Guillaume

--
Guillaume Chapron, PhD

Associate Professor

Grimsö Wildlife Research Station
Swedish University of Agricultural Sciences
SE - 73091 Riddarhyttan, Sweden

http://www.carnivore.science/
Twitter @CarnivoreSci

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


[R-pkg-devel] Rprintf with mclapply

2016-02-11 Thread Guillaume Chapron
Hello,

I have a R package with a compiled C executable to run simulations. Simulations 
can be very long so I use mclapply to launch several instances (each with a 
different R seed). I would need to follow the simulation progress and I used to 
do that with printf in the C code. But this is not allowed on CRAN and I have 
to use Rprintf. The problem is that with mclapply, Rprintf prints nothing in 
the R console! Any idea how can I fix this? Thanks!

Guillaume
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


[R-pkg-devel] scripting R through lldb

2016-06-15 Thread Guillaume Chapron
Hello,

I am debugging a package that contains some compiled C code. The C code uses R 
random number generator so I cannot (or have not figured out how to) debug it 
without running it through R. What I do is to type the following in the 
terminal:

R --debugger=lldb

then in the lldb prompt

r

and within R

source("~/Desktop/test.R”)

which is the file calling the C library. When the library crashes I can 
navigate the C memory to see where is the problem.

My question is whether there is a way to have this in single line or script to 
run from the terminal. I have looked on stackoverflow but could not find.

Many thanks

Guillaume
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Re: [R-pkg-devel] scripting R through lldb

2016-06-15 Thread Guillaume Chapron
Fantastic, it works perfectly, many thanks!

Guillaume

> On 15 Jun 2016, at 21:49, Kevin Ushey  wrote:
> 
> Hi Guillaume,
> 
> I have a script that does this (for OS X) here, taking advantage of
> the new '--batch' argument to lldb:
> 
>https://github.com/kevinushey/etc/blob/master/lang/r/mac/bin/r-lldb
> 
> Note that, on recent versions of OS X due to system integrity
> protection, this may not work with the system lldb, as it appears
> there are restrictions on how certain environment variables, e.g.
> DYLD_FALLBACK_LIBRARY_PATH, can be set for system executables. (If you
> are indeed on OS X, you can try building your own version of lldb --
> see e.g. 
> https://github.com/kevinushey/etc/blob/master/platform/mac/install-llvm.sh
> for that)
> 
> In the end, you should be able to use this script as e.g.
> 
>r-lldb -f test.R
> 
> and this will launch lldb, set R as the target process, with '-f
> test.R' passed to the R executable.
> 
> Depending on your OS / configuration, you might need to tweak the
> script, but it will hopefully either 'work out of the box' or at least
> serve as a starting point for your version.
> 
> Best,
> Kevin
> 
> On Wed, Jun 15, 2016 at 12:39 PM, Guillaume Chapron
>  wrote:
>> Hello,
>> 
>> I am debugging a package that contains some compiled C code. The C code uses 
>> R random number generator so I cannot (or have not figured out how to) debug 
>> it without running it through R. What I do is to type the following in the 
>> terminal:
>> 
>> R --debugger=lldb
>> 
>> then in the lldb prompt
>> 
>> r
>> 
>> and within R
>> 
>> source("~/Desktop/test.R”)
>> 
>> which is the file calling the C library. When the library crashes I can 
>> navigate the C memory to see where is the problem.
>> 
>> My question is whether there is a way to have this in single line or script 
>> to run from the terminal. I have looked on stackoverflow but could not find.
>> 
>> Many thanks
>> 
>> Guillaume
>> __
>> R-package-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

[R-pkg-devel] Compiler optimization flags with R package

2017-06-17 Thread Guillaume Chapron
Hello,

I am trying to debug some C code that is run only through a R package. I debug 
with lldb but I always get a message telling that the package "was compiled 
with optimization - stepping may behave oddly; variables may not be available.” 
And in fact, I cannot evaluate all variables and understand what is wrong. I 
have created a Makevars file in ~/.R/ that contains C=clang -O0 -g but it does 
not seem to change much. What should I do to make sure I can evaluate all 
variables in lldb? I am on a Mac and I wrote C=clang because I read that it 
produced much better error messages but I am happy to use gcc as well if this 
can be a solution.

Many thanks

Guillaume
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Re: [R-pkg-devel] Compiler optimization flags with R package

2017-06-18 Thread Guillaume Chapron
Thanks!

In that file, I see:

CC = clang
CFLAGS = -Wall -mtune=core2 -g -O2 $(LTO)

will having -O0 instead give me all the variables or is there a flag for better 
debugging (I guess -g does it a little already)?

Thanks again 

> On 17 Jun 2017, at 21:57, Dirk Eddelbuettel  wrote:
> 
> 
> On 17 June 2017 at 21:32, Guillaume Chapron wrote:
> | I am trying to debug some C code that is run only through a R package. I 
> debug with lldb but I always get a message telling that the package "was 
> compiled with optimization - stepping may behave oddly; variables may not be 
> available.” And in fact, I cannot evaluate all variables and understand what 
> is wrong. I have created a Makevars file in ~/.R/ that contains C=clang -O0 
> -g but it does not seem to change much. What should I do to make sure I can 
> evaluate all variables in lldb? I am on a Mac and I wrote C=clang because I 
> read that it produced much better error messages but I am happy to use gcc as 
> well if this can be a solution.
> 
> Edit the file Makeconf in e.g.
> 
>R> file.path(Sys.getenv("R_HOME"), "etc", "Makeconf")
>[1] "/usr/lib/R/etc/Makeconf"
>R> 
> 
> It has those settings hardwired from when R itself was compiled for you.  You
> probably want to keep a copy of the original file to be able to revert.
> 
> Dirk
> 
> -- 
> http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

[R-pkg-devel] R package with Swift code

2017-11-29 Thread Guillaume Chapron
Hello,

Does anyone know of an example of a R package that contains Swift code 
(https://swift.org), like many packages use C code?

Thanks

Guillaume
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel