<div dir="ltr"><div><div><div><div><div><div>Hello,<br><br></div>I am having trouble creating arma::mat with external writeable auxiliary memory as class member;<br></div><div>ie:<br><br></div><div>struct Example {<br></div>
<div> Example( double *ptr ){<br></div><div>// ..... see provided minimal working example<br></div><div> }<br><br></div><div>arma::mat ExtPtr; // this is what I would like to have<br></div><div>}<br></div></div></div>
</div><br></div><div>What is it that I am doing wrong? Can someone point to the right direction which preferably would use externally created memory location, with encapsulated data structures? <br><br></div><div>----------------------------------------------------------------------------------------------<br>
example: save it to 'arma_example.cpp', then: sourceCpp('arma_example.cpp'); arma_example();<br>----------------------------------------------------------------------------------------------<br>/*<br> * Contact: Steven Varga <br>
* <a href="mailto:steven.varga@gmail.com">steven.varga@gmail.com</a><br> * 2013 Toronto, On Canada <br> */<br><br>#include <stdlib.h><br>#include <RcppArmadillo.h><br>using namespace std;<br>
<br>// [[Rcpp::depends(RcppArmadillo)]] <br>struct Example {<br><br> Example(double *ptr) <br> /* <br> * Initilizationlist wont do <br> * error: invalid initialization of non-const reference of type <br> * ‘arma::mat& {aka arma::Mat<double>&}’ from an rvalue of type <br>
* ‘arma::mat {aka arma::Mat<double><br> : A(arma::mat(ptr, 3,2, false, true) ){<br> */<br> {<br> cout<<" creating arma mat, then assigning it to member B " <<endl;<br> B=arma::mat(ptr+6, 3,2, false, true ); // same area as original B<br>
cout<<"resetting B to 'zeros' should affect datablock outside of 'Example' struct " <<endl; <br> B.zeros(); // reset to zero<br> cout<<" printing B inside of Example: "<<endl;<br>
B.print();<br> cout<<" but B is a copy only, can I have a reference, or B with original ptr?"<<endl<<endl;<br> } <br><br> //arma::mat& A;<br> arma::mat B;<br>};<br>
<br><br>// [[Rcpp::depends(RcppArmadillo)]] <br>// [[Rcpp::export]] <br>void arma_example() {<br><br><br> double* p = static_cast<double*>( malloc( 30*sizeof(double) ));<br> arma::mat M(p, 3,10, false, true ); // this works nicely<br>
<br> bzero(p, 30*sizeof(double)); // reset M<br> M.zeros(); // same as above, but cleaner <br> <br> arma::mat A(p, 3,1, false, true ); // these work as advertised we have A,B<br> arma::mat B(p+6, 3,2, false, true ); // inside ptr area shifted slightly<br>
<br> A.ones(); B.ones(); // lets paint ptr with 'ones'<br> cout<<"area A and B are ones, rest are zeros as expected"<<endl;<br> M.print();<br><br> cout<< "Now I would like to have the above; encapsulating A,B inside 'Example' " <br>
<< "keeping 'M' and ptr outside of class 'Example' "<br> << "by passing 'ptr' in constructor "<br> << "initializer list fails to create reference to 'A' from temporary arma::mat "<br>
<< "when using operator '=' B is initilized with the COPY of ptr "<<endl;<br> Example E(p);<br> <br> cout<<"This is B outside of struct 'Example', expected to be zeros, see struct Example sonstructor"<<endl;<br>
B.print(); // but B is still 'ones' because '=' copied memory from ptr<br> // passing B by reference would solve the problem; but that requires<br> // having B outside of struct Example; and I preferred hiding it; but using <br>
// RcppArmadillo/matlab syntax inside struct Example on ptr for matrix multiplication<br> // and such<br> <br> // CONTEXT:<br> // neural network implementation with weights, gradients parameter unrolling, <br>
// and efficiently passing them to optimizer. <br> // size: layer[n]*layer[n-1]*layers* sizeof(double) <br> // roughly:<br> // ~ 2* 1000*1000*6*8 = 2 * 6*8*10^6 ~= 100 * 10^6 = 100Mbyte <br></div><div> // gets called 10^6~10^7 times per epoch, depending on minibatch size<br>
</div><div> // epoch := 40 ~ 4000 depending on optimizer<br></div><div> free(p); <br>}<br>// EOF<br><br><br></div></div>