Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
colvars_gpu::colvaratoms_gpu Class Reference

The main class for calculating the atom group properties on GPU. More...

#include <colvaratoms_gpu.h>

Collaboration diagram for colvars_gpu::colvaratoms_gpu:
Collaboration graph
[legend]

Public Member Functions

 colvaratoms_gpu ()
 Constructor. More...
 
 ~colvaratoms_gpu ()
 Destructor.
 
int init_gpu ()
 Initialize the object. More...
 
int destroy_gpu ()
 Destructor. More...
 
int sync_to_gpu_buffers (const cvm::atom_group *cpu_atoms)
 Synchronize atom-wise data fields from the CPU buffers. More...
 
int clear_gpu_buffers (const cvm::atom_group *cpu_atoms)
 Clear GPU atom-wise data fields. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
int after_read_data_sync (cvm::atom_group *cpu_atoms, bool copy_to_cpu, cudaStream_t stream)
 Update the CPU COM, COG and rotation object after GPU synchronization. More...
 
int begin_apply_force_gpu ()
 Clear the CPU force buffer for scalar components before applying forces on GPU.
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
void do_feature_side_effects_gpu (cvm::atom_group *cpu_atoms, int id)
 Function to be called when a colvardeps feature is enabled. More...
 
int setup_rotation (const cvm::atom_group *cpu_atoms)
 Setup the rotation object and copy the reference positions to GPU. More...
 
int setup_rotation_derivative (const cvm::atom_group *cpu_atoms)
 Setup the rotation derivative object on GPU. More...
 
colvaratoms_gpu_buffer_tget_gpu_buffers ()
 Get the GPU buffers. More...
 
int read_total_forces (cvm::atom_group *cpu_atoms)
 Read the total forces from the proxy buffer to the GPU buffer. More...
 
void apply_colvar_force_from_cpu (cvm::real const &cpu_force)
 Function to intercept the forces applied from the CPU interface. More...
 
void set_use_cpu_group_force (bool yesno)
 Set whether to use the CPU atom group force. More...
 

Private Attributes

colvaratoms_gpu_buffer_t gpu_buffers
 
colvaratoms_gpu_calc_fit_info_t calc_fit_gradients_gpu_info
 Temporary variables for calc_fit_gradients GPU kernel.
 
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.
 
colvaratoms_gpu_debug_graph_t debug_graphs
 Separate CUDA graphs for supporting debug gradients.
 
cvm::realh_sum_applied_colvar_force
 For intercepting the forces applied from the CPU interface.
 
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 begin_apply_force_gpu()
 
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 begin_apply_force_gpu()
 
colvars_gpu::rotation_gpu rot_gpu
 GPU rotation object.
 
colvars_gpu::rotation_derivative_gpurot_deriv_gpu
 GPU Rotation derivative;.
 

Detailed Description

The main class for calculating the atom group properties on GPU.

Constructor & Destructor Documentation

◆ colvaratoms_gpu()

colvars_gpu::colvaratoms_gpu::colvaratoms_gpu ( )

Constructor.

All variables are expected to initialize to 0.

Member Function Documentation

◆ add_apply_force_nodes()

int colvars_gpu::colvaratoms_gpu::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.

Parameters
[in]cpu_atomsCPU atom group class
[in]graphCUDA graph object
[in,out]nodes_mapA map to store the added nodes with operation name
[in]extra_initial_dependenciesAdditional dependencies to be added to the initial nodes
Returns
COLVARS_OK if succeeded

◆ add_calc_fit_gradients_nodes()

int colvars_gpu::colvaratoms_gpu::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.

Parameters
[in]cpu_atomsCPU atom group class
[in]graphCUDA graph object
[in,out]nodes_mapA map to store the added nodes with operation name
[in]use_cpu_buffersIf true, add a node for copying fit gradients from GPU to CPU
Returns
COLVARS_OK if succeeded

◆ add_calc_required_properties_nodes()

int colvars_gpu::colvaratoms_gpu::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.

This function adds nodes for calculating COM, COG, rotation matrix, rotated positions, and reference COG to the CUDA graph.

Parameters
[in]cpu_atomsCPU atom group class
[in]graphCUDA graph object
[in,out]nodes_mapA map to store the added nodes with operation name
[in]extra_initial_dependenciesAdditional dependencies to be added to the initial nodes
Returns
COLVARS_OK if succeeded

◆ add_read_positions_nodes()

int colvars_gpu::colvaratoms_gpu::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.

This function adds cudaMemcpy nodes to read positions and velocities from the CPU buffers to the GPU buffers to the CUDA graph.

Parameters
[in]cpu_atomsCPU atom group class
[in]graphCUDA graph object
[in,out]nodes_mapA map to store the added nodes with operation name
Returns
COLVARS_OK if succeeded

◆ add_reset_atoms_data_nodes()

int colvars_gpu::colvaratoms_gpu::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.

This function adds cudaMemset nodes to clear gradients, total forces and applied forces to the CUDA graph. The positions and velocities are not cleared since they are expected to be overwritten.

Parameters
[in]cpu_atomsCPU atom group class
[in]graphCUDA graph object
[in,out]nodes_mapA map to store the added nodes with operation name
Returns
COLVARS_OK if succeeded

◆ add_update_cpu_buffers_nodes()

int colvars_gpu::colvaratoms_gpu::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.

This function adds cudaMemcpy nodes to update COM, COG, original COG, rotation matrix, and fit gradients to the CPU buffers.

Parameters
[in]cpu_atomsCPU atom group class
[in]graphCUDA graph object
[in,out]nodes_mapA map to store the added nodes with operation name
Returns
COLVARS_OK if succeeded

