ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
particle_enumeration.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <config/config.hpp>
23
24#include "Cell.hpp"
25#include "CellStructure.hpp"
26
27#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
28#include <Kokkos_Core.hpp>
29#endif
30
31#include <cstddef>
32#include <numeric>
33#include <vector>
34
35/**
36 * @brief Run a kernel on all local particles with enumeration.
37 * The kernel is called with (index, particle) and is assumed to be thread-safe.
38 *
39 * @tparam Kernel Callable with signature <tt>void(std::size_t, Particle&)</tt>
40 * @param cs The cell structure containing the particles
41 * @param kernel The kernel to apply to each particle with its index
42 */
43template <typename Kernel>
45 Kernel &&kernel) {
46#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
48 auto const local_cells = cs.decomposition().local_cells();
49
50 std::vector<std::size_t> cell_offsets(local_cells.size(), std::size_t{0});
51 std::exclusive_scan(local_cells.begin(), local_cells.end(),
52 cell_offsets.begin(), std::size_t{0},
53 [](auto acc, auto const &cell) {
54 return acc + cell->particles().size();
55 });
56
57 Kokkos::parallel_for(
58 "enumerate_local_particles", local_cells.size(), [&](auto cell_idx) {
59 auto const base_offset = cell_offsets[cell_idx];
60 auto &cell_particles = local_cells[cell_idx]->particles();
61 auto const n_part = cell_particles.size();
62 for (std::size_t p_index{0}; p_index < n_part; ++p_index) {
63 auto global_index = base_offset + p_index;
64 kernel(global_index, *(cell_particles.begin() + p_index));
65 }
66 });
67 return;
68 }
69#endif // ESPRESSO_SHARED_MEMORY_PARALLELISM
70 // Sequential fallback
71 std::size_t index = 0;
72 for (auto &p : cs.local_particles()) {
73 kernel(index++, p);
74 }
75}
Describes a cell structure / cell system.
ParticleDecomposition const & decomposition() const
Get the underlying particle decomposition.
bool use_parallel_for_each_local_particle() const
whether to use parallel version of for_each_local_particle
virtual std::span< Cell *const > local_cells() const =0
Get pointer to local cells.
void enumerate_local_particles(CellStructure const &cs, Kernel &&kernel)
Run a kernel on all local particles with enumeration.