[Rcpp-devel] Grid Search in RcppParallel

2014-10-04 Thread Jeffrey Wong
I am trying to use RcppParallel to do a fast grid search.  The idea is to
construct a matrix using R's expand.grid, then for each row of that matrix,
x, call f(x).  For simplicity the function f will always return a double.
Since Rcpp can call an R function I was hoping RcppParallel would allow me
to use a parallelFor to process the rows of the grid quickly.  However, it
keeps crashing R, and I am wondering if this is because an R function
cannot be passed to RcppParallel?

require(Rcpp)
require(RcppParallel)

grid = as.matrix(expand.grid(a = 1:5, b = 1:5))

foo = function(pars) {pars[1]^2 + pars[2]^2}

RcppParallelGridSearch(grid, foo)

in .cpp file

#include 
using namespace Rcpp;

// [[Rcpp::depends(RcppParallel)]]
#include 
using namespace RcppParallel;

struct GridSearch : public Worker
{
   // source matrix
   const RMatrix grid;
   const Function f;

   // destination vector
   RVector output;

   // initialize with source and destination
   GridSearch(const NumericMatrix grid, const Function f, NumericVector
output)
  : grid(grid), f(f), 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++) {
RMatrix::Row parameters = grid.row(i);
output[i] = as(f(parameters));
  }
   }
};

// [[Rcpp::export]]
NumericVector RcppParallelGridSearch(NumericMatrix grid, Function f) {

  // allocate the output matrix
  NumericVector output(grid.nrow());

  // SquareRoot functor (pass input and output matrixes)
  GridSearch gridSearch(grid, f, output);

  // call parallelFor to do the work
  parallelFor(0, grid.nrow(), gridSearch);

  // return the output matrix
  return output;
}

-- 
Jeffrey Wong
My portfolio: http://jeffreycwong.com 
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Grid Search in RcppParallel

2014-10-04 Thread Romain Francois
Envoyé de mon iPhone

> Le 5 oct. 2014 à 07:51, Jeffrey Wong  a écrit :
> 
> I am trying to use RcppParallel to do a fast grid search.  The idea is to 
> construct a matrix using R's expand.grid, then for each row of that matrix, 
> x, call f(x).  For simplicity the function f will always return a double.  
> Since Rcpp can call an R function I was hoping RcppParallel would allow me to 
> use a parallelFor to process the rows of the grid quickly.  However, it keeps 
> crashing R, and I am wondering if this is because an R function cannot be 
> passed to RcppParallel?

You definitely cannot call an R function concurrently. 

> require(Rcpp)
> require(RcppParallel)
> 
> grid = as.matrix(expand.grid(a = 1:5, b = 1:5))
> 
> foo = function(pars) {pars[1]^2 + pars[2]^2}
> 
> RcppParallelGridSearch(grid, foo)
> 
> in .cpp file
> 
> #include 
> using namespace Rcpp;
> 
> // [[Rcpp::depends(RcppParallel)]]
> #include 
> using namespace RcppParallel;
> 
> struct GridSearch : public Worker
> {
>// source matrix
>const RMatrix grid;
>const Function f;
>
>// destination vector
>RVector output;
>
>// initialize with source and destination
>GridSearch(const NumericMatrix grid, const Function f, NumericVector 
> output) 
>   : grid(grid), f(f), 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++) {
> RMatrix::Row parameters = grid.row(i);
> output[i] = as(f(parameters));
>   }
>}
> };
> 
> // [[Rcpp::export]]
> NumericVector RcppParallelGridSearch(NumericMatrix grid, Function f) {
>   
>   // allocate the output matrix
>   NumericVector output(grid.nrow());
>   
>   // SquareRoot functor (pass input and output matrixes)
>   GridSearch gridSearch(grid, f, output);
>   
>   // call parallelFor to do the work
>   parallelFor(0, grid.nrow(), gridSearch);
>   
>   // return the output matrix
>   return output;
> }
> 
> -- 
> Jeffrey Wong
> My portfolio: http://jeffreycwong.com
> 
> ___
> Rcpp-devel mailing list
> Rcpp-devel@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Grid Search in RcppParallel

2014-10-05 Thread JJ Allaire
For executing R code in parallel you you may want to take a look at
multicore (part of the parallel package built in to R).

On Sun, Oct 5, 2014 at 1:51 AM, Jeffrey Wong  wrote:
> I am trying to use RcppParallel to do a fast grid search.  The idea is to
> construct a matrix using R's expand.grid, then for each row of that matrix,
> x, call f(x).  For simplicity the function f will always return a double.
> Since Rcpp can call an R function I was hoping RcppParallel would allow me
> to use a parallelFor to process the rows of the grid quickly.  However, it
> keeps crashing R, and I am wondering if this is because an R function cannot
> be passed to RcppParallel?
>
> require(Rcpp)
> require(RcppParallel)
>
> grid = as.matrix(expand.grid(a = 1:5, b = 1:5))
>
> foo = function(pars) {pars[1]^2 + pars[2]^2}
>
> RcppParallelGridSearch(grid, foo)
>
> in .cpp file
>
> #include 
> using namespace Rcpp;
>
> // [[Rcpp::depends(RcppParallel)]]
> #include 
> using namespace RcppParallel;
>
> struct GridSearch : public Worker
> {
>// source matrix
>const RMatrix grid;
>const Function f;
>
>// destination vector
>RVector output;
>
>// initialize with source and destination
>GridSearch(const NumericMatrix grid, const Function f, NumericVector
> output)
>   : grid(grid), f(f), 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++) {
> RMatrix::Row parameters = grid.row(i);
> output[i] = as(f(parameters));
>   }
>}
> };
>
> // [[Rcpp::export]]
> NumericVector RcppParallelGridSearch(NumericMatrix grid, Function f) {
>
>   // allocate the output matrix
>   NumericVector output(grid.nrow());
>
>   // SquareRoot functor (pass input and output matrixes)
>   GridSearch gridSearch(grid, f, output);
>
>   // call parallelFor to do the work
>   parallelFor(0, grid.nrow(), gridSearch);
>
>   // return the output matrix
>   return output;
> }
>
> --
> Jeffrey Wong
> My portfolio: http://jeffreycwong.com
>
>
> ___
> Rcpp-devel mailing list
> Rcpp-devel@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel