Threadsafe RANDOMIZE and RND

Forum for discussion about the documentation project.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

Paul wrote:it is not useful for you yet.
I use a simple mantra. If in doubt about whether to do something or not I simply ask:"How much will it cost to do something?". If the answer is next to nothing, then I do it.

At the end of PCG32II.bas I use:

Code: Select all

Dim Shared As pcg32 pcg
#undef Rnd
#define Rnd pcg.randse
and a similar construct with my other generators.

Here is the head of some timing code.

Code: Select all

'#include "PCG32II.bas" ' Use Rnd
'#include "MsWs.bas" ' Use Rnd
'#define Algo 2 ' Intel RdRand for CryptoRndII
'#include "CryptoRndII.bas" ' Use Rnd
'#Include "CMWC4096.bas" ' Use CMWCse
'#include "xoroshiro128.bas" 'Use Rnd
'#include "xoshiro256.bas" ' Use Rnd
The timing code uses RND except for CMWC4096 which is designed differently to the other generators and does not lend itself to using #undef.

So, when timing a particular generator I simply use the appropriate #include for all except CMWC4096.

Pasting code from the forum or using our code which uses Rnd we simply use #include and have nothing else to do.

So, if in doubt about randomness and/or speed required if we use #include "PCG32II.bas", with the correct path, for example, the two questions are rendered academic.

The above requires a bit of effort but it is only a one off task.

Some generators need to be warmed up, some don't. I have some code to test that but I dont use it - I warm up anyway. The warm up is in the nanosecond domain so costs me virtually nothing to implement; whether to warm up or not is also rendered academic.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by speedfixer »

For both of you:

re: '... lack of interest ...'

Look at the view count for these randomness topics. When your topic stays in single digits for a long time, people don't care.

A LOT of people care. I do. I'm simply not qualified to contribute.

I HAVE folded some of your faster rnd routines into my code. Thank you.

david
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

speedfixer wrote:I HAVE folded some of your faster rnd routines into my code. Thank you.
Thanks, David. I really appreciate your taking time out to write your post.

My 'A fast CPRNG' was started just over 3½ years ago, a month just after I joined the forum, and should have clocked up a few views but it is now pushing 10,000 so there is certainly some interest going on. However, it is difficult to tell what kind of interest. I sometimes think that people see a post from me and have a look to see what I am rambling on about rather than a particular interest in the topic. It is what I do. I won't mention names but there are some members whose posts I always look at simply because I like reading their posts even if the topic is not one that I am very much interested in.

Having said that, my perception of a lack of interest has not put me off otherwise I would not have posted 'Oh no, not another PRNG' with xoroshiro128** and xoshiro256** which has now clocked up over 3300 views since July 8.

What is disconcerting is when a thread gets many views but very few people joining the conversation. Take this thread which has now over 1100 views in less than two weeks but only a few responses.
I'm simply not qualified to contribute.
That might explain something. I have had an interest in generators since I joined PowerBASIC in 2003 so am not the slightest bit intimidated by the subject. When the 'big guns' like coderJeff, fxm, and paul doe are in a thread some folk may prefer to stay outside. I tend to stay outside a graphics conversation - some of which are quite interesting - but I am clueless about the subject.

Anyway, thanks again, David, you made my day. Image
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by coderJeff »

Well, I think we are on the right track with this change overall. 'fbmath.bi' needs some work to make it a little more user friendly and some updates to the documentation will help advertise it.

fxm, sorry, I didn't mean to turn this in to a development discussion.

For sure, we can update the following:
- RND & RANDOMIZE is thread-safe
- fbmath.bi has new constants in the FB namespace for the built-in generators.

I'm not going to worry too much about documenting the new generators in fbmath.bi until the constructors for seeding get added. So for now if it is mentioned in the wiki can note that it is a WIP.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threadsafe RANDOMIZE and RND

Post by fxm »

@Jeff,

You said "RANDOMIZE has become thread safe":
  • - Does this mean that one thread (with its own RND calls) can work with one algorithm and another thread (with its own RND calls) can work with another algorithm, independently of each other?
    - I don't see how we can do this with just a mutex without also setting static but thread-local variables (sort of MTLS).
    - If not, what is this kind of thread-safe RANDOMIZE?
RANDOMIZE documentation page update:
  • KeyPgRandomize → fxm [valid data values for algorithm selection are now defined in fbmath.bi from fbc version 1.08 (numeric values are kept for transition with previous versions)]
Already updated:
  • KeyPgRnd → fxm [RND() became thread-safe (by adding a mutex)]
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by coderJeff »

fxm wrote: what is this kind of thread-safe RANDOMIZE?
There is still only one global state and only one generator can be selected at any one time across all threads. I would expect that the user would want to call the RANDOMIZE function before starting any threads.

However, if for some reason RANDOMIZE were called from two separate threads at nearly the same time : one will complete and then the other will complete. RANDOMIZE & RND both use the same mutex to block execution on other threads until the current function either RND or RANDOMIZE completes and releases the lock.

RANDOMIZE & RND are thread-safe because only one function can update the global state at any one time.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threadsafe RANDOMIZE and RND

Post by fxm »

I think the best definition would be to say that:
RANDOMIZE and RND are thread-safe, but not thread-specific.

(unlike gfxlib2 graphics keywords which are both)
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threadsafe RANDOMIZE and RND

Post by fxm »

coderJeff wrote:I'm not going to worry too much about documenting the new generators in fbmath.bi until the constructors for seeding get added. So for now if it is mentioned in the wiki can note that it is a WIP.
If we stick to the FreeBASIC header files, they are only grouped and listed (with a very brief description) on two pages of the Programmer's Guide:
- Header Files (.bi)
- Using Prebuilt Libraries
I therefore propose to add "fbmath.bi" to it.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by coderJeff »

That sounds good, adding 'fbmath.bi'

I just noticed there is also:
- 'fbio.bi' for IsRedirected
- 'fbthread.bi' for ThreadDetach
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threadsafe RANDOMIZE and RND

Post by fxm »

- ProPgHeaderFiles → fxm [added 'fbio.bi', 'fbthread.bi' and 'fbmath.bi' to the FreeBASIC header file list]
- ProPgPrebuiltLibraries → fxm [added 'fbio.bi', 'fbthread.bi' and 'fbmath.bi' to the FreeBASIC header file list]
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

Just in case anyone missed it Bernard Widynski has 'pulled' his three round version of Squares because of a detected "statistical failure". He has replaced it, November 24, 2020, with his four round version. However, I am getting a 1GB failure with PractRand. His 'Middle Square' method is good to 16TB.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

Yours truly wrote:However, I am getting a 1GB failure with PractRand.
A bug has crept into the program I use to pipe data to PractRand. I have just gone past 64GB with the four round version and will let it run to, hopefully, 2TB.

I'll be back.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

OK, the four round version got to 2TB with PractRand with only one small anomaly.

The four round version is here.
Post Reply