Serving the Quantitative Finance Community

 
User avatar
gileper
Topic Author
Posts: 0
Joined: October 26th, 2002, 11:26 am

openmp mt code

July 18th, 2009, 11:41 am

Hi,I am looking for an openmp implementation of mersennte twister but searching on google I have find nothing.Can someonle help me? pm some code or some weblink?regardsg.
 
User avatar
nicolasito
Posts: 2
Joined: November 23rd, 2005, 5:23 pm

openmp mt code

July 18th, 2009, 9:23 pm

 
User avatar
gileper
Topic Author
Posts: 0
Joined: October 26th, 2002, 11:26 am

openmp mt code

July 19th, 2009, 1:00 pm

thx but I need openmp version... i already use a good c++ implementation!
 
User avatar
Yossarian22
Posts: 4
Joined: March 15th, 2007, 2:27 am

openmp mt code

June 9th, 2010, 2:40 pm

How far along did you get on this? I would like to put MT on a GPU, just started looking. Any suggestions.Y
 
User avatar
Yossarian22
Posts: 4
Joined: March 15th, 2007, 2:27 am

openmp mt code

June 9th, 2010, 2:40 pm

How far along did you get on this? I would like to put MT on a GPU, just started looking. Any suggestions.Y
 
User avatar
gsgiles
Posts: 0
Joined: December 13th, 2008, 1:39 pm

openmp mt code

June 9th, 2010, 3:21 pm

The GPU's I am famaliar with support C not C++. Cache faulting would cause the core to fetch from main memory which is what GPU's are designed not to do. Inline it as C to avoid this.This is the C# version I use, conversion to C/C++ should be trivial.''' ''' A random number generator with a uniform distribution using the Mersenne ''' Twister algorithm.''' Public Class MersenneTwister Private Const N As Integer = 624 Private Const M As Integer = 397 Private Const MATRIX_A As UInteger = &H9908B0DFUI Private Const UPPER_MASK As UInteger = &H80000000UI Private Const LOWER_MASK As UInteger = &H7FFFFFFFUI Private mt(N - 1) As UInt32 Private mti As Integer = N + 1 ''' ''' Create a new Mersenne Twister random number generator. ''' Public Sub New() Me.New(CUInt(Date.Now.Millisecond)) End Sub ''' ''' Create a new Mersenne Twister random number generator with a ''' particular seed. ''' ''' The seed for the generator. Public Sub New(ByVal pSeed As UInt32) mt(0) = pSeed For mti = 1 To N - 1 mt(mti) = CUInt((1812433253UL * (mt(mti - 1) Xor (mt(mti - 1) >> 30)) + CUInt(mti)) And &HFFFFFFFFUL) Next End Sub ''' ''' Create a new Mersenne Twister random number generator with a ''' particular initial key. ''' ''' The initial key. Public Sub New(ByVal initialKey() As UInt32) Me.New(19650218UI) Dim i, j, k As Integer i = 1 : j = 0 k = CInt(IIf(N > initialKey.Length, N, initialKey.Length)) For k = k To 1 Step -1 mt(i) = CUInt(((mt(i) Xor ((mt(i - 1) Xor (mt(i - 1) >> 30)) * 1664525UL)) + initialKey(j) + CUInt(j)) And &HFFFFFFFFUI) i += 1 : j += 1 If i >= N Then mt(0) = mt(N - 1) : i = 1 If j >= initialKey.Length Then j = 0 Next For k = N - 1 To 1 Step -1 mt(i) = CUInt(((mt(i) Xor ((mt(i - 1) Xor (mt(i - 1) >> 30)) * 1566083941UL)) - CUInt(i)) And &HFFFFFFFFUI) i += 1 If i >= N Then mt(0) = mt(N - 1) : i = 1 Next mt(0) = &H80000000UI End Sub ''' ''' Generates a random number between 0 and System.UInt32.MaxValue. ''' Public Function GenerateUInt32() As UInt32 Dim y As UInteger Static mag01() As UInteger = {&H0UI, MATRIX_A} If mti >= N Then Dim kk As Integer Debug.Assert(mti <> N + 1, "Failed initialization") For kk = 0 To N - M - 1 y = (mt(kk) And UPPER_MASK) Or (mt(kk + 1) And LOWER_MASK) mt(kk) = mt(kk + M) Xor (y >> 1) Xor mag01(CInt(y And &H1)) Next For kk = kk To N - 2 y = (mt(kk) And UPPER_MASK) Or (mt(kk + 1) And LOWER_MASK) mt(kk) = mt(kk + (M - N)) Xor (y >> 1) Xor mag01(CInt(y And &H1)) Next y = (mt(N - 1) And UPPER_MASK) Or (mt(0) And LOWER_MASK) mt(N - 1) = mt(M - 1) Xor (y >> 1) Xor mag01(CInt(y And &H1)) mti = 0 End If y = mt(mti) mti += 1 ' Tempering y = y Xor (y >> 11) y = y Xor ((y << 7) And &H9D2C5680UI) y = y Xor ((y << 15) And &HEFC60000UI) y = y Xor (y >> 18) Return y End Function ''' ''' Generates a random integer between 0 and System.Int32.MaxValue. ''' Public Function GenerateInt32() As Integer Return CInt(GenerateUInt32() >> 1) End Function ''' ''' Generates a random integer between 0 and maxValue. ''' ''' The maximum value. Must be greater than zero. Public Function GenerateInt32(ByVal maxValue As Integer) As Integer Return GenerateInt32(0, maxValue) End Function ''' ''' Generates a random integer between minValue and maxValue. ''' ''' The lower bound. ''' The upper bound. Public Function GenerateInt32(ByVal minValue As Integer, ByVal maxValue As Integer) As Integer Return CInt(Math.Floor((maxValue - minValue + 1) * GenerateDouble() + minValue)) End Function ''' ''' Generates a random floating point number between 0 and 1. ''' Public Function GenerateDouble() As Double Return GenerateUInt32() * (1.0 / 4294967295.0) End FunctionEnd Class
Last edited by gsgiles on June 8th, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
renorm
Posts: 1
Joined: February 11th, 2010, 10:20 pm

openmp mt code

June 9th, 2010, 3:25 pm

gileper,What do you mean by OpenMP implementation? Which language?I don't think mersenne twister will benefit from OpenMP, at least the speed won't be that great. Mersenne twister isn't suitable for shared memory multicore execution. You need to give each forked thread its own instance.If you are using C++, there is SSE2 enabled mersenne twister. It it about 2-3 times faster than boost:mt19937 and supports seed mixing and leapfrogging.