BioCMAMC-ST
model_kernel.hpp
1#ifndef __SIMULATION_MC_KERNEL_HPP
2#define __SIMULATION_MC_KERNEL_HPP
3
4#include <Kokkos_Assert.hpp>
5#include <Kokkos_Core.hpp>
6#include <Kokkos_Macros.hpp>
7#include <Kokkos_Printf.hpp>
8#include <Kokkos_Random.hpp>
9#include <biocma_cst_config.hpp>
10#include <cassert>
11#include <common/common.hpp>
12#include <mc/alias.hpp>
13#include <mc/domain.hpp>
14#include <mc/events.hpp>
15#include <mc/particles_container.hpp>
16#include <mc/prng/prng.hpp>
17#include <mc/traits.hpp>
18// #include <simulation/probability_leaving.hpp>
19#include <simulation/kernels/cycle_reducer.hpp>
20#include <simulation/probe.hpp>
21#include <utility>
22
23#define CHECK_STATUS_OR_RETURN(__idx__) \
24 if (particles.status(__idx__) != MC::Status::Idle) [[unlikely]] \
25 { \
26 return; \
27 }
28
30{
32 {
33 };
35 {
36 };
37 struct TagCycle
38 {
39 };
40
41 template <ModelType M> struct CycleFunctor
42 {
43 using TeamPolicy = Kokkos::TeamPolicy<ComputeSpace>;
44 using TeamMember = TeamPolicy::member_type;
46
47 CycleFunctor() = default;
48
49 std::size_t n_p{};
50
51 std::size_t m_p_team;
52
53 KOKKOS_INLINE_FUNCTION
54 CycleFunctor(std::size_t p_per_team,
56 MC::pool_type _random_pool,
57 MC::KernelConcentrationType _concentrations,
58 MC::EventContainer _event,
60 : m_p_team(p_per_team), d_t(0.), particles(std::move(_particles)),
61 random_pool(std::move(_random_pool)),
62 concentrations(std::move(_concentrations)), events(std::move(_event)),
63 probes(std::move(_probes))
64 {
65 }
66
67 [[nodiscard]] constexpr bool
69 {
70 return M::n_c > 0;
71 }
72
73 void
74 update(double _d_t, MC::ParticlesContainer<M> _particles)
75 {
76 this->d_t = _d_t;
77 this->particles = std::move(_particles);
78 n_p = this->particles.n_particles();
79 }
80
81 KOKKOS_INLINE_FUNCTION void
83 const TeamMember& team,
84 value_type& reduce_val) const
85 {
86
87 (void)_tag;
88 const std::size_t count = m_p_team;
89 const std::size_t p0 = team.league_rank() * count;
90 const std::size_t n_particle = n_p;
91 const auto _d_t = d_t;
92 const auto& status = particles.status;
93
94 const auto upper_bound
95 = ((p0 + count) >= n_particle) ? n_particle - p0 : count;
96
97 KOKKOS_ASSERT(upper_bound > 0);
98 KOKKOS_ASSERT(upper_bound <= n_particle);
99
100 const auto& model = particles.model;
101 const auto& contribs = particles.contribs;
102
103 value_type local;
104 Kokkos::parallel_reduce(
105 Kokkos::TeamThreadRange(team, 0, upper_bound),
106 [&](std::size_t relative_index, value_type& lv)
107 {
108 const std::size_t flatten_index = p0 + relative_index;
109 KOKKOS_ASSERT(flatten_index < upper_bound);
110 const bool active = status(flatten_index) == MC::Status::Idle;
111
112 if (active)
113 {
114 const auto pos = particles.position(flatten_index);
115 const auto new_status = M::update(random_pool,
116 d_t,
117 flatten_index,
118 model,
119 contribs,
120 pos,
122
123 if (new_status == MC::Status::Division)
124 {
125 division(flatten_index, lv);
126 }
127 else
128 {
129 particles.ages(flatten_index, 1) += d_t;
130 }
131 }
132 },
133 local);
134
135 team.team_barrier();
136
137 Kokkos::single(Kokkos::PerTeam(team), [&]() { reduce_val += local; });
138 }
139
140 KOKKOS_INLINE_FUNCTION void
141 division(const std::size_t idx, value_type& reduce_val) const
142 {
143 using mem_space = ComputeSpace::memory_space;
144 if constexpr (AutoGenerated::FlagCompileTime::use_probe)
145 {
146 // Register probe here to sample BEFORE division and even if division
147 // procedure fails to spawn new particle, the age is still the same
148 const auto _ = this->probes.template set<mem_space>(
149 particles.ages(idx, 1)); // Skip error
150 }
151
152 if (!particles.handle_division(random_pool, idx)) [[unlikely]]
153 {
154 reduce_val.waiting_allocation_particle += 1;
155 events.wrap_incr<MC::EventType::Overflow>();
156 Kokkos::printf("[KERNEL] Division Overflow\r\n");
157 }
158
160
161 /*
162 TODO, it seems that after some calculation (example cstr 0d)
163 size(division probes) = number of tally+1 where the last probe is 0
164 where size(leaving time) == tally
165 Is it a bug ?
166 */
167 }
168
169 M::FloatType d_t;
175 };
176
177} // namespace Simulation::KernelInline
178#endif
Main owning object for Monte-Carlo particles.
Definition particles_container.hpp:59
@ Overflow
Definition events.hpp:25
@ NewParticle
Spawn new particle.
Definition events.hpp:21
@ Division
Definition alias.hpp:127
@ Idle
Definition alias.hpp:126
gen_pool_type< Kokkos::DefaultExecutionSpace > pool_type
Definition alias.hpp:100
Kokkos::View< const double **, Kokkos::LayoutLeft, ComputeSpace, Kokkos::MemoryTraits< Kokkos::RandomAccess > > KernelConcentrationType
Definition alias.hpp:165
Definition cycle_reducer.hpp:13
Probes< AutoGenerated::probe_buffer_size > ProbeAutogeneratedBuffer
Definition probe.hpp:150
Use to count events that occurs during Monte-Carlo processing cycles.
Definition events.hpp:168
KOKKOS_INLINE_FUNCTION CycleFunctor(std::size_t p_per_team, MC::ParticlesContainer< M > _particles, MC::pool_type _random_pool, MC::KernelConcentrationType _concentrations, MC::EventContainer _event, ProbeAutogeneratedBuffer _probes)
Definition model_kernel.hpp:54
MC::EventContainer events
Definition model_kernel.hpp:173
M::FloatType d_t
Definition model_kernel.hpp:169
CycleReducerType value_type
Definition model_kernel.hpp:45
MC::KernelConcentrationType concentrations
Definition model_kernel.hpp:172
MC::ParticlesContainer< M > particles
Definition model_kernel.hpp:170
TeamPolicy::member_type TeamMember
Definition model_kernel.hpp:44
std::size_t n_p
Definition model_kernel.hpp:49
KOKKOS_INLINE_FUNCTION void operator()(TagCycle _tag, const TeamMember &team, value_type &reduce_val) const
Definition model_kernel.hpp:82
ProbeAutogeneratedBuffer probes
Definition model_kernel.hpp:174
KOKKOS_INLINE_FUNCTION void division(const std::size_t idx, value_type &reduce_val) const
Definition model_kernel.hpp:141
Kokkos::TeamPolicy< ComputeSpace > TeamPolicy
Definition model_kernel.hpp:43
std::size_t m_p_team
Definition model_kernel.hpp:51
void update(double _d_t, MC::ParticlesContainer< M > _particles)
Definition model_kernel.hpp:74
MC::pool_type random_pool
Definition model_kernel.hpp:171
constexpr bool do_contribs() const
Definition model_kernel.hpp:68
Definition cycle_reducer.hpp:15
std::size_t waiting_allocation_particle
Definition cycle_reducer.hpp:16
Definition model_kernel.hpp:35
Definition model_kernel.hpp:32
Definition model_kernel.hpp:38