[Rcpp-devel] NA_REAL in RcppParallel

Jason Foster jason.j.foster at gmail.com
Thu Jun 2 13:39:05 CEST 2016


Great! Thank you, Hadley.
On Jun 2, 2016 7:30 AM, "Hadley Wickham" <h.wickham at gmail.com> wrote:

> NA_REAL is just a NaN with a special value in the payload, so it's fine to
> use.
> Hadley
>
> On Thu, Jun 2, 2016 at 7:07 AM, Jason Foster <jason.j.foster at gmail.com>
> wrote:
> > Hi,
> >
> > 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
> > (
> http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2016-January/009061.html
> ),
> > 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).
> >
> > The following example is a modified version of "Transforming a Matrix in
> > Parallel using RcppParallel"
> > (http://gallery.rcpp.org/articles/parallel-matrix-transform/) 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):
> >
> > // [[Rcpp::depends(RcppParallel)]]
> > #include <Rcpp.h>
> > #include <RcppParallel.h>
> > using namespace Rcpp;
> > using namespace RcppParallel;
> >
> > struct SquareRoot : public Worker
> > {
> >   // source vector
> >   const RVector<double> input;
> >
> >   // destination vector
> >   RVector<double> output;
> >
> >   // initialize with source and destination
> >   SquareRoot(const NumericVector input, NumericVector output)
> >     : input(input), output(output) {}
> >
> >   // take the square root of the range of elements requested
> >   void operator()(std::size_t begin, std::size_t end) {
> >     for (std::size_t i = begin; i < end; i++) {
> >       if (std::isnan(input[i])) {
> >         output[i] = NA_REAL; // this is the desired output (instead of
> NAN)
> >       } else {
> >         output[i] = sqrt(input[i]);
> >       }
> >     }
> >   }
> > };
> >
> > // [[Rcpp::export]]
> > NumericVector parallelVectorSqrt(NumericVector x) {
> >
> >   // allocate the output vector
> >   NumericVector output(x.size());
> >
> >   // SquareRoot functor (pass input and output vectors)
> >   SquareRoot squareRoot(x, output);
> >
> >   // call parallelFor to do the work
> >   parallelFor(0, x.size(), squareRoot);
> >
> >   // return the output vector
> >   return output;
> >
> > }
> >
> > Any guidance or insight would be much appreciated. Thanks!
> >
> >
> >
> >
> >
> > _______________________________________________
> > Rcpp-devel mailing list
> > Rcpp-devel at lists.r-forge.r-project.org
> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
>
> --
> http://hadley.nz
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160602/184caf4f/attachment-0001.html>


More information about the Rcpp-devel mailing list