BioCMAMC-ST
old_contribs.hpp
1#ifndef __CONTRIBUTION_KERNEL_HPP__
2#define __CONTRIBUTION_KERNEL_HPP__
3#include "Kokkos_Assert.hpp"
4#include <Kokkos_Core.hpp>
5#include <mc/particles_container.hpp>
6#include <mc/traits.hpp>
7
8template <ModelType M> struct ContributionFunctor
9{
10 struct Tag0D
11 {
12 };
13 struct Tag3D
14 {
15 };
16
17 using TeamPolicy = Kokkos::TeamPolicy<ComputeSpace>;
18 using TeamMember = TeamPolicy::member_type;
19 using float_type = float;
21 = Kokkos::View<float_type*,
22 TeamPolicy::execution_space::scratch_memory_space>;
24 ContributionFunctor(std::size_t particle_per_team,
25 MC::ContributionView contribution_scatter,
27 : m_particle_per_team(particle_per_team),
28 m_contribution_scatter(std::move(contribution_scatter)),
29 m_particles(std::move(particles))
30 {
31 }
32
33 size_t np{};
34
35 void
37
38 {
39 this->m_particles = std::move(_particles);
40 np = m_particles.n_particles();
41 }
42
43 std::size_t m_particle_per_team;
44
47
48 KOKKOS_INLINE_FUNCTION
49 void
50 operator()(Tag0D _tag, const TeamMember& team) const
51 {
52 (void)_tag;
53 const std::size_t p0 = team.league_rank() * m_particle_per_team;
54 const std::size_t n_particle = m_particles.n_particles();
55 const auto& status = m_particles.status;
56 const auto& contribs = m_particles.contribs;
57 static const auto n_c = M::n_c;
58
59 ScratchView scratch(team.team_scratch(0), n_c);
60
61 Kokkos::parallel_for(Kokkos::TeamVectorRange(team, n_c),
62 [&](const std::size_t j)
63 { scratch(j) = float_type{ 0 }; });
64
65 const auto upper_bound = ((p0 + m_particle_per_team) >= n_particle)
66 ? n_particle - p0
68
69 KOKKOS_ASSERT(upper_bound > 0 && upper_bound <= m_particle_per_team
70 && (p0 + upper_bound) <= n_particle);
71
72 team.team_barrier();
73
74 Kokkos::parallel_for(
75 Kokkos::TeamThreadRange(team, 0, upper_bound),
76 [&](const std::size_t relative_index)
77 {
78 const std::size_t flatten_index = p0 + relative_index;
79 if (status(flatten_index) == MC::Status::Idle)
80 {
81 const auto weight = m_particles.get_weight(flatten_index);
82 for (std::size_t j = 0; j < n_c; ++j)
83 {
84 Kokkos::atomic_add(&scratch(j),
85 weight * contribs(flatten_index, j));
86 }
87 }
88 });
89
90 team.team_barrier();
91 const auto& cs = m_contribution_scatter;
92 Kokkos::single(Kokkos::PerTeam(team),
93 [=]()
94 {
95 auto access = cs.access();
96 for (std::size_t j = 0; j < M::n_c; ++j)
97 {
98 access(j, 0) += scratch(j);
99 }
100 });
101 }
102
103 KOKKOS_INLINE_FUNCTION
104 void
105 operator()(const Tag3D _tag, const TeamMember& team) const
106 {
107 (void)_tag;
108
109 const std::size_t p0 = team.league_rank() * m_particle_per_team;
110 const auto& c = m_particles.contribs;
111 const auto& status = m_particles.status;
112 const auto n_particle = m_particles.n_particles();
113 const auto& positions = m_particles.position;
114 constexpr std::size_t work_per_thread = 32;
115
116 const auto upper_bound = ((p0 + m_particle_per_team) >= n_particle)
117 ? n_particle - p0
119 // KOKKOS_ASSERT(upper_bound >= 0 && upper_bound < n_particle);
120 KOKKOS_ASSERT(upper_bound < n_particle);
121
122 const auto range = (upper_bound + work_per_thread - 1) / work_per_thread;
123
124 KOKKOS_ASSERT(m_particle_per_team % work_per_thread == 0);
125
126 Kokkos::parallel_for(
127 Kokkos::TeamThreadRange(team, 0, range),
128 [&](const std::size_t i)
129 {
130 auto access = m_contribution_scatter.access();
131 for (std::size_t k = 0; k < work_per_thread; ++k)
132 {
133 const std::size_t p = p0 + i * work_per_thread + k;
134 if (status(p) != MC::Status::Idle || i >= n_particle)
135 {
136 continue;
137 }
138 const double weight = m_particles.get_weight(p);
139 const auto pos = positions(p);
140 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team, 0, M::n_c),
141 [&](const int j)
142 { access(j, pos) += weight * c(p, j); });
143 }
144 });
145 }
146};
147
148#endif
Main owning object for Monte-Carlo particles.
Definition particles_container.hpp:59
decltype(Kokkos::Experimental::create_scatter_view( kernelContribution())) ContributionView
Definition alias.hpp:162
@ Idle
Definition alias.hpp:126
Definition contribution_kernel.hpp:15
Definition contribution_kernel.hpp:18
Definition contribution_kernel.hpp:12
Kokkos::View< float_type *, TeamPolicy::execution_space::scratch_memory_space > ScratchView
Definition contribution_kernel.hpp:24
TeamPolicy::member_type TeamMember
Definition contribution_kernel.hpp:22
KOKKOS_INLINE_FUNCTION void operator()(Tag0D _tag, const TeamMember &team, value_type &team_value_reduce) const
Definition contribution_kernel.hpp:92
ContributionFunctor(std::size_t particle_per_team, MC::ContributionView contribution_scatter, MC::ParticlesContainer< M > particles, MC::pool_type rp, MC::KernelConcentrationType _concentrations, MC::EventContainer _event, Simulation::ProbeAutogeneratedBuffer d_probes)
Definition contribution_kernel.hpp:40
MC::ParticlesContainer< M > m_particles
Definition contribution_kernel.hpp:32
Kokkos::TeamPolicy< ComputeSpace > TeamPolicy
Definition contribution_kernel.hpp:21
size_t np
Definition contribution_kernel.hpp:55
float float_type
Definition contribution_kernel.hpp:23
MC::ContributionView m_contribution_scatter
Definition contribution_kernel.hpp:31
void update(MC::ParticlesContainer< M > _particles, double _d_t)
Definition contribution_kernel.hpp:58
std::size_t m_particle_per_team
Definition contribution_kernel.hpp:29