Repository navigation
Grid unification (METAD/PBMETAD grid and KDE/gridtools grid) - #1707
Open
carlocamilloni wants to merge 5 commits into
Open
carlocamilloni wants to merge 5 commits into
carlocamilloni wants to merge 5 commits into
Conversation
…bject First step towards one grid implementation for METAD/PBMETAD (tools/Grid) and KDE/gridtools (tools/GridCoordinatesObject). - tools/GridExtension plans the extensions of the METAD/PBMETAD grid and of the KDE grid with the same rules: the points needed plus one zero point on each side, growth by at least a quarter of the points, GRID_SIZE_LIMIT (raised to twice the size of a larger initial grid). It replaces the two copies in MetaDBiasGrid::extend and KDEGridTools::extendGridBounds. - GridCoordinatesObject keeps its bounds as doubles: setLattice sets a grid without parsing strings, extend adds points on the same lattice (same arithmetic and bounds as GridBase::resize), onSameLattice/getShiftFrom give the exact shift between a grid and its extension, writeCheckpoint and readCheckpoint store it. - KDE no longer re-parses its own 12-digit bounds; ACCUMULATE places its data in an extended grid with the exact integer shift instead of a tolerance; both store the grid through the GridCoordinatesObject checkpoint. - The growth of the KDE grid is now based on the number of points, as in METAD (it was based on the number of bins): extended KDE grids can have one more point (references of rt-flatkde, rt-histogram, rt-q6-histogram, rt-fftw updated). The placement of a KDE grid is checked against GRID_SIZE_LIMIT. - METAD/PBMETAD results are bitwise unchanged. - New unit test regtest/basic/rt-make-gridextension. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016vwxtU1YMqwpKiqhUECCP4
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1707 +/- ##
==========================================
- Coverage 89.32% 89.28% -0.04%
==========================================
Files 662 666 +4
Lines 84504 84600 +96
==========================================
+ Hits 75482 75536 +54
- Misses 9022 9064 +42 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…sObject Second step towards one grid implementation for METAD/PBMETAD (tools/Grid) and KDE/gridtools (tools/GridCoordinatesObject). - tools/GridLattice is the geometry of a grid without data: range, spacing, periodicity, indices (column-major), points, neighbours, spline neighbours, extension and lattice shift. It is the code of GridBase, with its dimension-templated Accelerator, moved unchanged. - GridBase derives from GridLattice; its public interface does not change (METAD, PBMETAD, EXTERNAL, FUNCSUMHILLS, funnel and ves are not touched). - The flat grids of GridCoordinatesObject contain a GridLattice and use it for all their geometry; the interface of GridCoordinatesObject does not change (indices stay unsigned for now), fibonacci grids are unchanged. - Behaviour changes only where the old code gave an error or a wrong result: along a periodic direction the indices of a point at the upper end or outside the range are brought back into the grid; the neighbours of a point outside the grid are computed from its own lattice position (GridCoordinatesObject converted a negative position to unsigned: on arm64 the neighbours of a kernel centred below GRID_MIN were those of the first point); flat GridCoordinatesObject grids are limited to 16 dimensions and an index outside the grid is an error, as in GridBase. - METAD/PBMETAD/EXTERNAL/ves results are bitwise unchanged; no reference changes. KDE and HISTOGRAM are 3-11% faster. - New unit test regtest/basic/rt-make-gridlattice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016vwxtU1YMqwpKiqhUECCP4
| template<typename T> | ||
| unsigned GridLattice::splineNeighbors(const unsigned* indices, std::size_t indices_size, T* neighbors, std::size_t neighbors_size) const { | ||
| plumed_assert(indices_size==dimension_); | ||
| unsigned nneigh=1<<dimension_; // same as unsigned(pow(2.0,dimension_)); |
- GridCoordinatesObject indices are std::size_t (getIndex, getNumberOfPoints, getIndices, getGridPointCoordinates, getNeighbors, getSplineNeighbors, getStride...), as in GridBase; the KDE grid tools and the callers in gridtools, contour and ACCUMULATE are updated. - KDE/HISTOGRAM stored a table of grid points x kernels (e.g. 1000 kernels in 3D on about 80^3 points: 4.2 GB; 10000 kernels on 400x450 points: out of memory). Now only the pairs of a kernel and a grid point where it is not zero are stored, in compressed rows with the kernels of each point in the same order, so the results are bitwise identical (160 MB and 310 MB in those cases); KDE and HISTOGRAM are 2-2.8 times faster. - MetaDBiasGrid::addHillToPoints does not open an OpenMP parallel region when it runs on one thread (libgomp sets up a team at every region, i.e. at every hill): METAD/PBMETAD with grids 2-3% faster, bitwise identical. - GridBase constructor: the vectors are built with their size (gcc 13 at -O3 gave a -Warray-bounds false positive on a vector<bool> resized from empty, an error with -Werror). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016vwxtU1YMqwpKiqhUECCP4
- The spline interpolation of GridBase (METAD, EXTERNAL, ves...) and that of the gridtools Interpolator (EVALUATE_FUNCTION_FROM_GRID, INTERPOLATE_GRID) were two copies of the same code. GridLattice:: splineInterpolation is the only one: the values and derivatives at the corners of the cell are read through a function given by the caller (the data of the grid, or the Value). - Results are bitwise identical. The gridtools spline is about twice as fast (no allocations per call); the GridBase spline costs the same. - At the upper end of a periodic argument (e.g. pi) the point is brought back into the grid and the polynomial now uses the right cell offset (the gridtools spline used a wrong one). - The spline is tested in regtest/basic/rt-make-gridlattice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016vwxtU1YMqwpKiqhUECCP4
| // a point outside the range of a periodic direction (e.g. at its upper end) has been brought back into the grid: | ||
| // the cell is the same, but xfloor must be next to x | ||
| const double f=std::floor((x[j]-min_[j])/dx_[j]); | ||
| if(f!=static_cast<double>(indices[j])) { |
…the grid files. METAD's grids (GridBase), DUMPGRID and REFERENCE_GRID now all use them. Files written: nothing changes. Each action still writes the same style as before: METAD/sum_hills count nbins as points and name derivatives der_x; DUMPGRID counts bins and names them d<f>_x. Files read: the reader works out the style from the derivative columns. So REFERENCE_GRID can now read METAD/PBMETAD grids and sum_hills free energies, and EXTERNAL (or METAD with GRID_RFILE) can read DUMPGRID files. New regtest gridtools/rt-grid-file-styles: one grid in each style, each read by EXTERNAL and by REFERENCE_GRID. Both give identical values. CHANGES/v2.11.md: one user entry and one developer entry. Your rt-make-gridlattice failure (macOS and Intel) came from the part 1 test printing the spline results with 17 digits. The last digits depend on how the compiler fuses operations and on the maths library. The spline lines now print 10 decimals, with noise below 1e-10 shown as zero; the reference is updated. The fix is in the same patch. Checks: Full regtest, all modules: everything passes and no reference files changed. As before, I stopped the ves MPI test that hangs on 2 cores. Comparison with step A at 17 digits, 221 tests: only the known differences (MPI log timings and the rt-pbmetad-grid-mpi replica files), with the same exit codes. So METAD/PBMETAD results and the grid files they write are bitwise unchanged. Compiler warnings: -O3 -Wall -pedantic -Wshadow -Werror is clean on the changed files, and astyle has been applied. codecheck: nothing new in the changed code. The run was cut short, so I compared the touched files by hand. What it flags is either in the part 1 code too, or cppcheck's usual header-only "unused member" false positive on GridFile.h. CodeQL: your repo's CodeQL workflow runs on pull requests to master, so it will check these changes once they're on the PR. Meanwhile I reviewed the new code by hand against CodeQL's usual patterns and found nothing.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Grid unification (METAD/PBMETAD grid and KDE/gridtools grid)
Branch:
grid-unification(from master a430df5). Open as a draft: steps B and C will be added to the same PR.Push from the Mac:
git push -u origin grid-unification(the remote is SSH, the sandbox cannot push).Title
Grids: one implementation for METAD/PBMETAD and KDE/gridtools, where possible
Body
PLUMED has two grid implementations:
tools/Grid(GridBase,Grid,SparseGrid; used by METAD/PBMETAD, EXTERNAL, FUNCSUMHILLS, funnel, ves) andtools/GridCoordinatesObject(geometry only, data in theValue; used by KDE and the rest of gridtools, contour, fourier, ACCUMULATE). Since #1666 both extend their grids with the same rules, in two copies. This PR removes the duplication step by step, without changing the METAD/PBMETAD results and without slowing METAD down. Data storage stays separate: the METAD bias is not a grid of the action graph.Plan
GridCoordinatesObject(8d0dc0b)tools/GridLattice, GridBase's code, used by both) (23f938e)GridCoordinatesObjectindices tosize_t; KDE memory proportional to the kernel-point pairs; no OpenMP region per METAD hill with one thread (a941d4c)GridLattice::splineInterpolation, used byGridBaseand the gridtoolsInterpolator) (2cc5cf0)tools/GridFile, used byGridBase, DUMPGRID and REFERENCE_GRID) (patch delivered 2026-11-29, not committed yet; also fixesrt-make-gridlatticeon macOS/Intel)A: shared extension planner and exact lattice
tools/GridExtensionplans the extensions of the METAD/PBMETAD grid and of the KDE grid with the same rules: the points needed plus one zero point on each side, growth by at least a quarter of the points,GRID_SIZE_LIMIT(raised to twice the size of a larger initial grid). It replaces the two copies inMetaDBiasGrid::extendandKDEGridTools::extendGridBounds. It only computes numbers of points; the callers apply them and write their own errors and log lines.GridCoordinatesObjectkeeps its bounds as doubles:setLatticesets a grid without parsing strings;extendadds points on the same lattice with the same arithmetic and bounds asGridBase::resize(so that in BGridBasecan use it and METAD stays bitwise);onSameLattice/getShiftFromgive the exact shift between a grid and its extension;writeCheckpoint/readCheckpointstore it.GridCoordinatesObjectcheckpoint (new checkpoint keys; the checkpoint is new in 2.11).gridtools/rt-flatkde,rt-histogram,rt-q6-histogram(one more point at the lower end, data otherwise identical) andfourier/rt-fftw(the input histogram has 126 bins instead of 125, so the transform changes);GRID_SPACINGand no bounds is checked againstGRID_SIZE_LIMIT, as in METAD;regtest/basic/rt-make-gridextension.B: one geometry
tools/GridLatticeis the geometry of a grid without data: range, spacing, periodicity, indices (column-major), points, neighbours, spline neighbours, extension and lattice shift. It is GridBase's code, with its dimension-templated Accelerator, moved unchanged.GridBasederives from it (public interface unchanged: METAD, PBMETAD, EXTERNAL, FUNCSUMHILLS, funnel and ves are not touched); the flat grids ofGridCoordinatesObjectcontain one (its interface is unchanged and staysunsignedfor now; fibonacci grids are unchanged).GridCoordinatesObjectconverted a negative position tounsigned(undefined behaviour): right by accident on x86, but on arm64 the neighbours of a kernel centred belowGRID_MIN(withNO_EXTEND) were those of the first point, so the kernel was also added beyond its support;GridCoordinatesObjectflat grids are limited to 16 dimensions, and an index outside the grid is an error, as inGridBase.regtest/basic/rt-make-gridlattice.Follow-up of B
GridCoordinatesObjectindices arestd::size_t(asGridBase), and so are the neighbour lists of the KDE grid tools.-Werror=array-bounds(false positive invector<bool>::resizein theGridBaseconstructor): fixed.C, part 1: one spline interpolation
GridBase(METAD, EXTERNAL, ves...) and that of the gridtoolsInterpolator(EVALUATE_FUNCTION_FROM_GRID, INTERPOLATE_GRID) were the same code.GridLattice::splineInterpolationis the only copy, reading the values and derivatives through a function given by the caller (the grid's own data, or theValue).GridBasespline costs the same.rt-make-gridlattice(the spline lines print 10 decimals since part 2: with 17 digits the test failed on macOS and with the Intel compiler, from FMA/libm differences in the last digits).C, part 2: one grid file reader/writer
#! SET min_x max_x nbins_x periodic_xgrid files, with two incompatible conventions:GridBase(METAD, PBMETAD, sum_hills, EXTERNAL, ves...) writesnbins_x= number of points (bins+1 along a direction that is not periodic) and derivativesder_x; DUMPGRID writesnbins_x= number of bins and derivativesd<function>_x. So REFERENCE_GRID could not read a METAD grid, and EXTERNAL could not read a DUMPGRID file.tools/GridFile:GridFileWriterwrites either style (same columns, formats and blank lines as before, so no file written by PLUMED changes);GridFileReaderreads both, recognising the style from the derivative columns (der_xpoints,d<f>_xbins, none points), and gives the number of bins.GridBase::writeToFile/create(Grid and SparseGrid), the flat-grid branch of DUMPGRID and the FILE branch of REFERENCE_GRID use them;GridBase::writeHeaderis removed. Fibonacci grids, cube and xyz outputs are unchanged.periodic_x/nbins_x, is an error.gridtools/rt-grid-file-styles: a METAD grid and a DUMPGRID grid (phi periodic, d not) are each read by EXTERNAL and by REFERENCE_GRID + EVALUATE_FUNCTION_FROM_GRID, which give identical values; both are written again by DUMPGRID with the samenbins_d.Checks for C part 2 (Linux, gcc 13,
--enable-modules=all):-O3 -Wall -pedantic -Wshadow -Werroron the changed files: clean; astyle applied;unusedStructMembernotes onGridFile.h, whose members are used inGridFile.cpp);Checks for B:
Checks for A (Linux, gcc 13,
--enable-modules=all, FFTW):🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_016vwxtU1YMqwpKiqhUECCP4