Skip to content

[Rcpp-devel] Mersenne Twister in RcppArmadillo?

8 messages · Conrad S, Dirk Eddelbuettel, Simon Zehnder

#
Hi everyone,

In Armadillo 3.800 I've added .imbue(), allowing a matrix to be filled
using a functor or C++11 lambda expression. This allows the use of the
Mersenne twister random number engine built into C++11.  For example:

std::mt19937 engine;
std::uniform_real_distribution<double> distr(0.0, 1.0);

arma::mat X(4,5);
X.imbue( [&]() { return distr(engine); } );

More information at:
http://arma.sourceforge.net/docs.html#imbue

Dirk is likely to release the corresponding RcppArmadillo version soon.
On Fri, Feb 15, 2013 at 7:10 AM, Simon Zehnder <szehnder at uni-bonn.de> wrote:
#
On 3 March 2013 at 01:09, Conrad S wrote:
| Hi everyone,
| 
| In Armadillo 3.800 I've added .imbue(), allowing a matrix to be filled
| using a functor or C++11 lambda expression. This allows the use of the
| Mersenne twister random number engine built into C++11.  For example:
| 
| std::mt19937 engine;
| std::uniform_real_distribution<double> distr(0.0, 1.0);
| 
| arma::mat X(4,5);
| X.imbue( [&]() { return distr(engine); } );

Nice.  I should time this / update the timing example on the Rcpp Gallery.

| More information at:
| http://arma.sourceforge.net/docs.html#imbue
| 
| Dirk is likely to release the corresponding RcppArmadillo version soon.

(He did yesterday morning; it is now on CRAN. "They" had to deliberate for 20
hours whether we would be allowed in having violated the 'no more than six
releases in six months' preference.)

Dirk
#
Beautiful Conrad! 

This is exactly what helps to make individual random number generation easier in Armadillo/RcppArmadillo! Up to now I use solely the standard C++ RNG engine (std::mt19937) and run a loop. Now I can just use .imbue on a matrix and do not have to care about indices and index bounds. Does it work with iterators inside of .imbue? Do you think it is faster than a simple for loop? Let's wait for Dirk's experiment for the Rcpp gallery.  

Best

Simon
On Mar 2, 2013, at 4:09 PM, Conrad S <conradsand.arma at gmail.com> wrote:

            
#
That was fast Dirk! I will update my packages on Monday! Thank you for that quick release! 


Best

Simon
On Mar 2, 2013, at 4:56 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
#
On 2 March 2013 at 22:40, Simon Zehnder wrote:
| That was fast Dirk! I will update my packages on Monday! Thank you for that quick release! 

Well I have been releasing within 24 hours of Conrad all along for what must
now be two dozen releases ... so my release wasn't exactly news.

As for your other point, you also had

   NumericMatrix = rnorm(n, k);

for a long time, so there was no need to loop when you used Rcpp.

Dirk
#
Hi Dirk,

I recognized the function rnorm in Rcpp. But as I work most times with RcppArmadillo and Armadillo objects I wanted to avoid constructing NumericMatrix objects, fill them and convert them to arma::mat objects. Instead I decided to immediately generate arma::mat objects and fill them - which was impossible without a loop when using a controlled random number generation (for instance with the possibility to set the seed).

I would like to ask something connected to the new feature:
The C++ standard library (random) uses specific functions for random number generation (for example std::gamma_distribution) , that are only available when using a compiler supporting the C++11 standard. As far as I know R uses C++99. So in a package these functions would be useless when redistribution should be made possible. Do you know about some comments by the R core team regarding the C++11 standard? Does it come soon?

Best Simon
On Mar 2, 2013, at 11:09 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
#
Simon,
On 3 March 2013 at 11:52, Simon Zehnder wrote:
| Hi Dirk,
| 
| I recognized the function rnorm in Rcpp. But as I work most times with RcppArmadillo and Armadillo objects I wanted to avoid constructing NumericMatrix objects, fill them and convert them to arma::mat objects. Instead I decided to immediately generate arma::mat objects and fill them - which was impossible without a loop when using a controlled random number generation (for instance with the possibility to set the seed).

You need just two lines (one to call rnorm, and one use the result to
instantiate an arma mat using the constructor using the same memory). No
loop. One call to the RNG.
 
| I would like to ask something connected to the new feature:
| The C++ standard library (random) uses specific functions for random number generation (for example std::gamma_distribution) , that are only available when using a compiler supporting the C++11 standard. As far as I know R uses C++99. So in a package these functions would be useless when redistribution should be made possible. Do you know about some comments by the R core team regarding the C++11 standard? Does it come soon?
 
R being a C project, there aren't any strong C++ advocates on the R Core
team though some (like Duncan Murdoch) use it.  The main blocker is CRAN
which is not very forthcoming in communications, and it seems to be one (very
prominent) CRAN maintainer and R Core member in particular...

Dirk
#
Hi Dirk,

sorry for my premature judgement. You are right. Doing it the way you suggest below, should indeed give a very fast implementation, using the same memory. 

In regard to the C++11 standard in R: This is just constricting the possibilities R gives, in my opinion. And the fact, that most people in HPC use either Fortran or C++ - only a few use C - seems to point to the adequacy of C++ in this area of programming, which becomes more and more important today. Furthermore, C++ is an OO language and as R implements OOP as well, it seems to me a little inconsistent, that the language extending it is entirely not OO. 

Let us see how the things develop in near future. I think with Rcpp and RcppArmadillo R programmers got a powerful and still comfortable tool at hand and code/packages using C++ will accelerate. I myself use RcppArmadillo for a package that performs Bayesian Simulation and relies on S4 classes in R. It maps S4 objects to C++ objects, which also lets developers, who want to extend the package later on, understand the code more easily. Furthermore readability is enhanced in general, when using classes and I do not have to mention here Cs sometimes dangerous pointer arithmetic. 

Well enough here! First version of my package will rely on C++99 standard to make it possible to get installed on every R framework. Armadillo will give its warnings towards using gcc 4.7.1. I just hope, it runs with gcc 4.4 (my god, this version is really old)

Best
Simon
On Mar 3, 2013, at 2:02 PM, Dirk Eddelbuettel <edd at debian.org> wrote: