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-2026 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#include <Kokkos_Core.hpp>
28
29#include <cstddef>
30#include <numeric>
31#include <vector>
32
33/**
34 * @brief Run a kernel on all local particles with enumeration.
35 * The kernel is called with (index, particle) and is assumed to be thread-safe.
36 *
37 * @tparam Kernel Callable with signature <tt>void(std::size_t, Particle&)</tt>
38 * @param cs The cell structure containing the particles
39 * @param kernel The kernel to apply to each particle with its index
40 */
41template <typename Kernel>
43 Kernel &&kernel) {
45 auto const local_cells = cs.decomposition().local_cells();
46
47 std::vector<std::size_t> cell_offsets(local_cells.size(), std::size_t{0});
48 std::exclusive_scan(local_cells.begin(), local_cells.end(),
49 cell_offsets.begin(), std::size_t{0},
50 [](auto acc, auto const &cell) {
51 return acc + cell->particles().size();
52 });
53
54 Kokkos::parallel_for(
55 "enumerate_local_particles", local_cells.size(), [&](auto cell_idx) {
56 auto const base_offset = cell_offsets[cell_idx];
57 auto &cell_particles = local_cells[cell_idx]->particles();
58 auto const n_part = cell_particles.size();
59 for (std::size_t p_index{0}; p_index < n_part; ++p_index) {
60 auto global_index = base_offset + p_index;
61 kernel(global_index, *(cell_particles.begin() + p_index));
62 }
63 });
64 return;
65 }
66 // Sequential fallback
67 std::size_t index = 0;
68 for (auto &p : cs.local_particles()) {
69 kernel(index++, p);
70 }
71}
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.