BioCMAMC-ST
post_process.hpp
1#ifndef __CORE_POST_PROCESS_PUBLIC_HPP__
2#define __CORE_POST_PROCESS_PUBLIC_HPP__
3#include "Kokkos_Core_fwd.hpp"
4#include "common/common.hpp"
5#include "mc/traits.hpp"
6#include <Kokkos_Core.hpp>
7#include <cstdint>
8#include <mc/particles_container.hpp>
9
10template <typename MemorySpace>
11using ParticlePropertyViewType = Kokkos::View<double**, Kokkos::LayoutRight, MemorySpace>;
12
13using SubViewtype =
14 Kokkos::Subview<ParticlePropertyViewType<ComputeSpace>, decltype(Kokkos::ALL), std::size_t>;
15namespace PostProcessing
16{
18 {
19 ParticlePropertyViewType<HostSpace> particle_values;
20 ParticlePropertyViewType<HostSpace> spatial_values;
21 std::vector<std::string> vnames;
22 };
23
24 template <HasExportPropertiesFull Model, typename ExecutionSpace>
25 void inner(std::size_t n_p,
26 const MC::ParticlePositions& position,
27 const typename Model::SelfParticle& model,
28 ParticlePropertyViewType<ComputeSpace>& particle_values,
29 ParticlePropertyViewType<ComputeSpace>& spatial_values)
30 {
31 Kokkos::Experimental::ScatterView<double**, Kokkos::LayoutRight, ComputeSpace>
32 scatter_spatial_values(spatial_values);
33
34 Kokkos::parallel_for(
35 "kernel_get_properties",
36 Kokkos::RangePolicy<ExecutionSpace>(0, n_p),
37 KOKKOS_LAMBDA(const int i_particle) {
38 auto access = scatter_spatial_values.access();
39
40 for (std::size_t i = 0; i < Model::n_var; ++i)
41 {
42 access(i, position(i_particle)) += model(i_particle, i);
43 particle_values(i, i_particle) = model(i_particle, i);
44 }
45 auto mass = Model::mass(i_particle, model);
46 access(Model::n_var, position(i_particle)) += mass;
47 particle_values(Model::n_var, i_particle) = mass;
48 });
49 Kokkos::fence();
50 Kokkos::Experimental::contribute(spatial_values, scatter_spatial_values);
51 }
52
53 template <HasExportPropertiesPartial Model, typename ExecutionSpace>
54 void inner_partial(std::size_t n_p,
55 const MC::ParticlePositions& position,
56 const typename Model::SelfParticle& model,
57 ParticlePropertyViewType<ComputeSpace>& particle_values,
58 ParticlePropertyViewType<ComputeSpace>& spatial_values)
59 {
60 Kokkos::Experimental::ScatterView<double**, Kokkos::LayoutRight, ComputeSpace>
61 scatter_spatial_values(spatial_values);
62 static const auto indices = Model::get_number();
63
64 static Kokkos::View<size_t*,HostSpace> host_index("host_index",indices.size());
65
66 for(auto i =0;i< indices.size();++i)
67 {
68 host_index(i)=indices[i];
69 }
70
71 const Kokkos::View<const size_t*,ComputeSpace> kindices = Kokkos::create_mirror_view_and_copy(ComputeSpace(), host_index);
72
73 Kokkos::parallel_for(
74 "kernel_get_properties",
75 Kokkos::RangePolicy<ExecutionSpace>(0, n_p),
76 KOKKOS_LAMBDA(const int i_particle) {
77 auto access = scatter_spatial_values.access();
78
79 for (int i = 0; i < kindices.extent(0); ++i)
80 {
81 auto index_export = kindices(i);
82 access(i, position(i_particle)) += model(i_particle, index_export);
83 particle_values(i, i_particle) = model(i_particle, index_export);
84 }
85 auto mass = Model::mass(i_particle, model);
86 access(kindices.extent(0), position(i_particle)) += mass;
87 particle_values(kindices.extent(0), i_particle) = mass;
88 });
89 Kokkos::fence();
90 Kokkos::Experimental::contribute(spatial_values, scatter_spatial_values);
91 }
92
93 template <ModelType M>
94 std::optional<PostProcessing::BonceBuffer>
95 get_properties(const MC::ParticlesContainer<M>& container, const std::size_t n_compartment)
96 {
97 if constexpr (HasExportProperties<M>)
98 {
99
100 BonceBuffer properties;
101 const std::size_t n_p =
102 container.n_particles(); // USE list size not Kokkos View size. ParticleList
103 // allocates more particles than needed
104 auto ar = M::names();
105 properties.vnames = std::vector<std::string>(ar.begin(), ar.end());
106 properties.vnames.emplace_back("mass");
107
108 size_t n_var = M::n_var + 1;
109 if constexpr (HasExportPropertiesPartial<M>)
110 {
111 n_var = ar.size() + 1;
112 }
113 ParticlePropertyViewType<ComputeSpace> spatial_values(
114 "property_spatial", n_var, n_compartment);
115 ParticlePropertyViewType<ComputeSpace> particle_values("property_values", n_var, n_p);
116
117 if constexpr (HasExportPropertiesPartial<M>)
118 {
119
121 n_p, container.position, container.model, particle_values, spatial_values);
122 }
123 else
124 {
125
127 n_p, container.position, container.model, particle_values, spatial_values);
128 }
129
130 properties.particle_values =
131 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), particle_values);
132 properties.spatial_values =
133 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), spatial_values);
134 return properties;
135 }
136 else
137 {
138 return std::nullopt;
139 }
140 }
141
142} // namespace PostProcessing
143#endif
Main owning object for Monte-Carlo particles.
Definition particles_container.hpp:128
Model::SelfParticle model
Definition particles_container.hpp:139
KOKKOS_INLINE_FUNCTION std::size_t n_particles() const
Gets the number of particles in the container.
Definition particles_container.hpp:161
MC::ParticlePositions position
Definition particles_container.hpp:140
Definition traits.hpp:95
Definition traits.hpp:101
Kokkos::View< uint64_t *, ComputeSpace > ParticlePositions
Definition alias.hpp:19
Definition impl_post_process.hpp:17
void inner_partial(std::size_t n_p, const MC::ParticlePositions &position, const typename Model::SelfParticle &model, ParticlePropertyViewType< ComputeSpace > &particle_values, ParticlePropertyViewType< ComputeSpace > &spatial_values)
Definition post_process.hpp:54
std::optional< PostProcessing::BonceBuffer > get_properties(const MC::ParticlesContainer< M > &container, const std::size_t n_compartment)
Definition post_process.hpp:95
void inner(std::size_t n_p, const MC::ParticlePositions &position, const typename Model::SelfParticle &model, ParticlePropertyViewType< ComputeSpace > &particle_values, ParticlePropertyViewType< ComputeSpace > &spatial_values)
Definition post_process.hpp:25
Definition post_process.hpp:18
ParticlePropertyViewType< HostSpace > spatial_values
Definition post_process.hpp:20
std::vector< std::string > vnames
Definition post_process.hpp:21
ParticlePropertyViewType< HostSpace > particle_values
Definition post_process.hpp:19