0

This codeblock

  try
  {
    for(int i=0;i<N;i++)
    {
        std::shared_ptr<std::random_device> tmp=std::shared_ptr<std::random_device>(new std::random_device);
        rdThread.push_back(tmp);
        rngThread.push_back(std::mt19937((*rdThread[rdThread.size()-1])()));
        distThread.push_back(std::uniform_real_distribution<T>(0,1));
    }
  }
  catch(std::exception & ex)
  {
      std::cout<<ex.what()<<std::endl;
  }

produces an exception when N is 2000 or similar high value. Error message of runtime exception is:

random_device::random_device(const std::string&)
Segmentation fault (core dumped)

I can't find if error is about some std::string that random_device uses or gcc's internal limit on this number generator. But push_back into vector or dereferencing in next push_back seems to be using it somehow.

Is it possible that a compiler may not support a few thousands of random number generators?

rdThread is (shared_ptr)random_device vector.

rngThread is vector of mt19937.

distThread is a vector of uniform real distributions.

All are working fine for less than 2000 items, in a multithreaded part but this error is only on this initialization block.

  • g++ (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1
  • C++1y dialect is on
  • Optimization O3 is on
  • -m64 -mtune=generic compile options are included
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
  • 3
    I may be beside the point... but why are you generating so many random devices? Surely all the MT engines can share the same one, can they not? Or are you warding off data races? – StoryTeller - Unslander Monica Sep 20 '18 at 10:18
  • I started with 8(also my cpu is 8 threaded) then increased instances of objects to do totally independent simulations on 1000 items. Then I will move to CUDA. Before that, I need to acknowledge I can use thousands of different generators without breaking simulation (a genetic algorithm) behavior. Then, I will make my own LCG on CUDA. – huseyin tugrul buyukisik Sep 20 '18 at 10:19
  • [OT]: `rdThread[rdThread.size()-1]` might simply be `rdThread.back()` (for readability) – Jarod42 Sep 20 '18 at 10:20
  • @Jarod42 does it evade some things that dereferencing does? Or is it just to do a readable and probably faster code? – huseyin tugrul buyukisik Sep 20 '18 at 10:21
  • 2
    We mean, you only need `N` seeds (one by std::mt19937), not `N` `random_device`. And you don't need to store them BTW in a vector (as they would be in `std::mt19937` instances). – Jarod42 Sep 20 '18 at 10:21
  • @Jarod42 I tried .back() same error but much more readable thanks. Now are you saying that I can use only 1 random_device for N generators without an issue in multithreading? Doesn't it use in its internal data? Or is it just starting and forgetting? – huseyin tugrul buyukisik Sep 20 '18 at 10:24
  • The MT engine doesn't take a random device as input, it takes a seed. So long as your startup code isn't multi-threaded, then it shouldn't be a problem to use one random device to generate N seeds. – StoryTeller - Unslander Monica Sep 20 '18 at 10:25
  • 1
    Note that you can write simply `v.push_back(std::make_shared());`. Or, at least `move` from `tmp`. – Daniel Langr Sep 20 '18 at 10:26
  • Thank you, it works with single seed N generators N distributions for 2000 items. I wish it doesn't change nature of seeding of 2000 items, testing now, thanks again. – huseyin tugrul buyukisik Sep 20 '18 at 10:28
  • 1
    Function call operator of `std::random_device` simply returns a number, that is used as an argument (_seed_) of `mt19937`. Therefore, if you create all instance of `mt19937` in a seqeuntial loop with a single _random device_, then you can use them in threads, since that _random devices_ is not used any more. – Daniel Langr Sep 20 '18 at 10:30
  • 2
    You might simplify your code to something like [That](http://coliru.stacked-crooked.com/a/3d76c16660910772). – Jarod42 Sep 20 '18 at 10:31
  • So its instance is not needed anymore. Ok, problem solved, also thank you for simplifications on vector usage and other things. – huseyin tugrul buyukisik Sep 20 '18 at 10:34

0 Answers0