Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make each thread use its own RNG in C++11

I'm using the new random number generators in in C++11. Although there are varying opinions, from this thread it seems that the majority believe they are not thread safe. As a consequence, I would like to make a program, where each thread uses its own RNG.

An example is given in the related discussion of how to accomplish this with OpenMP:

#include <random>
#include <iostream>
#include <time.h>
#include "omp.h"

using namespace std;



int main()
{
    unsigned long long app = 0;
    {
        //mt19937_64 engine((omp_get_thread_num() + 1)); //USE FOR MULTITHREADING
        mt19937_64 engine; //USE FOR SINGLE THREAD
        uniform_real_distribution<double> zeroToOne(0.0, 1.0);

        //#pragma omp parallel for reduction(+:app) //USE FOR MULTITHREADING
        for (unsigned long long i = 0; i < 2000000000; i++)
        {
            if(zeroToOne(engine) < 0.5) app++;
        }
    }
    cout << app << endl;
    return 0;
}

When I run the multi-threaded and single-threaded version of this program and keep track of the time, they take the same amount of time to finish after execution. Also, app does not have the same size in the two cases, but I suspect that is merely because of the different seeds.

Question: Does the provided example correctly show how to force each thread to use its own RNG? If not, can I see an example of how this is done, or get a reference to some place where they explain how to achieve this?

like image 950
BillyJean Avatar asked Apr 10 '13 06:04

BillyJean


2 Answers

You must no share instances of random engine between multiple threads. You should either lock a single engine or create one engine for each thread (with different seed (please note the answer of e4e5f4 regarding creation of parallel MT engines)). In case of OpenMP you can easily store one engine per thread in a vector and retrieve it by result of omp_get_thread_num() which lies between 0 and omp_get_num_threads()–1.

class RNG
{
public:
    typedef std::mt19937 Engine;
    typedef std::uniform_real_distribution<double> Distribution;

    RNG() : engines(), distribution(0.0, 1.0)
    {
        int threads = std::max(1, omp_get_max_threads());
        for(int seed = 0; seed < threads; ++seed)
        {
            engines.push_back(Engine(seed));
        }
    }

    double operator()()
    {
        int id = omp_get_thread_num();
        return distribution(engines[id]);
    }

    std::vector<Engine> engines;
    Distribution distribution;
};

int main()
{
     RNG rand;
     unsigned long app = 0;

     #pragma omp parallel for reduction(+:app)
     for (unsigned long long i = 0; i < 2000000000; i++)
     {
         if(rand() < 0.5) app++;
     }
}
like image 192
hansmaad Avatar answered Oct 09 '22 09:10

hansmaad


I would refrain from using random seeding. It might end up with overlapping streams. This will eventually affect the final statistic.

I would suggest some tried and tested solution like this

like image 41
Nishanth Avatar answered Oct 09 '22 09:10

Nishanth