<div dir="ltr"><div class="markdown-here-wrapper" style><p style="margin:1.2em 0px!important">Oh nice catch! It’s not in the latest version on CRAN, but if you install the development version from github (<code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">devtools::install_github("kaneplusplus/bigmemory")</code>) you will be able to create a big matrix with a type float. You will just need to explicitly tell R to store the data as a float, i.e. <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">as.big.matrix(x, type="float")</code>. Be warned though, your print code will probably segfault when you create the arma::Mat and try to print the matrix.</p>
<div title="MDH:T2ggaW50ZXJlc3RpbmcuIEl0J3Mgbm90IGluIHRoZSBsYXRlc3QgdmVyc2lvbiBvbiBDUkFOLCBi
dXQgaWYgeW91IGluc3RhbGwgdGhlIGRldmVsb3BtZW50IHZlcnNpb24gZnJvbSBnaXRodWIgKGBk
ZXZ0b29sczo6aW5zdGFsbF9naXRodWIoImthbmVwbHVzcGx1cy9iaWdtZW1vcnkiKWApIHlvdSB3
aWxsIGJlIGFibGUgdG8gY3JlYXRlIGEgYmlnIG1hdHJpeCB3aXRoIGEgdHlwZSBmbG9hdC4gWW91
IHdpbGwganVzdCBuZWVkIHRvIGV4cGxpY2l0bHkgdGVsbCBSIHRvIHN0b3JlIHRoZSBkYXRhIGFz
IGEgZmxvYXQsIGkuZS7CoGBhcy5iaWcubWF0cml4KHgsIHR5cGU9ImZsb2F0IilgLiBCZSB3YXJu
ZWQgdGhvdWdoLCB5b3VyIHByaW50IGNvZGUgd2lsbCBwcm9iYWJseSBzZWdmYXVsdCB3aGVuIHlv
dSBjcmVhdGUgdGhlIGFybWE6Ok1hdCZsdDtkb3VibGUmZ3Q7IGFuZCB0cnkgdG8gcHJpbnQgdGhl
IG1hdHJpeC4=" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0">​</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 2 June 2015 at 14:30, Yue Li <span dir="ltr"><<a href="mailto:gorillayue@gmail.com" target="_blank">gorillayue@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I see. Thanks for the explanation Scott. Wish the bigmatrix will get native float type. I found a related post here: <a href="https://github.com/kaneplusplus/bigmemory/issues/4" target="_blank">https://github.com/kaneplusplus/bigmemory/issues/4</a><span class="HOEnZb"><font color="#888888"><div><br></div></font></span><div><span class="HOEnZb"><font color="#888888">Yue</font></span><div><div class="h5"><br><div><br></div><div><br><div><blockquote type="cite"><div>On Jun 2, 2015, at 12:27 AM, Scott Ritchie <<a href="mailto:sritchie73@gmail.com" target="_blank">sritchie73@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div><p style="margin:1.2em 0px!important">Hi Yue,</p><p style="margin:1.2em 0px!important">The call <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">(float *)xpMat->matrix()</code> is simply telling C++ to interpret the stored memory as a float, so it’s simply breaking up the stored binary data into float-sized chunks instead of double-sized chunks, so you get nonsense numbers. To store as a float, you would have to cast <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">double_bigmat</code> to a <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">float</code> type, which I believe makes a new copy of the object after casting each value, so would defeat the purpose. Also big.matrix objects in R only support <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">char</code>, <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">short</code>, <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">int</code>, and <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">double</code> as the underlying storage type (see help(“big.matrix”, “bigmemory”)), so you wouldn’t be able to access the float matrix from R as a big.matrix (without casting it back to a double) anyway.</p><p style="margin:1.2em 0px!important">Regards,</p><p style="margin:1.2em 0px!important">Scott</p>
<div title="MDH:SGkgWXVlLDxicj48YnI+VGhlIGNhbGwgYChmbG9hdCAqKXhwTWF0LSZndDttYXRyaXgoKWAgaXMg
c2ltcGx5IHRlbGxpbmcgQysrIHRvIGludGVycHJldCB0aGUgc3RvcmVkIG1lbW9yeSBhcyBhIGZs
b2F0LCBzbyBpdCdzIHNpbXBseSBicmVha2luZyB1cCB0aGUgc3RvcmVkIGJpbmFyeSBkYXRhIGlu
dG8gZmxvYXQtc2l6ZWQgY2h1bmtzIGluc3RlYWQgb2YgZG91YmxlLXNpemVkIGNodW5rcywgc28g
eW91IGdldCBub25zZW5zZSBudW1iZXJzLiBUbyBzdG9yZSBhcyBhIGZsb2F0LCB5b3Ugd291bGQg
aGF2ZSB0byBjYXN0IGBkb3VibGVfYmlnbWF0YCB0byBhIGBmbG9hdGAgdHlwZSwgd2hpY2ggSSBi
ZWxpZXZlIG1ha2VzIGEgbmV3IGNvcHkgb2YgdGhlIG9iamVjdCBhZnRlciBjYXN0aW5nIGVhY2gg
dmFsdWUsIHNvIHdvdWxkIGRlZmVhdCB0aGUgcHVycG9zZS4gQWxzb8KgYmlnLm1hdHJpeCBvYmpl
Y3RzIGluIFIgb25seSBzdXBwb3J0IGBjaGFyYCwgYHNob3J0YCwgYGludGAsIGFuZCBgZG91Ymxl
YCBhcyB0aGUgdW5kZXJseWluZyBzdG9yYWdlIHR5cGUgKHNlZSBoZWxwKCJiaWcubWF0cml4Iiwg
ImJpZ21lbW9yeSIpKSwgc28geW91IHdvdWxkbid0IGJlIGFibGUgdG8gYWNjZXNzIHRoZSBmbG9h
dCBtYXRyaXggZnJvbSBSIGFzIGEgYmlnLm1hdHJpeCAod2l0aG91dCBjYXN0aW5nIGl0IGJhY2sg
dG8gYSBkb3VibGUpIGFueXdheS48YnI+PGJyPlJlZ2FyZHMsPGJyPjxicj5TY290dA==" style="min-height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0">​</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 2 June 2015 at 12:58, Yue Li <span dir="ltr"><<a href="mailto:gorillayue@gmail.com" target="_blank">gorillayue@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Dear List,<div><br></div><div>I wonder if there is a way to convert a big matrix to ‘float’ instead of ‘double’ within a Rcpp program. The reason for using float is mainly for performance improvement.</div><div><br></div><div>For instance, I have a simple function named ‘print_bigmat’ as shown below.</div><div><br></div><div>As shown in the output, ‘double_bigmat' will save the correct values of the original matrix ‘x’ but not ‘float_bigmat’.</div><div><br></div><div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono';color:rgb(0,132,0)">// [[Rcpp::export]]</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'"><span style="color:#bb2ca2">int</span> print_bigmat(SEXP pBigMat) {</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono';min-height:19px">    <br></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'">    XPtr<BigMatrix> xpMat(pBigMat);</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono';min-height:19px">    <br></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'">    <span style="color:#bb2ca2">const</span> mat& double_bigmat = arma::Mat<<span style="color:#bb2ca2">double</span>>((<span style="color:#bb2ca2">double</span> *)xpMat->matrix(), xpMat->nrow(), xpMat->ncol(), <span style="color:#bb2ca2">false</span>);</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono';min-height:19px">    <br></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'">    <span style="color:#bb2ca2">const</span> fmat& float_bigmat = arma::Mat<<span style="color:#bb2ca2">float</span>>((<span style="color:#bb2ca2">float</span> *)xpMat->matrix(), xpMat->nrow(), xpMat->ncol(), <span style="color:#bb2ca2">false</span>);</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono';min-height:19px">    <br></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'">    Rcout << double_bigmat << endl;</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono';min-height:19px">    <br></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'">    Rcout << float_bigmat << endl;</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono';min-height:19px">    <br></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'">    <span style="color:#bb2ca2">return</span> <span style="color:#272ad8">0</span>;</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'">}</div></div><div><br></div><div><br></div><div>Output:</div><div><br></div><div><div>> x <- matrix(rnorm(10), nrow=2)</div><div>> x</div><div>            [,1]        [,2]      [,3]       [,4]       [,5]</div><div>[1,] -0.05514382 -0.03943825 1.4145593 -0.1161918  2.3282466</div><div>[2,] -1.22023371 -0.35592125 0.7714512  0.6865120 -0.3504811</div><div>> print_bigmat(as.big.matrix(x)@address)</div><div>  -0.0551  -0.0394   1.4146  -0.1162   2.3282</div><div>  -1.2202  -0.3559   0.7715   0.6865  -0.3505</div><div><br></div><div>  -3.3865e-14  -8.6552e+04   4.5441e-07  -5.0912e+23  -1.5184e+34</div><div>  -1.3456e+00  -1.9025e+00  -1.2828e+00  -1.6780e+00   1.9268e+00</div><div><br></div><div>[1] 0</div><div>> </div></div><div><br></div><div><br></div><div>Thanks much,</div><div><div>Yue</div></div><div><br></div><div><br></div></div><br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></div></div></blockquote></div><br></div>