◆ after_read_data_sync()

int colvars_gpu::colvaratoms_gpu::after_read_data_sync ( cvm::atom_group cpu_atoms,
bool  copy_to_cpu,
cudaStream_t  stream 
)

Update the CPU COM, COG and rotation object after GPU synchronization.

Parameters
[in]cpu_atomsCPU atom group class
[in]copy_to_cpuIf true, copy the COM, COG and rotation object to CPU buffers
[in]streamCUDA stream to be synchronized
Returns
COLVARS_OK if succeeded

◆ apply_colvar_force_from_cpu()

void colvars_gpu::colvaratoms_gpu::apply_colvar_force_from_cpu ( cvm::real const &  cpu_force)

Function to intercept the forces applied from the CPU interface.

This function is called when apply_colvar_force() is called on CPU. It stores the applied force in a temporary variable, which will be added to the GPU buffer.

Parameters
[in]cpu_forceForce applied from the CPU interface

◆ calc_required_properties_gpu_debug()

int colvars_gpu::colvaratoms_gpu::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.

Parameters
[in]cpu_atomsCPU atom group class
[in]to_cpuIf true, copy the calculated properties to CPU buffers
[in]streamCUDA stream to be used
Returns
COLVARS_OK if succeeded

◆ clear_gpu_buffers()

int colvars_gpu::colvaratoms_gpu::clear_gpu_buffers ( const cvm::atom_group cpu_atoms)

Clear GPU atom-wise data fields.

This function clears the GPU buffers for proxy indices, charges, masses, weights, positions, gradients, total forces, velocities, and applied forces.

Parameters
[in]cpu_atomsCPU atom group class
Returns
COLVARS_OK if succeeded

◆ destroy_gpu()

int colvars_gpu::colvaratoms_gpu::destroy_gpu ( )

Destructor.

Returns
COLVARS_OK if succeeded

◆ do_feature_side_effects_gpu()

void colvars_gpu::colvaratoms_gpu::do_feature_side_effects_gpu ( cvm::atom_group cpu_atoms,
int  id 
)

Function to be called when a colvardeps feature is enabled.

This function performs any side effects needed when a feature is enabled. Specifically, it allocates the GPU buffers for unrotated positions if f_ag_fit_gradients is enabled, and sets up the rotation object if f_ag_rotate is enabled.

Parameters
[in]cpu_atomsCPU atom group class
[in]idFeature ID that has been enabled

◆ get_gpu_buffers()

colvaratoms_gpu_buffer_t & colvars_gpu::colvaratoms_gpu::get_gpu_buffers ( )
inline

Get the GPU buffers.

Returns
Reference to the GPU buffers struct

◆ init_gpu()

int colvars_gpu::colvaratoms_gpu::init_gpu ( )

Initialize the object.

This function re-allocates the GPU buffers for COM, COG, atomic counters, reference COG, and the temporary variables used for fit gradients. However, it does not (re)allocate the atom-wise data fields like positions. The atom-wise data fields are expected to be updated when sync_to_gpu_buffers is called.

Returns
COLVARS_OK if succeeded

◆ read_positions_gpu_debug()

int colvars_gpu::colvaratoms_gpu::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.

This function reads the atom positions from the proxy buffer to the atom group GPU buffer, and optionally modifies one of the coordinates for testing the numerical gradients.

Parameters
[in]cpu_atomsCPU atom group class
[in]change_fitting_groupIf true, change the fitting group atom position
[in]change_atom_iIndex of the atom to be changed
[in]xyzCoordinate index (0, 1, or 2) to be changed
[in]to_cpuIf true, copy the changed positions to CPU buffers
[in]signSign of the change (+1 or -1)
[in]streamCUDA stream to be used
Returns
COLVARS_OK if succeeded

◆ read_total_forces()

int colvars_gpu::colvaratoms_gpu::read_total_forces ( cvm::atom_group cpu_atoms)

Read the total forces from the proxy buffer to the GPU buffer.

Parameters
[in]cpu_atomsCPU atom group class
Returns
COLVARS_OK if succeeded

◆ set_use_cpu_group_force()

void colvars_gpu::colvaratoms_gpu::set_use_cpu_group_force ( bool  yesno)
inline

Set whether to use the CPU atom group force.

This function is called when group_force_object is used on CPU. It sets a flag to indicate that the group force should be added to the GPU buffer.

Parameters
[in]yesnoTrue to use the CPU group force, false otherwise

◆ setup_rotation()

int colvars_gpu::colvaratoms_gpu::setup_rotation ( const cvm::atom_group cpu_atoms)

Setup the rotation object and copy the reference positions to GPU.

Parameters
[in]cpu_atomsCPU atom group class
Returns
COLVARS_OK if succeeded

◆ setup_rotation_derivative()

int colvars_gpu::colvaratoms_gpu::setup_rotation_derivative ( const cvm::atom_group cpu_atoms)

Setup the rotation derivative object on GPU.

Parameters
[in]cpu_atomsCPU atom group class
Returns
COLVARS_OK if succeeded

◆ sync_to_gpu_buffers()

int colvars_gpu::colvaratoms_gpu::sync_to_gpu_buffers ( const cvm::atom_group cpu_atoms)

Synchronize atom-wise data fields from the CPU buffers.

This function (re)allocates the GPU buffers for proxy indices, charges, masses, weights, positions, velocities, total forces, gradients, and forces that will be applied. Then the proxy indices, charges, masses, positions, velocities, gradients and total forces will be updated from the corresponding CPU buffers.

Parameters
[in]cpu_atomsCPU atom group class
Returns
COLVARS_OK if succeeded

The documentation for this class was generated from the following files: