Skip to content

GPU coordnum - #940

Merged
HanatoK merged 33 commits into
Colvars:masterfrom
HanatoK:coordnum-template-gpu-2
Sep 1, 2026
Merged

HanatoK merged 33 commits into
Colvars:masterfrom
HanatoK:coordnum-template-gpu-2

Conversation

@HanatoK

@HanatoK HanatoK commented Jul 15, 2026 •

Copy link
Copy Markdown
Member

This PR should wait for #919 and #938. #919 is necessary for wrapping the distances using the internal PBC function on GPU. This PR is independent from #926.

Regarding the pairlist implementation, the GPU kernels in this PR try to match the CPU pairlist, which is slow for the time being. A better option is to implement at least a NAMD-style GPU pairlist, but since Colvars (i) does not allow atoms in an atom group to be reordered, and (ii) uses only a single cutoff for both the switching function and the pairlist, it is impossible or very diffcult to do so.

@HanatoK HanatoK self-assigned this Jul 15, 2026
@HanatoK
HanatoK force-pushed the coordnum-template-gpu-2 branch 2 times, most recently from 8eac9a6 to 6c72b36 Compare July 28, 2026 19:09
@HanatoK
HanatoK marked this pull request as ready for review July 28, 2026 19:46
@HanatoK
HanatoK requested review from giacomofiorin and jhenin July 28, 2026 19:48
@HanatoK

HanatoK commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

Initial performance benchmark of computing selfcoordnum of 718 atoms in NAMD:

  • CPU: 63.2372 ns/day
  • CPU without cvm::error_static in cvm::system_boundary_conditions::position_distance: 77.2318 ns/day
  • GPU: 130.812 ns/day

Nsys shows that the GPU kernel is still very computationally expensive as its execution time is multiple times of the NB kernel:

colvars_coordnum_gpu

I would prefer optimizing the overall calculations in a new PR or #926.

Test Colvars configuration:

colvarsTrajFrequency 10
colvarsRestartFrequency 1000
smp gpu

