random routines

This commit is contained in:
2026-04-14 14:03:04 -04:00
parent 4530ed3603
commit caf347e8f5
45 changed files with 1086 additions and 1 deletions

View File

@ -52,6 +52,8 @@ namespace amscuda
#include <amsculib3/amscuda_binarrrw.hpp>
#include <amsculib3/amscu_random.hpp>
#include <amsculib3/random/amscurandom.cuh>
#include <amsculib3/amscuarray_dops.hpp>
#include <amsculib3/amscurarray.cuh>

View File

@ -0,0 +1,23 @@
#ifndef __AMSCUFHASH_CUH__
#define __AMSCUFHASH_CUH__
namespace amscuda
{
namespace random
{
namespace fhash
{
//Floating point hash functions
};
};
};
#endif

View File

@ -0,0 +1,22 @@
#ifndef __AMSCULCG_CUH__
#define __AMSCULCG_CUH__
namespace amscuda
{
namespace random
{
namespace lcg
{
//Legacy linear congruential generators
};
};
};
#endif

View File

@ -0,0 +1,19 @@
#ifndef __AMSCUPCG_CUH__
#define __AMSCUPCG_CUH__
namespace amscuda
{
namespace random
{
};
};
#endif

View File

@ -0,0 +1,55 @@
#ifndef __AMSCU_RANDOM1_HPP__
#define __AMSCU_RANDOM1_HPP__
#include <amsculib3/random/amscupcg.cuh>
#include <amsculib3/random/amsxoroshiro.cuh>
#include <amsculib3/random/amsculcg.cuh>
#include <amsculib3/random/amscufhash.cuh>
namespace amscuda
{
namespace random
{
typedef xoroshiro::xs64ss_state randstate_t;
extern randstate_t global_randstate;
//default random number generator functions that wrap one of the generators
//defined in the prefix libraries. Choosing xoroshiro64** for now due to
//only 32 bit operations being needed.
__host__ void rand_seed(const uint32_t seed);
__host__ __device__ void rand_state_increment(const int32_t inc, randstate_t *state = NULL);
__host__ __device__ void rand_next(randstate_t *state = NULL);
__host__ __device__ uint32_t randui32(randstate_t *state = NULL);
__host__ __device__ int randint(int low, int high, randstate_t *state = NULL);
__host__ __device__ float randf(randstate_t *state = NULL);
__host__ __device__ double rand(randstate_t *state = NULL);
__host__ __device__ float randnf(randstate_t *state = NULL);
__host__ __device__ double randn(randstate_t *state = NULL);
//Operations to fill a host buffer with random values
__host__ int hbuff_randf(float *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int hbuff_rand(double *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int hbuff_randnf(float *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int hbuff_randn(double *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int hbuff_randint(int *hbuffer, int64_t size, int low, int high, randstate_t *state = NULL);
//Operations to fill a device buffer with random values
__host__ int dbuff_randf(float *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int dbuff_rand(double *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int dbuff_randnf(float *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int dbuff_randn(double *hbuffer, int64_t size, randstate_t *state = NULL);
__host__ int dbuff_randint(int *hbuffer, int64_t size, int low, int high, randstate_t *state = NULL);
};
};
#endif

View File

@ -0,0 +1,69 @@
#ifndef __AMSXOROSHIRO_CUH__
#define __AMSXOROSHIRO_CUH__
//implementations of the xoroshiro family of PRNGs
//Described by: https://prng.di.unimi.it/
//ref: https://prng.di.unimi.it/xoroshiro64star.c
//ref: David Blackman and Sebastiano Vigna. Scrambled linear pseudorandom number generators. ACM Trans. Math. Softw., 47:132, 2021.
namespace amscuda
{
namespace random
{
namespace xoroshiro
{
//https://github.com/joelkp/ranoise/blob/main/splitmix32.c
__device__ __host__ uint32_t splitmix32_next(uint32_t *splitmix32_state);
__host__ __device__ void splitmix64_next(uint64_t *state);
__host__ __device__ uint64_t splitmix64_nextint(uint64_t *state);
struct xs64ss_state
{
public:
uint32_t low;
uint32_t high;
__device__ __host__ xs64ss_state();
__device__ __host__ xs64ss_state(const uint32_t seed);
__device__ __host__ xs64ss_state(const uint32_t _low, const uint32_t _high);
};
extern xs64ss_state xs64ss_globalstate;
__host__ void xs64ss_seed(const uint32_t q);
// __host__ void xs64ss_seed(const uint32_t _a, const uint32_t _b);
//implements the xoroshiro64** PRNG
__host__ __device__ uint32_t xs64ss_next(xs64ss_state *state);
//The problem with xoroshiro128+ is that 64 bit integer operations on GPUs are 20 (multiplication) to 80 times slower
//than the native 32 bit integer opeations. Everywhere a 64 bit integer shows up and is multiplied by anything, things slow down
struct xs128pp_state
{
public:
uint64_t low;
uint64_t high;
__device__ __host__ xs128pp_state();
__device__ __host__ xs128pp_state(const uint64_t seed);
__device__ __host__ xs128pp_state(const uint64_t _low, const uint64_t _high);
};
extern xs128pp_state xs128pp_globalstate;
__host__ __device__ uint64_t xs128pp_rotl(const uint64_t x, int k);
__host__ __device__ uint64_t xs128pp_next(xs128pp_state* state);
//equivalent to 2^64 calls to xs128pp_next()
__device__ __host__ void xs128pp_jump(xs128pp_state* state);
__host__ void xs128pp_seed(uint64_t seed);
};
};
};
#endif