Nektar++
Loading...
Searching...
No Matches
DeviceRandomGenerator.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: DeviceRandomGenerator.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Device random number generator, over cuRAND in
32// DeviceRandomGenerator.cu, hipRAND in DeviceRandomGenerator.hip and rocRAND,
33// oneMath or oneMKL in DeviceRandomGenerator.cpp. This header is backend
34// agnostic, so the vendor libraries stay private to LibUtilities.
35//
36///////////////////////////////////////////////////////////////////////////////
37
38#pragma once
39
41
42#include <cstddef>
43#include <cstdint>
44#include <memory>
45
46namespace Nektar::Math
47{
48
49/**
50 * \brief Fills device memory with pseudo-random numbers.
51 *
52 * Each stream has a Philox generator of its own, created on first use with a
53 * seed derived from the seed given here and the stream ID, and only ever
54 * launched on that stream. Generation on different streams therefore shares
55 * no state, and successive calls on one stream continue its sequence. The
56 * numbers a buffer receives depend on the stream it is filled on.
57 */
59{
60public:
62 const std::uint64_t seed);
63
65
68
69 /// Fill the n entries of x with numbers uniform between a and b.
70 template <typename TData>
71 LIB_UTILITIES_EXPORT void Uniform(const TData a, const TData b,
72 const size_t n, TData *x,
73 const unsigned int streamID = 0);
74
75 /// Fill the n entries of x with normal numbers of the given mean and
76 /// standard deviation.
77 template <typename TData>
78 LIB_UTILITIES_EXPORT void Normal(const TData mean, const TData sigma,
79 const size_t n, TData *x,
80 const unsigned int streamID = 0);
81
82private:
83 struct Engines;
84
85 std::uint64_t m_seed;
86 std::unique_ptr<Engines> m_engines;
87
88 /// Seed of the generator of stream streamID.
89 static std::uint64_t StreamSeed(const std::uint64_t seed,
90 const unsigned int streamID)
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 }
99};
100
101} // namespace Nektar::Math
#define LIB_UTILITIES_EXPORT
Fills device memory with pseudo-random numbers.
static std::uint64_t StreamSeed(const std::uint64_t seed, const unsigned int streamID)
Seed of the generator of stream streamID.
DeviceRandomGenerator & operator=(const DeviceRandomGenerator &)=delete
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.
DeviceRandomGenerator(const DeviceRandomGenerator &)=delete
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.