[Rd] invoking R function in C++ in parallel

2015-01-31 Thread Zheng Da
Hello,

I'm writing an R extension in C++. In the extension, I want to invoke
an R function directly and it works when I use Rcpp in the serial
code. But what I really want is to invoke the R function in parallel
with openmp. When I do so, I got segmentation fault.

I remember someone said that R isn't thread-safe. I think it's also
understandable because an R function also has environment variables.

My question is:
Can we make a copy of an R function (including its environment
variables) for each thread?
Are there any ways to invoke an R function in C/C++ in multiple threads safely?

Thanks,
Da

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


Re: [Rd] invoking R function in C++ in parallel

2015-01-31 Thread Zheng Da
Thank you both.
I think I have got the answer I need.
I'll need to turn to another direction to search for my solution.

Best,
Da

On Sat, Jan 31, 2015 at 8:32 PM, Dirk Eddelbuettel e...@debian.org wrote:

 On 31 January 2015 at 19:29, Gábor Csárdi wrote:
 | On Sat, Jan 31, 2015 at 7:05 PM, Dirk Eddelbuettel e...@debian.org wrote:
 | [...]
 |
 | - either work in something like OpenMP and run in multiple threads that
 |remain /completely/ shielded from R, ie no R calls, and not even R 
 data
 |types as you cannot trigger gc() calls from different threads
 |
 |
 | I think you can use R objects, as long as you don't call R functions on them
 | (not even from R's C api, although some of them are currently fine) and
 | consider them as read-only. E.g. if you have a numeric vector, you can do
 | double *cvec = REAL(vec);
 | and then use cvec in your thread(s).

 You and I are saying the same thing here: you call it read-only access; I
 called ot do not use R types. We recommend the same approach in other
 places: instantiate std::vectordouble from RcppVector, don't use R calls.

 My recommendation is to make it pretty plain: do not call R functions. Use
 other types. There are equivalent read-only constructors for some relevant
 data types.

 Recall that OP, in the parts you chose not to quote, asked to call back to R
 in a multithreaded way. And we both say 'nope' here.

 All this has come up before on rcpp-devel and SO.

 Dirk

 --
 http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

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


[Rd] How to maintain memory in R extension

2014-11-12 Thread Zheng Da
Hello,

I wrote a system to perform data analysis in C++. Now I am integrating
it to R. I need to allocate memory for my own C++ data structures,
which can't be represented by any R data structures. I create a global
hashtable to keep a reference to the C++ data structures. Whenever I
allocate one, I register it in the hashtable and return its key to the
R code. So later on, the R code can access the C++ data structures
with their keys.

The problem is how to perform garbage collection on the C++ data
structures. Once an R object that contains the key is garbage
collected, the R code can no longer access the corresponding C++ data
structure, so I need to deallocate it. Is there any way that the C++
code can get notification when an R object gets garbage collected? If
not, what is the usual way to manage memory in R extensions?

Thanks,
Da

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


Re: [Rd] How to maintain memory in R extension

2014-11-12 Thread Zheng Da
Thank you, Gabor and Martin. It helps a lot.

Da

On Wed, Nov 12, 2014 at 1:20 PM, Martin Morgan mtmor...@fredhutch.org wrote:
 On 11/12/2014 05:36 AM, Zheng Da wrote:

 Hello,

 I wrote a system to perform data analysis in C++. Now I am integrating
 it to R. I need to allocate memory for my own C++ data structures,
 which can't be represented by any R data structures. I create a global
 hashtable to keep a reference to the C++ data structures. Whenever I
 allocate one, I register it in the hashtable and return its key to the
 R code. So later on, the R code can access the C++ data structures
 with their keys.

 The problem is how to perform garbage collection on the C++ data
 structures. Once an R object that contains the key is garbage
 collected, the R code can no longer access the corresponding C++ data
 structure, so I need to deallocate it. Is there any way that the C++
 code can get notification when an R object gets garbage collected? If
 not, what is the usual way to manage memory in R extensions?


 register a finalizer that runs when there are no longer references to the R
 object, see ?reg.finalizer or the interface to R and C finalizers in
 Rinternals.h. If you return more than one reference to a key, then of course
 you'll have to manage these in your own C++ code.

 Martin Morgan



 Thanks,
 Da

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



 --
 Computational Biology / Fred Hutchinson Cancer Research Center
 1100 Fairview Ave. N.
 PO Box 19024 Seattle, WA 98109

 Location: Arnold Building M1 B861
 Phone: (206) 667-2793

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