<p dir="ltr">Great! Thank you, Hadley.</p>
<div class="gmail_quote">On Jun 2, 2016 7:30 AM, "Hadley Wickham" <<a href="mailto:h.wickham@gmail.com">h.wickham@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">NA_REAL is just a NaN with a special value in the payload, so it's fine to use.<br>
Hadley<br>
<br>
On Thu, Jun 2, 2016 at 7:07 AM, Jason Foster <<a href="mailto:jason.j.foster@gmail.com">jason.j.foster@gmail.com</a>> wrote:<br>
> Hi,<br>
><br>
> My question is how can I return a thread safe NA_REAL using RcppParallel?<br>
> I've read many posts about how it's not thread safe to call R or Rcpp APIs<br>
> when using RcppParallel<br>
> (<a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-January/009061.html" rel="noreferrer" target="_blank">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-January/009061.html</a>),<br>
> so in the past I've played it safe by returning NAN in C++ instead (as it's<br>
> my understanding NA_REAL is specific to R).<br>
><br>
> The following example is a modified version of "Transforming a Matrix in<br>
> Parallel using RcppParallel"<br>
> (<a href="http://gallery.rcpp.org/articles/parallel-matrix-transform/" rel="noreferrer" target="_blank">http://gallery.rcpp.org/articles/parallel-matrix-transform/</a>) that checks<br>
> whether the input value is NA before taking the sqrt, otherwise NA is<br>
> returned (note: this is the desired behavior and for illustrative purposes<br>
> only):<br>
><br>
> // [[Rcpp::depends(RcppParallel)]]<br>
> #include <Rcpp.h><br>
> #include <RcppParallel.h><br>
> using namespace Rcpp;<br>
> using namespace RcppParallel;<br>
><br>
> struct SquareRoot : public Worker<br>
> {<br>
>   // source vector<br>
>   const RVector<double> input;<br>
><br>
>   // destination vector<br>
>   RVector<double> output;<br>
><br>
>   // initialize with source and destination<br>
>   SquareRoot(const NumericVector input, NumericVector output)<br>
>     : input(input), output(output) {}<br>
><br>
>   // take the square root of the range of elements requested<br>
>   void operator()(std::size_t begin, std::size_t end) {<br>
>     for (std::size_t i = begin; i < end; i++) {<br>
>       if (std::isnan(input[i])) {<br>
>         output[i] = NA_REAL; // this is the desired output (instead of NAN)<br>
>       } else {<br>
>         output[i] = sqrt(input[i]);<br>
>       }<br>
>     }<br>
>   }<br>
> };<br>
><br>
> // [[Rcpp::export]]<br>
> NumericVector parallelVectorSqrt(NumericVector x) {<br>
><br>
>   // allocate the output vector<br>
>   NumericVector output(x.size());<br>
><br>
>   // SquareRoot functor (pass input and output vectors)<br>
>   SquareRoot squareRoot(x, output);<br>
><br>
>   // call parallelFor to do the work<br>
>   parallelFor(0, x.size(), squareRoot);<br>
><br>
>   // return the output vector<br>
>   return output;<br>
><br>
> }<br>
><br>
> Any guidance or insight would be much appreciated. Thanks!<br>
><br>
><br>
><br>
><br>
><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>
<br>
<br>
<br>
--<br>
<a href="http://hadley.nz" rel="noreferrer" target="_blank">http://hadley.nz</a><br>
</blockquote></div>