colvar {
  name cv
  outputAppliedForce on
  selfCoordNum {
    group1 {
      atomNumbers {23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86 89 92 95 98 101 104 107 110 113 116 119 122 125 128 131 134 137 140 143 146 149 152 155 158 161 164 167 170 173 176 179 182 185 188 191 194 197 200 203 206 209 212 215 218 221 224 227 230 233 236 239 242 245 248 251 254 257 260 263 266 269 272 275 278 281 284 287 290 293 296 299 302 305 308 311 314 317 320 323 326 329 332 335 338 341 344 347 350 353 356 359 362 365 368 371 374 377 380 383 386 389 392 395 398 401 404 407 410 413 416 419 422 425 428 431 434 437 440 443 446 449 452 455 458 461 464 467 470 473 476 479 482 485 488 491 494 497 500 503 506 509 512 515 518 521 524 527 530 533 536 539 542 545 548 551 554 557 560 563 566 569 572 575 578 581 584 587 590 593 596 599 602 605 608 611 614 617 620 623 626 629 632 635 638 641 644 647 650 653 656 659 662 665 668 671 674 677 680 683 686 689 692 695 698 701 704 707 710 713 716 719 722 725 728 731 734 737 740 743 746 749 752 755 758 761 764 767 770 773 776 779 782 785 788 791 794 797 800 803 806 809 812 815 818 821 824 827 830 833 836 839 842 845 848 851 854 857 860 863 866 869 872 875 878 881 884 887 890 893 896 899 902 905 908 911 914 917 920 923 926 929 932 935 938 941 944 947 950 953 956 959 962 965 968 971 974 977 980 983 986 989 992 995 998 1001 1004 1007 1010 1013 1016 1019 1022 1025 1028 1031 1034 1037 1040 1043 1046 1049 1052 1055 1058 1061 1064 1067 1070 1073 1076 1079 1082 1085 1088 1091 1094 1097 1100 1103 1106 1109 1112 1115 1118 1121 1124 1127 1130 1133 1136 1139 1142 1145 1148 1151 1154 1157 1160 1163 1166 1169 1172 1175 1178 1181 1184 1187 1190 1193 1196 1199 1202 1205 1208 1211 1214 1217 1220 1223 1226 1229 1232 1235 1238 1241 1244 1247 1250 1253 1256 1259 1262 1265 1268 1271 1274 1277 1280 1283 1286 1289 1292 1295 1298 1301 1304 1307 1310 1313 1316 1319 1322 1325 1328 1331 1334 1337 1340 1343 1346 1349 1352 1355 1358 1361 1364 1367 1370 1373 1376 1379 1382 1385 1388 1391 1394 1397 1400 1403 1406 1409 1412 1415 1418 1421 1424 1427 1430 1433 1436 1439 1442 1445 1448 1451 1454 1457 1460 1463 1466 1469 1472 1475 1478 1481 1484 1487 1490 1493 1496 1499 1502 1505 1508 1511 1514 1517 1520 1523 1526 1529 1532 1535 1538 1541 1544 1547 1550 1553 1556 1559 1562 1565 1568 1571 1574 1577 1580 1583 1586 1589 1592 1595 1598 1601 1604 1607 1610 1613 1616 1619 1622 1625 1628 1631 1634 1637 1640 1643 1646 1649 1652 1655 1658 1661 1664 1667 1670 1673 1676 1679 1682 1685 1688 1691 1694 1697 1700 1703 1706 1709 1712 1715 1718 1721 1724 1727 1730 1733 1736 1739 1742 1745 1748 1751 1754 1757 1760 1763 1766 1769 1772 1775 1778 1781 1784 1787 1790 1793 1796 1799 1802 1805 1808 1811 1814 1817 1820 1823 1826 1829 1832 1835 1838 1841 1844 1847 1850 1853 1856 1859 1862 1865 1868 1871 1874 1877 1880 1883 1886 1889 1892 1895 1898 1901 1904 1907 1910 1913 1916 1919 1922 1925 1928 1931 1934 1937 1940 1943 1946 1949 1952 1955 1958 1961 1964 1967 1970 1973 1976 1979 1982 1985 1988 1991 1994 1997 2000 2003 2006 2009 2012 2015 2018 2021 2024 2027 2030 2033 2036 2039 2042 2045 2048 2051 2054 2057 2060 2063 2066 2069 2072 2075 2078 2081 2084 2087 2090 2093 2096 2099 2102 2105 2108 2111 2114 2117 2120 2123 2126 2129 2132 2135 2138 2141 2144 2147 2150 2153 2156 2159 2162 2165 2168 2171 2174}
    }
  }
}

harmonic {
  colvars cv
  centers 4.67348234758518e+03
  forceConstant 0.001
}

@HanatoK

HanatoK commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

Performance test of coordnum with Apoa1

The configuration file is

indexFile index.ndx
# smp gpu

colvar {
  name cv
  coordNum {
    group1 {
      indexGroup backbone
    }
    group2 {
      indexGroup waters
    }
    cutoff 10.0
  }
}

harmonic {
  colvars cv
  centers 16774.0
  forceConstant 0.00001
}

smp gpu was turned on when testing with this PR and CUDAGM.

  • Equilibrium simulation without Colvars: 42.4303 ns/day
  • Colvars with CPU coordNum: 0.552597 ns/day
  • Colvars with GPU coordNum (this PR): 5.82301 ns/day

@HanatoK
HanatoK force-pushed the coordnum-template-gpu-2 branch 2 times, most recently from 4307cb5 to 749cf75 Compare August 11, 2026 15:36
@HanatoK
HanatoK force-pushed the coordnum-template-gpu-2 branch from 746569f to ec87d1e Compare August 12, 2026 14:45
Assisted-by: Claude Code v2.1.222 (Opus 5)
Assisted-by: Claude Code v2.1.222 (Opus 5)
Assisted-by: Claude Code v2.1.222 (Opus 5)
Assisted-by: Claude Code v2.1.222 (Opus 5)
…berGroupToCenterKernel

