Skip to content

[RFC] Refactor atom groups read data - #953

Closed
HanatoK wants to merge 5 commits into
Colvars:masterfrom
HanatoK:refactor_atom_groups_read_data
Closed

HanatoK wants to merge 5 commits into
Colvars:masterfrom
HanatoK:refactor_atom_groups_read_data

Conversation

@HanatoK

@HanatoK HanatoK commented Aug 13, 2026

Copy link
Copy Markdown
Member

This PR implements the idea discussed in #938 (comment), and also refactors the update of lattice (boundary condition) so that boundaries_ is updated only once in the GPU code path. This PR also lays the foundation of reusing atom groups.

This PR depends on #940. Sadly I still haven't figured out how to make a stacked PR...

@HanatoK
HanatoK force-pushed the refactor_atom_groups_read_data branch from e19dc7c to ab69a1d Compare August 17, 2026 16:41
@HanatoK
HanatoK marked this pull request as ready for review August 18, 2026 19:41
@HanatoK HanatoK mentioned this pull request Aug 19, 2026
@HanatoK HanatoK self-assigned this Aug 20, 2026
Comment thread src/colvarmodule.cpp Resolved
@HanatoK
HanatoK force-pushed the refactor_atom_groups_read_data branch from ab69a1d to f65ae75 Compare September 1, 2026 21:43
@HanatoK
HanatoK marked this pull request as draft September 1, 2026 22:15
@HanatoK

HanatoK commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

I will look into the test failures in https://github.com/HanatoK/colvars/actions/runs/33562573020/job/100038292990 tomorrow.

@HanatoK
HanatoK marked this pull request as ready for review September 2, 2026 20:02
@HanatoK
HanatoK force-pushed the refactor_atom_groups_read_data branch from 0e80deb to 17ab353 Compare September 10, 2026 15:01
@HanatoK
HanatoK force-pushed the refactor_atom_groups_read_data branch from 17ab353 to f1fac18 Compare November 12, 2026 21:39
@HanatoK
HanatoK requested a review from a team November 12, 2026 21:39
@HanatoK

HanatoK commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

Close by #999

@HanatoK HanatoK closed this Nov 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved lifetime and evaluation-order defects can cause use-after-free crashes and produce stale forces and gradients.

7 open issues
What changed in this PR

Refactors Colvars’ atom-group preparation into module-level scheduling, laying groundwork for shared atom groups and a single GPU boundary update per evaluation. It builds on the stream-based design in #938 and depends on #940.

Changes:

  • Adds a central atom-group registry and moves data reads, fit-gradient calculation, and GPU force submission into the module.
  • Replaces per-component boundary caches with proxy-managed boundary state.
  • Separates gradient debugging from component-gradient calculation.
File Description
src/​colvars_system.h Exposes boundary fields and disables the unsupported-boundary check.
src/​colvarproxy_system.h Adds an explicit boundary-update hook and const-reference getter.
src/​colvarmodule.h Declares the atom-group registry, counters, and synchronization state.
src/​colvarmodule.cpp Centralizes group preparation, fit gradients, force handling, and boundary updates.
src/​colvarcomp.h Removes component-level preparation APIs and the boundary cache.
src/​colvarcomp.cpp Adds registry lifecycle calls and removes component preparation routines.
src/​colvarcomp_distances.cpp Reads proxy boundary state directly in distance calculations.
src/​colvarcomp_coordnums.cpp Uses proxy boundary state in CPU and GPU coordination calculations.
src/​colvarcomp_angles.cpp Reads proxy boundary state in angle and dihedral calculations.
src/​colvar.h Declares the separate gradient-debugging method.
src/​colvar.cpp Removes relocated preparation stages and separates gradient debugging.
namd/​cudaglobalmaster/​colvarproxy_cudaglobalmaster.C Implements explicit lattice synchronization and boundary updates.

Review effort: Balanced

Comment thread src/colvarcomp.cpp
Comment on lines +397 to +399
for (size_t i = 0; i < atom_groups.size(); i++) {
cvmodule->unregister_atom_group_from_cvc(atom_groups[i]);
}
Comment thread src/colvarmodule.cpp
Comment on lines +969 to +971
for (auto cvi = variables_active()->begin(); cvi != variables_active()->end(); cvi++) {
if (!(*cvi)->is_enabled(colvardeps::f_cv_total_force_current_step)) {
error_code |= (*cvi)->calc_cvc_total_force(0, (*cvi)->num_cvcs());
Comment thread src/colvarmodule.cpp
Comment on lines +1024 to +1025
// Calculate atom group fit gradients
if (use_gpu) {
Comment thread src/colvarmodule.cpp
error_code |= (*cvi)->debug_cvc_gradients();
}
if ((*cvi)->is_enabled(colvardeps::f_cv_total_force_current_step)) {
error_code |= (*cvi)->calc_cvc_total_force(0, (*cvi)->num_cvcs());
Comment thread src/colvarmodule.cpp
Comment on lines +1355 to +1356
for (auto ag = registered_atom_groups.begin(); ag != registered_atom_groups.end(); ++ag) {
error_code |= ag->first->get_gpu_atom_group()->add_force_to_proxy_gpu(ag->first);
Comment thread src/colvar.cpp
} else {
(cvcs[i])->read_data();
}
if (use_gpu && cvcs[i]->is_enabled(f_cvc_support_gpu)) {
Comment thread src/colvarmodule.cpp
Comment on lines +978 to +992
for (auto it = registered_atom_groups.begin(); it != registered_atom_groups.end(); ++it) {
auto parent_cvcs = it->first->get_parents();
bool use_cpu_buffers = false;
for (auto c = parent_cvcs.begin(); c != parent_cvcs.end(); ++c) {
if ((*c)->get_object_type() == colvardeps::object_t::colvarcomp) {
auto cvc = dynamic_cast<colvar::cvc*>(*c);
use_cpu_buffers |= cvc->is_enabled(colvardeps::f_cvc_require_cpu_buffers);
}
}
if (use_cpu_buffers) {
it->first->reset_atoms_data();
}
error_code |= it->first->get_gpu_atom_group()->read_data_gpu(it->first);
error_code |= it->first->get_gpu_atom_group()->after_read_data_sync(it->first, use_cpu_buffers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants