Threadsafe RANDOMIZE and RND

Forum for discussion about the documentation project.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threadsafe RANDOMIZE and RND

Post by fxm »

Same code, compiled with:
-gen gas -exx -arch 686 -w pedantic -w constness
for the first case, and:
-mt -gen gas -exx -arch 686 -w pedantic -w constness
for the second case.
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

fxm wrote:compiled with:
But that is not the same command line that coderJeff uses.

Which begs the question of why are both of you messing about with error checking in production code and why, fxm, did you use different error checking and neglect to say so?

Sorry, boys this is not good enough. Image
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threadsafe RANDOMIZE and RND

Post by fxm »

Functionally, I don't see a lot of noticeable differences between our different commands.
deltarho[1859] wrote:Sorry, boys this is not good enough. Image
And you, why don't you test it yourself?
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

fxm wrote:Functionally, I don't see a lot of noticeable differences between our different commands.
Really? So, how do you explain such a marked difference between the tables?
fxm wrote:And you, why don't you test it yourself?
That is easily answered: I wouldn't know where to start nor do I have any interest in learning how to.

Now don't come back with "Don't complain then." because my lack of interest has nothing to do with your using a different command line and then publish a table which looks nothing like coderJeff's table and not take a stab at why you think they are so different.

Anyway, this conversation isn't getting us anywhere. My concern is that many members will look at the two sets of tables and vacate this thread faster than they came in and not say a word. There has been quite a few views in 24 hours but only you and I have responded - no change there then. Image
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threadsafe RANDOMIZE and RND

Post by fxm »

My CPU: Intel Core I7 - 7500 U, 2.70GHz
I am assuming Jeff's processor used for this test is less performing.

I was just talking about the smaller relative deviation (between 4 threads and 1 thread) from Jeff's results.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by coderJeff »

I had to start somewhere:
- I don't think can test multi-threaded built-in RND previosly due the collisions (non thread-safe)
- I am using the extra error checking option '-exx' because I feel like it might be a common option for users
- I am using an un-optimized rtlib with debug information so I can trace through execution in a debugger as changes are developed
- I would expect better results compared to the baseline on my CPU by removing '-exx' and adding gcc optimizations
- I want to make sure that overall I'm not making anything worse with the changes

The full list of tests I am using just to get an idea of comparison.

Code: Select all

fbc-win32 -gen gas dotest.bas -exx -d NTHREADS=1
if errorlevel 1 goto ERROR
dotest

fbc-win32 -gen gas dotest.bas -exx -d NTHREADS=1 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win32 -gen gas dotest.bas -exx -d NTHREADS=2 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win32 -gen gas dotest.bas -exx -d NTHREADS=4 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win32 -gen gcc dotest.bas -exx -d NTHREADS=1
if errorlevel 1 goto ERROR
dotest

fbc-win32 -gen gcc dotest.bas -exx -d NTHREADS=1 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win32 -gen gcc dotest.bas -exx -d NTHREADS=2 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win32 -gen gcc dotest.bas -exx -d NTHREADS=4 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win64 -gen gas64 dotest.bas -exx -d NTHREADS=1
if errorlevel 1 goto ERROR
dotest

fbc-win64 -gen gas64 dotest.bas -exx -d NTHREADS=1 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win64 -gen gas64 dotest.bas -exx -d NTHREADS=2 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win64 -gen gas64 dotest.bas -exx -d NTHREADS=4 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win64 -gen gcc dotest.bas -exx -d NTHREADS=1
if errorlevel 1 goto ERROR
dotest

fbc-win64 -gen gcc dotest.bas -exx -d NTHREADS=1 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win64 -gen gcc dotest.bas -exx -d NTHREADS=2 -mt
if errorlevel 1 goto ERROR
dotest

fbc-win64 -gen gcc dotest.bas -exx -d NTHREADS=4 -mt
if errorlevel 1 goto ERROR
dotest

