Boost logo

Boost :

Subject: Re: [boost] [random] new threefry random engine
From: Thijs van den Berg (thijs_at_[hidden])
Date: 2014-05-02 08:49:56


On 02 May 2014, at 14:32, Peter Dimov <lists_at_[hidden]> wrote:

> Thijs van den Berg wrote:
>> In that case it’s best to use a random engine adaptor.
>>
>> counter_based_engine< Threefry<2,unsigned> > eng;
>> uniform_real_distribution<float> zero_one(0., 1.);
>>
>> eng.seed(thread_id);
>>
>> for(int i=0; i<n; ++i)
>> {
>> out[i] = in[i]*zero_one( eng );
>> }
>
> The adaptor needs to be in the loop; you don't have a thread_id outside it.
ok, .. so the block inside the loop gets automatically distributed across threads.
>
> uniform_real_distribution<float> zero_one(0., 1.);
>
> #pragma omp parallel for
> for( int i=0; i<n; ++i )
> {
> counter_based_engine< Threefry<2,unsigned> > eng;
> eng.seed( i );
>
> out[i] = in[i]*zero_one( eng );
> }
>
> In principle, this works with any engine, not just a counter-based one, as long as creating and seeding is quick and consecutive seeds result in random first output.
>
The way I read this ”first random output” is that you are saying that it will only use 1 random number from the block generated by the random function (for threefry e.g. 4x 64bits)? This is probably an artefact of the example you chose? Normally each thread would do millions of samples? I thought the idea was to *not* have the engine store a counter but use the “i” from the loop. I now understand that the loop is the parallel part.

The counter_based_engine design I’m working on (I’ll share an example soon) works with 3 type of random function:
* random function that convert an input buffer to an output buffer. For these I use the input as an counter, and let the seed() set the highest bits of the counter. The engine uses the following random function interfaces
void random_function(input&,output&)

* random function that also use an additional key. For this version I have the seed() random engine set a key buffer, and that key gets passed to the random function
random_function(input&,output&,key&)

* random function that have a state dependent of the key, and computing the state takes some time. These random functions have an additional member function that the counter_engine uses when it gets seeded
random_function.set_key(key&)// called by random engine seed()
...
random_function(input&,output&) // sometimes called by random engine operator(), whenever it needs a new batch of random data


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk