54 template <
class PLayout,
typename... IP>
59 , nextID_m(Comm->rank())
60 , numNodes_m(Comm->size()) {
61 if constexpr (EnableIDs) {
66 R.set_name(
"position");
69 template <
class PLayout,
typename... IP>
75 template <
class PLayout,
typename... IP>
76 template <
typename MemorySpace>
78 attributes_m.template get<MemorySpace>().push_back(&pa);
79 pa.setParticleCount(localNum_m);
82 template <
class PLayout,
typename... IP>
90 template <
class PLayout,
typename... IP>
92 PAssert(layout_m !=
nullptr);
95 forAllAttributes([&]<
typename Attribute>(Attribute& attribute) {
96 attribute->create(nLocal, non_destructive);
99 if constexpr (EnableIDs) {
104 Kokkos::RangePolicy<size_type, typename particle_index_type::execution_space>;
105 auto pIDs = ID.getView();
106 auto nextID = this->nextID_m;
107 auto numNodes = this->numNodes_m;
108 auto offset = localNum_m;
109 Kokkos::parallel_for(
110 "ParticleBase<...>::create(size_t,bool)",
111 policy_type(localNum_m, localNum_m + nLocal),
112 KOKKOS_LAMBDA(
const std::int64_t i) {
113 pIDs(i) = nextID + numNodes * (i - offset);
115 nextID_m += numNodes_m * nLocal;
119 localNum_m += nLocal;
122 Comm->allreduce(localNum_m, totalNum_m, 1, std::plus<size_type>());
125 template <
class PLayout,
typename... IP>
127 PAssert(layout_m !=
nullptr);
128 forAllAttributes([&]<
typename Attribute>(Attribute& attribute) {
129 attribute->alloc(nLocal);
134 template <
class PLayout,
typename... IP>
136 PAssert(layout_m !=
nullptr);
138 size_type n = (
id > -1) ? 1 : 0;
141 index_type tmpNextID = nextID_m;
147 nextID_m = tmpNextID;
148 numNodes_m = Comm->size();
151 template <
class PLayout,
typename... IP>
153 PAssert(layout_m !=
nullptr);
156 size_type nLocal = nTotal / numNodes_m;
158 const size_t rank = Comm->rank();
160 size_type rest = nTotal - nLocal * rank;
168 template <
class PLayout,
typename... IP>
169 template <
typename... Properties>
171 const size_type destroyNum) {
172 this->internalDestroy(invalid, destroyNum);
174 Comm->allreduce(localNum_m, totalNum_m, 1, std::plus<size_type>());
177 template <
class PLayout,
typename... IP>
178 template <
typename... Properties>
180 const Kokkos::View<bool*, Properties...>& invalid,
const size_type destroyNum) {
181 PAssert(destroyNum <= localNum_m);
184 if (destroyNum == 0) {
191 if (destroyNum == localNum_m) {
196 using view_type = Kokkos::View<
bool*, Properties...>;
197 using memory_space =
typename view_type::memory_space;
198 using execution_space =
typename view_type::execution_space;
199 using policy_type = Kokkos::RangePolicy<execution_space>;
200 auto& locDeleteIndex = deleteIndex_m.get<memory_space>();
201 auto& locKeepIndex = keepIndex_m.get<memory_space>();
204 detail::runForAllSpaces([&]<
typename MemorySpace>() {
205 if (attributes_m.template get<memory_space>().size() > 0) {
206 int overalloc = Comm->getDefaultOverallocation();
207 auto& del = deleteIndex_m.get<memory_space>();
208 auto& keep = keepIndex_m.get<memory_space>();
209 if (del.size() < destroyNum) {
210 Kokkos::realloc(del, destroyNum * overalloc);
211 Kokkos::realloc(keep, destroyNum * overalloc);
217 Kokkos::deep_copy(locDeleteIndex, -1);
220 Kokkos::parallel_scan(
221 "Scan in ParticleBase::destroy()", policy_type(0, localNum_m - destroyNum),
222 KOKKOS_LAMBDA(
const size_t i,
int& idx,
const bool final) {
223 if (
final && invalid(i)) {
224 locDeleteIndex(idx) = i;
233 size_type maxDeleteIndex = 0;
234 Kokkos::parallel_reduce(
235 "Reduce in ParticleBase::destroy()", policy_type(0, destroyNum),
236 KOKKOS_LAMBDA(
const size_t i,
size_t& maxIdx) {
237 if (locDeleteIndex(i) >= 0 && i > maxIdx) {
241 Kokkos::Max<size_type>(maxDeleteIndex));
244 Kokkos::parallel_scan(
245 "Second scan in ParticleBase::destroy()",
246 Kokkos::RangePolicy<size_type, execution_space>(localNum_m - destroyNum, localNum_m),
247 KOKKOS_LAMBDA(
const size_t i,
int& idx,
const bool final) {
248 if (
final && !invalid(i)) {
249 locKeepIndex(idx) = i;
258 localNum_m -= destroyNum;
263 auto filter = [&]<
typename MemorySpace>() {
264 return attributes_m.template get<MemorySpace>().size() > 0;
266 deleteIndex_m.copyToOtherSpaces<memory_space>(filter);
267 keepIndex_m.copyToOtherSpaces<memory_space>(filter);
273 forAllAttributes([&]<
typename Attribute>(Attribute*& attribute) {
274 using att_memory_space =
typename Attribute::memory_space;
275 auto& del = deleteIndex_m.get<att_memory_space>();
276 auto& keep = keepIndex_m.get<att_memory_space>();
277 attribute->destroy(del, keep, maxDeleteIndex + 1);
281 template <
class PLayout,
typename... IP>
282 template <
typename HashType>
284 std::vector<MPI_Request>& requests,
285 const HashType& hash) {
286 size_type nSends = hash.size();
287 requests.resize(requests.size() + 1);
289 auto hashes = hash_container_type(hash, [&]<
typename MemorySpace>() {
290 return attributes_m.template get<MemorySpace>().size() > 0;
293 detail::runForAllSpaces([&]<
typename MemorySpace>() {
294 size_type bufSize = packedSize<MemorySpace>(nSends);
299 auto buf = Comm->getBuffer<MemorySpace>(bufSize);
301 Comm->isend(rank, tag++, *
this, *buf, requests.back(), nSends);
302 buf->resetWritePos();
306 template <
class PLayout,
typename... IP>
308 detail::runForAllSpaces([&]<
typename MemorySpace>() {
309 size_type bufSize = packedSize<MemorySpace>(nRecvs);
314 auto buf = Comm->getBuffer<MemorySpace>(bufSize);
316 Comm->recv(rank, tag++, *
this, *buf, bufSize, nRecvs);
322 template <
class PLayout,
typename... IP>
323 template <
typename Archive>
325 using memory_space =
typename Archive::buffer_type::memory_space;
326 forAllAttributes<memory_space>([&]<
typename Attribute>(Attribute& att) {
327 att->serialize(ar, nsends);
331 template <
class PLayout,
typename... IP>
332 template <
typename Archive>
334 using memory_space =
typename Archive::buffer_type::memory_space;
335 forAllAttributes<memory_space>([&]<
typename Attribute>(Attribute& att) {
336 att->deserialize(ar, nrecvs);
340 template <
class PLayout,
typename... IP>
341 template <
typename MemorySpace>
344 forAllAttributes<MemorySpace>([&]<
typename Attribute>(
const Attribute& att) {
345 total += att->packedSize(count);
350 template <
class PLayout,
typename... IP>
352 detail::runForAllSpaces([&]<
typename MemorySpace>() {
353 auto& att = attributes_m.template get<MemorySpace>();
354 for (
unsigned j = 0; j < att.size(); j++) {
355 att[j]->pack(hash.template get<MemorySpace>());
360 template <
class PLayout,
typename... IP>
362 detail::runForAllSpaces([&]<
typename MemorySpace>() {
363 auto& att = attributes_m.template get<MemorySpace>();
364 for (
unsigned j = 0; j < att.size(); j++) {
365 att[j]->unpack(nrecvs);
368 localNum_m += nrecvs;
Definition ParticleBase.h:87
void addAttribute(detail::ParticleAttribBase< MemorySpace > &pa)
Definition ParticleBase.hpp:77
void globalCreate(size_type nTotal)
Definition ParticleBase.hpp:152
void initialize(Layout_t &layout)
Definition ParticleBase.hpp:83
void create(size_type nLocal, bool non_destructive=false)
Definition ParticleBase.hpp:91
void destroy(const Kokkos::View< bool *, Properties... > &invalid, const size_type destroyNum)
Definition ParticleBase.hpp:170
void alloc(size_type nLocal)
Definition ParticleBase.hpp:126
void createWithID(index_type id)
Definition ParticleBase.hpp:135
ParticleBase()
Definition ParticleBase.hpp:55
particle_position_type R
view of particle positions
Definition ParticleBase.h:115
particle_index_type ID
view of particle IDs
Definition ParticleBase.h:118
Definition ParticleAttribBase.h:30