goto DONE
:ERROR
echo !!! error !!!
goto DONE
:DONE
EDIT: this is from a couple weeks ago: Obviously going to be specific to '-exx' & debug rtlib & my AMD A6-3620 APU. But as I make additional changes, I can compare to this baseline.

Code: Select all

----------------------------------------------------------------------
32-bit - gas
Single threaded
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        176       168       242       161      7282
rnd32                       88        29       114        29      7188
rndproc (nolock)           176       153       229       144      7230
rndproc32 (nolock)          96        44       122        47      7131
----------------------------------------------------------------------
32-bit - gas
Threads          : 1
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        252       232       301       229      7435
rnd32                      187       129       209       129      7365
rndproc (nolock)           166       140       213       135      7242
rndproc32 (nolock)          99        44       118        49      7247
----------------------------------------------------------------------
32-bit - gas
Threads          : 2
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        762       675       777       718      8485
rnd32                      574       640       713       635      8114
rndproc (nolock)           165       140       212       135      7385
rndproc32 (nolock)          99        44       118        49      7250
----------------------------------------------------------------------
32-bit - gas
Threads          : 4
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        522       446       587       512      8943
rnd32                      378       400       497       404      8790
rndproc (nolock)           167       140       212       135      7176
rndproc32 (nolock)          95        44       118        49      7155
----------------------------------------------------------------------
32-bit - gcc
Single threaded
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        218       202       281       206      7392
rnd32                       88        26       123        31      7207
rndproc (nolock)           204       185       273       193      7223
rndproc32 (nolock)         101        49       123        51      7161
----------------------------------------------------------------------
32-bit - gcc
Threads          : 1
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        319       281       359       290      7520
rnd32                      195       132       212       132      7334
rndproc (nolock)           211       179       259       183      7321
rndproc32 (nolock)         107        46       129        51      7292
----------------------------------------------------------------------
32-bit - gcc
Threads          : 2
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        815       763       875       784      8580
rnd32                      622       612       724       634      8372
rndproc (nolock)           213       179       259       183      7167
rndproc32 (nolock)         104        46       129        51      7111
----------------------------------------------------------------------
32-bit - gcc
Threads          : 4
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        660       576       670       603      9015
rnd32                      427       359       500       378      8865
rndproc (nolock)           207       179       259       183      7386
rndproc32 (nolock)         106        46       129        51      7125
----------------------------------------------------------------------
64-bit - gas64
Single threaded
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        195       117       240       126      4917
rnd32                       86        28       126        28      4738
rndproc (nolock)           155       105       222       115      4792
rndproc32 (nolock)          96        40       135        41      4758
----------------------------------------------------------------------
64-bit - gas64
Threads          : 1
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        243       177       309       184      5075
rnd32                      177       123       203       127      4884
rndproc (nolock)           159       106       233       115      4771
rndproc32 (nolock)          99        40       135        42      4797
----------------------------------------------------------------------
64-bit - gas64
Threads          : 2
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        699       737       797       748      5463
rnd32                      655       344       716       403      5191
rndproc (nolock)           160       104       225       117      4813
rndproc32 (nolock)         105        39       135        42      4791
----------------------------------------------------------------------
64-bit - gas64
Threads          : 4
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        606       681       654       623      6289
rnd32                      500       230       595       214      5669
rndproc (nolock)           165       106       225       116      4672
rndproc32 (nolock)          96        39       135        42      4556
----------------------------------------------------------------------
64-bit - gcc
Single threaded
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        193       116       241       121      4929
rnd32                       84        32       133        32      4638
rndproc (nolock)           159       111       223       114      4824
rndproc32 (nolock)         101        40       139        42      4640
----------------------------------------------------------------------
64-bit - gcc
Threads          : 1
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        240       168       312       182      5000
rnd32                      183       123       208       122      4740
rndproc (nolock)           160       102       221       116      4772
rndproc32 (nolock)         101        40       141        42      4601
----------------------------------------------------------------------
64-bit - gcc
Threads          : 2
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        671       658       767       728      5430
rnd32                      618       319       665       401      5362
rndproc (nolock)           160       106       223       114      4962
rndproc32 (nolock)         103        40       141        42      4741
----------------------------------------------------------------------
64-bit - gcc
Threads          : 4
Threads (nolock) : 1 (main only)
Average of 3 trials (in nanosec)
                           CRT      FAST    MTWIST        QB      REAL