Assisted-by: Claude Code v2.1.222 (Opus 5)
…oupCUDAKernel1

Assisted-by: Claude Code v2.1.222 (Opus 5)
Assisted-by: Claude Code v2.1.222 (Opus 5)
Assisted-by: Claude Code v2.1.222 (Opus 5)
Assisted-by: Claude Code v2.1.222 (Opus 5)
1. Move the copy of lattice information before the copy of atoms
2. Lattice is not GPU-resident for the time being, so we just skip
   waiting update_lattice before entering the kernels

Assisted-by: Claude Code v2.1.222 (Opus 5)
gpu_warp_size

Assisted-by: Claude Code v2.1.222 (Opus 5)
Assisted-by: Claude Code v2.1.222 (Opus 5)
This commit also reverts the "pragma unroll" in
9ff3e96.
…ernel1

Assisted-by: Claude Code v2.1.222 (Opus 5)

@jhenin jhenin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Excellent! Just a small question about an API change.

Comment thread src/colvarproxy_system.h Resolved

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.

Pull request overview

Adds CUDA/HIP acceleration for coordination-number CVs, including pairlists, gradients, COM handling, PBC, and NAMD integration.

Changes:

  • Implements GPU kernels for coordination-number variants.
  • Adds GPU buffers, synchronization, device metadata, and dummy-group handling.
  • Integrates kernels into functional-test and NAMD builds.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/functional_gpu/CMakeLists.txt Builds coordination-number GPU kernels.
src/cuda/colvarcomp_coordnums_kernel.h Declares GPU coordination APIs.
src/cuda/colvarcomp_coordnums_kernel.cu Implements coordination kernels and pairlists.
src/cuda/colvaratoms_kernel.h Declares weighted-gradient support.
src/cuda/colvaratoms_kernel.cu Implements weighted-gradient kernel.
src/colvarproxy.cpp Logs GPU device details.
src/colvarproxy_system.h Makes boundary retrieval overridable.
src/colvarproxy_gpu.h Exposes GPU warp size.
src/colvarproxy_gpu.cpp Queries GPU warp size.
src/colvarmodule.h Enables integer powers on devices.
src/colvarmodule.cpp Consolidates GPU logging.
src/colvarcomp.cpp Supplies PBC data to GPU CVCs.
src/colvarcomp_distances.cpp Reports GPU RMSD activation.
src/colvarcomp_coordnums.h Adds coordination GPU interfaces.
src/colvarcomp_coordnums.cpp Integrates GPU coordination execution.
src/colvaratoms.cpp Synchronizes dummy positions to GPU.
src/colvaratoms_gpu.h Adds dummy and weighted-gradient APIs.
src/colvaratoms_gpu.cpp Manages new GPU atom buffers.
src/colvar_gpu_support.h Adds HIP device-attribute mappings.
namd/cudaglobalmaster/colvarproxy_cudaglobalmaster.C Synchronizes NAMD lattice updates.
namd/cudaglobalmaster/CMakeLists.txt Builds coordination kernels for NAMD.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/cuda/colvarcomp_coordnums_kernel.cu Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@giacomofiorin giacomofiorin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great to me! I have some minor comments below, but as far as I'm concerned they can be looked at in later PRs, or not at all.

Please merge as-is if you don't have any further commits.

Comment thread src/colvarcomp_coordnums.cpp Resolved
Comment thread src/colvarcomp_coordnums.cpp Resolved
Comment thread src/cuda/colvarcomp_coordnums_kernel.cu Resolved
Comment thread src/colvarmodule.cpp Resolved
Comment thread src/colvarcomp_coordnums.cpp Resolved
@HanatoK
HanatoK merged commit f4b3b12 into Colvars:master Sep 1, 2026
2 checks passed
acmnpv pushed a commit to gromacs/gromacs that referenced this pull request Oct 14, 2026
Updating the bundled copy of the Colvars library ahead of the 2027 release.

This branch is based on top of the branch of !6316, which changes the location of those files so the dependency is marked here to avoid conflicts.

