<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div>Dear Rcpp-Devels,<br><br>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).<br><br>Two facts to know about my project:<br><br>1. In R, my project relies fully on S4 classes.<br>2. In C++, it relies on RcppArmadillo.<br><br>Now, to the matter of my question: Return Values from C++ back to R<br><br>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.  <br>2. I thought about several approaches how to handle return values, where the following points are crucial:<br><br><span class="Apple-tab-span" style="white-space: pre; ">     </span>2.1 At the end I have to give back, out of my R function, an S4 'mcmcoutput' object to the user.<br><span class="Apple-tab-span" style="white-space: pre; ">       </span>2.2 Performance of returning the values from C++ back into my R function should be very good, i.e. no avoidable copying.<br><span class="Apple-tab-span" style="white-space: pre; ">       </span>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. <br><br><span class="Apple-tab-span" style="white-space: pre; ">  </span>Approach A:<br><span class="Apple-tab-span" style="white-space: pre; ">    </span><br><span class="Apple-tab-span" style="white-space: pre; ">       </span>A.1 Create an S4 'mcmcoutput' object (with allocated memory for arrays, etc.) inside the R function and pass it to C++. <br><span class="Apple-tab-span" style="white-space: pre; ">  </span>A.2 Create an C++ class containing Rcpp Objects (NumericMatrix, List) reusing the memory allocating in R.<br><span class="Apple-tab-span" style="white-space: pre; ">      </span>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.<br><span class="Apple-tab-span" style="white-space: pre; ">     </span>A.4 Return the SEXP of the S4 'mcmcout' object.<br><br><span class="Apple-tab-span" style="white-space: pre; ">      </span>Problem: How to create the Rcpp::Lists with the R 'list' objects and make them accessible for value storing inside the algorithm?<br><span class="Apple-tab-span" style="white-space: pre; ">      </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span> Using just Rcpp::List cppList(classS4.slot("R_list")) does not give easy access to the matrices inside an R 'list' object (if ever),<br><span class="Apple-tab-span" style="white-space: pre; ">   </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span> e.g.<br><span class="Apple-tab-span" style="white-space: pre; ">     </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span>cppList["lambda"](0, 1) = value;   <span class="Apple-tab-span" style="white-space: pre; ">  </span>// does not work, as the '()'-operator is not defined without explicitly setting the <br><span class="Apple-tab-span" style="white-space: pre; ">     </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span>   <span class="Apple-tab-span" style="white-space: pre; ">       </span>// field "lambda" to a NumericMatrix object <br><br><span class="Apple-tab-span" style="white-space: pre; ">  </span>Approach B:<br><br><span class="Apple-tab-span" style="white-space: pre; ">  </span>B.1 Create Armadillo matrices in C++ and fill them with the values from the MCMC sampling. <br><span class="Apple-tab-span" style="white-space: pre; ">       </span>B.2 At the end create an RcppResultSet instance (as described in <a href="http://cran.r-project.org/web/packages/RcppExamples/RcppExamples.pdf">http://cran.r-project.org/web/packages/RcppExamples/RcppExamples.pdf</a>)<br><span class="Apple-tab-span" style="white-space: pre; ">   </span>       and fill it with the Armadillo objects<br><span class="Apple-tab-span" style="white-space: pre; ">       </span>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.<br><br><span class="Apple-tab-span" style="white-space: pre; ">   </span>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<br><span class="Apple-tab-span" style="white-space: pre; ">    </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span> and then give it back to the user. <br><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><span class="Apple-tab-span" style="white-space: pre; "> </span> 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).<br><br><br><span class="Apple-tab-span" style="white-space: pre; ">      </span>Approach C:<br><br><span class="Apple-tab-span" style="white-space: pre; ">  </span>C.1 Create Armadillo matrices in C++ and fill them with the values from the MCMC sampling. <br><span class="Apple-tab-span" style="white-space: pre; ">       </span>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.<br><span class="Apple-tab-span" style="white-space: pre; ">        </span>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<br><span class="Apple-tab-span" style="white-space: pre; "> </span>       it will be considered as an instance of the S4 'mcmcout' class in R. Then give it back to the user. <br><br><span class="Apple-tab-span" style="white-space: pre; ">  </span>Problem: Again, I would assume, that the returning of the S4 object from C++ to R includes copying all return values.<br><br>Can anyone give me here some suggestions? I appreciate your help!<br><br><br>Best<br><br>Simon</body></html>