Nektar++
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
Nektar::Math::DeviceRandomGenerator Class Reference

Fills device memory with pseudo-random numbers. More...

#include <DeviceRandomGenerator.hpp>

Classes

struct  Engines
 

Public Member Functions

 DeviceRandomGenerator (const std::uint64_t seed)
 
 ~DeviceRandomGenerator ()
 
 DeviceRandomGenerator (const DeviceRandomGenerator &)=delete
 
DeviceRandomGenerator & operator= (const DeviceRandomGenerator &)=delete
 
template<typename TData >
void Uniform (const TData a, const TData b, const size_t n, TData *x, const unsigned int streamID=0)
 Fill the n entries of x with numbers uniform between a and b.
 
template<typename TData >
void Normal (const TData mean, const TData sigma, const size_t n, TData *x, const unsigned int streamID=0)
 Fill the n entries of x with normal numbers of the given mean and standard deviation.
 

Static Private Member Functions

static std::uint64_t StreamSeed (const std::uint64_t seed, const unsigned int streamID)
 Seed of the generator of stream streamID.
 

Private Attributes

std::uint64_t m_seed
 
std::unique_ptr< Engines > m_engines
 

Detailed Description

Fills device memory with pseudo-random numbers.

Each stream has a Philox generator of its own, created on first use with a seed derived from the seed given here and the stream ID, and only ever launched on that stream. Generation on different streams therefore shares no state, and successive calls on one stream continue its sequence. The numbers a buffer receives depend on the stream it is filled on.

Definition at line 58 of file DeviceRandomGenerator.hpp.

Constructor & Destructor Documentation

◆ DeviceRandomGenerator() [1/2]

Nektar::Math::DeviceRandomGenerator::DeviceRandomGenerator ( const std::uint64_t  seed)
explicit

Definition at line 292 of file DeviceRandomGenerator.cpp.

293 : m_seed(seed), m_engines(std::make_unique<Engines>())
294{
295}

◆ ~DeviceRandomGenerator()

Nektar::Math::DeviceRandomGenerator::~DeviceRandomGenerator ( )
default

◆ DeviceRandomGenerator() [2/2]

Nektar::Math::DeviceRandomGenerator::DeviceRandomGenerator ( const DeviceRandomGenerator &  )
delete

Member Function Documentation

◆ Normal()

template<typename TData >
template void Nektar::Math::DeviceRandomGenerator::Normal< float > ( const TData  mean,
const TData  sigma,
const size_t  n,
TData *  x,
const unsigned int  streamID = 0 
)

Fill the n entries of x with normal numbers of the given mean and standard deviation.

Normal numbers are generated in pairs, as the cuRAND backend of oneMath requires, so the last entry of an odd n is taken from a pair generated into scratch.

Definition at line 337 of file DeviceRandomGenerator.cpp.

340{
341 if (n == 0)
342 {
343 return;
344 }
345
346 auto &engine = m_engines->Get(m_seed, streamID);
347#if defined(SYCL_ENABLE_HIP)
348 auto scratch = reinterpret_cast<TData *>(engine.scratch);
349
350 if (n % 2 == 0)
351 {
352 GenerateNormal(engine.generator, mean, sigma, n, x, streamID);
353 }
354 else
355 {
356 GenerateNormal(engine.generator, mean, sigma, n - 1, x, streamID);
357 GenerateNormal(engine.generator, mean, sigma, size_t(2), scratch,
358 streamID);
359 deviceMemcpy<eDeviceToDevice>(x + n - 1, scratch, sizeof(TData),
360 streamID);
361 }
362#elif defined(NEKTAR_SYCL_HAVE_RNG_LIBRARY)
363 auto scratch = reinterpret_cast<TData *>(engine.scratch);
364
365 if (n % 2 == 0)
366 {
367 Generate(rng::gaussian<TData>(mean, sigma), *engine.generator, n, x,
368 streamID);
369 }
370 else
371 {
372 Generate(rng::gaussian<TData>(mean, sigma), *engine.generator, n - 1, x,
373 streamID);
374 Generate(rng::gaussian<TData>(mean, sigma), *engine.generator, 2,
375 scratch, streamID);
376 deviceMemcpy<eDeviceToDevice>(x + n - 1, scratch, sizeof(TData),
377 streamID);
378 }
379#else
380 std::normal_distribution<TData> distribution(mean, sigma);
381 std::vector<TData> host(n);
382 for (auto &value : host)
383 {
384 value = distribution(engine.generator);
385 }
386 CopyToDevice(host, x, streamID);
387#endif
388}
static void CopyToDevice(const std::vector< TData > &host, TData *x, const unsigned int streamID)

References Nektar::Math::CopyToDevice(), m_engines, and m_seed.

◆ operator=()

DeviceRandomGenerator & Nektar::Math::DeviceRandomGenerator::operator= ( const DeviceRandomGenerator &  )
delete

◆ StreamSeed()

static std::uint64_t Nektar::Math::DeviceRandomGenerator::StreamSeed ( const std::uint64_t  seed,
const unsigned int  streamID 
)
inlinestaticprivate

Seed of the generator of stream streamID.

Definition at line 89 of file DeviceRandomGenerator.hpp.

91 {
92 // splitmix64 of the pair, so nearby seeds and stream IDs give
93 // unrelated Philox keys.
94 std::uint64_t z = seed + 0x9E3779B97F4A7C15ull * (streamID + 1ull);
95 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
96 z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
97 return z ^ (z >> 31);
98 }

Referenced by Nektar::Math::DeviceRandomGenerator::Engines::Get().

◆ Uniform()

template<typename TData >
template void Nektar::Math::DeviceRandomGenerator::Uniform< float > ( const TData  a,
const TData  b,
const size_t  n,
TData *  x,
const unsigned int  streamID = 0 
)

Fill the n entries of x with numbers uniform between a and b.

rocRAND generates on (0, 1], which is then mapped onto (a, b] by the Math kernels, on the same stream.

Definition at line 304 of file DeviceRandomGenerator.cpp.

307{
308 if (n == 0)
309 {
310 return;
311 }
312
313 auto &engine = m_engines->Get(m_seed, streamID);
314#if defined(SYCL_ENABLE_HIP)
315 GenerateUniform(engine.generator, n, x, streamID);
316 mulKernel<NektarSpaces::Device>(n, b - a, x, x, streamID);
317 addScalarKernel<NektarSpaces::Device>(n, a, x, x, streamID);
318#elif defined(NEKTAR_SYCL_HAVE_RNG_LIBRARY)
319 Generate(rng::uniform<TData>(a, b), *engine.generator, n, x, streamID);
320#else
321 std::uniform_real_distribution<TData> distribution(a, b);
322 std::vector<TData> host(n);
323 for (auto &value : host)
324 {
325 value = distribution(engine.generator);
326 }
327 CopyToDevice(host, x, streamID);
328#endif
329}

References Nektar::Math::CopyToDevice(), m_engines, and m_seed.

Member Data Documentation

◆ m_engines

std::unique_ptr<Engines> Nektar::Math::DeviceRandomGenerator::m_engines
private

Definition at line 86 of file DeviceRandomGenerator.hpp.

Referenced by Normal(), and Uniform().

◆ m_seed

std::uint64_t Nektar::Math::DeviceRandomGenerator::m_seed
private

Definition at line 85 of file DeviceRandomGenerator.hpp.

Referenced by Normal(), and Uniform().