<div dir="ltr">Oh, the question is not about  supporting native Save/Load from R. I understand the hardness of that due to no customized loader from external pointer. The function listed here aims to write a customized function like ```save.list``` instead of R's native save/load, which is doable. The question is how to get the underlying external ptr from an Rcpp class_<MyClass> Object, within the life cycle of the R session. Sorry about the confusion.<div><br></div><div> Let me rephrase the question in this way: I want to implement a customized element-wise sum of a List of GPUArray object, which I exposed using Rcpp Module class. However, in order to do that, I need to somehow get the external Pointer of GPUArray from the S4 object, to redirect the call to InternalSumOnGPU function. </div><div><br></div><div>After the computation, we need to wrap(construct) a new GPUArray S4 object from C++ side(what was normally provided was constructor from R side).</div><div><br></div><div>```</div><div><div>class GPUArray {</div><div>public:</div><div>   static SEXP Sum(SEXP list) {</div><div>          Rcpp::List data_lst(list);</div><div>          std::vector<GPUArray *> internals(data_lst[i]);</div><div>          for (size_t i = 0; i < data_lst.size(); ++i) {</div><div>               SEXP obj = data_lst[i];</div><div>               // Need to Get internal pointers from S4 GPUArray object</div><div>               internals[i] = GetPtr<GPUArray>(obj);</div><div>          }</div><div><span class="" style="white-space:pre">             </span>  GPUArray *ret = InternalSumOnGPU(internals);</div><div><span class="" style="white-space:pre">            </span>  // need to return the S4 object</div><div><span class="" style="white-space:pre">         </span>  return class_<GPUArray>(ret);</div><div>   }</div><div><br></div><div>};</div><div><br></div><div>RCPP_MODULE(GPUArray) {</div><div>     class_<GPUArray>("GPUArray");</div><div><br></div><div>     function("gpu.sum", &GPUArray::Sum);</div><div>}</div></div><div>```</div><div><br></div><div><br></div><div>Tianqi</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 6, 2015 at 5:55 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
On 6 October 2015 at 14:40, Tianqi Chen wrote:<br>
| Dear Rcpp Developers:<br>
|<br>
| ```c++<br>
| class MyClass {<br>
| public:<br>
|    static void SaveList(SEXP list, const std::string& filename) {<br>
|           Rcpp::List data_lst(list);<br>
|           std::vector<MyClass *> internals(data_lst[i]);<br>
|           for (size_t i = 0; i < data_lst.size(); ++i) {<br>
|                SEXP obj = data_lst[i];<br>
|                // Need to Get internal pointers from S4 MyClass object<br>
|                internals[i] = GetPtr<MyClass>(obj);<br>
|           }<br>
|           SomeRealSave(internals, filename);<br>
|    }<br>
|    static void LoadList(const std::string& filename) {<br>
|           std::vector<MyClass > internals;<br>
|           SomeReadLoad(&internals);<br>
|           Rcpp::List out(internals.size());<br>
|           for (size_t i = 0; i < internals.size(); ++i) {<br>
|               // need to construct a MyClass object from C++ side<br>
|               out[i] = MyClassConstructor(internals[i]);<br>
|           }<br>
|          return out;<br>
|    }<br>
| };<br>
|<br>
| RCPP_MODULE(MyClass) {<br>
|      class_<MyClass>("MyClass");<br>
|<br>
|      function("save.myclass.list", &MyClass::SaveList);<br>
|      function("load.myclass.list", &MyClass::LoadList);<br>
| }<br>
| ```<br>
|<br>
| To be specific, We have ways to Save/Load the pointers to the MyClass object<br>
| (in the SomeRealSave and SomeRealLoad). In our specific use-case, It was an<br>
| Array on GPU.<br>
<br>
</div></div>As I recall, we had several discussions here a few years ago about the<br>
impossibility of serialising Module objects to disk.  In short, you<br>
can't. You write out a bunch of ram which you can re-read, but that doesn't<br>
make an object.<br>
<br>
You will have to rebuild the objects at the next run time so that memory is<br>
allocated and correctly pointed to.<br>
<br>
See here for one such prior email:<br>
<a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-May/007539.html" rel="noreferrer" target="_blank">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-May/007539.html</a><br>
<br>
and an older one<br>
<a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-April/002107.html" rel="noreferrer" target="_blank">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-April/002107.html</a><br>
<span class=""><br>
| So the question is how is there any solution to  GetPtr<MyClass> and <br>
| MyClassConstructor, so we can get the internal pointer of MyClass from SEXP<br>
| (which is the S4 object), and construct the corresponding S4 object given the<br>
| information of MyClass.<br>
|<br>
| What was wanted is indeed possible if we choose to only expose XPtr<MyClass>,<br>
| so we can simply do <br>
| ```<br>
|     MyClass * GetPtr<MyClass>(SEXP obj) {<br>
|          XPtr<MyClass>(obj).get();<br>
|     } <br>
|     MyClassConstructor(MyClass *ptr) {<br>
|         return XPtr<MyClass>(ptr); <br>
|    } <br>
|<br>
| ```<br>
|<br>
| But it would be really nice we can directly return a wrapped S4 object instead<br>
| of raw ptr<br>
<br>
</span>Yes, it would be -- but that doesn't meam it is possible under the current<br>
framework.<br>
<br>
Sorry,  Dirk<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</font></span></blockquote></div><br></div>