1#ifndef __PARTICLES_CONTAINER_HPP__
2#define __PARTICLES_CONTAINER_HPP__
4#include "common/common.hpp"
6#include <Kokkos_Core.hpp>
7#include <common/has_serialize.hpp>
9#include <mc/prng/prng.hpp>
10#include <mc/traits.hpp>
16 using TeamPolicy = Kokkos::TeamPolicy<ComputeSpace>;
17 using TeamMember = TeamPolicy::member_type;
19 template <ModelType M>
struct FillGapFunctor
23 M::SelfParticle _model,
26 std::size_t _to_remove,
27 std::size_t _last_used_index)
28 : status(std::move(_status)), model(std::move(_model)),
29 position(std::move(_position)), ages(std::move(_ages)),
30 offset(
"offset"), to_remove(_to_remove),
31 last_used_index(_last_used_index)
34 Kokkos::deep_copy(offset, 0);
37 KOKKOS_INLINE_FUNCTION
void
38 operator()(
const int i, std::size_t& update,
const bool final)
const
42 std::size_t scan_index = update;
43 update += is_dead ? 1 : 0;
45 if (
final && is_dead && scan_index < to_remove)
48 const auto i_to_remove = i;
50 last_used_index - Kokkos::atomic_fetch_add(&offset(), 1);
52 idx_to_move ==
static_cast<std::size_t
>(i_to_remove))
55 last_used_index - Kokkos::atomic_fetch_add(&offset(), 1);
59 Kokkos::atomic_exchange(&position(i_to_remove), position(idx_to_move));
61 for (std::size_t i_properties = 0; i_properties < M::n_var;
64 model(i_to_remove, i_properties) = model(idx_to_move, i_properties);
66 ages(i_to_remove, 0) = ages(idx_to_move, 0);
67 ages(i_to_remove, 1) = ages(idx_to_move, 1);
72 M::SelfParticle model;
75 Kokkos::View<std::size_t, ComputeSpace> offset;
76 std::size_t to_remove;
77 std::size_t last_used_index;
80 template <ModelType M>
struct InsertFunctor
82 InsertFunctor(std::size_t _original_size,
83 M::SelfParticle _model,
85 M::SelfParticle _buffer_model,
87 : original_size(_original_size), model(std::move(_model)),
88 position(std::move(_position)),
89 buffer_model(std::move(_buffer_model)),
90 buffer_position(std::move(_buffer_position))
107 KOKKOS_INLINE_FUNCTION
108 void operator()(
const TeamMember& team)
const
110 auto range = M::n_var;
111 const int i = team.league_rank();
113 position(original_size + i) = buffer_position(i);
114 Kokkos::parallel_for(
115 Kokkos::TeamVectorRange(team, range),
117 { model(original_size + i, j) = buffer_model(i, j); });
120 std::size_t original_size;
121 M::SelfParticle model;
123 M::SelfParticle buffer_model;
154 bool virtual_position =
false);
180 [[nodiscard]] KOKKOS_INLINE_FUNCTION std::size_t
n_particles()
const;
184 KOKKOS_INLINE_FUNCTION
void
204 [[nodiscard]] KOKKOS_INLINE_FUNCTION
bool
206 std::size_t idx1)
const;
214 template <
class Archive>
void save(Archive& ar)
const;
216 template <
class Archive>
void load(Archive& ar);
218 [[nodiscard]] KOKKOS_INLINE_FUNCTION
double
229 std::size_t threshold);
243 void _resize(std::size_t new_size,
bool force =
false);
250 template <ModelType Model>
255 "ModelType: Constapply_weight()");
259 auto access = contributions.access();
263 const int rel = i -
begin;
265 const double c = weight *
model(idx, i);
267 access(pos, rel) += c;
272 template <ModelType Model>
273 [[nodiscard]] KOKKOS_INLINE_FUNCTION std::size_t
279 template <ModelType Model>
291 template <ModelType Model>
292 [[maybe_unused]] [[nodiscard]]
auto
298 template <ModelType Model>
303 template <ModelType Model>
309 template <ModelType Model>
310 template <
class Archive>
316 deserialize_view(ar,
status);
317 deserialize_view(ar,
model);
318 deserialize_view(ar,
ages);
322 template <ModelType Model>
323 template <
class Archive>
329 serialize_view(ar,
status);
330 serialize_view(ar,
model);
331 serialize_view(ar,
ages);
334 template <ModelType Model>
337 const std::size_t dead,
338 const std::size_t threshold)
351 template <ModelType Model>
357 const auto idx2 = Kokkos::atomic_fetch_add(&
buffer_index(), 1);
363 Kokkos::printf(
"%ld %ld\r\n",
371 PROFILE_SECTION(
"ParticlesContainer::merge_buffer")
378 _resize(original_size + n_add_item);
381 Kokkos::parallel_for(
383 TeamPolicy(n_add_item, Kokkos::AUTO, Model::n_var),
384 InsertFunctor<Model>(
392 template <ModelType Model>
395 PROFILE_SECTION(
"ParticlesContainer::_resize")
404 const auto new_allocated_size =
static_cast<std::size_t
>(
412 Kokkos::resize(
model,
431 template <ModelType Model>
434 PROFILE_SECTION(
"ParticlesContainer::__allocate_buffer__")
436 if (
static_cast<double>(buffer_size) /
440 buffer_size =
static_cast<std::size_t
>(
452 template <ModelType M>
454 bool virtual_position)
455 :
model(Kokkos::view_alloc(Kokkos::WithoutInitializing,
"particle_model"),
457 position(Kokkos::view_alloc(Kokkos::WithoutInitializing,
458 "particle_position"),
461 Kokkos::view_alloc(Kokkos::WithoutInitializing,
"particle_status"),
464 Kokkos::view_alloc(Kokkos::WithoutInitializing,
"particle_weigth"),
466 ages(Kokkos::view_alloc(Kokkos::WithoutInitializing,
"particle_age"),
477 auto bounds = M::get_bounds();
478 begin = bounds.begin;
482 template <ModelType M>
484 :
model(Kokkos::view_alloc(Kokkos::WithoutInitializing,
"particle_model"),
486 position(Kokkos::view_alloc(Kokkos::WithoutInitializing,
487 "particle_position"),
490 Kokkos::view_alloc(Kokkos::WithoutInitializing,
"particle_status"),
493 Kokkos::view_alloc(Kokkos::WithoutInitializing,
"particle_weigth"),
495 ages(Kokkos::view_alloc(Kokkos::WithoutInitializing,
"particle_age"),
497 buffer_model(Kokkos::view_alloc(Kokkos::WithoutInitializing,
498 "buffer_particle_model"),
501 "buffer_particle_model"),
509 auto bounds = M::get_bounds();
510 begin = bounds.begin;
514 template <ModelType M>
517 PROFILE_SECTION(
"ParticlesContainer::remove_dead")
530 throw std::runtime_error(
"clean_dead: Error in kernel cannot remove more "
531 "element than existing");
539 Kokkos::parallel_scan(
638 template <ModelType M>
639 [[nodiscard]] KOKKOS_INLINE_FUNCTION
double
Kokkos::Random_XorShift1024_Pool< Kokkos::DefaultExecutionSpace > pool_type
Definition prng.hpp:17
std::size_t get_inactive() const
Definition particles_container.hpp:280
~ParticlesContainer()=default
Default destructor.
void force_remove_dead()
Definition particles_container.hpp:285
Model::SelfParticle model
Definition particles_container.hpp:157
KOKKOS_INLINE_FUNCTION std::size_t n_particles() const
Gets the number of particles in the container.
Definition particles_container.hpp:274
double allocation_factor
Definition particles_container.hpp:238
ParticleWeigths weights
Definition particles_container.hpp:160
int end
Definition particles_container.hpp:247
std::size_t update_and_clean_dead(std::size_t out, std::size_t dead, std::size_t threshold)
Definition particles_container.hpp:336
double get_allocation_factor() const
Definition particles_container.hpp:299
Kokkos::View< uint64_t, Kokkos::SharedSpace > buffer_index
Definition particles_container.hpp:237
void _resize(std::size_t new_size, bool force=false)
Definition particles_container.hpp:393
void __allocate_buffer__()
Definition particles_container.hpp:432
ParticlesContainer()
Definition particles_container.hpp:483
KOKKOS_INLINE_FUNCTION bool handle_division(const MC::KPRNG::pool_type &random_pool, std::size_t idx1) const
Definition particles_container.hpp:352
ParticlesContainer & operator=(ParticlesContainer &&)=default
uint64_t n_used_elements
Definition particles_container.hpp:240
auto get_buffer_index() const
Definition particles_container.hpp:293
void merge_buffer()
Definition particles_container.hpp:369
Model::SelfParticle buffer_model
Definition particles_container.hpp:235
ParticlesContainer(std::size_t n_particle, bool virtual_position=false)
Definition particles_container.hpp:453
KOKKOS_INLINE_FUNCTION double get_weight(std::size_t idx) const
Definition particles_container.hpp:640
KOKKOS_INLINE_FUNCTION void get_contributions(std::size_t idx, const ContributionView &contributions) const
Definition particles_container.hpp:251
std::size_t capacity() const
Definition particles_container.hpp:304
ParticlePositions buffer_position
Definition particles_container.hpp:236
MC::ParticlePositions position
Definition particles_container.hpp:158
void save(Archive &ar) const
Definition particles_container.hpp:324
std::size_t inactive_counter
Definition particles_container.hpp:241
static constexpr double default_allocation_factor
Definition particles_container.hpp:234
static constexpr double buffer_ratio
Definition particles_container.hpp:145
ParticleAges ages
Definition particles_container.hpp:161
Model UsedModel
Alias for the model used by the container.
Definition particles_container.hpp:151
void clean_dead(std::size_t to_remove)
Definition particles_container.hpp:515
ParticlesContainer & operator=(const ParticlesContainer &)=default
ParticlesContainer(const ParticlesContainer &)=default
Default copy and move constructors and assignment operators.
void load(Archive &ar)
Definition particles_container.hpp:311
ParticlesContainer(ParticlesContainer &&)=default
std::size_t n_allocated_elements
Definition particles_container.hpp:239
int begin
Definition particles_container.hpp:246
MC::ParticleStatus status
Definition particles_container.hpp:159
bool flag_virtual_position
Definition particles_container.hpp:244
Concept to check if a model type has uniform_weight
Definition traits.hpp:192
Namespace that contains classes and structures related to Monte Carlo (MC) simulations.
Definition alias.hpp:9
Kokkos::View< double *, ComputeSpace > ParticleWeigths
Definition alias.hpp:33
Kokkos::View< Status *, ComputeSpace > ParticleStatus
Definition alias.hpp:32
Kokkos::View< uint64_t *, ComputeSpace > ParticlePositions
Definition alias.hpp:31
Kokkos::Experimental:: ScatterView< double **, Kokkos::LayoutRight > ContributionView
Definition alias.hpp:47
@ Idle
Definition alias.hpp:12
Kokkos::View< double *[2], Kokkos::LayoutLeft, ComputeSpace > ParticleAges
Definition alias.hpp:34