Random Generator in kernel

Hi all,


I'm looking for an algorithm to generate random numbers in a kernel shader function, similar to "curand" for Cuda, but I couldn't find it. Is there some interesting library?


Thank you so much,

Answered by sbrodhead in 89663022

This is a singled threaded CPU implementation you have posted. Notice the for() loop in Shaders.metal.

This will be very poor performing in Metal.


You need a Parallel implementation.

See:


https://www.fixstars.com/en/opencl/book/OpenCLProgrammingBook/mersenne-twister/

https://developer.nvidia.com/opencl There is a OpenCL Mersenne Twister example on this page.


Porting from OpenCL to Metal is easy.

This blog post describes a decent method applicable to any modern shading language: reedbeta dot com/blog/2013/01/12/quick-and-easy-gpu-random-numbers-in-d3d11/

Thank you.


Finally, I've rewritten a code to adapt it to Metal. It's pretty uniform...



<<<< mt.h >>>>

/*
* Code adapted to Metal Shader
* Helio Tejedor <heltena@gmail.com>
*/
/*
* C++ Mersenne Twister wrapper class written by
* Jason R. Blevins <jrblevin@sdf.lonestar.org> on July 24, 2006.
* Based on the original MT19937 C code by
* Takuji Nishimura and Makoto Matsumoto.
*/
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.

Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).

Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Any feedback is very welcome.
 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
 */
#pragma once
#include <metal_stdlib>
using namespace metal;
constant const int mt19937_N = 624;
constant const int mt19937_M = 397;
constant const uint mt19937_MATRIX_A = 0x9908b0dfUL;
constant const uint mt19937_UPPER_MASK = 0x80000000UL;
constant const uint mt19937_LOWER_MASK = 0x7fffffffUL;
class mt19937 {
public:
    mt19937();
    void srand(uint u) { init_genrand(u); }
    void srand(float f) { init_genrand(as_type<uint>(f)); }
    uint rand_uint() { return genrand_int32(); }
    float rand(void) { return genrand_real1(); }
    
private:
    void init_genrand(uint s);
    void init_by_array(uint ikey0, uint ikey1, uint ikey2, uint ikey3);
    
    uint genrand_int32(void);
    float genrand_real1(void);
    
    uint mt_[mt19937_N];                  // the state vector
    int mti_;                             // mti == N+1 means mt not initialized 
};


<<<< mt.metal >>>>

/*
* Code adapted to Metal Shader
* Helio Tejedor <heltena@gmail.com>
*/
/*
* C++ Mersenne Twister wrapper class written by
* Jason R. Blevins <jrblevin@sdf.lonestar.org> on July 24, 2006.
* Based on the original MT19937 C code by
* Takuji Nishimura and Makoto Matsumoto.
*/
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.

Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).

Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Any feedback is very welcome.
 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
 */
