I would like someone who understands better than me to help me implement a mclapply with rcpp in a project I am working on. The fragment I want to transfer is this one, where data_by is a list of data.table and if a parameter of a function called core is not null, it calls rcpp_mclapply, but rather rcpp_lapply, which is the example that is referenced in the rcpp documentation
data_by <- split(data, by = by)
if (!is.null(cores)) {
M_list <- rcpp_mclapply(data_by, function(d) {
data_tmp <- get_internal_data(data = d, vars = c(group, unit))
cbind(unique(d[, ..by]), M = M_value(data = data_tmp, group = group, unit = unit))
})
example that returns data_by, which are many factor type columns and fw is numerical
$region_1
year school district csep ethnicity rural region sch_type gender grade fw
1: 2016 4531 8101 partially-subsidized non-indian urban region_1 public female 4 22
2: 2016 4531 8101 subsidized non-indian urban region_1 public female 4 19
3: 2016 4531 8101 subsidized indian urban region_1 public female 4 2
4: 2016 4531 8101 non-subsidized non-indian urban region_1 public female 4 5
5: 2016 4531 8101 partially-subsidized indian urban region_1 public female 4 2
$region_2
...
example of what mcapply would return
[[1]]
region M
1: region_1 0.2312423
[[2]]
...
When trying to change the normal for with a parrallelFor, the program dies when trying to iterate.
using namespace Rcpp;
using namespace RcppThread;
// [[Rcpp::export]]
List rcpp_mclapply(List input, Function f) {
R_xlen_t n = input.length();
List out(n);
parallelFor(0, n, [&] (int i) {
out[i] = f(input[i]);
});
return out;
}
Before, in another project where I only worked with data.frame of numbers, I converted these into an rcpparmadillo matrix, but when trying to do something here it became impossible, since since not all the columns are not numeric, they cannot be converted, much less make the list as a vec and also tried using std::vector but also without success. The ideal thing I was looking for is to use the function as an expression since in the project I am doing there are several calls to mclapply from R and I would like to make the modifications directly to the data in rcpp in only 1 file if it were possible but when calling parallelFor Don't worry about Rcpp so it doesn't crash.
I also have doubts if the program dies when also occupying the Rcpp Function function if it could achieve the data conversion correctly.