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#include "kokkos_helpers.hpp"
27
28#include <Kokkos_Core.hpp>
29
30#include <cstddef>
31#include <numeric>
32#include <vector>
33
34/**
35 * @brief Run a kernel on all local particles with enumeration.
36 * The kernel is called with (index, particle) and is assumed to be thread-safe.
37 *
38 * @tparam Kernel Callable with signature <tt>void(std::size_t, Particle&)</tt>
39 * @param cs The cell structure containing the particles
40 * @param kernel The kernel to apply to each particle with its index
41 */
42template <typename Kernel>
44 Kernel &&kernel) {
46 auto const local_cells = cs.decomposition().local_cells();
47
48 std::vector<std::size_t> cell_offsets(local_cells.size(), std::size_t{0});
49 std::exclusive_scan(local_cells.begin(), local_cells.end(),
50 cell_offsets.begin(), std::size_t{0},
51 [](auto acc, auto const &cell) {
52 return acc + cell->particles().size();
53 });
54
56 "enumerate_local_particles", std::size_t{0}, local_cells.size(),
57 [&](auto cell_idx) {
58 auto const base_offset = cell_offsets[cell_idx];
59 auto &cell_particles = local_cells[cell_idx]->particles();
60 auto const n_part = cell_particles.size();
61 for (std::size_t p_index{0}; p_index < n_part; ++p_index) {
63 kernel(global_index, *(cell_particles.begin() + p_index));
64 }
65 });
66 return;
67 }
68 // Sequential fallback
69 std::size_t index = 0;
70 for (auto &p : cs.local_particles()) {
71 kernel(index++, p);
72 }
73}
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
ParticleRange local_particles() const
virtual std::span< Cell *const > local_cells() const =0
Get pointer to local cells.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
ESPRESSO_ATTR_ALWAYS_INLINE void kokkos_parallel_range_for(auto const &name, auto start, auto end, auto const &kernel)
Wrapper for Kokkos::parallel_for that skips fork/join when the number of threads is 1.
void enumerate_local_particles(CellStructure const &cs, Kernel &&kernel)
Run a kernel on all local particles with enumeration.