Discussion:
[Rcpp-devel] Rcpp-devel Digest, Vol 107, Issue 17
Joseph Wood
2018-09-28 11:50:53 UTC
Permalink
Hey Ralf,

You are right. In the past, when I have had to have a lot of
explanation, that generally points to me lacking understanding.

After reading your example again with your explanation, it seems clear
that I can transition to RcppParallel.

I will respond back here with an update.

I really appreciate all of the help/patience and sorry for the many
rookie mistakes.

Joseph Wood
On Fri, Sep 28, 2018 at 6:00 AM
Send Rcpp-devel mailing list submissions to
To subscribe or unsubscribe via the World Wide Web, visit
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
or, via email, send a message with subject or body 'help' to
You can reach the person managing the list at
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Rcpp-devel digest..."
1. Re: Populate a Matrix in Parallel (Text Version) (Ralf Stubner)
----------------------------------------------------------------------
Message: 1
Date: Fri, 28 Sep 2018 11:07:49 +0200
Subject: Re: [Rcpp-devel] Populate a Matrix in Parallel (Text Version)
Content-Type: text/plain; charset="utf-8"
Hi Joseph,
I understand that parallelism is not a magic wand. Have you read my
original post? I have managed to parallelize generating permutations
by taking advantage of the fact that I can generate the ith
permutation via nthPerm. My question is about making this thread safe
not if it is possible.
unfortunately, you messages are a bit long and contradictory. I had the
same reaction as Jeff when I read
My situation is fundamentally different. The algorithm that fills the
matrix does so in a way that relies on the previous row and more
[...]
If you have code that generates the n-th row based on some global input,
then the example I refered to would be the right starting point. It uses
global state (seed) and column indices to fill a matrix by column. It
does it for multiple columns together, since it is more efficient to
have one thread process multiple columns. However, the default grain
size for parallelFor is one, so it is easy to create one thread per
column and perform some action based on some global input and on the
#include <Rcpp.h>
// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
// [[Rcpp::plugins(cpp11)]]
struct ParallelFill : public RcppParallel::Worker {
RcppParallel::RMatrix<double> output;
RcppParallel::RVector<double> global_input;
ParallelFill(Rcpp::NumericMatrix output,
output(output), global_input(input) {};
std::vector<double> create_column(std::size_t index) {
std::vector<double> result(global_input.size());
std::transform(global_input.begin(),
global_input.end(),
result.begin(),
[&index] (double a) {return a + index;});
return result;
}
// default grain size is 1, i.e. end == begin + 1
void operator()(std::size_t begin, std::size_t end) {
std::vector<double> column = create_column(begin);
std::copy(column.begin(), column.end(),
output.begin() + begin * output.nrow());
}
};
// [[Rcpp::export]]
Rcpp::NumericMatrix parallel_matrix(const int n,
Rcpp::NumericVector input) {
Rcpp::NumericMatrix res(input.length(), n);
ParallelFill parallelFill(res, input);
RcppParallel::parallelFor(0, n, parallelFill);
return res;
}
/*** R
set.seed(42)
res <- parallel_matrix(8, runif(1e7))
head(res)
*/
All this is done *by column*, since matrices in R are stored that way.
cheerio
ralf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20180928/fb0a9e04/attachment-0001.sig>
------------------------------
Subject: Digest Footer
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
------------------------------
End of Rcpp-devel Digest, Vol 107, Issue 17
*******************************************
Loading...