Dear Rcpp-Devels,
I am at a point in my project where I think intensively about best
implementation regarding performance (as my project contains Monte Carlo Markov
Chain applications at its Core - this is important).
Two facts to know about my project:
1. In R, my project relies fully on S4 classes.
2. In C++, it relies on RcppArmadillo.
Now, to the matter of my question: Return Values from C++ back to R
1. In R I have an S4 class 'mcmcoutput', that contains all output data from
MCMC sampling. It contains arrays and R 'list' objects of arrays.
2. I thought about several approaches how to handle return values, where the
following points are crucial:
2.1 At the end I have to give back, out of my R function, an S4
'mcmcoutput' object to the user.
2.2 Performance of returning the values from C++ back into my R
function should be very good, i.e. no avoidable copying.
2.3 Performance inside C++, when filling the containers with values in
every iteration and generating the object to return back to R, should be high.
Approach A:
A.1 Create an S4 'mcmcoutput' object (with allocated memory for arrays,
etc.) inside the R function and pass it to C++.
A.2 Create an C++ class containing Rcpp Objects (NumericMatrix, List)
reusing the memory allocating in R.
A.3 Fill in every sweep of the algorithm the Rcpp Objects in the C++
instance of the class with the values from Armadillo objects.
A.4 Return the SEXP of the S4 'mcmcout' object.
Problem: How to create the Rcpp::Lists with the R 'list' objects and
make them accessible for value storing inside the algorithm?
Using just Rcpp::List cppList(classS4.slot("R_list"))
does not give easy access to the matrices inside an R 'list' object (if ever),
e.g.
cppList["lambda"](0, 1) = value; // does
not work, as the '()'-operator is not defined without explicitly setting the
// field "lambda" to a NumericMatrix object
Approach B:
B.1 Create Armadillo matrices in C++ and fill them with the values from
the MCMC sampling.
B.2 At the end create an RcppResultSet instance (as described in
http://cran.r-project.org/web/packages/RcppExamples/RcppExamples.pdf)
and fill it with the Armadillo objects
B.3 Return the RcppResultSet back to R, create an S4 object of class
'mcmcout' and fill it with the fields from the result 'list' object given back
via the RcppResultSet.
Problem: Coding the returning of result values is not really smoothly.
I have to create an object in C++, pass it back to R, create an object in R,
fill it
and then give it back to the user.
Further, I would assume, that returning the
RcppResultSet will probably result in an implicit copy of all values (at least
if a standard before C++11 is used).
Approach C:
C.1 Create Armadillo matrices in C++ and fill them with the values from
the MCMC sampling.
C.2 At the end create an S4 object in C++ using Rcpp::S4 and set the
slots as in 'mcmcout'. Fill it with the Armadillo Objects.
C.3 Return the S4 object from C++ to R and in the R function use
'class() <- "mcmcout"' on the returned S4 object to ensure, that
it will be considered as an instance of the S4 'mcmcout' class
in R. Then give it back to the user.
Problem: Again, I would assume, that the returning of the S4 object
from C++ to R includes copying all return values.
Can anyone give me here some suggestions? I appreciate your help!
Best
Simon_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel