<div dir="ltr"><div style="font-size:12.8px"><font face="monospace, monospace">> if (std::isnan(input[i])) {</font></div><div style="font-size:12.8px"><font face="monospace, monospace">> output[i] = NA_REAL; // this is the desired output (instead of NAN)</font></div><div style="font-size:12.8px"><font face="monospace, monospace"><br></font></div><div style="font-size:12.8px"><font face="monospace, monospace">In this case I think you could replace the second line with</font></div><div style="font-size:12.8px"><font face="monospace, monospace"> output[i] = input[i];</font></div><div style="font-size:12.8px"><font face="monospace, monospace">to emulate the usual R mapping of NA to NA and NaN to NaN.</font></div><div style="font-size:12.8px"><font face="monospace, monospace"><br></font></div><div style="font-size:12.8px"><font face="monospace, monospace">(I have seen C compilers that messed up my proposed assignment by mapping</font></div><div style="font-size:12.8px"><font face="monospace, monospace">all NaN values to a common one, but they would mess up the original</font></div><div style="font-size:12.8px"><font face="monospace, monospace">one also. That is one reason S and S+ had a set_na(void* data, int mode)</font></div><div style="font-size:12.8px"><font face="monospace, monospace">function.)</font></div><div style="font-size:12.8px"><font face="monospace, monospace"><br></font></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">Bill Dunlap<br>TIBCO Software<br>wdunlap <a href="http://tibco.com" target="_blank">tibco.com</a></div></div>
<br><div class="gmail_quote">On Thu, Jun 2, 2016 at 4:07 AM, Jason Foster <span dir="ltr"><<a href="mailto:jason.j.foster@gmail.com" target="_blank">jason.j.foster@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 dir="ltr">Hi,<div><br></div><div>My question is how can I return a thread safe NA_REAL using RcppParallel? I've read many posts about how it's not thread safe to call R or Rcpp APIs when using RcppParallel (<a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-January/009061.html" target="_blank">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-January/009061.html</a>), so in the past I've played it safe by returning NAN in C++ instead (as it's my understanding NA_REAL is specific to R).</div><div><br></div><div>The following example is a modified version of "Transforming a Matrix in Parallel using RcppParallel" (<a href="http://gallery.rcpp.org/articles/parallel-matrix-transform/" target="_blank">http://gallery.rcpp.org/articles/parallel-matrix-transform/</a>) that checks whether the input value is NA before taking the sqrt, otherwise NA is returned (note: this is the desired behavior and for illustrative purposes only):</div><div><div><br></div><div><div><font face="monospace, monospace">// [[Rcpp::depends(RcppParallel)]]</font></div><div><font face="monospace, monospace">#include <Rcpp.h></font></div><div><font face="monospace, monospace">#include <RcppParallel.h></font></div><div><font face="monospace, monospace">using namespace Rcpp;</font></div><div><font face="monospace, monospace">using namespace RcppParallel;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">struct SquareRoot : public Worker</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace"> // source vector</font></div><div><font face="monospace, monospace"> const RVector<double> input;</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> // destination vector</font></div><div><font face="monospace, monospace"> RVector<double> output;</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> // initialize with source and destination</font></div><div><font face="monospace, monospace"> SquareRoot(const NumericVector input, NumericVector output) </font></div><div><font face="monospace, monospace"> : input(input), output(output) {}</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> // take the square root of the range of elements requested</font></div><div><font face="monospace, monospace"> void operator()(std::size_t begin, std::size_t end) {</font></div><div><font face="monospace, monospace"> for (std::size_t i = begin; i < end; i++) {</font></div><div><font face="monospace, monospace"> if (std::isnan(input[i])) {</font></div><div><font face="monospace, monospace"> output[i] = NA_REAL; // this is the desired output (instead of NAN)</font></div><div><font face="monospace, monospace"> } else {</font></div><div><font face="monospace, monospace"> output[i] = sqrt(input[i]);</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace">};</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">// [[Rcpp::export]]</font></div><div><font face="monospace, monospace">NumericVector parallelVectorSqrt(NumericVector x) {</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> // allocate the output vector</font></div><div><font face="monospace, monospace"> NumericVector output(x.size());</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> // SquareRoot functor (pass input and output vectors)</font></div><div><font face="monospace, monospace"> SquareRoot squareRoot(x, output);</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> // call parallelFor to do the work</font></div><div><font face="monospace, monospace"> parallelFor(0, x.size(), squareRoot);</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace"> // return the output vector</font></div><div><font face="monospace, monospace"> return output;</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace">}</font></div></div><div><br></div><div>Any guidance or insight would be much appreciated. Thanks!<br></div><div><br></div><div><br></div><div><br></div><div><br></div></div></div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">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" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>