Cost of constructing models of RandomDistribution
I have a fairly large simulation code, that I need to get into a state in which I can multi-thread it. One of my problems is a global random number generator state. I am thinking that I should replace this global object with a collection of generator engines, one for each thread, which would be passed down by non-const reference from the top level of each thread to the places where random numbers are needed. In this case it seems best to pass around engines that model PseudoRandomNumberGenerator. In each place that I want a random number (I rarely need more than 1 or 2 at once or with the same parameters) I would then have to construct and use a RandomDistribution, such as uniform_int, exponential_distribution (or poisson, but that is a question for another day). This approach relies on these models of RandomDistribution being fairly cheap to construct. I cannot see any reason why this should not be the case, but equally I cannot see any promise in the docs that they will be cheap to construct. Am I proposing to use the library in a sensible way? Thanks Robert Patterson
AMDG Robert Patterson wrote:
I have a fairly large simulation code, that I need to get into a state in which I can multi-thread it. One of my problems is a global random number generator state.
I am thinking that I should replace this global object with a collection of generator engines, one for each thread, which would be passed down by non-const reference from the top level of each thread to the places where random numbers are needed. In this case it seems best to pass around engines that model PseudoRandomNumberGenerator. In each place that I want a random number (I rarely need more than 1 or 2 at once or with the same parameters) I would then have to construct and use a RandomDistribution, such as uniform_int, exponential_distribution (or poisson, but that is a question for another day).
This approach relies on these models of RandomDistribution being fairly cheap to construct. I cannot see any reason why this should not be the case, but equally I cannot see any promise in the docs that they will be cheap to construct.
Am I proposing to use the library in a sensible way?
None of the distributions do any expensive precomputation. The exponential and uniform distributions don't need any precomputation. Some of the other distributions, like the binomial and poisson distributions might have some precomputation in the future, but you probably don't want to use them now anyway, as they are very inefficient. In Christ, Steven Watanabe
Robert Patterson
I have a fairly large simulation code, that I need to get into a state in which I can multi-thread it. One of my problems is a global random number generator state.
I ran into the same problem, and for better or worse I created a singleton to manage the global state (to preempt the anti-singleton crowd: I didn't want to construct if not necessary, wanted easy solution to ODR for header only, not worry about destruction order with singleton dependencies, etc.) I used the loki singleton class, though I am would love a boost version. One thing I really wanted to do was have this singleton theadlocal, but wasn't sure how to do it properly. Here is my simple code, including creation of a MKL stream. I use the reset_all_seeds for simulations when I want to repeat results. http://econtoolkit.com/browser/libraries/trunk/etk/etk/random/random.hpp -Jesse
participants (3)
-
Jesse Perla
-
Robert Patterson
-
Steven Watanabe