rnd                        560       592       668       697      6132
rnd32                      556       214       567       222      5621
rndproc (nolock)           159       106       221       115      4766
rndproc32 (nolock)          99        40       141        42      4599
----------------------------------------------------------------------
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

coderjeff wrote:- I don't think can test multi-threaded built-in RND previosly due the collisions (non thread-safe)
Just do a cycles/second test (Hz) in each thread. Normalizing Mersenne Twister to 100% I got 16% in each of two threads. With a mutex we should get, in theory, 50% in each of two threads. For separate locations for the state vector we should get, in theory, 100% in each of two threads; which is pretty much what I get with PCG32II and my version of Bernard Widynski's 'Middle Square Weyl Sequence RNG' which also thread safe.
- I am using the extra error checking option '-exx' because I feel like it might be a common option for users
It should be mandatory for development but removed from production code, in my opinion. My Cswitches User Tool for WinFBE has a '-exx' checkbox. Click it and it gets added to the command line, click it again and it gets removed.
- I am using an un-optimized rtlib with debug information so I can trace through execution in a debugger as changes are developed
Well, we all work in different ways. To my mind publishing timings with compiled code up to its arm pits with error checking and so on is a pointless exercise. I would rip all that stuff out and say: "This is what it looks like so far, now back to development."
- I would expect better results compared to the baseline on my CPU by removing '-exx' and adding gcc optimizations
Amen to that.
- I want to make sure that overall I'm not making anything worse with the changes
No arguments with that.

I am now going to shut my mouth - I have done enough belly aching to last me for - well the rest of the day really. Image
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Threadsafe RANDOMIZE and RND

Post by paul doe »

@coderjeff: You can always implement them as header files, and then the user can #include them as they see fit. All you have to do is include it as part of the currently official distribution. No compiler bloat, faster error reporting and correcting, easily patchable and modifiable...
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

Hi Paul. I owe you an apology. A few posts back I wrote that only fxm and myself has responded in this thread. Dear oh dear - you wrote the second post!
and then the user can #include them as they see fit.
I burst out laughing when I read that. I should not have done but I just could not help myself. I cannot explain why otherwise I may get suspended. Image

Bit grumpy at the moment. Trump may get back in on Tuesday. The UK may go back into lockdown on Wednesday. I have a heart condition which has flared up ad my cardiologist has put me onto another pill which would normally knock out a bull elephant, and I am now struggling keep my eyes open and it has only just turned 22:00 in the UK. The week started off badly when I accidentally broke the spout off my favourite teapot. I was on Amazon faster than a greyhound and got a replacement within 48 hours. All being well I may keel over on Thursday and I will be well out of it. I now have to decide whom to bequeath my new teapot and my collection of emojis. I will leave my stocks and shares to those nice people at HM Treasury some of which have been using a food bank in Westminster this last couple of weeks. Image
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Threadsafe RANDOMIZE and RND

Post by dodicat »

deltarho[~~] -- Not the UK, England.
Please remember the U bit of UK (United - four separate ancient nations united together).
The other three nations are applying their own lockdown systems just now.
(I was not going to post in documentation again myself, I am not even a member of the signup club, so I shall really try not to come back here).
Hope you keep well.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Threadsafe RANDOMIZE and RND

Post by paul doe »

deltarho[1859] wrote:Hi Paul. I owe you an apology. A few posts back I wrote that only fxm and myself has responded in this thread. Dear oh dear - you wrote the second post!
Hi David. No apology needed; it's not like I'd give it too much thought.
..
I burst out laughing when I read that. I should not have done but I just could not help myself. I cannot explain why otherwise I may get suspended. Image
Am I missing something here? Because I can't honestly see the fun in it. Other than it's so painfully obvious that I actually feel dumber just by having to mention it.
deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by deltarho[1859] »

