News for hackers. Stuff that matters.
Re:Make your program slower with threads by ScottBurson on Sunday December 07, 2014 @01:06AM (#8711422)
Uh, you can't just drop in 'random_r' in place of 'random'. If you do that, what will happen is that you'll get the same sequence of random numbers in every thread, making the whole multithreading exercise useless!

You have to be sure to seed the generator differently in each thread, before calling 'random_r'.

Re:Make your program slower with threads by frozenport on Sunday December 07, 2014 @02:33AM (#8711568)
Moreover, he probably improved the single threaded version, and should re-check the scaling curve. There should be a `1 with random_r`, `2 with random_r`, and `3 with random_r`.
Re:Make your program slower with threads by onestone on Sunday December 07, 2014 @09:56AM (#8712327)
It's unlikely the single-threaded version was improved noticeably. Futexes are nearly zero-cost when not contended.
Re:Make your program slower with threads by mjb on Sunday December 07, 2014 @10:31AM (#8712428)
I left the random_r numbers with single threads out of the post for exactly this reason. Performance with random_r on a single thread was less than 3% faster, with a slightly higher variance (which I can't explain, to be honest).
Re:Make your program slower with threads by mjb on Sunday December 07, 2014 @10:37AM (#8712443)
Yes, absolutely. That can be done using the global state (i.e. calling rand() or random()), or is probably better done by pulling from the system entropy pool (via /dev/urandom on Linux).
Re:Make your program slower with threads by mbq on Sunday December 07, 2014 @10:44AM (#8712470)
Still no good; you can get spurious correlations between rng outputs on each core. You need something like L'Ecuyer RNG streams.
Re:Make your program slower with threads by ScottBurson on Sunday December 07, 2014 @02:05PM (#8713168)
Well, using /dev/urandom is a vast improvement over doing nothing.

But your point is well taken. People doing serious Monte Carlo simulations should probably use a more sophisticated RNG.

Re:Make your program slower with threads by Dylan16807 on Sunday December 07, 2014 @03:42AM (#8711689)
It's just an example.