title: How to call user-defined function in RcppParallel?
body: <p>Inspired by the artical <a href="http://gallery.rcpp.org/articles/parallel-distance-matrix/" rel="noreferrer">http://gallery.rcpp.org/articles/parallel-distance-matrix/</a>, I try to use RcppParallel to run brute-force search in high-dimensional parametric space for backtesting using multithreads. I am stuck in how to call a self-defined function in the <code>struct</code> part. The idea is like this: </p>
  
  <p>First, create a parametric matrix <code>NumericMatrix params_mat</code> in R first, and use the backtesting data with <code>List, NumericVector, CharacterVector</code> datatype, such as <code>List Data_1, NumericVector Data_2, CharacterVector Data_3, ...</code>, which are static for each parametric scenario <code>params_vec</code> (Note that it is the row of <code>params_mat</code>).</p>
  
  <p>Next, define the backtesting function that output a vector that consisting 3 key variables to evaluate  the strategy performance. </p>
  
  <p>Here is an example of my <code>params_mat</code> and <code>Backtesting_Fun</code> that can be run in R and Rcpp, respectively. </p>
  
  <pre><code>//[[Rcpp::export]]
  NumericMatrix data_frame_rcpp(const Rcpp::List&amp; list_params) 
  {
    NumericMatrix res = list_params[0];
    return res;
  }
  
  # R codes to generate params_mat
  params &lt;- expand.grid (x_1=seq(1,100,1), x_2=seq(3,100,2), ..., x_n=seq(4,200,1));                           
  list_params = list(ts(params));
  tmp_params_data = data_frame_rcpp(list_params);                                              
  params_mat = matrix(tmp_params_data, ncol = ncol(tmp_params_data), dimnames = NULL); 
  params_vec = params_mat[ii,];
  
  # User-defined Rcpp codes for backtesting
  NumericVector Backtesting_Fun (List Data_1, NumericVector Data_2, CharacterVector Data_3, ..., NumericVector params_vec)
  {
    // Main function parts to run backtesting for each params_vec scenario.
    ... etc
  
    // save 3 key result variables together with each params_vec (just a simple illustration).
    NumericVector res = NumericVector::create(params_vec[0],...,params_vec[size-1],
                                              key_1, key_2, key_3); 
    return res;
  }
  </code></pre>
  
  <p>Certainly we need to rewrite/modify the original Rcpp <code>Backtesting_Fun</code> with RVector/RMatrix types, and then use the following <code>RcppParallel</code>codes to call <code>Backtesting_Fun</code> in <code>struct Backtest_parallel</code>: </p>
  
  <pre><code>// [[Rcpp::depends(RcppParallel)]]
  #include &lt;RcppParallel.h&gt;
  using namespace RcppParallel;
  
  RVector&lt;double&gt; Backtesting_Fun (const RVector&lt;double&gt; Data_1, const RVector&lt;double&gt; Data_2, 
                                  const RVector&lt;string&gt; Data_3,..., const RVector&lt;double&gt; params_vec)
  {
     // Main function parts to run backtesting for each params_vec scenario.
     ... etc;
  
     // save 3 key result variables together with each params_vec
     ... etc; 
  
     return res;
  }
  
  struct Backtest_parallel : public Worker 
  {       
     // input matrix to read from
     const RVector&lt;List&gt; Data_1;
     const RVector&lt;double&gt; Data_2;
     const RVector&lt;string&gt; Data_3;
     ...
     const RMatrix&lt;double&gt; params_mat;
  
     // output matrix to write to
     RMatrix&lt;double&gt; rmat;
  
     // initialize from Rcpp input and output matrixes (the RMatrix class
     // can be automatically converted to from the Rcpp matrix type)
     Backtest_parallel(const List Data_1, const NumericVector Data_2, 
     const CharacterVector Data_3, ..., const NumericMatrix params_mat)
        : Data_1(Data_1), Data_2(Data_2), Data_3(Data_3), ..., params_mat(params_mat) {}
  
     // function call operator that work for the specified range (begin/end)
     void operator()(std::size_t begin, std::size_t end) 
     {
        for (std::size_t ii = begin; ii &lt; end; i++) 
        {
           // params rows that we will operate on
           RMatrix&lt;double&gt;::Row params_row = params_mat.row(ii);
  
           // Run the backtesting function defined above
           RVector&lt;double&gt; res = Backtesting_Fun(Data_1, Data_2, ..., params_row)
           for (std::size_t jj = 0; jj &lt; res.length(); jj++) 
           {
              // write to output matrix
              rmat(ii,jj) = res[jj];
           }
        }
     }
  };
  
  // [[Rcpp::export]]
  NumericMatrix rcpp_parallel_backtest(List Data_1, NumericVector Data_2, CharacterVector Data_3,
                                       ..., NumericMatrix params_mat) 
  {      
     // allocate the matrix we will return
     NumericMatrix rmat(params_mat.nrow(), params_mat.nrow()+3);
  
     // create the worker
     Backtest_parallel backtest_parallel(Data_1, Date_2, ..., params_mat);
  
     // call it with parallelFor
     parallelFor(0, rmat.nrow(), backtest_parallel);
  
     return rmat;
  }
  </code></pre>
  
  <p>Here are my questions:</p>
  
  <ol>
  <li><p>Can <code>RVector</code> contains <code>List</code> datatype, or is there any specific container in <code>RcppParallel</code> to contain <code>List</code>; </p></li>
  <li><p>In the <code>Backtesting_Fun</code>, the input should be <code>RVector/RMatrix</code> types, does that mean we really need to convert the orginal Rcpp main codes with <code>NumericVector</code> into <code>RVector</code>? </p></li>
  </ol>
  
  <p>Or is there any better way to do parallel computing for my case in RcppParallel? Thanks in advance.</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <ol>
  <li><p>I look at the other examples regarding RcppPararrel in <a href="http://gallery.rcpp.org/articles/parallel-matrix-transform/" rel="noreferrer">http://gallery.rcpp.org/articles/parallel-matrix-transform/</a>, <a href="http://gallery.rcpp.org/articles/parallel-inner-product/" rel="noreferrer">http://gallery.rcpp.org/articles/parallel-inner-product/</a>, the common idea in <code>struct operator()</code>is to use pointers to manipulate the data input for <code>operator()</code>, so is there any way to build a user defined function in my case with pointer inputs? </p></li>
  <li><p>If the above way doesn't work, is it feasible to use <code>wrap</code> to convert <code>RVector/RMatrix</code> back into Rcpp datatype, i.e., <code>NumericVector..</code> in <code>operator()</code> so that the input types of user-defined function <code>Backtesting_Fun</code> can remain unchanged.</p></li>
  </ol>