1#ifndef __SIMULATION_MOVE_KERNEL_HPP__
2#define __SIMULATION_MOVE_KERNEL_HPP__
4#include "Kokkos_Macros.hpp"
5#include <Kokkos_Assert.hpp>
6#include <Kokkos_Core.hpp>
7#include <Kokkos_Printf.hpp>
8#include <Kokkos_Random.hpp>
9#include <biocma_cst_config.hpp>
11#include <common/common.hpp>
12#include <mc/alias.hpp>
13#include <mc/domain.hpp>
14#include <mc/events.hpp>
15#include <mc/prng/prng.hpp>
16#include <mc/traits.hpp>
17#include <simulation/probability_leaving.hpp>
18#include <simulation/probe.hpp>
71 KOKKOS_INLINE_FUNCTION std::size_t
75 cumulative_probability,
76 const std::size_t i_compartment,
77 const double random_number)
79 const int max_neighbor =
static_cast<int>(neighbors.extent(1));
81 KOKKOS_ASSERT(max_neighbor >= 1);
82 KOKKOS_ASSERT(random_number <= 1. && random_number >= 0.);
83 KOKKOS_ASSERT(neighbors.extent(1) == cumulative_probability.extent(1));
98 int right = max_neighbor - 1;
101 const int mid = (left + right) >> 1;
102 const auto pm = cumulative_probability(i_compartment, mid);
103 const int mask =
static_cast<int>(random_number > pm);
104 left = mask * (mid + 1) + (1 - mask) * left;
105 right = mask * right + (1 - mask) * mid;
107 KOKKOS_ASSERT(left >= 0 &&
static_cast<size_t>(left) < neighbors.extent(1));
108 return neighbors(i_compartment, left);
111 template <
typename ViewType1>
112 KOKKOS_INLINE_FUNCTION
void
114 const std::size_t position,
118 std::size_t i_flow = 0;
119 const std::size_t n_flow = leaving_flow.size();
125 const auto& [index, flow, liquid_volume] = leaving_flow(i_flow++);
126 if (position == index)
129 _liquid_volume = liquid_volume;
132 }
while (i_flow < n_flow);
141 std::size_t p_team_leave,
156 std::
move(_random_pool),
164 std::size_t p_team_leave,
201 this->
move = std::move(move_i);
203 this->
status = std::move(_status);
212 KOKKOS_INLINE_FUNCTION
void
214 const Kokkos::TeamPolicy<ComputeSpace>::member_type& team,
218 const std::size_t p0 = team.league_rank() * N;
221 KOKKOS_ASSERT(upper_bound > 0 && upper_bound <=
n_particles);
224 const auto& proba =
move.move_probability;
225 const std::size_t p = team.team_size();
226 const std::size_t m = (upper_bound + p - 1) / p;
228 std::size_t team_move_count = 0;
229 Kokkos::parallel_reduce(
230 Kokkos::TeamThreadRange(team, 0, p),
231 [&](
const std::size_t tid, std::size_t& moved_count)
233 auto gen = rp.get_state();
235 for (std::size_t k = 0; k < m; ++k)
237 const std::size_t idx = tid + k * p;
239 if (idx >= upper_bound)
243 const std::size_t flat_index = p0 + idx;
251 const std::size_t next
265 Kokkos::single(Kokkos::PerTeam(team),
266 [&]() { local_tally.
moved += team_move_count; });
269 KOKKOS_INLINE_FUNCTION
void
271 const Kokkos::TeamPolicy<ComputeSpace>::member_type& team_handle,
272 std::size_t& local_dead_count)
const
276 const std::size_t p0 = team_handle.league_rank() * count;
279 const auto upper_bound
280 = ((p0 + count) >= n_particle) ? n_particle - p0 : count;
282 KOKKOS_ASSERT(upper_bound > 0 && upper_bound <= n_particle);
283 std::size_t team_dead_count = 0;
284 const auto& lf =
move.leaving_flow;
285 Kokkos::parallel_reduce(
286 Kokkos::TeamThreadRange(team_handle, 0, upper_bound),
287 [&](
const std::size_t idx, std::size_t& thread_dead_count)
289 const auto flat_index = p0 + idx;
294 team_handle.team_barrier();
296 Kokkos::PerTeam(team_handle),
299 if constexpr (AutoGenerated::FlagCompileTime::enable_event_counter)
303 local_dead_count += team_dead_count;
312 KOKKOS_INLINE_FUNCTION
void
314 const Kokkos::TeamPolicy<ComputeSpace>::member_type& team,
318 const std::size_t p0 = team.league_rank() * N;
321 KOKKOS_ASSERT(upper_bound > 0 && upper_bound <=
n_particles);
324 const auto& lf =
move.leaving_flow;
325 const auto& proba =
move.move_probability;
327 const std::size_t p = team.team_size();
328 const std::size_t m = (upper_bound + p - 1) / p;
332 Kokkos::parallel_reduce(
333 Kokkos::TeamThreadRange(team, 0, p),
336 auto gen = rp.get_state();
338 for (std::size_t k = 0; k < m; ++k)
340 const std::size_t idx = tid + k * p;
342 if (idx >= upper_bound)
346 const std::size_t flat_index = p0 + idx;
349 std::size_t position =
positions(flat_index);
354 const std::size_t next
382 Kokkos::single(Kokkos::PerTeam(team),
383 [&]() { local_tally += team_tally; });
398 KOKKOS_INLINE_FUNCTION
void
400 const Kokkos::TeamPolicy<ComputeSpace>::member_type& team_handle,
406 const auto& lf =
move.leaving_flow;
413 Kokkos::PerTeam(team_handle),
414 [&lf, dt = this->
d_t](
double& local_lambda)
416 const auto& [_, flow_value, liquid_volume] = lf(0);
418 local_lambda = dt * flow_value / liquid_volume;
422 team_handle.team_barrier();
432 const std::size_t p0 = team_handle.league_rank() * N;
435 KOKKOS_ASSERT(upper_bound > 0 && upper_bound <=
n_particles);
439 const std::size_t p = team_handle.team_size();
441 const std::size_t m = (upper_bound + p - 1) / p;
443 std::size_t team_dead_count = 0;
449 Kokkos::parallel_reduce(
450 Kokkos::TeamThreadRange(team_handle, 0, p),
451 [&](
const std::size_t tid, std::size_t& dead_count)
453 auto gen = rp.get_state();
455 for (std::size_t k = 0; k < m; ++k)
457 const std::size_t idx = tid + k * p;
459 if (idx >= upper_bound)
463 const std::size_t flat_index = p0 + idx;
469 const double r = gen.drand(0., 1.);
479 team_handle.team_barrier();
481 Kokkos::single(Kokkos::PerTeam(team_handle),
482 [&]() { local_tally.
dead += team_dead_count; });
489 template <
typename GenType>
490 KOKKOS_FORCEINLINE_FUNCTION std::size_t
492 const std::size_t i_current_compartment,
498 i_current_compartment <
move.liquid_volume.extent(0)
499 &&
"Particle position is incorect (greater than compartment number)");
501 moved = move_probability(i_current_compartment) > gen.frand(0.F, 1.F);
505 return i_current_compartment;
509 move.cumulative_probability,
510 i_current_compartment,
511 gen.frand(0.F, 1.F));
513 next <
move.liquid_volume.extent(0)
514 &&
" Position after move is greater than compartment number");
521 template <
typename ExecSpace,
typename GenType>
522 KOKKOS_FORCEINLINE_FUNCTION
void
524 const std::size_t idx,
525 const Kokkos::View<const MC::LeavingFlow*, ExecSpace>& leaving_flow,
526 const std::size_t position,
528 std::size_t& dead_count)
const
532 find_flow(leaving_flow, position, found_flow_value, found_liquid_volume);
535 if (found_flow_value != 0.)
537 const auto r = gen.frand(0., 1.);
538 KOKKOS_ASSERT(found_liquid_volume > 0.);
539 KOKKOS_ASSERT(found_flow_value > 0.);
541 r, found_liquid_volume, found_flow_value,
d_t),
547 template <
typename ExecSpace>
548 KOKKOS_FORCEINLINE_FUNCTION std::size_t
550 const std::size_t idx,
551 const Kokkos::View<const MC::LeavingFlow*, ExecSpace>& leaving_flow,
552 std::size_t& dead_count)
const
555 const std::size_t position =
positions(idx);
563 find_flow(leaving_flow, position, found_flow_value, found_liquid_volume);
567 if (found_flow_value != 0.)
571 const auto r = gen.frand(0., 1.);
574 KOKKOS_ASSERT(found_liquid_volume > 0.);
575 KOKKOS_ASSERT(found_flow_value > 0.);
577 r, found_liquid_volume, found_flow_value,
d_t);
584 KOKKOS_INLINE_FUNCTION
void
586 const std::size_t idx,
587 std::size_t& dead_count)
const
594 if constexpr (AutoGenerated::FlagCompileTime::use_probe)
597 using mem_space = ComputeSpace::memory_space;
599 const auto _ =
probes.set<mem_space>(time);
627 struct reduction_identity<
Simulation::KernelInline::MoveLeaveTally>
Definition move_kernel.hpp:625
std::conditional_t< is_const, Kokkos::View< const std::size_t **, Kokkos::LayoutRight, ExecSpace, Kokkos::MemoryTraits< Kokkos::RandomAccess > >, Kokkos::View< std::size_t **, Kokkos::LayoutRight, ExecSpace > > NeighborsView
Definition alias.hpp:216
ParticleAgesBase< ComputeSpace > ParticleRefTime
Definition alias.hpp:149
std::conditional_t< is_const, Kokkos::View< const double *, ExecSpace, Kokkos::MemoryTraits< Kokkos::RandomAccess > >, Kokkos::View< double *, ExecSpace > > MoveProbabilityView
d_t * flow / volume per compartment. Owned by ReactorDomain
Definition alias.hpp:183
Kokkos::View< Status *, ComputeSpace > ParticleStatus
Definition alias.hpp:144
@ Exit
Remove particle from list due to move in domain.
Definition events.hpp:22
ParticlePositionT< ComputeSpace > ParticlePositions
Definition alias.hpp:143
@ Idle
Definition alias.hpp:126
@ Exit
Definition alias.hpp:128
gen_pool_type< Kokkos::DefaultExecutionSpace > pool_type
Definition alias.hpp:100
std::conditional_t< is_const, Kokkos::View< const double **, Kokkos::LayoutRight, ExecSpace, Kokkos::MemoryTraits< Kokkos::RandomAccess > >, Kokkos::View< double **, Kokkos::LayoutRight, ExecSpace > > CumulativeProbabilityView
Definition alias.hpp:207
Definition cycle_reducer.hpp:13
constexpr bool enable_leave
Definition move_kernel.hpp:23
constexpr bool enable_move
Definition move_kernel.hpp:26
constexpr bool disable_leave
Definition move_kernel.hpp:24
KOKKOS_INLINE_FUNCTION bool probability_leaving(const T random_number, const double lambda)
Definition probability_leaving.hpp:26
KOKKOS_INLINE_FUNCTION std::size_t __find_next_compartment(const MC::NeighborsView< ComputeSpace, true > &neighbors, const MC::CumulativeProbabilityView< ComputeSpace, true > &cumulative_probability, const std::size_t i_compartment, const double random_number)
probably overkill binary search to find next compartment
Definition move_kernel.hpp:72
KOKKOS_INLINE_FUNCTION void find_flow(const ViewType1 &leaving_flow, const std::size_t position, MC::LeavingFlow::float_type &val_flow, MC::LeavingFlow::float_type &_liquid_volume)
Definition move_kernel.hpp:113
int precision_tag
Definition probability_leaving.hpp:12
constexpr bool disable_move
Definition move_kernel.hpp:25
Namespace that contains classes and structures related to simulation handling.
Definition host_specific.hpp:12
Probes< AutoGenerated::probe_buffer_size > ProbeAutogeneratedBuffer
Definition probe.hpp:150
static KOKKOS_FORCEINLINE_FUNCTION Simulation::KernelInline::MoveLeaveTally sum()
Definition move_kernel.hpp:630
Structure to store information about domain needed during MC cycle data is likely to change between e...
Definition domain.hpp:29
Use to count events that occurs during Monte-Carlo processing cycles.
Definition events.hpp:168
double float_type
Definition domain.hpp:19
bool do_move() const
Definition move_kernel.hpp:387
MoveFunctor(std::size_t p_team_move, std::size_t p_team_leave, MC::ParticlePositions p, MC::ParticleStatus _status, MC::DomainState< ComputeSpace > m, MC::pool_type _random_pool, MC::EventContainer _events, ProbeAutogeneratedBuffer _probes, MC::ParticleRefTime _t_ref)
Definition move_kernel.hpp:140
KOKKOS_FORCEINLINE_FUNCTION std::size_t next_compartment(const std::size_t i_current_compartment, const MC::MoveProbabilityView< ComputeSpace, true > &move_probability, GenType &gen, bool &moved) const
Definition move_kernel.hpp:491
MC::ParticleRefTime p_times
Definition move_kernel.hpp:615
bool m_enable_leave
Definition move_kernel.hpp:620
void update(double absolute_time, double _d_t, std::size_t n_p, MC::DomainState< ComputeSpace > move_i, MC::ParticlePositions _positions, MC::ParticleStatus _status, MC::ParticleRefTime _t_ref, bool b_move, bool b_leave)
Definition move_kernel.hpp:185
double current_time
Definition move_kernel.hpp:606
KOKKOS_INLINE_FUNCTION void operator()(TagLeave _tag, const Kokkos::TeamPolicy< ComputeSpace >::member_type &team_handle, std::size_t &local_dead_count) const
Definition move_kernel.hpp:270
MoveFunctor(std::size_t p_team_move, std::size_t p_team_leave, double _d_t, MC::ParticlePositions p, MC::ParticleStatus _status, std::size_t n_p, MC::DomainState< ComputeSpace > m, MC::pool_type _random_pool, MC::EventContainer _events, ProbeAutogeneratedBuffer _probes, MC::ParticleRefTime _t_ref, bool b_move, bool b_leave)
Definition move_kernel.hpp:163
KOKKOS_INLINE_FUNCTION void operator()(TagMove, const Kokkos::TeamPolicy< ComputeSpace >::member_type &team, MoveLeaveTally &local_tally) const
Definition move_kernel.hpp:213
bool need_launch() const
Definition move_kernel.hpp:207
Kokkos::TeamPolicy< ComputeSpace > TeamPolicy
Definition move_kernel.hpp:137
TeamPolicy::member_type TeamMember
Definition move_kernel.hpp:138
MC::ParticlePositions positions
Definition move_kernel.hpp:608
std::size_t m_p_team_move
Definition move_kernel.hpp:617
double d_t
Definition move_kernel.hpp:607
MC::DomainState< ComputeSpace, true > move
Definition move_kernel.hpp:610
KOKKOS_FORCEINLINE_FUNCTION std::size_t handle_exit(const std::size_t idx, const Kokkos::View< const MC::LeavingFlow *, ExecSpace > &leaving_flow, std::size_t &dead_count) const
Definition move_kernel.hpp:549
KOKKOS_FORCEINLINE_FUNCTION void handle_exit_at(const std::size_t idx, const Kokkos::View< const MC::LeavingFlow *, ExecSpace > &leaving_flow, const std::size_t position, GenType &gen, std::size_t &dead_count) const
Definition move_kernel.hpp:523
std::size_t n_particles
Definition move_kernel.hpp:609
MC::EventContainer events
Definition move_kernel.hpp:613
ProbeAutogeneratedBuffer probes
Definition move_kernel.hpp:614
KOKKOS_INLINE_FUNCTION void perform_exit(const bool proba, const std::size_t idx, std::size_t &dead_count) const
Definition move_kernel.hpp:585
KOKKOS_INLINE_FUNCTION void operator()(TagMoveLeave, const Kokkos::TeamPolicy< ComputeSpace >::member_type &team, MoveLeaveTally &local_tally) const
Definition move_kernel.hpp:313
MC::ParticleStatus status
Definition move_kernel.hpp:612
bool m_enable_move
Definition move_kernel.hpp:619
std::size_t m_p_team_leave
Definition move_kernel.hpp:616
KOKKOS_INLINE_FUNCTION void operator()(TagLeaveB0D _tag, const Kokkos::TeamPolicy< ComputeSpace >::member_type &team_handle, MoveLeaveTally &local_tally) const
Definition move_kernel.hpp:399
MC::pool_type random_pool
Definition move_kernel.hpp:611
bool do_leave() const
Definition move_kernel.hpp:393
Definition move_kernel.hpp:51
std::size_t dead
Definition move_kernel.hpp:53
std::size_t moved
Definition move_kernel.hpp:52
KOKKOS_INLINE_FUNCTION MoveLeaveTally & operator+=(const MoveLeaveTally &other)
Definition move_kernel.hpp:56
Definition move_kernel.hpp:37
Definition move_kernel.hpp:33
Definition move_kernel.hpp:44
Definition move_kernel.hpp:29