okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
15.3 KiB382 linesraw
1//==---- rng_utils_detail.hpp --------------------------*- C++ -*-----------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __DPCT_RNG_UTILS_DETAIL_HPP__
10#define __DPCT_RNG_UTILS_DETAIL_HPP__
11
12namespace dpct::rng::host::detail {
13static const std::string OneMKLNotSupport =
14    "The oneAPI Math Kernel Library (oneMKL) Interfaces Project does not "
15    "support this API.";
16class rng_generator_base {
17public:
18  /// Set the seed of host rng_generator.
19  /// \param seed The engine seed.
20  virtual void set_seed(const std::uint64_t seed) = 0;
21
22  /// Set the dimensions of host rng_generator.
23  /// \param dimensions The engine dimensions.
24  virtual void set_dimensions(const std::uint32_t dimensions) = 0;
25
26  /// Set the queue of host rng_generator.
27  /// \param queue The engine queue.
28  virtual void set_queue(sycl::queue *queue) = 0;
29
30  /// Set the mode of host rng_generator.
31  /// \param mode The engine mode.
32  virtual void set_mode(const random_mode mode) = 0;
33
34  /// Generate unsigned int random number(s) with 'uniform_bits' distribution.
35  /// \param output The pointer of the first random number.
36  /// \param n The number of random numbers.
37  virtual inline void generate_uniform_bits(unsigned int *output,
38                                            std::int64_t n) = 0;
39
40  /// Generate unsigned long long random number(s) with 'uniform_bits'
41  /// distribution.
42  /// \param output The pointer of the first random number.
43  /// \param n The number of random numbers.
44  virtual inline void generate_uniform_bits(unsigned long long *output,
45                                            std::int64_t n) = 0;
46
47  /// Generate float random number(s) with 'lognormal' distribution.
48  /// \param output The pointer of the first random number.
49  /// \param n The number of random numbers.
50  /// \param m Mean of associated normal distribution
51  /// \param s Standard deviation of associated normal distribution.
52  virtual inline void generate_lognormal(float *output, std::int64_t n, float m,
53                                         float s) = 0;
54
55  /// Generate double random number(s) with 'lognormal' distribution.
56  /// \param output The pointer of the first random number.
57  /// \param n The number of random numbers.
58  /// \param m Mean of associated normal distribution
59  /// \param s Standard deviation of associated normal distribution.
60  virtual inline void generate_lognormal(double *output, std::int64_t n,
61                                         double m, double s) = 0;
62
63  /// Generate float random number(s) with 'gaussian' distribution.
64  /// \param output The pointer of the first random number.
65  /// \param n The number of random numbers.
66  /// \param mean Mean of normal distribution
67  /// \param stddev Standard deviation of normal distribution.
68  virtual inline void generate_gaussian(float *output, std::int64_t n,
69                                        float mean, float stddev) = 0;
70
71  /// Generate double random number(s) with 'gaussian' distribution.
72  /// \param output The pointer of the first random number.
73  /// \param n The number of random numbers.
74  /// \param mean Mean of normal distribution
75  /// \param stddev Standard deviation of normal distribution.
76  virtual inline void generate_gaussian(double *output, std::int64_t n,
77                                        double mean, double stddev) = 0;
78
79  /// Generate unsigned int random number(s) with 'poisson' distribution.
80  /// \param output The pointer of the first random number.
81  /// \param n The number of random numbers.
82  /// \param lambda Lambda for the Poisson distribution.
83  virtual inline void generate_poisson(unsigned int *output, std::int64_t n,
84                                       double lambda) = 0;
85
86  /// Generate float random number(s) with 'uniform' distribution.
87  /// \param output The pointer of the first random number.
88  /// \param n The number of random numbers.
89  virtual inline void generate_uniform(float *output, std::int64_t n) = 0;
90
91  /// Generate double random number(s) with 'uniform' distribution.
92  /// \param output The pointer of the first random number.
93  /// \param n The number of random numbers.
94  virtual inline void generate_uniform(double *output, std::int64_t n) = 0;
95
96  /// Skip ahead several random number(s).
97  /// \param num_to_skip The number of random numbers to be skipped.
98  virtual void skip_ahead(const std::uint64_t num_to_skip) = 0;
99
100  /// Set the direction numbers of host rng_generator. Only Sobol engine
101  /// supports this method.
102  /// \param direction_numbers The engine direction numbers.
103  virtual void set_direction_numbers(
104      const std::vector<std::uint32_t> &direction_numbers) = 0;
105
106  /// Set the engine index of host rng_generator. Only MT2203 engine
107  /// supports this method.
108  /// \param engine_idx The engine index.
109  virtual void set_engine_idx(std::uint32_t engine_idx) = 0;
110
111protected:
112  /// Construct the host rng_generator.
113  /// \param queue The queue where the generator should be executed.
114  rng_generator_base(sycl::queue *queue) : _queue(queue) {}
115
116  sycl::queue *_queue = nullptr;
117  std::uint64_t _seed{0};
118  std::uint32_t _dimensions{1};
119  random_mode _mode{random_mode::best};
120  std::vector<std::uint32_t> _direction_numbers;
121  std::uint32_t _engine_idx{0};
122};
123
124/// The random number generator on host.
125template <typename engine_t = oneapi::mkl::rng::philox4x32x10>
126class rng_generator : public rng_generator_base {
127public:
128  /// Constructor of rng_generator.
129  /// \param q The queue where the generator should be executed.
130  rng_generator(sycl::queue &q = ::dpct::cs::get_default_queue())
131      : rng_generator_base(&q),
132        _engine(create_engine(&q, _seed, _dimensions, _mode)) {}
133
134  /// Set the seed of host rng_generator.
135  /// \param seed The engine seed.
136  void set_seed(const std::uint64_t seed) {
137    if (seed == _seed)
138      return;
139    _seed = seed;
140    _engine = create_engine(_queue, _seed, _dimensions, _mode);
141  }
142
143  /// Set the dimensions of host rng_generator.
144  /// \param dimensions The engine dimensions.
145  void set_dimensions(const std::uint32_t dimensions) {
146    if (dimensions == _dimensions)
147      return;
148    _dimensions = dimensions;
149    _engine = create_engine(_queue, _seed, _dimensions, _mode);
150  }
151
152  /// Set the queue of host rng_generator.
153  /// \param queue The engine queue.
154  void set_queue(sycl::queue *queue) {
155    if (queue == _queue)
156      return;
157    _queue = queue;
158    _engine = create_engine(_queue, _seed, _dimensions, _mode);
159  }
160
161  /// Set the mode of host rng_generator.
162  /// \param mode The engine mode.
163  void set_mode(const random_mode mode) {
164#ifndef __INTEL_MKL__
165    throw std::runtime_error(OneMKLNotSupport);
166#else
167    if constexpr (!std::is_same_v<engine_t, oneapi::mkl::rng::mrg32k3a>) {
168      throw std::runtime_error("Only mrg32k3a engine support this method.");
169    }
170    if (mode == _mode)
171      return;
172    _mode = mode;
173    _engine = create_engine(_queue, _seed, _dimensions, _mode);
174#endif
175  }
176
177  /// Set the direction numbers of Sobol host rng_generator.
178  /// \param direction_numbers The user-defined direction numbers.
179  void
180  set_direction_numbers(const std::vector<std::uint32_t> &direction_numbers) {
181#ifndef __INTEL_MKL__
182    throw std::runtime_error(OneMKLNotSupport);
183#else
184    if constexpr (std::is_same_v<engine_t, oneapi::mkl::rng::sobol>) {
185      if (direction_numbers == _direction_numbers)
186        return;
187      _direction_numbers = direction_numbers;
188      _engine =
189          create_engine(_queue, _seed, _dimensions, _mode, _direction_numbers);
190    } else {
191      throw std::runtime_error("Only Sobol engine supports this method.");
192    }
193#endif
194  }
195
196  /// Set the engine index of MT2203 host rng_generator.
197  /// \param engine_idx The user-defined engine index.
198  void set_engine_idx(std::uint32_t engine_idx) {
199#ifndef __INTEL_MKL__
200    throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) "
201                             "Interfaces Project does not support this API.");
202#else
203    if constexpr (std::is_same_v<engine_t, oneapi::mkl::rng::mt2203>) {
204      if (engine_idx == _engine_idx)
205        return;
206      _engine_idx = engine_idx;
207      _engine = create_engine(_queue, _seed, _dimensions, _mode, std::nullopt,
208                              _engine_idx);
209    } else {
210      throw std::runtime_error("Only MT2203 engine supports this method.");
211    }
212#endif
213  }
214
215  /// Generate unsigned int random number(s) with 'uniform_bits' distribution.
216  /// \param output The pointer of the first random number.
217  /// \param n The number of random numbers.
218  inline void generate_uniform_bits(unsigned int *output, std::int64_t n) {
219#ifndef __INTEL_MKL__
220    throw std::runtime_error(OneMKLNotSupport);
221#else
222    static_assert(sizeof(unsigned int) == sizeof(std::uint32_t));
223    generate<oneapi::mkl::rng::uniform_bits<std::uint32_t>>(
224        (std::uint32_t *)output, n);
225#endif
226  }
227
228  /// Generate unsigned long long random number(s) with 'uniform_bits'
229  /// distribution.
230  /// \param output The pointer of the first random number.
231  /// \param n The number of random numbers.
232  inline void generate_uniform_bits(unsigned long long *output,
233                                    std::int64_t n) {
234#ifndef __INTEL_MKL__
235    throw std::runtime_error(OneMKLNotSupport);
236#else
237    static_assert(sizeof(unsigned long long) == sizeof(std::uint64_t));
238    generate<oneapi::mkl::rng::uniform_bits<std::uint64_t>>(
239        (std::uint64_t *)output, n);
240#endif
241  }
242
243  /// Generate float random number(s) with 'lognormal' distribution.
244  /// \param output The pointer of the first random number.
245  /// \param n The number of random numbers.
246  /// \param m Mean of associated normal distribution
247  /// \param s Standard deviation of associated normal distribution.
248  inline void generate_lognormal(float *output, std::int64_t n, float m,
249                                 float s) {
250    generate<oneapi::mkl::rng::lognormal<float>>(output, n, m, s);
251  }
252
253  /// Generate double random number(s) with 'lognormal' distribution.
254  /// \param output The pointer of the first random number.
255  /// \param n The number of random numbers.
256  /// \param m Mean of associated normal distribution
257  /// \param s Standard deviation of associated normal distribution.
258  inline void generate_lognormal(double *output, std::int64_t n, double m,
259                                 double s) {
260    generate<oneapi::mkl::rng::lognormal<double>>(output, n, m, s);
261  }
262
263  /// Generate float random number(s) with 'gaussian' distribution.
264  /// \param output The pointer of the first random number.
265  /// \param n The number of random numbers.
266  /// \param mean Mean of normal distribution
267  /// \param stddev Standard deviation of normal distribution.
268  inline void generate_gaussian(float *output, std::int64_t n, float mean,
269                                float stddev) {
270    generate<oneapi::mkl::rng::gaussian<float>>(output, n, mean, stddev);
271  }
272
273  /// Generate double random number(s) with 'gaussian' distribution.
274  /// \param output The pointer of the first random number.
275  /// \param n The number of random numbers.
276  /// \param mean Mean of normal distribution
277  /// \param stddev Standard deviation of normal distribution.
278  inline void generate_gaussian(double *output, std::int64_t n, double mean,
279                                double stddev) {
280    generate<oneapi::mkl::rng::gaussian<double>>(output, n, mean, stddev);
281  }
282
283  /// Generate unsigned int random number(s) with 'poisson' distribution.
284  /// \param output The pointer of the first random number.
285  /// \param n The number of random numbers.
286  /// \param lambda Lambda for the Poisson distribution.
287  inline void generate_poisson(unsigned int *output, std::int64_t n,
288                               double lambda) {
289    generate<oneapi::mkl::rng::poisson<unsigned int>>(output, n, lambda);
290  }
291
292  /// Generate float random number(s) with 'uniform' distribution.
293  /// \param output The pointer of the first random number.
294  /// \param n The number of random numbers.
295  inline void generate_uniform(float *output, std::int64_t n) {
296    generate<oneapi::mkl::rng::uniform<float>>(output, n);
297  }
298
299  /// Generate double random number(s) with 'uniform' distribution.
300  /// \param output The pointer of the first random number.
301  /// \param n The number of random numbers.
302  inline void generate_uniform(double *output, std::int64_t n) {
303    generate<oneapi::mkl::rng::uniform<double>>(output, n);
304  }
305
306  /// Skip ahead several random number(s).
307  /// \param num_to_skip The number of random numbers to be skipped.
308  void skip_ahead(const std::uint64_t num_to_skip) {
309#ifndef __INTEL_MKL__
310    oneapi::mkl::rng::skip_ahead(_engine, num_to_skip);
311#else
312    if constexpr (std::is_same_v<engine_t, oneapi::mkl::rng::mt2203>)
313      throw std::runtime_error("no skip_ahead method of mt2203 engine.");
314    else
315      oneapi::mkl::rng::skip_ahead(_engine, num_to_skip);
316#endif
317  }
318
319private:
320  static inline engine_t
321  create_engine(sycl::queue *queue, const std::uint64_t seed,
322                const std::uint32_t dimensions, const random_mode mode,
323                std::optional<std::vector<std::uint32_t>> direction_numbers =
324                    std::nullopt,
325                std::optional<std::uint32_t> engine_idx = std::nullopt) {
326#ifdef __INTEL_MKL__
327    if constexpr (std::is_same_v<engine_t, oneapi::mkl::rng::mrg32k3a>) {
328      // oneapi::mkl::rng::mrg32k3a_mode is only supported for GPU device. For
329      // other devices, this argument will be ignored.
330      if (queue->get_device().is_gpu()) {
331#if __cplusplus < 202002L
332        // before C++20
333        switch (mode) {
334        case random_mode::best:
335          return engine_t(*queue, seed,
336                          oneapi::mkl::rng::mrg32k3a_mode::custom{81920});
337        case random_mode::legacy:
338          return engine_t(*queue, seed,
339                          oneapi::mkl::rng::mrg32k3a_mode::custom{4096});
340        case random_mode::optimal:
341          return engine_t(*queue, seed,
342                          oneapi::mkl::rng::mrg32k3a_mode::optimal_v);
343        }
344#else
345        // since C++20
346#if defined(_MSC_VER) && !defined(__clang__)
347#pragma message("warning: oneapi::mkl::rng::mrg32k3a_mode::custom cannot be "  \
348                "initialized with a value since C++20. Ignore the parameter "  \
349                "`mode`.")
350#else
351#warning "oneapi::mkl::rng::mrg32k3a_mode::custom cannot be initialized with " \
352         "a value since C++20. Ignore the parameter `mode`."
353#endif
354        return engine_t(*queue, dimensions);
355#endif
356      }
357    } else if constexpr (std::is_same_v<engine_t, oneapi::mkl::rng::mt2203>) {
358      if (engine_idx.has_value()) {
359        return engine_t(*queue, seed, engine_idx.value());
360      }
361    } else if constexpr (std::is_same_v<engine_t, oneapi::mkl::rng::sobol>) {
362      if (direction_numbers.has_value()) {
363        return engine_t(*queue, direction_numbers.value());
364      }
365      return engine_t(*queue, dimensions);
366    }
367#endif
368    return engine_t(*queue, seed);
369  }
370
371  template <typename distr_t, typename buffer_t, class... distr_params_t>
372  void generate(buffer_t *output, const std::int64_t n,
373                const distr_params_t... distr_params) {
374    auto output_buf = ::dpct::detail::get_memory<buffer_t>(output);
375    oneapi::mkl::rng::generate(distr_t(distr_params...), _engine, n,
376                               output_buf);
377  }
378  engine_t _engine{};
379};
380} // namespace dpct::rng::host::detail
381
382#endif // __DPCT_RNG_UTILS_DETAIL_HPP__