35#include <thrust/copy.h>
36#include <thrust/device_vector.h>
45#if defined(OMPI_MPI_H) || defined(_MPI_H)
46#error CU-file includes mpi.h! This should not happen!
50 return thrust::raw_pointer_cast(vec.data());
53template <
class SpanLike> std::size_t
byte_size(SpanLike
const &v) {
54 return v.size() *
sizeof(
typename SpanLike::value_type);
59 thrust::device_vector<T>().swap(vec);
73#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
77#ifdef ESPRESSO_ROTATION
82#ifdef ESPRESSO_DIPOLES
85#ifdef ESPRESSO_ELECTROSTATICS
100#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
110#ifdef ESPRESSO_ROTATION
123#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
129#ifdef ESPRESSO_ROTATION
142 m_data = std::make_unique<GpuParticleData::Storage>();
143 get_system().cleanup_queue.push<DeviceMemory>(shared_from_this());
146void GpuParticleData::deinitialize() noexcept { m_data.reset(); }
149 return m_data->particle_data_device.size();
153 return m_data->particle_pos_device;
159#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
165#ifdef ESPRESSO_ROTATION
171#ifdef ESPRESSO_DIPOLES
173 return m_data->particle_dip_device;
177#ifdef ESPRESSO_ELECTROSTATICS
179 return m_data->particle_q_device;
184 return m_data->energy_device;
188 m_need_particles_update =
true;
189 m_data->m_need[property] =
true;
190#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
193 m_split_particle_struct =
true;
197 m_split_particle_struct =
true;
200 enable_particle_transfer();
203bool GpuParticleData::has_compatible_device_impl()
const {
215void GpuParticleData::gpu_init_particle_comm() {
218 }
catch (cuda_runtime_error
const &err) {
219 throw cuda_fatal_error(err.what());
221 m_data->realloc_device_memory();
231#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
236#ifdef ESPRESSO_ROTATION
245#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
250#ifdef ESPRESSO_ROTATION
258 cudaMemcpyHostToDevice,
stream[0]);
261void GpuParticleData::copy_particles_to_device(
ParticleRange const &particles,
263 if (m_communication_enabled) {
264 gather_particle_data(particles, m_data->particle_data_host,
this_node);
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();
277 if (m_communication_enabled) {
280 m_data->copy_particle_forces_to_host();
281#ifdef ESPRESSO_ROTATION
282 m_data->copy_particle_torques_to_host();
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();
290 auto torques_buffer = std::span<float>();
294 particles_scatter_forces(particles, forces_buffer, torques_buffer);
297#ifdef ESPRESSO_DIPOLE_FIELD_TRACKING
300 if (m_communication_enabled) {
303 m_data->copy_particle_dip_fld_to_host();
306 auto dipole_field_buffer = m_data->get_particle_dip_fld_host_span();
309 particles_scatter_dip_fld(particles, dipole_field_buffer);
315 if (m_communication_enabled) {
316 if (m_data->energy_device ==
nullptr) {
325 if (m_communication_enabled) {
326 cuda_safe_mem(cudaMemcpy(&energy_host, m_data->energy_device,
327 sizeof(
GpuEnergy), cudaMemcpyDeviceToHost));
334 float *r, std::size_t n) {
335 auto idx = blockDim.x * blockIdx.x + threadIdx.x;
339 auto const &p = particles[idx];
341 r[idx + 0u] = p.p[0u];
342 r[idx + 1u] = p.p[1u];
343 r[idx + 2u] = p.p[2u];
346#ifdef ESPRESSO_ELECTROSTATICS
349 float *r,
float *q, std::size_t n) {
350 auto const idx = blockDim.x * blockIdx.x + threadIdx.x;
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];
363 float *q, std::size_t n) {
364 auto const idx = blockDim.x * blockIdx.x + threadIdx.x;
368 auto const &p = particles[idx];
373#ifdef ESPRESSO_DIPOLES
376 float *dip, std::size_t n) {
377 auto idx = blockDim.x * blockIdx.x + threadIdx.x;
381 auto const &p = particles[idx];
385 dip[idx + 0u] = p.dip[0u];
386 dip[idx + 1u] = p.dip[1u];
387 dip[idx + 2u] = p.dip[2u];
397 dim3
const threadsPerBlock{512u, 1u, 1u};
398 dim3
const numBlocks{
static_cast<unsigned>(n_part / threadsPerBlock.x + 1ul)};
400#ifdef ESPRESSO_ELECTROSTATICS
414#ifdef ESPRESSO_DIPOLES
433#ifdef ESPRESSO_DIPOLES
442#ifdef ESPRESSO_ELECTROSTATICS
454 auto const free_device_pointer = [](
auto *&ptr)
noexcept {
455 if (ptr !=
nullptr) {
456 cudaFree(
reinterpret_cast<void *
>(ptr));
461#ifdef ESPRESSO_DIPOLES
464#ifdef ESPRESSO_ELECTROSTATICS
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
float * particle_q_device
thrust::device_vector< float > particle_dip_fld_device
void split_particle_struct()
std::span< float > get_particle_forces_host_span()
pinned_vector< float > particle_torques_host
thrust::device_vector< GpuParticle > particle_data_device
void copy_particle_torques_to_host()
float * particle_pos_device
thrust::device_vector< float > particle_forces_device
void copy_particle_dip_fld_to_host()
GpuParticleData::prop::bitset m_need
Which particle properties are needed by GPU methods.
void copy_particle_forces_to_host()
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
float * particle_dip_device
pinned_vector< float > particle_dip_fld_host
std::span< float > get_particle_torques_host_span()
void copy_particles_to_device()
void realloc_device_memory()
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 clear_energy_on_device()
void enable_property(std::size_t property)
std::size_t n_particles() const
float * get_particle_positions_device() const
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,...
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
static constexpr std::size_t torque
static constexpr std::size_t pos
static constexpr std::size_t dip
static constexpr std::size_t q