Hi Paul. It was not your making the statement but the expectation of the user that I found hilarious.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Threadsafe RANDOMIZE and RND

Post by paul doe »

deltarho[1859] wrote:Hi Paul. It was not your making the statement but the expectation of the user that I found hilarious.
Ah, I see. Kind of have a point in there...
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Threadsafe RANDOMIZE and RND

Post by coderJeff »

paul doe wrote:@coderjeff: You can always implement them as header files, and then the user can #include them as they see fit. All you have to do is include it as part of the currently official distribution. No compiler bloat, faster error reporting and correcting, easily patchable and modifiable...
I have an idea what might work. It's a little over the top but it touches on some patterns that are above and beyond just PRNG. I refactored & rewrote math_rnd.c from the rtlib and added new stuff to fbc-int/math.bi. (I'll have to go back and edit my posts). So for now, I'll just mention what I have in mind.

I read Bernard Widynski's paper for middle square Weyl sequence and feel that interpretation of the method is obvious (i.e. I understand it) so I will add that in as a new generator. I starting looking over PCG's but I didn't find a source I feet comfortable with yet. I need to find something I can use as reference but not be worried about license or patent or copyright.

Might be couple days before I get the next changes up.

Anyway, using middle square (MSWS) as an example:

Very simply, use it like any other PRNG through RANDOMIZE & RND

Code: Select all

randomize ,6  '' select MSWS PRNG with automatic seed value
print rnd  '' return double
And then minimally to get the 32-bit returning function, copy & paste:

Code: Select all

declare function rnd32 alias "fb_Rnd32" () as ulong
print rnd32
For anything else there will have to be an #include. Currently have everything in fbc-int/math.bi in the FBC namespace. If some of it is considered good enough to keep without much change, then can maybe move to a new header fbmath.bi in the FB namespace.

With a rewrite of the rtlib internals and a new header can instance PRNG's in the form of:

Code: Select all

#include "fbc-int/math.bi"
	dim m as fbc.RndMSWS32
	for i as integer = 1 to 10
		print m.rnd, m.rnd32
	next
So, obviously can instance across multiple threads but also create a copy (deep copy too for PRNGs with a buffer, though currently only the Copy-Constructor works and fbc doesn't seem to register the Let Operator member)

Code: Select all

#include "fbc-int/math.bi"

dim m1 as fbc.RndMSWS32
dim m2 as fbc.RndMSWS32 = m1 '' create a copy
for i as integer = 1 to 10
	print m1.rnd32, m2.rnd32 '' same sequence
next
Have a few other tweaks, like can allocate a buffer for FB_RND_REAL (or use the default of 4KB if not specified)

Code: Select all

	const seed = 12345ull
	dim m as fbc.RndREAL32 = fbc.RndREAL32( seed, 1024 * 1024 )
	for i as integer = 1 to 10
		print m.rnd, m.rnd32
	next
I've changed the rtlib API so it's possible to pass 64-bit seed values. They are currently still only using the low 32-bits of the parameter though.

The fbc-int/math.bi has a ton of other low-level declarations so can also build up your own class or framework. I'll try to get the changes up and running soon as I can.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Threadsafe RANDOMIZE and RND

Post by paul doe »

coderJeff wrote:...
I read Bernard Widynski's paper for middle square Weyl sequence and feel that interpretation of the method is obvious (i.e. I understand it) so I will add that in as a new generator. I starting looking over PCG's but I didn't find a source I feet comfortable with yet. I need to find something I can use as reference but not be worried about license or patent or copyright.
...
Nice. Also check out this post:

viewtopic.php?f=7&t=28806

Another generator by Widynski that might prove useful. Even simpler than MSWS, and under the same lax license.
Post Reply