<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">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" class="">https://github.com/kaneplusplus/bigmemory/issues/4</a><div class=""><br class=""></div><div class="">Yue<br class=""><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 2, 2015, at 12:27 AM, Scott Ritchie <<a href="mailto:sritchie73@gmail.com" class="">sritchie73@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="markdown-here-wrapper" style=""><p style="margin:1.2em 0px!important" class="">Hi Yue,</p><p style="margin:1.2em 0px!important" class="">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)" class="">(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)" class="">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)" class="">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)" class="">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)" class="">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)" class="">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)" class="">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" class="">Regards,</p><p style="margin:1.2em 0px!important" class="">Scott</p>
<div title="MDH:SGkgWXVlLDxicj48YnI+VGhlIGNhbGwgYChmbG9hdCAqKXhwTWF0LSZndDttYXRyaXgoKWAgaXMg
c2ltcGx5IHRlbGxpbmcgQysrIHRvIGludGVycHJldCB0aGUgc3RvcmVkIG1lbW9yeSBhcyBhIGZs
b2F0LCBzbyBpdCdzIHNpbXBseSBicmVha2luZyB1cCB0aGUgc3RvcmVkIGJpbmFyeSBkYXRhIGlu
dG8gZmxvYXQtc2l6ZWQgY2h1bmtzIGluc3RlYWQgb2YgZG91YmxlLXNpemVkIGNodW5rcywgc28g
eW91IGdldCBub25zZW5zZSBudW1iZXJzLiBUbyBzdG9yZSBhcyBhIGZsb2F0LCB5b3Ugd291bGQg
aGF2ZSB0byBjYXN0IGBkb3VibGVfYmlnbWF0YCB0byBhIGBmbG9hdGAgdHlwZSwgd2hpY2ggSSBi
ZWxpZXZlIG1ha2VzIGEgbmV3IGNvcHkgb2YgdGhlIG9iamVjdCBhZnRlciBjYXN0aW5nIGVhY2gg
dmFsdWUsIHNvIHdvdWxkIGRlZmVhdCB0aGUgcHVycG9zZS4gQWxzb8KgYmlnLm1hdHJpeCBvYmpl
Y3RzIGluIFIgb25seSBzdXBwb3J0IGBjaGFyYCwgYHNob3J0YCwgYGludGAsIGFuZCBgZG91Ymxl
YCBhcyB0aGUgdW5kZXJseWluZyBzdG9yYWdlIHR5cGUgKHNlZSBoZWxwKCJiaWcubWF0cml4Iiwg
ImJpZ21lbW9yeSIpKSwgc28geW91IHdvdWxkbid0IGJlIGFibGUgdG8gYWNjZXNzIHRoZSBmbG9h
dCBtYXRyaXggZnJvbSBSIGFzIGEgYmlnLm1hdHJpeCAod2l0aG91dCBjYXN0aW5nIGl0IGJhY2sg
dG8gYSBkb3VibGUpIGFueXdheS48YnI+PGJyPlJlZ2FyZHMsPGJyPjxicj5TY290dA==" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0" class="">​</div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On 2 June 2015 at 12:58, Yue Li <span dir="ltr" class=""><<a href="mailto:gorillayue@gmail.com" target="_blank" class="">gorillayue@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Dear List,<div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">For instance, I have a simple function named ‘print_bigmat’ as shown below.</div><div class=""><br class=""></div><div class="">As shown in the output, ‘double_bigmat' will save the correct values of the original matrix ‘x’ but not ‘float_bigmat’.</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono';color:rgb(0,132,0)" class="">// [[Rcpp::export]]</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'" class=""><span style="color:#bb2ca2" class="">int</span> print_bigmat(SEXP pBigMat) {</div><div style="margin: 0px; font-size: 16px; font-family: 'Bitstream Vera Sans Mono'; min-height: 19px;" class="">    <br class=""></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'" class="">    XPtr<BigMatrix> xpMat(pBigMat);</div><div style="margin: 0px; font-size: 16px; font-family: 'Bitstream Vera Sans Mono'; min-height: 19px;" class="">    <br class=""></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'" class="">    <span style="color:#bb2ca2" class="">const</span> mat& double_bigmat = arma::Mat<<span style="color:#bb2ca2" class="">double</span>>((<span style="color:#bb2ca2" class="">double</span> *)xpMat->matrix(), xpMat->nrow(), xpMat->ncol(), <span style="color:#bb2ca2" class="">false</span>);</div><div style="margin: 0px; font-size: 16px; font-family: 'Bitstream Vera Sans Mono'; min-height: 19px;" class="">    <br class=""></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'" class="">    <span style="color:#bb2ca2" class="">const</span> fmat& float_bigmat = arma::Mat<<span style="color:#bb2ca2" class="">float</span>>((<span style="color:#bb2ca2" class="">float</span> *)xpMat->matrix(), xpMat->nrow(), xpMat->ncol(), <span style="color:#bb2ca2" class="">false</span>);</div><div style="margin: 0px; font-size: 16px; font-family: 'Bitstream Vera Sans Mono'; min-height: 19px;" class="">    <br class=""></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'" class="">    Rcout << double_bigmat << endl;</div><div style="margin: 0px; font-size: 16px; font-family: 'Bitstream Vera Sans Mono'; min-height: 19px;" class="">    <br class=""></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'" class="">    Rcout << float_bigmat << endl;</div><div style="margin: 0px; font-size: 16px; font-family: 'Bitstream Vera Sans Mono'; min-height: 19px;" class="">    <br class=""></div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'" class="">    <span style="color:#bb2ca2" class="">return</span> <span style="color:#272ad8" class="">0</span>;</div><div style="margin:0px;font-size:16px;font-family:'Bitstream Vera Sans Mono'" class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Output:</div><div class=""><br class=""></div><div class=""><div class="">> x <- matrix(rnorm(10), nrow=2)</div><div class="">> x</div><div class="">            [,1]        [,2]      [,3]       [,4]       [,5]</div><div class="">[1,] -0.05514382 -0.03943825 1.4145593 -0.1161918  2.3282466</div><div class="">[2,] -1.22023371 -0.35592125 0.7714512  0.6865120 -0.3504811</div><div class="">> print_bigmat(as.big.matrix(x)@address)</div><div class="">  -0.0551  -0.0394   1.4146  -0.1162   2.3282</div><div class="">  -1.2202  -0.3559   0.7715   0.6865  -0.3505</div><div class=""><br class=""></div><div class="">  -3.3865e-14  -8.6552e+04   4.5441e-07  -5.0912e+23  -1.5184e+34</div><div class="">  -1.3456e+00  -1.9025e+00  -1.2828e+00  -1.6780e+00   1.9268e+00</div><div class=""><br class=""></div><div class="">[1] 0</div><div class="">> </div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Thanks much,</div><div class=""><div class="">Yue</div></div><div class=""><br class=""></div><div class=""><br class=""></div></div><br class="">_______________________________________________<br class="">
Rcpp-devel mailing list<br class="">
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" class="">Rcpp-devel@lists.r-forge.r-project.org</a><br class="">
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank" class="">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></body></html>