The MASM Forum

64 bit assembler => 64 bit assembler. Conceptual Issues => Topic started by: Gunther on October 04, 2013, 10:07:16 PM

Title: Monte Carlo Simulation with RDRAND
Post by: Gunther on October 04, 2013, 10:07:16 PM
Attached is the file mc.zip. It contains the following files:
A typical output of the program is:

Generating 200 Million random numbers with RDRAND.
That'll take a little while ...

Area           = 0.250001985000000
Absolute Error = 0.000001985000000
Elapsed Time   = 15.71 Seconds

Generating 200 Million random numbers with C.
That'll take a little while ...

Area           = 0.250020340000000
Absolute Error = 0.000020340000000
Elapsed Time   = 5.02 Seconds

A special thanks goes to Dave aka dedndave. :t He did the proof reading for the algorithmic background and he made an excellent job.

Gunther
Title: Re: Monte Carlo Simulation with RDRAND
Post by: dedndave on October 05, 2013, 01:30:21 AM
turned out great, Gunther   :t

something i didn't catch in the edit, though   :(
in the table of contents, it says "Literature"
then, that section is named "References" (i like this one better)   :P
sorry about that
Title: Re: Monte Carlo Simulation with RDRAND
Post by: Gunther on October 05, 2013, 02:56:36 AM
Dave,

Quote from: dedndave on October 05, 2013, 01:30:21 AM
something i didn't catch in the edit, though   :(
in the table of contents, it says "Literature"
then, that section is named "References" (i like this one better)   :P
sorry about that

That's a bagatelle; no big deal. You were a great help.

Gunther
Title: Re: Monte Carlo Simulation with RDRAND
Post by: jj2007 on October 05, 2013, 03:27:05 AM
Hi Gunther,
Can't test it with my 32-bit OS but it looks good. Just one question: Is RDRAND really slower than the C implementation??
Title: Re: Monte Carlo Simulation with RDRAND
Post by: GoneFishing on October 05, 2013, 07:09:18 AM
Hi Gunther !
Thank you for your work  :t
Can't test it on my machine ... but it seems like RDRAND is doing a lot of work behind the scene 

BTW  What's the CPU / OS you  ran your test on ?
Title: Re: Monte Carlo Simulation with RDRAND
Post by: Gunther on October 05, 2013, 09:55:15 PM
Jochen,

Quote from: jj2007 on October 05, 2013, 03:27:05 AM
Hi Gunther,
Can't test it with my 32-bit OS but it looks good. Just one question: Is RDRAND really slower than the C implementation??

yes, it tends to be slow.

Gunther
Title: Re: Monte Carlo Simulation with RDRAND
Post by: sinsi on October 05, 2013, 10:20:28 PM
Any reason for doing this in 64-bit?
Title: Re: Monte Carlo Simulation with RDRAND
Post by: Gunther on October 05, 2013, 11:24:49 PM
Quote from: sinsi on October 05, 2013, 10:20:28 PM
Any reason for doing this in 64-bit?

Yes. The generated random number is 64 bit wide.

Quote from: vertograd on October 05, 2013, 07:09:18 AM
BTW  What's the CPU / OS you  ran your test on ?

Windows 7 (64 bit), i7, Ivy Bridge.

Gunther
Title: Re: Monte Carlo Simulation with RDRAND
Post by: sinsi on October 05, 2013, 11:34:47 PM
But you can use ax/eax/rax, would that change the time taken? No need for 64-bit Windows if it's 32-bit code.
Title: Re: Monte Carlo Simulation with RDRAND
Post by: dedndave on October 06, 2013, 12:32:12 AM
QuoteRDRAND retrieves a hardware generated random value from the DRNG and
stores it in the destination register given as an argument to the instruction. The
size of the random value (16-, 32-, or 64-bits) is determined by the size of the
register given. The carry flag (CF) must be checked to determine whether a
random value was available at the time of instruction execution.
Note that RDRAND is available to any system or application software running on
the platform. That is, there are no hardware ring requirements that restrict
access based on process privilege level. As such, RDRAND may be invoked as
part of an operating system or hypervisor system library, a shared software
library, or directly by an application.
To determine programmatically whether a given Intel platform supports RDRAND,
the user can use the CPUID instruction to examine bit 30 of the ECX register.

i found a document that said it was 128-bits wide - lol
guess they were wrong - or they were testing the new 128-bit processor   :P
Title: Re: Monte Carlo Simulation with RDRAND
Post by: TWell on October 06, 2013, 03:32:35 AM
Is that function CheckRdRand correct ?
In amd64 Win7 64-bit it crash when trying to use RdRnd.
In PellesC this function works:int CheckRdRandC(void) {
int r[4]; // EAX, EBX, ECX and EDX
_cpuid(r, 1);
if (r[2] & 0x40000000) return 1;
return 0;
}
EDIT Thank's qWord !
Title: Re: Monte Carlo Simulation with RDRAND
Post by: dedndave on October 06, 2013, 03:43:01 AM
        push       rbx                   ; ebx is affected by CPUID
        mov        eax, 1                ; get version information
        cpuid

        ; check RDRAND support

        bt         ecx, 30               ; RDRAND supported?
        jnc        .failed               ; no: jump

.done:

        mov        rax, 1                ; indicate success
        pop        rbx
        ret

.failed:

        sub        rax, rax              ; indicate failure
        jmp        short .done


little oops, there
        mov        rax, 1                ; indicate success

.done:
        pop        rbx
        ret


even better
mov  rax,1
push rbx
cpuid
xor  rax,rax
bt   ecx,30
pop  rbx
rcl  rax,1
ret
Title: Re: Monte Carlo Simulation with RDRAND
Post by: qWord on October 06, 2013, 03:44:35 AM
Quote from: TWell on October 06, 2013, 03:32:35 AM
Is that function CheckRdRand correct ?
Quote
   if ((r[2] & 0x40000000) == 0x40000000)
Title: Re: Monte Carlo Simulation with RDRAND
Post by: Gunther on October 06, 2013, 09:48:50 AM
Hi TWell,

Quote from: TWell on October 06, 2013, 03:32:35 AM
Is that function CheckRdRand correct ?
In amd64 Win7 64-bit it crash when trying to use RdRnd.

I think so.

Gunther
Title: Re: Monte Carlo Simulation with RDRAND
Post by: dedndave on October 06, 2013, 10:02:10 AM
it has a slight bug, Gunther
see reply #11

the result is always set to 1
Title: Re: Monte Carlo Simulation with RDRAND
Post by: Gunther on October 06, 2013, 10:09:46 AM
Dave,

Quote from: dedndave on October 06, 2013, 10:02:10 AM
it has a slight bug, Gunther
see reply #11

the result is always set to 1

yes, the lable was wrong. It's fixed in MC1.ZIP.

Gunther
Title: Re: Monte Carlo Simulation with RDRAND
Post by: qWord on October 07, 2013, 09:40:11 AM
Might not be very useful, but in the attachment a cute multithreaded x64 program that plots a map from the random numbers (64bit). For interpreting the map: as darker a pixel gets, has higher the corresponding "hit" count - because the scaling is relative to the map's minimal and maximal value it will never get monochrome.
jWasm, polink and WinInc is used for building (the paths in build.bat must be adapted of course).

regards, qWord


Title: Re: Monte Carlo Simulation with RDRAND
Post by: Gunther on October 07, 2013, 07:58:16 PM
Hi qWord,

interesting idea. Thank you for providing the material.  :t

Gunther