News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Monte Carlo Simulation with RDRAND

Started by Gunther, October 04, 2013, 10:07:16 PM

Previous topic - Next topic

Gunther

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:

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

dedndave

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

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

jj2007

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

#4
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 ?

Gunther

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

sinsi


Gunther

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

sinsi

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

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

TWell

#10
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 !

dedndave

        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

qWord

Quote from: TWell on October 06, 2013, 03:32:35 AM
Is that function CheckRdRand correct ?
Quote
   if ((r[2] & 0x40000000) == 0x40000000)
MREAL macros - when you need floating point arithmetic while assembling!

Gunther

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

dedndave

it has a slight bug, Gunther
see reply #11

the result is always set to 1