#include "mt.h"
mt19937::mt19937(void):
    mti_(mt19937_N+1)
{
    init_by_array(0x123, 0x234, 0x345, 0x456);
}
void mt19937::init_by_array(uint ikey0, uint ikey1, uint ikey2, uint ikey3)
{
    const int key_length = 4;
    uint init_key[] = { ikey0, ikey1, ikey2, ikey3 };
    // Store the key array
    int i, j, k;
    init_genrand(19650218UL);
    i=1; j=0;
    k = (mt19937_N>key_length ? mt19937_N : key_length);
    for (; k; k--) {
        mt_[i] = (mt_[i] ^ ((mt_[i-1] ^ (mt_[i-1] >> 30)) * 1664525UL))
          + init_key[j] + j; /* non linear */
        mt_[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
        i++; j++;
        if (i>=mt19937_N) { mt_[0] = mt_[mt19937_N-1]; i=1; }
        if (j>=key_length) j=0;
    }
    for (k=mt19937_N-1; k; k--) {
        mt_[i] = (mt_[i] ^ ((mt_[i-1] ^ (mt_[i-1] >> 30)) * 1566083941UL))
          - i; / non linear */
        mt_[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
        i++;
        if (i>=mt19937_N) { mt_[0] = mt_[mt19937_N-1]; i=1; }
    }
    mt_[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
}
void mt19937::init_genrand(uint s)
{
    mt_[0]= s & 0xffffffffUL;
    for (mti_=1; mti_<mt19937_N; mti_++) {
        mt_[mti_] =
        (1812433253UL * (mt_[mti_-1] ^ (mt_[mti_-1] >> 30)) + mti_);
        /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
        /* In the previous versions, MSBs of the seed affect   */
        /* only MSBs of the array mt_[].                        */
        /* 2002/01/09 modified by Makoto Matsumoto             */
        mt_[mti_] &= 0xffffffffUL;
        /* for >32 bit machines */
    }
}
/*
 * Generates a random number on [0,0xffffffff]-interval
 *
 * \return random number on [0, 0xffffffff]
 */
uint mt19937::genrand_int32(void)
{
    uint y;
    const uint mag01[2] = {0x0UL, mt19937_MATRIX_A};
    /* mag01[x] = x * MATRIX_A  for x=0,1 */
    if (mti_ >= mt19937_N) { /* generate N words at one time */
        int kk;
        if (mti_ == mt19937_N+1)   /* if init_genrand() has not been called, */
            init_genrand(5489UL); /* a default initial seed is used */
        for (kk=0;kk<mt19937_N-mt19937_M;kk++) {
            y = (mt_[kk]&mt19937_UPPER_MASK)|(mt_[kk+1]&mt19937_LOWER_MASK);
            mt_[kk] = mt_[kk+mt19937_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
        }
        for (;kk<mt19937_N-1;kk++) {
            y = (mt_[kk]&mt19937_UPPER_MASK)|(mt_[kk+1]&mt19937_LOWER_MASK);
            mt_[kk] = mt_[kk+(mt19937_M-mt19937_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
        }
        y = (mt_[mt19937_N-1]&mt19937_UPPER_MASK)|(mt_[0]&mt19937_LOWER_MASK);
        mt_[mt19937_N-1] = mt_[mt19937_M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
        mti_ = 0;
    }
  
    y = mt_[mti_++];
    /* Tempering */
    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680UL;
    y ^= (y << 15) & 0xefc60000UL;
    y ^= (y >> 18);
    return y;
}
float mt19937::genrand_real1(void)
{
    return genrand_int32()*(1.0/4294967295.0); 
    /* divided by 2^32-1 */ 
}


<<<< Shaders.metal >>>>

#include <metal_stdlib>
#include "mt.h"
using namespace metal;
kernel void simpleKernel(device float *inputValues [[buffer(0)]],
                         device float *outputValues [[buffer(1)]],
                         uint3 idx [[ thread_position_in_threadgroup ]]) {
    mt19937 mt;
    mt.srand(inputValues[idx.x]);
    for(int i = 0; i < 50000; i++) {
        float f = mt.rand();
        outputValues[i] = f;
    }
}
Accepted Answer

This is a singled threaded CPU implementation you have posted. Notice the for() loop in Shaders.metal.

This will be very poor performing in Metal.


You need a Parallel implementation.

See:


https://www.fixstars.com/en/opencl/book/OpenCLProgrammingBook/mersenne-twister/

https://developer.nvidia.com/opencl There is a OpenCL Mersenne Twister example on this page.


Porting from OpenCL to Metal is easy.

@heltena solution works well

in  init_by_array a /* is missing in the line with
Code Block
- i; / non linear */



I know I am writing this after 5 years. But I found this useful now and it took me sometime to debug it.

Check this SO https://stackoverflow.com/a/78963952/44964. I have modified (you'll see) Loki pseudorandom generator to return proper random numbers [0, 1].

Random Generator in kernel
 
 
Q