Most of the changes are to the bundled copy of the library. The only two significant changes to the interface files in `src/gromacs/applied_forces/colvars` are:
1. Replacing the name of the pointer to the `colvarmodule` object, which was renamed during ongoing work to remove static pointers from the library
2. PBC settings and periodic cell dimensions (if available) are now copied and stored in the Colvars objects, thus replacing calls to `pbc_dx()` with inline code in Colvars.

The following is a list of pull requests in the [Colvars repo](https://github.com/Colvars/colvars/) affecting GROMACS merged since 2025-10-13 (version included in GROMACS 2026):

- Check that the restarted step value is consistent with MTS settings
  Colvars/colvars#961 (@giacomofiorin)

- fix: make max gradient error accumulation atomic
  Colvars/colvars#960 (@jhenin)

- fix: reject the use of "pmf on" without "pmfColvars"
  Colvars/colvars#959 (@HanatoK, @giacomofiorin)

- Optimize the switching function of coordnum on GPU
  Colvars/colvars#956 (@HanatoK, @Copilot)

- Optimize the GPU pairlist of the coordnum kernel
  Colvars/colvars#955 (@HanatoK)

- Optimize the GPU pairlist of the selfcoordnum kernel
  Colvars/colvars#954 (@HanatoK)

- test: use -reprod for gmx multisim test
  Colvars/colvars#952 (@jhenin)

- test: fix run_tests.sh in GROMACS test suite
  Colvars/colvars#948 (@HanatoK, @jhenin)

- Improve documentation of spinAngle and tilt
  Colvars/colvars#945 (@giacomofiorin, @jhenin)

- fix: correctly implement the wrap function for orientation_angle
  Colvars/colvars#943 (@HanatoK)

- fix: fix MSVC compilation warning about <stacktrace>
  Colvars/colvars#941 (@HanatoK)

- GPU coordnum
  Colvars/colvars#940 (@HanatoK, @Copilot)

- refactor: use separate CUDA streams for children of colvardeps and use CUDA events to synchronize the calculations
  Colvars/colvars#938 (@HanatoK, @Copilot, @jhenin)

- Update Colvars_traj documentation
  Colvars/colvars#929 (@capritchard)

- refactor: remove unused dq0_1 in calc_fit_forces_impl
  Colvars/colvars#927 (@HanatoK)

- Handle boundary conditions internally
  Colvars/colvars#919 (@giacomofiorin, @Copilot, @HanatoK)

- fix: the macros of determining the device code should be reversed
  Colvars/colvars#917 (@HanatoK)

- colvars_grid improvements
  Colvars/colvars#912 (@jhenin)

- Simplify and optimize coordination numbers
  Colvars/colvars#909 (@giacomofiorin, @HanatoK)

- Do not require harmonic walls start with nonzero k
  Colvars/colvars#906 (@jhenin)

- Fix: Implement numerically safe switching function
  Colvars/colvars#904 (@jhenin, @giacomofiorin)

- fix: initialize variable, appease Coverity
  Colvars/colvars#901 (@jhenin)

- fix: fix the out-of-bound access in from_simple_string
  Colvars/colvars#897 (@HanatoK)

- doc: urge users the importance of specifying names for sub-CVs
  Colvars/colvars#896 (@HanatoK)

- Warn when discarding state of unknown object
  Colvars/colvars#893 (@jhenin, @giacomofiorin)

- Refactor: replace static colvarmodule with class member
  Colvars/colvars#886 (@jhenin, @HanatoK)

- Trap floating-point exceptions
  Colvars/colvars#877 (@jhenin, @HanatoK)

- Weighted poisson integration
  Colvars/colvars#872 (@jhenin, @HanatoK, @levo-str)

- Fix debug gradients recalc gradients
  Colvars/colvars#868 (@HanatoK)

- Make non-MTS colvars always awake
  Colvars/colvars#866 (@jhenin)

- Loop over replicas when computing well-tempered metadynamics scaling factor
  Colvars/colvars#820 (@giacomofiorin)

Authors: @capritchard, @Copilot, @giacomofiorin, @HanatoK, @jhenin, @levo-str
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.

4 participants