Reputation: 53
I'm attempting to write code in C++ that loops over an Rcpp::List
of IntegerVector
s. The computation itself is complicated to explain, but for the sake of example let's suppose that I wanted to take the sum of sums of each IntegerVector
(ignoring the fact that I could concatenate this list into one IntegerVector
). This could be achieved by the following code:
int sumIntegerVector(const Rcpp::IntegerVector vec) {
int sum = 0;
for (int i = 0; i < vec.length(); i++) {
sum += vec[i];
}
return sum;
}
int sumList(const Rcpp::List l){
int sum = 0;
for (int j = 0; j < l.length(); j++) {
sum += sumIntegerVector(l[j]);
}
return sum;
}
Now, I would like to make this computation thread safe, so that I could parallelize over the loop in the sumList
function (and not the sumIntegerVector
function). Is there any thread safe way of doing this? I'm aware that Rcpp has a template RVector
for thread safety. However, because I am looping over a list, it doesn't seem like the RVector
template would be sufficient. Is there an RList
template or something equivalent? Otherwise, could someone suggest a workaround?
Thanks!
Upvotes: 1
Views: 83