IPPL API Reference
Independent Parallel Particle Layer C++ API
Loading...
Searching...
No Matches
ParticleAttrib.h
1//
2// Class ParticleAttrib
3// Templated class for all particle attribute classes.
4//
5// This templated class is used to represent a single particle attribute.
6// An attribute is one data element within a particle object, and is
7// stored as a Kokkos::View. This class stores the type information for the
8// attribute, and provides methods to create and destroy new items, and
9// to perform operations involving this attribute with others.
10//
11// ParticleAttrib is the primary element involved in expressions for
12// particles (just as LField is the primary element there). This file
13// defines the necessary templated classes and functions to make
14// ParticleAttrib a capable expression-template participant.
15//
16#ifndef IPPL_PARTICLE_ATTRIB_H
17#define IPPL_PARTICLE_ATTRIB_H
18
19#include <cstring>
20
22
23#include "Interpolation/CIC.h"
24#include "Particle/ParticleAttribBase.h"
25
26namespace ippl {
27
28 // ParticleAttrib class definition
29 template <typename T, class... Properties>
30 class ParticleAttrib : public detail::ParticleAttribBase<>::with_properties<Properties...>,
31 public detail::Expression<
32 ParticleAttrib<T, Properties...>,
33 sizeof(typename detail::ViewType<T, 1, Properties...>::view_type)> {
34 public:
35 typedef T value_type;
36 constexpr static unsigned dim = 1;
37
38 using Base = typename detail::ParticleAttribBase<>::with_properties<Properties...>;
39
40 using hash_type = typename Base::hash_type;
41
42 using view_type = typename detail::ViewType<T, 1, Properties...>::view_type;
43
44 using host_mirror_type = typename view_type::host_mirror_type;
45
46 using memory_space = typename view_type::memory_space;
47 using execution_space = typename view_type::execution_space;
48
49 using size_type = detail::size_type;
50
51 // Create storage for M particle attributes. The storage is uninitialized.
52 // New items are appended to the end of the array. When non_destructive is
53 // true, existing entries are preserved across a capacity grow.
54 void create(size_type, bool non_destructive = false) override;
55
56 // Allocate capacity for n particles (multiplied by the default overallocation
57 // factor) without touching the logical particle count. Existing data is
58 // discarded.
59 void alloc(size_type) override;
60
68 void destroy(const hash_type& deleteIndex, const hash_type& keepIndex,
69 size_type invalidCount) override;
70
71 void pack(const hash_type&) override;
72
73 void unpack(size_type) override;
74
75 void serialize(detail::Archive<memory_space>& ar, size_type nsends) override {
76 ar.serialize(buf_m, nsends);
77 }
78
79 void deserialize(detail::Archive<memory_space>& ar, size_type nrecvs) override {
80 ar.deserialize(buf_m, nrecvs);
81 }
82
83 virtual ~ParticleAttrib() = default;
84
85 size_type size() const override { return dview_m.extent(0); }
86
87 size_type packedSize(const size_type count) const override {
88 return count * sizeof(value_type);
89 }
90
91 void resize(size_type n) { Kokkos::resize(dview_m, n); }
92
103 void realloc(size_type n) { Kokkos::realloc(dview_m, n); }
104
105 void print() {
106 host_mirror_type hview = Kokkos::create_mirror_view(dview_m);
107 Kokkos::deep_copy(hview, dview_m);
108 for (size_type i = 0; i < *(this->localNum_mp); ++i) {
109 std::cout << hview(i) << std::endl;
110 }
111 }
112
113 KOKKOS_INLINE_FUNCTION T& operator()(const size_t i) const { return dview_m(i); }
114
115 view_type& getView() { return dview_m; }
116
117 const view_type& getView() const { return dview_m; }
118
119 host_mirror_type getHostMirror() const { return Kokkos::create_mirror(dview_m); }
120
121 void set_name(const std::string& name_) override {
122 size_t len = name_.size();
123 if (len >= detail::ATTRIB_NAME_MAX_LEN) {
124 len = detail::ATTRIB_NAME_MAX_LEN - 1;
125 }
126 std::memcpy(this->name_m, name_.c_str(), len);
127 this->name_m[len] = '\0';
128 }
129
130 std::string get_name() const override { return std::string(this->name_m); }
131
135 // KOKKOS_INLINE_FUNCTION
136 ParticleAttrib<T, Properties...>& operator=(T x);
137
145 template <typename E, size_t N>
146 // KOKKOS_INLINE_FUNCTION
147 ParticleAttrib<T, Properties...>& operator=(detail::Expression<E, N> const& expr);
148
176 template <typename Field, typename P2, typename policy_type>
177 void scatter(Field& f, const ParticleAttrib<Vector<P2, Field::dim>, Properties...>& pp,
178 policy_type iteration_policy, hash_type hash_array = {}) const;
179
198 template <typename Field, typename P2>
199 void gather(Field& f, const ParticleAttrib<Vector<P2, Field::dim>, Properties...>& pp,
200 const bool addToAttribute = false);
201
202 T sum();
203 T max();
204 T min();
205 T prod();
206
217 void applyPermutation(const hash_type& permutation) override;
218
227 void internalCopy(const hash_type& indices) override;
228
229 private:
230 view_type dview_m;
231 view_type buf_m;
232 };
233} // namespace ippl
234
235#include "Particle/ParticleAttrib.hpp"
236
237#endif
Definition Field.h:18
Definition ParticleAttrib.h:33
void destroy(const hash_type &deleteIndex, const hash_type &keepIndex, size_type invalidCount) override
Definition ParticleAttrib.hpp:46
void internalCopy(const hash_type &indices) override
Copy and create values of given indices.
Definition ParticleAttrib.hpp:262
void scatter(Field &f, const ParticleAttrib< Vector< P2, Field::dim >, Properties... > &pp, policy_type iteration_policy, hash_type hash_array={}) const
Scatter particle attribute data onto a field.
void gather(Field &f, const ParticleAttrib< Vector< P2, Field::dim >, Properties... > &pp, const bool addToAttribute=false)
Gather field data into the particle attribute.
Definition ParticleAttrib.hpp:188
void realloc(size_type n)
Reallocate the underlying view with a new size.
Definition ParticleAttrib.h:103
void applyPermutation(const hash_type &permutation) override
Sort the attribute according to a permutation.
Definition ParticleAttrib.hpp:243
ParticleAttrib< T, Properties... > & operator=(T x)
Definition ParticleAttrib.hpp:98
Definition Vector.h:23
Definition Archive.h:29
void serialize(const Kokkos::View< T *, ViewArgs... > &view, size_type nsends)
Definition Archive.hpp:25
void deserialize(Kokkos::View< T *, ViewArgs... > &view, size_type nrecvs)
Definition Archive.hpp:76
Definition ParticleAttribBase.h:30
Definition Archive.h:20
Definition IpplExpressions.h:26
Definition ViewTypes.h:44