Blitz logo

Blitz Support :

From: Todd Veldhuizen (tveldhui_at_[hidden])
Date: 2003-07-19 18:15:18


Hi Patrik,

1. There's no built-in way to do this. However, it should be
straightforward to add this capability to whichever underlying
RNG you want to use.

MersenneTwister (random/mt.h) keeps its state as a std::vector<unsigned>
plus an iterator. (This is the default generator). I'd recommend adding
methods like this to MersenneTwister or whatever:

    public size_t getStateSize(); // Buffer size required for state
    public void storeState(void*);
    public void restoreState(void*);

There's no reason such methods couldn't be incorporated into the
blitz cvs tree if you submit patches.

2. The RNGs themselves are not thread safe, so yes they would be
corrupted if you had multiple threads sharing a single RNG. Simple enough
to inherit from e.g. MersenneTwister and add a mutex guard to the
generator. (Again, consider submitting patches if you do this). Or the
easy way out is to just put a mutex guard around your own method that gets
random numbers.

If you don't share the RNG amongst threads, then there's a problem of
having multiple random number generators running in parallel and possibly
duplicating some of the numbers they generate. I'm not up on exactly
how big an issue this is. Obviously it's really bad if you start all
your RNGs with the same seed, but if they all have different seeds
it's probably more complicated. The MersenneTwister generator isn't
supposed to repeat until after 2^19937-1 numbers or so.

Hope that helps! Let me know if you need more detail about how to
make any changes.

Cheers,
Todd

-- 
Todd Veldhuizen  /  tveldhui_at_[hidden]  /  Indiana University Computer Science
On Sat, 19 Jul 2003, Patrik wrote:
> Hi,
>
> I have two questions about the random member generators.
>
> 1.  I would like to be able to save the state of the rng, so I can continue
> the same sequence when the program restarts.  Is this possible?  I don't
> see anything in the documentation about it.
>
> 2.  There's a note saying the rng's are "not suitable" for parallel
> programs.  What does this mean?  Does it mean that the shared data
> structure is not protected even if compiled with bz_threadsafe and they
> will be corrupted if multiple threads try to use them?  Or does it simply
> mean that they are not efficient for concurrent access?  Or any other reason?
>
> Thanks,
>
> /Patrik
>
> _______________________________________________
> Blitz-support mailing list
> Blitz-support_at_[hidden]
> http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>