ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
GpuParticleData_cuda.cu
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 * @file
21 * CUDA kernels to convert the particles AoS to a SoA on the device.
22 */
23
24#include <config/config.hpp>
25
26#include "GpuParticleData.hpp"
27#include "System.hpp"
28
29#include "ParticleRange.hpp"
30#include "errorhandling.hpp"
31
32#include "cuda/init.hpp"
33#include "cuda/utils.cuh"
34
35#include <thrust/copy.h>
36#include <thrust/device_vector.h>
37
38#include <cuda.h>
39
40#include <cstddef>
41#include <cstdio>
42#include <memory>
43#include <span>
44
45#if defined(OMPI_MPI_H) || defined(_MPI_H)
46#error CU-file includes mpi.h! This should not happen!
47#endif
48
49template <class T> T *raw_data_pointer(thrust::device_vector<T> &vec) {
50 return thrust::raw_pointer_cast(vec.data());
51}
52
53template <class SpanLike> std::size_t byte_size(SpanLike const &v) {
54 return v.size() * sizeof(typename SpanLike::value_type);
55}
56
57template <typename T> void free_device_vector(thrust::device_vector<T> &vec) {
58 vec.clear();
59 thrust::device_vector<T>().swap(vec);
60}
61
62/** @brief Host and device containers for particle data. */
64public:
65 /** @brief Which particle properties are needed by GPU methods. */
68 std::size_t current_size = 0ul;
70 thrust::device_vector<GpuParticle> particle_data_device;
72 thrust::device_vector<float> particle_forces_device;
73#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
75 thrust::device_vector<float> particle_dip_fld_device;
76#endif
77#ifdef ESPRESSO_ROTATION
79 thrust::device_vector<float> particle_torques_device;
80#endif
81 float *particle_pos_device = nullptr;
82#ifdef ESPRESSO_DIPOLES
83 float *particle_dip_device = nullptr;
84#endif
85#ifdef ESPRESSO_ELECTROSTATICS
86 float *particle_q_device = nullptr;
87#endif
88
89 ~Storage();
94 if (not particle_forces_device.empty()) {
95 thrust::copy(particle_forces_device.begin(), particle_forces_device.end(),
96 particle_forces_host.begin());
97 }
98 }
99
100#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
102 if (not particle_dip_fld_device.empty()) {
103 thrust::copy(particle_dip_fld_device.begin(),
105 particle_dip_fld_host.begin());
106 }
107 }
108#endif
109
110#ifdef ESPRESSO_ROTATION
112 if (not particle_torques_device.empty()) {
113 thrust::copy(particle_torques_device.begin(),
115 particle_torques_host.begin());
116 }
117 }
118#endif
119 std::span<float> get_particle_forces_host_span() {
120 return {particle_forces_host.data(), particle_forces_host.size()};
121 }
122
123#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
125 return {particle_dip_fld_host.data(), particle_dip_fld_host.size()};
126 }
127#endif
128
129#ifdef ESPRESSO_ROTATION
131 return {particle_torques_host.data(), particle_torques_host.size()};
132 }
133#endif
134};
135
136// default ctor/dtor definitions must appear out-of-line due to the
137// forward-declaration of the Storage class
140
142 m_data = std::make_unique<GpuParticleData::Storage>();
143 get_system().cleanup_queue.push<DeviceMemory>(shared_from_this());
144}
145
146void GpuParticleData::deinitialize() noexcept { m_data.reset(); }
147
148std::size_t GpuParticleData::n_particles() const {
149 return m_data->particle_data_device.size();
150}
151
153 return m_data->particle_pos_device;
154}
155
157 return raw_data_pointer(m_data->particle_forces_device);
158}
159#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
161 return raw_data_pointer(m_data->particle_dip_fld_device);
162}
163#endif
164
165#ifdef ESPRESSO_ROTATION
167 return raw_data_pointer(m_data->particle_torques_device);
168}
169#endif
170
171#ifdef ESPRESSO_DIPOLES
173 return m_data->particle_dip_device;
174}
175#endif
176
177#ifdef ESPRESSO_ELECTROSTATICS
179 return m_data->particle_q_device;
180}
181#endif
182
184 return m_data->energy_device;
185}
186
187void GpuParticleData::enable_property(std::size_t property) {
188 m_need_particles_update = true;
189 m_data->m_need[property] = true;
190#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
191 if (property != prop::force and property != prop::torque and
192 property != prop::dip_fld) {
193 m_split_particle_struct = true;
194 }
195#else
196 if (property != prop::force and property != prop::torque) {
197 m_split_particle_struct = true;
198 }
199#endif
200 enable_particle_transfer();
201}
202
203bool GpuParticleData::has_compatible_device_impl() const {
204 auto result = false;
205 invoke_skip_cuda_exceptions([&result]() {
207 result = true;
208 });
209 return result;
210}
211
212/**
213 * @brief Setup and call particle reallocation from the host.
214 */
215void GpuParticleData::gpu_init_particle_comm() {
216 try {
218 } catch (cuda_runtime_error const &err) {
219 throw cuda_fatal_error(err.what());
220 }
221 m_data->realloc_device_memory();
222}
223
225 // resize buffers
226 auto const n_part = particle_data_host.size();
227 particle_data_device.resize(n_part);
228 particle_forces_host.resize(3ul * n_part);
229 particle_forces_device.resize(3ul * n_part);
230
231#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
232 particle_dip_fld_host.resize(3ul * n_part);
233 particle_dip_fld_device.resize(3ul * n_part);
234#endif
235
236#ifdef ESPRESSO_ROTATION
237 particle_torques_host.resize(3ul * n_part);
238 particle_torques_device.resize(3ul * n_part);
239#endif
240
241 // zero out device memory for forces and torques
242 cudaMemsetAsync(raw_data_pointer(particle_forces_device), 0x0,
244
245#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
246 cudaMemsetAsync(raw_data_pointer(particle_dip_fld_device), 0x0,
248#endif
249
250#ifdef ESPRESSO_ROTATION
251 cudaMemsetAsync(raw_data_pointer(particle_torques_device), 0x0,
253#endif
254
255 // copy particles to device
256 cudaMemcpyAsync(raw_data_pointer(particle_data_device),
258 cudaMemcpyHostToDevice, stream[0]);
259}
260
261void GpuParticleData::copy_particles_to_device(ParticleRange const &particles,
262 int this_node) {
263 if (m_communication_enabled) {
264 gather_particle_data(particles, m_data->particle_data_host, this_node);
265 if (this_node == 0) {
266 m_data->copy_particles_to_device();
267 if (m_split_particle_struct) {
268 m_data->realloc_device_memory();
269 m_data->split_particle_struct();
270 }
271 }
272 }
273}
274
276 int this_node) {
277 if (m_communication_enabled) {
278 // copy results from device memory to host memory
279 if (this_node == 0) {
280 m_data->copy_particle_forces_to_host();
281#ifdef ESPRESSO_ROTATION
282 m_data->copy_particle_torques_to_host();
283#endif
284 }
285
286 auto forces_buffer = m_data->get_particle_forces_host_span();
287#ifdef ESPRESSO_ROTATION
288 auto torques_buffer = m_data->get_particle_torques_host_span();
289#else
290 auto torques_buffer = std::span<float>();
291#endif
292
293 // add forces and torques to the particles
294 particles_scatter_forces(particles, forces_buffer, torques_buffer);
295 }
296}
297#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
299 int this_node) {
300 if (m_communication_enabled) {
301 // copy results from device memory to host memory
302 if (this_node == 0) {
303 m_data->copy_particle_dip_fld_to_host();
304 }
305
306 auto dipole_field_buffer = m_data->get_particle_dip_fld_host_span();
307
308 // add dip_fld to the particles
309 particles_scatter_dip_fld(particles, dipole_field_buffer);
310 }
311}
312#endif
313
315 if (m_communication_enabled) {
316 if (m_data->energy_device == nullptr) {
317 cuda_safe_mem(cudaMalloc(&m_data->energy_device, sizeof(GpuEnergy)));
318 }
319 cuda_safe_mem(cudaMemset(m_data->energy_device, 0, sizeof(GpuEnergy)));
320 }
321}
322
324 GpuEnergy energy_host{};
325 if (m_communication_enabled) {
326 cuda_safe_mem(cudaMemcpy(&energy_host, m_data->energy_device,
327 sizeof(GpuEnergy), cudaMemcpyDeviceToHost));
328 }
329 return energy_host;
330}
331
332// Position only
334 float *r, std::size_t n) {
335 auto idx = blockDim.x * blockIdx.x + threadIdx.x;
336 if (idx >= n)
337 return;
338
339 auto const &p = particles[idx];
340 idx *= 3u;
341 r[idx + 0u] = p.p[0u];
342 r[idx + 1u] = p.p[1u];
343 r[idx + 2u] = p.p[2u];
344}
345
346#ifdef ESPRESSO_ELECTROSTATICS
347// Position and charge
349 float *r, float *q, std::size_t n) {
350 auto const idx = blockDim.x * blockIdx.x + threadIdx.x;
351 if (idx >= n)
352 return;
353
354 auto const &p = particles[idx];
355 r[3u * idx + 0u] = p.p[0u];
356 r[3u * idx + 1u] = p.p[1u];
357 r[3u * idx + 2u] = p.p[2u];
358 q[idx] = p.q;
359}
360
361// Charge only
363 float *q, std::size_t n) {
364 auto const idx = blockDim.x * blockIdx.x + threadIdx.x;
365 if (idx >= n)
366 return;
367
368 auto const &p = particles[idx];
369 q[idx] = p.q;
370}
371#endif
372
373#ifdef ESPRESSO_DIPOLES
374// Dipole moment
376 float *dip, std::size_t n) {
377 auto idx = blockDim.x * blockIdx.x + threadIdx.x;
378 if (idx >= n)
379 return;
380
381 auto const &p = particles[idx];
382
383 idx *= 3u;
384
385 dip[idx + 0u] = p.dip[0u];
386 dip[idx + 1u] = p.dip[1u];
387 dip[idx + 2u] = p.dip[2u];
388}
389#endif
390
392 auto const n_part = particle_data_device.size();
393 if (n_part == 0ul)
394 return;
395
397 dim3 const threadsPerBlock{512u, 1u, 1u};
398 dim3 const numBlocks{static_cast<unsigned>(n_part / threadsPerBlock.x + 1ul)};
399
400#ifdef ESPRESSO_ELECTROSTATICS
401 if (m_need[prop::q] and m_need[prop::pos]) {
404 particle_q_device, n_part);
405 } else if (m_need[prop::q]) {
408 } else
409#endif
410 if (m_need[prop::pos]) {
413 }
414#ifdef ESPRESSO_DIPOLES
415 if (m_need[prop::dip]) {
418 }
419#endif
420}
421
424 auto const new_size = particle_data_device.size();
425 auto const resize_needed = new_size != current_size;
426 if (m_need[prop::pos] and (resize_needed or particle_pos_device == nullptr)) {
427 if (particle_pos_device != nullptr) {
429 }
431 cudaMalloc(&particle_pos_device, 3ul * new_size * sizeof(float)));
432 }
433#ifdef ESPRESSO_DIPOLES
434 if (m_need[prop::dip] and (resize_needed or particle_dip_device == nullptr)) {
435 if (particle_dip_device != nullptr) {
437 }
439 cudaMalloc(&particle_dip_device, 3ul * new_size * sizeof(float)));
440 }
441#endif
442#ifdef ESPRESSO_ELECTROSTATICS
443 if (m_need[prop::q] and (resize_needed or particle_q_device == nullptr)) {
444 if (particle_q_device != nullptr) {
446 }
447 cuda_safe_mem(cudaMalloc(&particle_q_device, new_size * sizeof(float)));
448 }
449#endif
450 current_size = new_size;
451}
452
454 auto const free_device_pointer = [](auto *&ptr) noexcept {
455 if (ptr != nullptr) {
456 cudaFree(reinterpret_cast<void *>(ptr));
457 ptr = nullptr;
458 }
459 };
460 free_device_pointer(particle_pos_device);
461#ifdef ESPRESSO_DIPOLES
462 free_device_pointer(particle_dip_device);
463#endif
464#ifdef ESPRESSO_ELECTROSTATICS
465 free_device_pointer(particle_q_device);
466#endif
467 free_device_pointer(energy_device);
468}
std::vector< T, CudaHostAllocator< T > > pinned_vector
void free_device_vector(thrust::device_vector< T > &vec)
T * raw_data_pointer(thrust::device_vector< T > &vec)
std::size_t byte_size(SpanLike const &v)
__global__ void split_kernel_rq(GpuParticleData::GpuParticle *particles, float *r, float *q, std::size_t n)
__global__ void split_kernel_dip(GpuParticleData::GpuParticle *particles, float *dip, std::size_t n)
__global__ void split_kernel_r(GpuParticleData::GpuParticle *particles, float *r, std::size_t n)
__global__ void split_kernel_q(GpuParticleData::GpuParticle *particles, float *q, std::size_t n)
Host and device containers for particle data.
thrust::device_vector< float > particle_torques_device
thrust::device_vector< float > particle_dip_fld_device
std::span< float > get_particle_forces_host_span()
pinned_vector< float > particle_torques_host
thrust::device_vector< GpuParticle > particle_data_device
thrust::device_vector< float > particle_forces_device
GpuParticleData::prop::bitset m_need
Which particle properties are needed by GPU methods.
pinned_vector< GpuParticle > particle_data_host
std::span< float > get_particle_dip_fld_host_span()
pinned_vector< float > particle_forces_host
GpuParticleData::GpuEnergy * energy_device
pinned_vector< float > particle_dip_fld_host
std::span< float > get_particle_torques_host_span()
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
float * get_particle_positions_device() const
A range of particles.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
int this_node
The number of this node.
void invoke_skip_cuda_exceptions(F &&f, Args &&...args)
Invoke a function and silently ignore any thrown cuda_runtime_error error.
void cuda_check_device()
Check that a device is available, that its compute capability is sufficient for ESPResSo,...
Definition init_cuda.cu:102
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
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
#define cuda_safe_mem(a)
Definition utils.cuh:73