6#ifndef IPPL_INVERSE_TRANSFORM_SAMPLING_H
7#define IPPL_INVERSE_TRANSFORM_SAMPLING_H
9#include "Random/Utility.h"
25 template <
typename T,
unsigned Dim,
class DeviceType,
class Distribution>
29 using size_type = ippl::detail::size_type;
44 template <
class RegionLayout>
49 , ntotal_m(ntotal_r) {
50 const typename RegionLayout::host_mirror_type regions =
51 rlayout_r.gethLocalRegions();
52 int rank = ippl::Comm->rank();
55 for (
unsigned d = 0; d < Dim; ++d) {
56 locrmax[d] = regions(rank)[d].max();
57 locrmin[d] = regions(rank)[d].min();
78 , ntotal_m(ntotal_r) {
94 , ntotal_m(ntotal_r) {
119 int rank = ippl::Comm->rank();
121 for (
unsigned d = 0; d < Dim; ++d) {
122 nr_m[d] = dist_m.
getCdf(locrmax[d], d) - dist_m.
getCdf(locrmin[d], d);
123 dr_m[d] = dist_m.
getCdf(rmax[d], d) - dist_m.
getCdf(rmin[d], d);
124 umin_m[d] = dist_m.
getCdf(locrmin[d], d);
125 umax_m[d] = dist_m.
getCdf(locrmax[d], d);
127 T pnr = std::accumulate(nr_m.begin(), nr_m.end(), 1.0, std::multiplies<T>());
128 T pdr = std::accumulate(dr_m.begin(), dr_m.end(), 1.0, std::multiplies<T>());
130 T factor = pnr / pdr;
131 nlocal_m = (size_type)(factor * ntotal_m);
133 size_type nglobal = 0;
134 ippl::Comm->allreduce(&nlocal_m, &nglobal, 1, std::plus<size_type>());
136 int rest = (int)(ntotal_m - nglobal);
157 for (
unsigned d = 0; d < Dim; ++d) {
158 umin_m[d] = dist_m.
getCdf(rmin[d], d);
159 umax_m[d] = dist_m.
getCdf(rmax[d], d);
171 template <
class GeneratorPool>
173 using value_type = T;
176 GeneratorPool pool_m;
193 : targetdist_m(dist_r)
195 , pool_m(rand_pool_r)
205 KOKKOS_INLINE_FUNCTION
void operator()(
const size_t i)
const {
206 typename GeneratorPool::generator_type rand_gen = pool_m.get_state();
210 u = rand_gen.drand(minbound_m[dim_m], maxbound_m[dim_m]);
213 sample_m(i)[dim_m] = targetdist_m.
getEstimate(u, dim_m);
219 solver.
solve(dim_m, sample_m(i)[dim_m], u);
221 pool_m.free_state(rand_gen);
245 void generate(view_type view, Kokkos::Random_XorShift64_Pool<> rand_pool64) {
249 size_type numlocal_m = nlocal_m;
250 for (
unsigned d = 0; d < Dim; ++d) {
251 Kokkos::parallel_for(numlocal_m,
fill_random<Kokkos::Random_XorShift64_Pool<>>(
252 targetdist_m, view, rand_pool64,
253 minbound_m, maxbound_m, d));
267 void generate(view_type view, size_type startIndex, size_type endIndex,
268 Kokkos::Random_XorShift64_Pool<> rand_pool64) {
272 for (
unsigned d = 0; d < Dim; ++d) {
273 Kokkos::parallel_for(
274 Kokkos::RangePolicy<>(startIndex, endIndex),
276 targetdist_m, view, rand_pool64, minbound_m, maxbound_m, d));
The class that represents a distribution.
Definition Distribution.h:33
KOKKOS_INLINE_FUNCTION T getCdf(T x, unsigned int d) const
A wrapper to change the signature arguments of cdf in each dimension d from (x, d,...
Definition Distribution.h:75
KOKKOS_INLINE_FUNCTION T getEstimate(T x, unsigned int d) const
A wrapper to change the signature arguments of estimate in each dimension d from (x,...
Definition Distribution.h:83
Definition ViewTypes.h:44
Functor for solving equations using the Newton-Raphson method.
Definition Utility.h:28
KOKKOS_INLINE_FUNCTION void solve(unsigned int d, T &x, T &u)
Solve an equation using the Newton-Raphson method.
Definition Utility.h:52