ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
GpuParticleData.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014-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#ifdef ESPRESSO_CUDA
25
26#include "ParticleRange.hpp"
27#include "ResourceCleanup.hpp"
29#include "system/Leaf.hpp"
30
31#include <utils/Vector.hpp>
32
33#include <bitset>
34#include <cstddef>
35#include <memory>
36#include <span>
37
38/**
39 * @brief Particle data communication manager for the GPU.
40 *
41 * When data is synchronized between host and device memory, a subset
42 * of the @ref Particle struct is copied from each particle on the host
43 * to the corresponding @ref GpuParticle struct on the device via
44 * @ref GpuParticleData::update(). Once the transfer is complete,
45 * the particle AoS on the device is copied (or "split") to a SoA
46 * automatically.
47 *
48 * Note that once a particle member is requested, the corresponding device
49 * memory is allocated and populated at every time step, even when the GPU
50 * method that originally requested the data is disabled.
51 */
52class GpuParticleData : public System::Leaf<GpuParticleData>,
53 public std::enable_shared_from_this<GpuParticleData> {
54public:
55 /** @brief Particle properties that need to be communicated to the GPU. */
56 struct prop {
57 static constexpr std::size_t pos = 0;
58 static constexpr std::size_t force = 1;
59 static constexpr std::size_t torque = 2;
60 static constexpr std::size_t q = 3;
61 static constexpr std::size_t dip = 4;
62 static constexpr std::size_t dip_fld = 5;
63 using bitset = std::bitset<6>;
64 };
65
66 /** @brief Energies that are retrieved from the GPU. */
67 struct GpuEnergy {
69 };
70
71 /** @brief Subset of @ref Particle which is copied to the GPU. */
72 struct GpuParticle {
74#ifdef ESPRESSO_DIPOLES
76#endif
77#ifdef ESPRESSO_ELECTROSTATICS
78 float q;
79#endif
81 };
82
83private:
84 // forward declare
85 class Storage;
86 std::unique_ptr<Storage> m_data;
87 void deinitialize() noexcept;
89 ResourceCleanup::Attorney<&GpuParticleData::deinitialize>;
91
92 /** @brief Whether a device was found and data structures were allocated. */
93 bool m_communication_enabled = false;
94 /** @brief Whether to convert particle properties from AoS to SoA. */
95 bool m_split_particle_struct = false;
96 /** @brief Whether particle transfer to the GPU was requested. */
97 bool m_need_particles_update = false;
98 /** @brief Host and device containers. */
99
100 bool has_compatible_device_impl() const;
101 void gpu_init_particle_comm();
102 void enable_particle_transfer();
103 void copy_particles_to_device();
104 void copy_particles_to_device(ParticleRange const &particles, int this_node);
105 /** @brief Collect particles from all nodes to the head node. */
107 pinned_vector<GpuParticle> &particle_data_host,
108 int this_node);
109 void particles_scatter_forces(ParticleRange const &particles,
110 std::span<float> host_forces,
111 std::span<float> host_torques) const;
112#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
113 void particles_scatter_dip_fld(ParticleRange const &particles,
114 std::span<float> host_dip_fld) const;
115#endif
116
117public:
120
121 void update() {
122 if (m_need_particles_update and m_communication_enabled) {
123 copy_particles_to_device();
124 }
125 }
126 void initialize();
127 void enable_property(std::size_t property);
129 void copy_forces_to_host(ParticleRange const &particles, int this_node);
130#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
131 void copy_dip_fld_to_host(ParticleRange const &particles, int this_node);
132#endif
133 std::size_t n_particles() const;
134 bool has_compatible_device() const;
135
136 GpuEnergy copy_energy_to_host() const;
137 GpuEnergy *get_energy_device() const;
138 float *get_particle_positions_device() const;
139 float *get_particle_forces_device() const;
140#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
141 float *get_particle_dip_fld_device() const;
142#endif
143#ifdef ESPRESSO_ROTATION
144 float *get_particle_torques_device() const;
145#endif
146#ifdef ESPRESSO_DIPOLES
147 float *get_particle_dipoles_device() const;
148#endif
149#ifdef ESPRESSO_ELECTROSTATICS
150 float *get_particle_charges_device() const;
151#endif
152};
153
154#endif // ESPRESSO_CUDA
std::vector< T, CudaHostAllocator< T > > pinned_vector
Vector implementation and trait types for boost qvm interoperability.
Host and device containers for particle data.
Particle data communication manager for the GPU.
float * get_particle_dip_fld_device() const
float * get_particle_torques_device() const
float * get_particle_charges_device() const
void copy_forces_to_host(ParticleRange const &particles, int this_node)
GpuEnergy * get_energy_device() const
void copy_dip_fld_to_host(ParticleRange const &particles, int this_node)
float * get_particle_dipoles_device() const
float * get_particle_forces_device() const
GpuEnergy copy_energy_to_host() const
void enable_property(std::size_t property)
std::size_t n_particles() const
bool has_compatible_device() const
float * get_particle_positions_device() const
A range of particles.
Attorney for a resource deallocator.
Queue to deallocate resources before normal program termination.
Abstract class that represents a component of the system.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
int this_node
The number of this node.
static auto gather_particle_data(BoxGeometry const &box_geo, ParticleRange const &particles)
STL namespace.
Energies that are retrieved from the GPU.
Subset of Particle which is copied to the GPU.
Particle properties that need to be communicated to the GPU.
static constexpr std::size_t force
static constexpr std::size_t dip_fld
std::bitset< 6 > bitset
static constexpr std::size_t torque
static constexpr std::size_t pos
static constexpr std::size_t dip
static constexpr std::size_t q