IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
Randn.h
Go to the documentation of this file.
1// Struct Randn
2// This struct can be used for sampling normal distribution function
3// on unbounded domain.
4//
5#ifndef IPPL_RANDN_H
6#define IPPL_RANDN_H
7
9#include "Random/Utility.h"
10
11namespace ippl {
12 namespace random {
13
25 template <typename T, unsigned Dim>
26 struct randn {
27 using view_type =
29 using GeneratorPool = typename Kokkos::Random_XorShift64_Pool<>;
30
31 // Output View for the random numbers
33
34 // The GeneratorPool
36
47 KOKKOS_INLINE_FUNCTION randn(view_type v_, GeneratorPool rand_pool_, T* mu_p, T* sd_p)
48 : v(v_)
49 , rand_pool(rand_pool_) {
50 for (unsigned int i = 0; i < Dim; i++) {
51 mu[i] = mu_p[i];
52 sd[i] = sd_p[i];
53 }
54 }
55
56 KOKKOS_INLINE_FUNCTION randn(view_type v_, GeneratorPool rand_pool_)
57 : v(v_)
58 , rand_pool(rand_pool_) {
59 for (unsigned int i = 0; i < Dim; i++) {
60 mu[i] = 0.0;
61 sd[i] = 1.0;
62 }
63 }
64
70 KOKKOS_INLINE_FUNCTION const T& getMu(unsigned int idx) const { return mu[idx]; }
71
77 KOKKOS_INLINE_FUNCTION const T& getSd(unsigned int idx) const { return sd[idx]; }
78
84 KOKKOS_INLINE_FUNCTION void operator()(const size_t i) const {
85 // Get a random number state from the pool for the active thread
86 typename GeneratorPool::generator_type rand_gen = rand_pool.get_state();
87
88 for (unsigned d = 0; d < Dim; ++d) {
89 v(i)[d] = mu[d] + sd[d] * rand_gen.normal(0.0, 1.0);
90 }
91
92 // Give the state back, which will allow another thread to acquire it
93 rand_pool.free_state(rand_gen);
94 }
95 };
96 } // namespace random
97} // namespace ippl
98
99#endif
constexpr unsigned Dim
Definition Archive.h:20
view_type v
Definition Randn.h:32
KOKKOS_INLINE_FUNCTION randn(view_type v_, GeneratorPool rand_pool_)
Definition Randn.h:56
GeneratorPool rand_pool
Definition Randn.h:35
typename ippl::detail::ViewType< ippl::Vector< double, Dim >, 1 >::view_type view_type
Definition Randn.h:27
KOKKOS_INLINE_FUNCTION randn(view_type v_, GeneratorPool rand_pool_, T *mu_p, T *sd_p)
Constructor for the randn functor.
Definition Randn.h:47
KOKKOS_INLINE_FUNCTION const T & getSd(unsigned int idx) const
Getter function for the standard deviation in idx dimension.
Definition Randn.h:77
typename Kokkos::Random_XorShift64_Pool<> GeneratorPool
Definition Randn.h:29
KOKKOS_INLINE_FUNCTION const T & getMu(unsigned int idx) const
Getter function for mean in idx dimension.
Definition Randn.h:70
KOKKOS_INLINE_FUNCTION void operator()(const size_t i) const
Operator to generate random numbers.
Definition Randn.h:84