Author Topic: Monte Carlo Simulation with RDRAND  (Read 19232 times)

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Monte Carlo Simulation with RDRAND
« on: October 04, 2013, 10:07:16 PM »
Attached is the file mc.zip. It contains the following files:
  • b_mc.bat:            batch file to build the application
  • mc.asm:              assembly language source
  • mc.obj:               object file
  • mc.pdf:               algorithmic background
  • montecarlo.c:      C source
  • montecarlo.exe:   application
  • montecarlo.o:      object file
A typical output of the program is:
Code: [Select]
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
« Last Edit: October 06, 2013, 10:09:09 AM by Gunther »
You have to know the facts before you can distort them.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Monte Carlo Simulation with RDRAND
« Reply #1 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

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND
« Reply #2 on: October 05, 2013, 02:56:36 AM »
Dave,

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
You have to know the facts before you can distort them.

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: Monte Carlo Simulation with RDRAND
« Reply #3 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??

GoneFishing

  • Member
  • *****
  • Posts: 1072
  • Gone fishing
Re: Monte Carlo Simulation with RDRAND
« Reply #4 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 ?
« Last Edit: October 05, 2013, 11:05:23 PM by vertograd »

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND
« Reply #5 on: October 05, 2013, 09:55:15 PM »
Jochen,

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
You have to know the facts before you can distort them.

sinsi

  • Guest
Re: Monte Carlo Simulation with RDRAND
« Reply #6 on: October 05, 2013, 10:20:28 PM »
Any reason for doing this in 64-bit?

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND
« Reply #7 on: October 05, 2013, 11:24:49 PM »
Any reason for doing this in 64-bit?

Yes. The generated random number is 64 bit wide.

BTW  What's the CPU / OS you  ran your test on ?

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

Gunther
You have to know the facts before you can distort them.

sinsi

  • Guest
Re: Monte Carlo Simulation with RDRAND
« Reply #8 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.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Monte Carlo Simulation with RDRAND
« Reply #9 on: October 06, 2013, 12:32:12 AM »
Quote
RDRAND 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

TWell

  • Member
  • ****
  • Posts: 743
Re: Monte Carlo Simulation with RDRAND
« Reply #10 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:
Code: [Select]
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 !
« Last Edit: October 06, 2013, 05:37:05 PM by TWell »

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Monte Carlo Simulation with RDRAND
« Reply #11 on: October 06, 2013, 03:43:01 AM »
Code: [Select]
        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
Code: [Select]
        mov        rax, 1                ; indicate success

.done:
        pop        rbx
        ret

even better
Code: [Select]
mov  rax,1
push rbx
cpuid
xor  rax,rax
bt   ecx,30
pop  rbx
rcl  rax,1
ret

qWord

  • Member
  • *****
  • Posts: 1475
  • The base type of a type is the type itself
    • SmplMath macros
Re: Monte Carlo Simulation with RDRAND
« Reply #12 on: October 06, 2013, 03:44:35 AM »
Is that function CheckRdRand correct ?
Quote
   if ((r[2] & 0x40000000) == 0x40000000)
MREAL macros - when you need floating point arithmetic while assembling!

Gunther

  • Member
  • *****
  • Posts: 4198
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND
« Reply #13 on: October 06, 2013, 09:48:50 AM »
Hi TWell,

Is that function CheckRdRand correct ?
In amd64 Win7 64-bit it crash when trying to use RdRnd.

I think so.

Gunther
You have to know the facts before you can distort them.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Monte Carlo Simulation with RDRAND
« Reply #14 on: October 06, 2013, 10:02:10 AM »
it has a slight bug, Gunther
see reply #11

the result is always set to 1