Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvaratoms_gpu.h
Go to the documentation of this file.
1#ifndef COLVARATOMS_GPU_H
2#define COLVARATOMS_GPU_H
3
4#include "colvarmodule.h"
5#include "colvartypes.h"
6#include "colvar_rotation_derivative.h"
7
13namespace colvars_gpu {
14
15#if defined (COLVARS_CUDA) || defined (COLVARS_HIP)
55 unsigned int* d_com_cog_tbcount;
66};
67
73 double3* d_atom_grad;
75 double* d_sum_dxdq;
79 unsigned int* d_tbcount;
80};
81
86public:
87 enum class event_type {
88 read_and_calculate = 0,
89 calc_fit_gradients,
90 apply_force,
91 num_event_types,
92 };
98 colvaratoms_gpu(colvarmodule *cvmodule_in);
114 int init_gpu();
120 int destroy_gpu();
124 int reset_gpu_graphs();
128 int read_data_gpu(cvm::atom_group* cpu_atoms);
145 int sync_to_gpu_buffers(const cvm::atom_group* cpu_atoms);
155 int clear_gpu_buffers(const cvm::atom_group* cpu_atoms);
169 const cvm::atom_group* cpu_atoms, cudaGraph_t& graph,
170 std::unordered_map<std::string, cudaGraphNode_t>& nodes_map);
183 const cvm::atom_group* cpu_atoms, cudaGraph_t& graph,
184 std::unordered_map<std::string, cudaGraphNode_t>& nodes_map);
199 const cvm::atom_group* cpu_atoms, cudaGraph_t& graph,
200 std::unordered_map<std::string, cudaGraphNode_t>& nodes_map,
201 const std::vector<cudaGraphNode_t>& extra_initial_dependencies = {});
214 cvm::atom_group* cpu_atoms, cudaGraph_t& graph,
215 std::unordered_map<std::string, cudaGraphNode_t>& nodes_map);
223 int after_read_data_sync(cvm::atom_group* cpu_atoms, bool copy_to_cpu);
239 const cvm::atom_group* cpu_atoms, cudaGraph_t& graph,
240 std::unordered_map<std::string, cudaGraphNode_t>& nodes_map,
241 const std::vector<cudaGraphNode_t>& extra_initial_dependencies = {});
252 cvm::atom_group* cpu_atoms, cudaGraph_t& graph,
253 std::unordered_map<std::string, cudaGraphNode_t>& nodes_map,
254 bool use_cpu_buffers = false);
271 cvm::atom_group* cpu_atoms, bool change_fitting_group, size_t change_atom_i,
272 int xyz, bool to_cpu, double sign, cudaStream_t stream);
282 cvm::atom_group* cpu_atoms, bool to_cpu, cudaStream_t stream);
295 cvm::atom_group* cpu_atoms, int id);
302 int setup_rotation(const cvm::atom_group* cpu_atoms);
309 int setup_rotation_derivative(const cvm::atom_group* cpu_atoms);
315 colvaratoms_gpu_buffer_t& get_gpu_buffers() { return gpu_buffers; }
322 int read_total_forces(cvm::atom_group* cpu_atoms);
332 void apply_colvar_force_from_cpu(cvm::real const& cpu_force);
342 void set_use_cpu_group_force(bool yesno) { use_group_force = yesno; }
347 const colvars_gpu::rotation_gpu& get_rot_gpu() const { return rot_gpu; }
356 const cudaEvent_t& get_event(event_type type) const {
357 return events[static_cast<int>(type)];
358 }
368 static int set_weighted_gradient_gpu(
369 cvm::atom_group* cpu_atoms,
370 const cvm::rvector* d_com_grad,
371 cudaStream_t stream);
372private:
373 colvaratoms_gpu_buffer_t gpu_buffers;
401 std::array<cudaEvent_t, static_cast<int>(event_type::num_event_types)> events = {};
402};
403
404#endif // defined (COLVARS_CUDA) || defined (COLVARS_HIP)
405}
406
407#endif // COLVARATOMS_GPU_H
2-dimensional array of real numbers with three components along each dimension (works with colvarmodu...
Definition: colvartypes.h:903
vector of real numbers with three components
Definition: colvartypes.h:728
Collective variables module (main class)
Definition: colvarmodule.h:71
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:98
The main class for calculating the atom group properties on GPU.
Definition: colvaratoms_gpu.h:85
int begin_apply_force_gpu()
Clear the CPU force buffer for scalar components before applying forces on GPU.
Definition: colvaratoms_gpu.cpp:791
int after_read_data_sync(cvm::atom_group *cpu_atoms, bool copy_to_cpu)
Update the CPU COM, COG and rotation object after GPU synchronization.
Definition: colvaratoms_gpu.cpp:637
int clear_gpu_buffers(const cvm::atom_group *cpu_atoms)
Clear GPU atom-wise data fields.
Definition: colvaratoms_gpu.cpp:266
int setup_rotation(const cvm::atom_group *cpu_atoms)
Setup the rotation object and copy the reference positions to GPU.
Definition: colvaratoms_gpu.cpp:1056
int calc_fit_gradients_gpu(cvm::atom_group *cpu_atoms)
Calculate the fit gradients on GPU.
Definition: colvaratoms_gpu.cpp:197
int sync_to_gpu_buffers(const cvm::atom_group *cpu_atoms)
Synchronize atom-wise data fields from the CPU buffers.
Definition: colvaratoms_gpu.cpp:240
gpu_graph_t graph_apply_force
CUDA graph for applying forces.
Definition: colvaratoms_gpu.h:399
int add_force_to_proxy_gpu(cvm::atom_group *cpu_atoms)
Add forces to proxy after being communicated from colvarmodule.
Definition: colvaratoms_gpu.cpp:1108
int calc_required_properties_gpu_debug(cvm::atom_group *cpu_atoms, bool to_cpu, cudaStream_t stream)
Function for calculating the required properties used for debug gradients.
Definition: colvaratoms_gpu.cpp:1010
int destroy_gpu()
Destructor.
Definition: colvaratoms_gpu.cpp:78
gpu_graph_t graph_read_compute
CUDA graph for reading atoms and compute required properties.
Definition: colvaratoms_gpu.h:395
bool use_apply_colvar_force
If the CPU code path use apply_colvar_force(), this will be set to true, and then reset to false in b...
Definition: colvaratoms_gpu.h:384
colvarmodule * cvmodule
Pointer to the parent colvarmodule.
Definition: colvaratoms_gpu.h:393
colvaratoms_gpu_calc_fit_info_t calc_fit_gradients_gpu_info
Temporary variables for calc_fit_gradients GPU kernel.
Definition: colvaratoms_gpu.h:375
int reset_gpu_graphs()
Reset the CUDA graphs. This is used when proxy_gpu_buffers_reallocated() is called.
Definition: colvaratoms_gpu.cpp:128
void do_feature_side_effects_gpu(cvm::atom_group *cpu_atoms, int id)
Function to be called when a colvardeps feature is enabled.
Definition: colvaratoms_gpu.cpp:1027
int add_apply_force_nodes(const cvm::atom_group *cpu_atoms, cudaGraph_t &graph, std::unordered_map< std::string, cudaGraphNode_t > &nodes_map, const std::vector< cudaGraphNode_t > &extra_initial_dependencies={})
Add nodes for applying forces to the CUDA graph.
Definition: colvaratoms_gpu.cpp:799
int add_update_cpu_buffers_nodes(cvm::atom_group *cpu_atoms, cudaGraph_t &graph, std::unordered_map< std::string, cudaGraphNode_t > &nodes_map)
Add nodes for updating the CPU buffers after the GPU calculation.
Definition: colvaratoms_gpu.cpp:577
static int set_weighted_gradient_gpu(cvm::atom_group *cpu_atoms, const cvm::rvector *d_com_grad, cudaStream_t stream)
Shorthand: save the specified gradient on each atom to the GPU buffer, weighting with the atom mass (...
Definition: colvaratoms_gpu.cpp:1133
int add_read_positions_nodes(const cvm::atom_group *cpu_atoms, cudaGraph_t &graph, std::unordered_map< std::string, cudaGraphNode_t > &nodes_map)
Add a node for reading positions and velocities to the CUDA graph.
Definition: colvaratoms_gpu.cpp:310
colvars_gpu::rotation_derivative_gpu * rot_deriv_gpu
GPU Rotation derivative;.
Definition: colvaratoms_gpu.h:391
gpu_graph_t graph_debug
Separate CUDA graphs for supporting debug gradients.
Definition: colvaratoms_gpu.h:379
colvars_gpu::rotation_gpu & get_rot_gpu()
Getter of the internal GPU rotation object.
Definition: colvaratoms_gpu.h:346
bool use_group_force
If the CPU code path use group_force_object, this will be set to true, and then reset to false in beg...
Definition: colvaratoms_gpu.h:387
cvm::real * h_sum_applied_colvar_force
For intercepting the forces applied from the CPU interface.
Definition: colvaratoms_gpu.h:381
int add_calc_fit_gradients_nodes(cvm::atom_group *cpu_atoms, cudaGraph_t &graph, std::unordered_map< std::string, cudaGraphNode_t > &nodes_map, bool use_cpu_buffers=false)
Add nodes for calculating the fit gradients to the CUDA graph.
Definition: colvaratoms_gpu.cpp:699
colvaratoms_gpu_buffer_t & get_gpu_buffers()
Get the GPU buffers.
Definition: colvaratoms_gpu.h:315
int add_reset_atoms_data_nodes(const cvm::atom_group *cpu_atoms, cudaGraph_t &graph, std::unordered_map< std::string, cudaGraphNode_t > &nodes_map)
Add a node for resetting some atom-wise data fields to the CUDA graph.
Definition: colvaratoms_gpu.cpp:286
void apply_colvar_force_from_cpu(cvm::real const &cpu_force)
Function to intercept the forces applied from the CPU interface.
Definition: colvaratoms_gpu.cpp:1103
int init_gpu()
Initialize the object.
Definition: colvaratoms_gpu.cpp:30
void set_use_cpu_group_force(bool yesno)
Set whether to use the CPU atom group force.
Definition: colvaratoms_gpu.h:342
const cudaEvent_t & get_event(event_type type) const
Getter of a specific CUDA event.
Definition: colvaratoms_gpu.h:356
colvars_gpu::rotation_gpu rot_gpu
GPU rotation object.
Definition: colvaratoms_gpu.h:389
int read_total_forces(cvm::atom_group *cpu_atoms)
Read the total forces from the proxy buffer to the GPU buffer.
Definition: colvaratoms_gpu.cpp:1083
~colvaratoms_gpu()
Destructor.
Definition: colvaratoms_gpu.cpp:26
colvars_gpu::rotation_derivative_gpu * get_rot_deriv_gpu()
Getter of the internal GPU rotation derivative object.
Definition: colvaratoms_gpu.h:351
int add_calc_required_properties_nodes(const cvm::atom_group *cpu_atoms, cudaGraph_t &graph, std::unordered_map< std::string, cudaGraphNode_t > &nodes_map, const std::vector< cudaGraphNode_t > &extra_initial_dependencies={})
Add nodes for calculating the required properties to the CUDA graph.
Definition: colvaratoms_gpu.cpp:343
gpu_graph_t graph_calc_fit_gradients
CUDA graph for calculating fit gradients.
Definition: colvaratoms_gpu.h:397
int read_positions_gpu_debug(cvm::atom_group *cpu_atoms, bool change_fitting_group, size_t change_atom_i, int xyz, bool to_cpu, double sign, cudaStream_t stream)
Function for reading atom positions used for debug gradients.
Definition: colvaratoms_gpu.cpp:965
colvaratoms_gpu_calc_fit_info_t calc_fit_forces_gpu_info
Temporary variables for calc_fit_forces (or "calc_fit_gradients" for vector CVCs) GPU kernel.
Definition: colvaratoms_gpu.h:377
std::array< cudaEvent_t, static_cast< int >(event_type::num_event_types)> events
CUDA events.
Definition: colvaratoms_gpu.h:401
int read_data_gpu(cvm::atom_group *cpu_atoms)
Read the data of atom group on GPU and calculate the required properties.
Definition: colvaratoms_gpu.cpp:137
int setup_rotation_derivative(const cvm::atom_group *cpu_atoms)
Setup the rotation derivative object on GPU.
Definition: colvaratoms_gpu.cpp:1066
A struct for holding a CUDA graph and its execution object.
Definition: colvar_gpu_support.h:419
Definition: colvartypes.h:1580
Store the information of a group of atoms in a structure-of-arrays (SoA) style.
Definition: colvaratoms.h:52
Collective variables main module.
A struct for holding GPU atom group buffers.
Definition: colvaratoms_gpu.h:19
cvm::rvector * d_cog
GPU center-of-geometry.
Definition: colvaratoms_gpu.h:49
cvm::rvector * d_com
GPU center-of-mass.
Definition: colvaratoms_gpu.h:45
cvm::real * d_atoms_pos_unrotated
GPU atom positions (size: 3 * num_atoms)
Definition: colvaratoms_gpu.h:43
cvm::rvector * d_com_tmp
GPU temporary buffer for COM, used for avoiding memset.
Definition: colvaratoms_gpu.h:47
cvm::real * d_atoms_charge
GPU atom charges (size: num_atoms)
Definition: colvaratoms_gpu.h:25
cvm::rvector * d_cog_orig
GPU center of geometry before any fitting.
Definition: colvaratoms_gpu.h:53
cvm::rvector * h_com
Center-of-mass on the host-pinned memory for CPU compatibility.
Definition: colvaratoms_gpu.h:57
cvm::real * d_ref_pos
GPU reference coordinates for f_ag_center or f_ag_rotate.
Definition: colvaratoms_gpu.h:41
cvm::rvector * h_cog
Center-of-geometry on the host-pinned memory for CPU compatibility.
Definition: colvaratoms_gpu.h:59
cvm::real * d_fit_gradients
GPU fit gradients.
Definition: colvaratoms_gpu.h:39
cvm::rvector * h_dummy_atom_pos
Dummy atom position.
Definition: colvaratoms_gpu.h:65
cvm::rvector * h_cog_orig
Center-of-geometry before any fitting on the host-pinned memory for CPU compatibility.
Definition: colvaratoms_gpu.h:61
cvm::real * d_atoms_total_force
GPU atom total forces (size: 3 * num_atoms)
Definition: colvaratoms_gpu.h:33
cvm::rvector * d_ref_pos_cog
GPU center of geometry of the reference coordinates.
Definition: colvaratoms_gpu.h:63
cvm::real * d_atoms_pos
GPU atom positions (size: 3 * num_atoms)
Definition: colvaratoms_gpu.h:23
cvm::real * d_atoms_weight
Atom masses divided by total mass (size: num_atoms)
Definition: colvaratoms_gpu.h:35
cvm::real * d_atoms_applied_force
GPU atom applied force.
Definition: colvaratoms_gpu.h:37
unsigned int * d_com_cog_tbcount
GPU atomic counter for block reduction.
Definition: colvaratoms_gpu.h:55
cvm::real * d_atoms_vel
GPU atom velocities (size: 3 * num_atoms)
Definition: colvaratoms_gpu.h:27
cvm::real * d_atoms_grad
GPU atom gradients (size: 3 * num_atoms)
Definition: colvaratoms_gpu.h:31
cvm::real * d_atoms_mass
GPU atom mass (size: num_atoms)
Definition: colvaratoms_gpu.h:29
int * d_atoms_index
GPU atom proxy indices (size: num_atoms)
Definition: colvaratoms_gpu.h:21
cvm::rvector * d_cog_tmp
GPU temporary buffer for COG, used for avoiding memset.
Definition: colvaratoms_gpu.h:51
A struct for temporary variables for calculating the fit gradients.
Definition: colvaratoms_gpu.h:71
double3 * d_atom_grad
Fit gradients due to centering.
Definition: colvaratoms_gpu.h:73
unsigned int * d_tbcount
GPU atomic counter for block reduction.
Definition: colvaratoms_gpu.h:79
cvm::rmatrix * d_dxdC
Gradients of the CV with respect to the correlation matrix.
Definition: colvaratoms_gpu.h:77
double * d_sum_dxdq
Gradients of the CV with respect to the quaternion.
Definition: colvaratoms_gpu.h:75
Definition: colvar_rotation_derivative.h:622