diff --git a/.github/workflows/cmdata.yml b/.github/workflows/cmdata.yml index c19973e1..8152cae8 100644 --- a/.github/workflows/cmdata.yml +++ b/.github/workflows/cmdata.yml @@ -36,11 +36,15 @@ jobs: - name: Build cmdata run: | cd tools/cmdata; mkdir build; cd build - cmake ../ + cmake ../ -DCMDATA_BUILD_TESTS=ON make sudo make install + - name: Test cmdata + run: | + cd tools/cmdata/build + make test cmdata --help - - name: Run cmdata test + - name: Run cmdata regtests run: | source /usr/local/gromacs/bin/GMXRC cd tests; bash run_cmdata.sh; cd .. diff --git a/tests/run_cmdata.sh b/tests/run_cmdata.sh index 7b623755..04dd488e 100755 --- a/tests/run_cmdata.sh +++ b/tests/run_cmdata.sh @@ -3,15 +3,15 @@ set -o pipefail # Relative and absolute tolerances for numerical comparison. # Adjust RTOL / ATOL here to tighten or loosen the test. -RTOL=${CMDATA_RTOL:-1e-4} +RTOL=${CMDATA_RTOL:-5e-4} ATOL=${CMDATA_ATOL:-1e-7} SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" COMPARE="$SCRIPT_DIR/compare_histo.py" -mkdir test_inputs/cmdata/histo +mkdir -p test_inputs/cmdata/histo cd test_inputs/cmdata/histo -cmdata -f ../traj.xtc -s ../protein.tpr --noh5 +OMP_NUM_THREADS=2 cmdata -f ../traj.xtc -s ../protein.tpr --noh5 cd ../../../ tar -zxf test_outputs/cmdata/hh.tgz -C test_outputs/cmdata/ diff --git a/tools/cmdata/.gitignore b/tools/cmdata/.gitignore index 55fae157..c3565cb9 100644 --- a/tools/cmdata/.gitignore +++ b/tools/cmdata/.gitignore @@ -1,2 +1,3 @@ build/ exe/ +test/test_cmdata diff --git a/tools/cmdata/CMakeLists.txt b/tools/cmdata/CMakeLists.txt index 6dd20319..abba786c 100644 --- a/tools/cmdata/CMakeLists.txt +++ b/tools/cmdata/CMakeLists.txt @@ -26,6 +26,17 @@ endif() # find gromacs find_package(GROMACS REQUIRED NAMES gromacs gromacs_mpi gromacs_d gromacs_mpi_d HINTS "$ENV{GROMACS_DIR}") +find_package(OpenMP) +if(OpenMP_CXX_FOUND) + message(STATUS "OpenMP ${OpenMP_CXX_VERSION} found — parallel build enabled (set OMP_NUM_THREADS at runtime)") +else() + message(WARNING "OpenMP not found — building single-threaded.\n" + " On macOS: brew install libomp, then re-run cmake with:\n" + " -DOpenMP_CXX_FLAGS='-Xpreprocessor -fopenmp'\n" + " -DOpenMP_CXX_LIB_NAMES=omp\n" + " -DOpenMP_omp_LIBRARY=$(brew --prefix libomp)/lib/libomp.dylib") +endif() + # check if gromacs version is at least 2026 string(REPLACE "." ";" GMX_VER_LIST "${GROMACS_VERSION}") list(GET GMX_VER_LIST 0 GMX_MAJOR_VER) @@ -61,6 +72,12 @@ FetchContent_Declare( DOWNLOAD_EXTRACT_TIMESTAMP TRUE ) FetchContent_MakeAvailable(popt) +# Suppress all warnings from popt (third-party, no control) +target_compile_options(popt PRIVATE -w) +get_target_property(_popt_inc popt INTERFACE_INCLUDE_DIRECTORIES) +if(_popt_inc) + set_target_properties(popt PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${_popt_inc}") +endif() message(STATUS "Fetching xdrfile") FetchContent_Declare( @@ -72,9 +89,15 @@ FetchContent_Declare( # default so the warning is suppressed in that subdirectory. set(CMAKE_POLICY_DEFAULT_CMP0048 NEW) FetchContent_MakeAvailable(xdrfile) +# Suppress all warnings from xdrfile (third-party, no control) +target_compile_options(xdrfile PRIVATE -w) +get_target_property(_xdr_inc xdrfile INTERFACE_INCLUDE_DIRECTORIES) +if(_xdr_inc) + set_target_properties(xdrfile PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${_xdr_inc}") +endif() # link libraries -target_link_libraries(${CMDATA} PRIVATE Gromacs::libgromacs xdrfile popt) +target_link_libraries(${CMDATA} PRIVATE Gromacs::libgromacs xdrfile popt $<$:OpenMP::OpenMP_CXX>) # Workaround for macOS 26+ SDK: libm.tbd was removed because math functions are # now fully part of libSystem. GROMACS (and other deps) may export an absolute @@ -103,6 +126,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_P install(TARGETS ${CMDATA} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) # build test +option(CMDATA_BUILD_TESTS "Build and register unit tests (no GROMACS required)" OFF) if(CMDATA_BUILD_TESTS) enable_testing() add_subdirectory(test/) diff --git a/tools/cmdata/README.md b/tools/cmdata/README.md index b061e76f..10da2c16 100644 --- a/tools/cmdata/README.md +++ b/tools/cmdata/README.md @@ -1,12 +1,12 @@ # cmdata -`cmdata` calculates interatomic distance histograms from GROMACS trajectories and outputs probability density and CDF files used by multi-eGO to build its input contact matrices. +`cmdata` calculates interatomic distance histograms from GROMACS trajectories and outputs probability density and cumulative distribution function (CDF) files used by multi-eGO to build its contact matrices. ## Installation ### Prerequisites -GROMACS 2023, 2024, or 2026+ must be compiled and installed with: +**GROMACS** (2023+) must be built with the legacy API and shared libraries enabled: ```bash cmake .. -DGMX_INSTALL_LEGACY_API=ON \ @@ -15,7 +15,23 @@ cmake .. -DGMX_INSTALL_LEGACY_API=ON \ make -j$(nproc) && make install ``` -HDF5 is optional. If found by CMake, output files can be written in HDF5 format (`.h5`) in addition to plain text (`.dat`). +**OpenMP** is used for parallelism and is detected automatically at build time. On Linux it is provided by GCC or Clang out of the box. On macOS, Apple Clang does not include OpenMP; install it via Homebrew: + +```bash +brew install libomp +``` + +then point CMake at it: + +```bash +cmake .. -DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp" \ + -DOpenMP_CXX_LIB_NAMES="omp" \ + -DOpenMP_omp_LIBRARY=$(brew --prefix libomp)/lib/libomp.dylib +``` + +If OpenMP is not found, cmdata builds and runs correctly but uses only one core. + +**HDF5** is optional but recommend. If found by CMake, output files can be written in HDF5 format (`.h5`) in addition to plain text (`.dat`). ### Building cmdata @@ -26,14 +42,13 @@ cmake .. -DCMAKE_INSTALL_PREFIX=/path/to/install \ make -j$(nproc) && make install ``` -> **macOS note:** the build system automatically strips the stale `libm.tbd` reference that GROMACS exports in its CMake config — this stub was removed from the macOS 26+ SDK (math is now part of `libSystem`). No manual workaround is needed. - Set `GROMACS_DIR` to the root of your GROMACS installation if it is not in a standard location. -To build with tests: +To build with unit tests (no GROMACS required for the tests themselves): ```bash cmake .. -DCMDATA_BUILD_TESTS=ON +make && ctest ``` ## Usage @@ -46,7 +61,7 @@ cmdata -f TRAJ -s TOP [OPTIONS] | Option | Description | |--------|-------------| -| `-f`, `--traj=FILE` | Input trajectory file (`.xtc`, `.trr`, `.gro`, or `.pdb`) | +| `-f`, `--traj=FILE` | Input trajectory file (`.xtc`) | | `-s`, `--top=FILE` | Input topology file (`.tpr`) | | `--mode=STRING` | Calculation mode (see below) | @@ -54,19 +69,17 @@ cmdata -f TRAJ -s TOP [OPTIONS] | Option | Default | Description | |--------|---------|-------------| -| `-o`, `--out=STRING` | `""` (current dir) | Output file prefix | -| `-b`, `--t_begin=FLOAT` | `0.0` | Start time (ps) | -| `-e`, `--t_end=FLOAT` | `-1` (end of traj) | End time (ps); `-1` means read to the end | -| `--dt=INT` | `0` (every frame) | Only process frames at multiples of this time (ps) | -| `--cutoff=FLOAT` | `0.75` | Distance cutoff for atom pairs (nm) | -| `--mol_cutoff=FLOAT` | `6.0` | Centre-of-mass cutoff for molecule pairs (nm) | -| `--nskip=INT` | `0` | Skip every N frames (0 = no skipping) | -| `--num_threads=INT` | `1` | Number of threads for the mindist accumulation step | -| `--mol_threads=INT` | `1` | Number of concurrent molecule threads | -| `--weights=FILE` | `""` (none) | Per-frame weight file (one float per line) | -| `--bkbn_H=STRING` | `""` (none) | Backbone hydrogen atom name to include (all other H atoms are skipped) | -| `--no_pbc` | off | Disable periodic boundary corrections | -| `--noh5` | off | Write plain-text `.dat` files instead of HDF5 `.h5` files (ignored if HDF5 was not found at build time) | +| `-o`, `--out=STRING` | `""` (current dir) | Output file prefix | +| `-b`, `--t_begin=FLOAT` | `0.0` | Start time (ps) | +| `-e`, `--t_end=FLOAT` | `-1` (end of traj) | End time (ps); `-1` means read to the end | +| `--dt=INT` | `0` (every frame) | Only process frames at multiples of this time (ps) | +| `--cutoff=FLOAT` | `0.75` | Distance cutoff for atom pairs (nm) | +| `--mol_cutoff=FLOAT` | `6.0` | Centre-of-mass cutoff for molecule pairs (nm) | +| `--nskip=INT` | `0` | Skip every N frames (0 = no skipping) | +| `--weights=FILE` | `""` (none) | Per-frame weight file (one float per line) | +| `--bkbn_H=STRING` | `""` (none) | Backbone hydrogen atom name to include (all other H are skipped) | +| `--no_pbc` | off | Disable periodic boundary corrections | +| `--noh5` | off | Write plain-text `.dat` files instead of HDF5 `.h5` (ignored if HDF5 was not found at build time) | ### Mode @@ -80,40 +93,54 @@ cmdata -f TRAJ -s TOP [OPTIONS] Examples: `--mode intra`, `--mode same+cross`, `--mode intra+same+cross`. +### Parallelism + +cmdata uses **OpenMP** to parallelise the per-molecule pairwise distance loop. The number of threads is controlled at runtime via the standard `OMP_NUM_THREADS` environment variable — no build-time flags are needed: + +```bash +OMP_NUM_THREADS=8 cmdata -f traj.xtc -s topol.tpr --mode intra+same +``` + +If `OMP_NUM_THREADS` is not set, OpenMP defaults to using all available cores. Set it to `1` to force single-threaded execution. + ## Output files -Each output file is named `___aa_.h5` (or `.dat`), where: +Each output file is named `___aa_.dat` (or `.h5`), where: + +- `` is `intra_mol`, `inter_mol`, or `inter_mol_c` +- ``, `` are 1-based molecule-type indices +- `` is the 1-based atom index within molecule type `i` -- `` is `intra_mol`, `inter_mol`, or `inter_mol_c` (cross) -- ``, `` are zero-based molecule-type indices -- `` is the atom index within molecule type `i` +Each file contains one column per atom in molecule type `j`. The rows correspond to histogram bins uniformly spaced from `dx/2` to `cutoff`, with `dx = cutoff / n_bins`. -Each file contains one column per atom pair `(ii, jj)`. The rows are histogram bins from `dx/2` to `cutoff` in steps of `dx = cutoff / n_bins`. +For `same` and `cross` modes, two files are written per atom: -For `same` and `cross` modes two files are written per atom: one for the full density histogram and one for the per-molecule maximum-CDF (`maxcdf`). +- `inter_mol_*` — probability density histogram +- `inter_mol_c_*` — per-molecule maximum CDF (used directly by multi-eGO) ## Examples Compute intra-molecular histograms only, using 8 threads: ```bash -cmdata -f traj.xtc -s topol.tpr --mode intra --num_threads 8 -o output/ +OMP_NUM_THREADS=8 cmdata -f traj.xtc -s topol.tpr --mode intra -o output/ ``` Compute all three modes with a 1.0 nm cutoff, skipping the first 10 ns: ```bash -cmdata -f traj.xtc -s topol.tpr \ - --mode intra+same+cross \ - --cutoff 1.0 \ - --t_begin 10000 \ - --num_threads 4 --mol_threads 4 \ - -o run1/ +OMP_NUM_THREADS=8 cmdata -f traj.xtc -s topol.tpr \ + --mode intra+same+cross \ + --cutoff 1.0 \ + --t_begin 10000 \ + -o run1/ ``` -Use a per-frame weight file (e.g., from replica-exchange or metadynamics): +Use a per-frame weight file (e.g. from replica-exchange or metadynamics): ```bash -cmdata -f traj.xtc -s topol.tpr --mode intra+same \ - --weights weights.dat -o weighted/ +OMP_NUM_THREADS=8 cmdata -f traj.xtc -s topol.tpr \ + --mode intra+same \ + --weights weights.dat \ + -o weighted/ ``` diff --git a/tools/cmdata/main.cpp b/tools/cmdata/main.cpp index 29023821..1e5398d1 100644 --- a/tools/cmdata/main.cpp +++ b/tools/cmdata/main.cpp @@ -10,13 +10,13 @@ int main(int argc, const char** argv) { std::cout << "################################################" << std::endl; - std::cout << "#################### CMDATA ####################" << std::endl; - std::cout << "################################################" << std::endl; - std::cout << "################## Version 0.1 #################" << std::endl; + std::cout << "#################### cmdata ####################" << std::endl; + std::cout << "################### Multi-eGO ##################" << std::endl; + std::cout << "################## Version 1.0 #################" << std::endl; std::cout << "################################################\n" << std::endl; float cutoff = 0.75, mol_cutoff = 6.0; - int nskip = 0, num_threads = 1, mol_threads = -1, dt = 0; + int nskip = 0, dt = 0; float t_begin = 0.0, t_end = -1.0; char *p_traj_path = NULL, *p_top_path = NULL, *p_mode = NULL,*p_bkbn_H = NULL, *p_weights_path = NULL; char *p_out_prefix = NULL; @@ -42,8 +42,6 @@ int main(int argc, const char** argv) {"cutoff", '\0', POPT_ARG_DOUBLE | POPT_ARGFLAG_OPTIONAL, &cutoff, 0, "Cutoff distance", "DOUBLE"}, {"mol_cutoff", '\0', POPT_ARG_DOUBLE | POPT_ARGFLAG_OPTIONAL, &mol_cutoff, 0, "Molecule cutoff distance", "DOUBLE"}, {"nskip", '\0', POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, &nskip, 0, "Number of frames to skip", "INT"}, - {"num_threads", '\0', POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, &num_threads, 0, "Number of threads", "INT"}, - {"mol_threads", '\0', POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, &mol_threads, 0, "Number of molecule threads", "INT"}, {"mode", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &p_mode, 0, "Mode of operation", "STRING"}, {"bkbn_H", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &p_bkbn_H, 0, "Extra backbone H name (H and HN are always included)", "STRING"}, {"weights", '\0', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &p_weights_path, 0, "Weights file", "FILE"}, @@ -115,17 +113,6 @@ int main(int argc, const char** argv) } } } - if ( num_threads != 1 ) - { - std::cerr << "Number of threads is currently unused!" << std::endl; - num_threads = 1; - //return 6; - } - if ( mol_threads != 1 ) - { - std::cout << "threads cannot be used currently!" << std::endl; - mol_threads = num_threads; - } if ( dt < 0 ) { std::cerr << "Time step must be a positive number!" << std::endl; @@ -158,7 +145,7 @@ int main(int argc, const char** argv) } cmdata::CMData cmdata( - top_path, traj_path, cutoff, mol_cutoff, nskip, num_threads, mol_threads, dt, + top_path, traj_path, cutoff, mol_cutoff, nskip, dt, mode, bkbn_H, weights_path, nopbc, t_begin, t_end, h5 ); cmdata.run(); diff --git a/tools/cmdata/src/cmdata/cmdata.hpp b/tools/cmdata/src/cmdata/cmdata.hpp index e0a100e4..a503854c 100644 --- a/tools/cmdata/src/cmdata/cmdata.hpp +++ b/tools/cmdata/src/cmdata/cmdata.hpp @@ -17,23 +17,15 @@ #include "density.hpp" #include "mindist.hpp" #include "xtc_frame.hpp" -#include "function_types.hpp" // standard library imports #include -#include -#include -#include -#include -#include #include -#include #include #include #include #include #include -#include // xdrfile includes #include @@ -46,7 +38,8 @@ class CMData { private: // general fields - float cutoff_, mol_cutoff_, mcut2_, cut_sig_2_; + static constexpr float LARGE_DIST = 100.f; // sentinel for per-frame minimum distance reset + float cutoff_, mcut2_, cut_sig_2_; int nskip_, n_x_; float dt_, t_begin_, t_end_; gmx_mtop_t *mtop_; @@ -68,7 +61,7 @@ class CMData std::vector> atom_active_; // weights fields - float weights_sum_; + float weights_sum_ = 0.f; std::string weights_path_; std::vector weights_; @@ -94,74 +87,35 @@ class CMData cmdata_matrix interm_same_maxcdf_mol_; cmdata_matrix interm_cross_maxcdf_mol_; - // temporary containers for maxcdf operations + // temporary containers for per-frame minimum distances std::vector> frame_same_mat_; std::vector> frame_cross_mat_; - std::vector> frame_same_mutex_; - std::vector> frame_cross_mutex_; - - // parallelization fields - int num_threads_; - int num_mol_threads_; - std::vector threads_; - std::vector same_thread_indices_; - std::vector cross_thread_indices_; - - // persistent molecule thread pool — workers are created once in initAnalysis() - // and reused every frame, avoiding per-frame OS thread construction overhead. - // A generation counter prevents workers from re-entering the work queue after - // finishing a frame while frame_ready is still true. - struct MolPool { - std::vector workers; - std::mutex mtx; - std::condition_variable cv_work; // main → workers: new generation dispatched - std::condition_variable cv_done; // workers → main: all workers finished - std::atomic next_mol{0}; - std::atomic done_count{0}; - int total_mol{0}; - int num_workers{0}; - int generation{0}; // incremented each frame dispatch - bool stop{false}; - // frame-local data pointers set by main thread before each dispatch - t_pbc *pbc{nullptr}; - rvec *x{nullptr}; - float weight{1.f}; - } pool_; - - // mode selection, booleans and functions + + // mode selection std::string mode_; bool intra_ = false, same_ = false, cross_ = false; bool h5_ = false; - // No std::function members for the density routines — molecule_routine is - // templated on FIntra/FSame/FCross so the compiler can inline all three. - + // molecule_routine is templated on FIntra/FSame/FCross so the compiler can + // inline all three density paths and the no-op lambdas for disabled modes. template - static void molecule_routine( - const int i, const int nindex_, t_pbc *pbc, rvec *x, const std::vector &inv_num_mol_, const float cut_sig_2_, - const std::vector &natmol2_, const std::vector &num_mol_unique_, const std::vector &mol_id_, - const std::vector> &cross_index_, const std::vector &density_bins_, const float mcut2_, - rvec *xcm_, const gmx::RangePartitioning &mols_, - const std::vector> &atom_active_, - std::vector> &frame_same_mat_, std::vector> &frame_same_mutex_, - cmdata_matrix &intram_mat_density_, cmdata_matrix &interm_same_mat_density_, std::vector> &frame_cross_mat_, - std::vector> &frame_cross_mutex_, cmdata_matrix &interm_cross_mat_density_, + void molecule_routine( + int i, t_pbc *pbc, rvec *x, const FIntra &f_intra_mol_, const FSame &f_inter_mol_same_, const FCross &f_inter_mol_cross_, float weight ) { - int tmp_i = 0; + std::size_t tmp_i = 0; std::size_t mol_i = i, mol_j = 0; - while ( static_cast(mol_i) - num_mol_unique_[tmp_i] >= 0 ) + while ( mol_i >= static_cast(num_mol_unique_[tmp_i]) ) { mol_i -= num_mol_unique_[tmp_i]; tmp_i++; - if (tmp_i == static_cast(num_mol_unique_.size())) break; + if (tmp_i == num_mol_unique_.size()) break; } if (mol_i == static_cast(num_mol_unique_[mol_id_[i]])) mol_i = 0; const int mt_i = mol_id_[i]; - /* Loop over molecules */ for (int j = 0; j < nindex_; j++) { if (j != 0) @@ -173,76 +127,53 @@ class CMData rvec dx; if (pbc != nullptr) pbc_dx(pbc, xcm_[i], xcm_[j], dx); else rvec_sub(xcm_[i], xcm_[j], dx); - float dx2 = iprod(dx, dx); - if (dx2 > mcut2_) continue; + if (iprod(dx, dx) > mcut2_) continue; } /* for molecules of different specie we fill half a matrix */ if (mol_id_[i] != mol_id_[j] && j < i) continue; - std::size_t a_i = 0; const int mt_j = mol_id_[j]; - /* cycle over the atoms of a molecule i */ - for (std::size_t ii = mols_.block(i).begin(); ii < mols_.block(i).end(); ii++) + const std::size_t ii_begin = static_cast(mols_.block(i).begin()); + const std::size_t ii_end = static_cast(mols_.block(i).end()); + const std::size_t jj_begin = static_cast(mols_.block(j).begin()); + const std::size_t jj_end = static_cast(mols_.block(j).end()); + std::size_t a_i = 0; + for (std::size_t ii = ii_begin; ii < ii_end; ii++) { std::size_t a_j = 0; - if (!atom_active_[mt_i][a_i]) - { - a_i++; - continue; - } - /* cycle over the atoms of a molecule j */ - for (std::size_t jj = mols_.block(j).begin(); jj < mols_.block(j).end(); jj++) + if (!atom_active_[mt_i][a_i]) { a_i++; continue; } + for (std::size_t jj = jj_begin; jj < jj_end; jj++) { - if (!atom_active_[mt_j][a_j]) - { - a_j++; - continue; - } - std::size_t delta = a_i - a_j; + if (!atom_active_[mt_j][a_j]) { a_j++; continue; } + std::size_t delta = a_i - a_j; rvec sym_dx; if (pbc != nullptr) pbc_dx(pbc, x[ii], x[jj], sym_dx); else rvec_sub(x[ii], x[jj], sym_dx); float dx2 = iprod(sym_dx, sym_dx); - if (i==j) + if (i == j) { if (dx2 < cut_sig_2_) - { // intra molecule species - f_intra_mol_(i, a_i, a_j, dx2, weight, mol_id_, natmol2_, density_bins_, inv_num_mol_, frame_same_mutex_, intram_mat_density_); - } + f_intra_mol_(i, a_i, a_j, dx2, weight, mol_id_, natmol2_, density_bins_, inv_num_mol_, intram_mat_density_); } - else + else if (mol_id_[i] == mol_id_[j]) { - if (mol_id_[i]==mol_id_[j]) - { // inter same molecule specie - if (dx2 < cut_sig_2_) - { - f_inter_mol_same_( - i, mol_i, a_i, a_j, dx2, weight, mol_id_, natmol2_, density_bins_, frame_same_mutex_, frame_same_mat_, interm_same_mat_density_ - ); - } - if (delta != 0) { - // this is to account for inversion atom/molecule - if (pbc != nullptr) pbc_dx(pbc, x[ii-delta], x[jj+delta], sym_dx); - else rvec_sub(x[ii-delta], x[jj+delta], sym_dx); - dx2 = iprod(sym_dx, sym_dx); - if (dx2 < cut_sig_2_) - { - f_inter_mol_same_( - i, mol_i, a_i, a_j, dx2, weight, mol_id_, natmol2_, density_bins_, frame_same_mutex_, frame_same_mat_, interm_same_mat_density_ - ); - } - } - } - else - { // inter cross molecule species + if (dx2 < cut_sig_2_) + f_inter_mol_same_(i, mol_i, a_i, a_j, dx2, weight, mol_id_, natmol2_, density_bins_, frame_same_mat_, interm_same_mat_density_); + if (delta != 0) + { + // account for atom/molecule index inversion + if (pbc != nullptr) pbc_dx(pbc, x[ii-delta], x[jj+delta], sym_dx); + else rvec_sub(x[ii-delta], x[jj+delta], sym_dx); + dx2 = iprod(sym_dx, sym_dx); if (dx2 < cut_sig_2_) - { - f_inter_mol_cross_( - i, j, mol_i, mol_j, a_i, a_j, dx2, weight, mol_id_, natmol2_, cross_index_, density_bins_, num_mol_unique_, frame_cross_mutex_, frame_cross_mat_, interm_cross_mat_density_ - ); - } + f_inter_mol_same_(i, mol_i, a_i, a_j, dx2, weight, mol_id_, natmol2_, density_bins_, frame_same_mat_, interm_same_mat_density_); } } + else + { + if (dx2 < cut_sig_2_) + f_inter_mol_cross_(i, j, mol_i, mol_j, a_i, a_j, dx2, weight, mol_id_, natmol2_, cross_index_, density_bins_, num_mol_unique_, frame_cross_mat_, interm_cross_mat_density_); + } ++a_j; } ++a_i; @@ -254,12 +185,12 @@ class CMData public: CMData( const std::string &top_path, const std::string &traj_path, - float cutoff, float mol_cutoff, int nskip, int num_threads, int num_mol_threads, - int dt, const std::string &mode, const std::string &bkbn_H, const std::string &weights_path, + float cutoff, float mol_cutoff, int nskip, + int dt, const std::string &mode, const std::string &bkbn_H, const std::string &weights_path, bool no_pbc, float t_begin, float t_end, bool h5 - ) : cutoff_(cutoff), mol_cutoff_(mol_cutoff), nskip_(nskip), dt_(dt), t_begin_(t_begin), t_end_(t_end), - bkbn_H_(bkbn_H), weights_path_(weights_path), no_pbc_(no_pbc), num_threads_(num_threads), - num_mol_threads_(num_mol_threads), mode_(mode), h5_(h5) + ) : cutoff_(cutoff), mcut2_(mol_cutoff * mol_cutoff), nskip_(nskip), dt_(dt), + t_begin_(t_begin), t_end_(t_end), bkbn_H_(bkbn_H), weights_path_(weights_path), + no_pbc_(no_pbc), mode_(mode), h5_(h5) { matrix boxtop_; mtop_ = (gmx_mtop_t*)malloc(sizeof(gmx_mtop_t)); @@ -267,9 +198,7 @@ class CMData pbcType_ = read_tpx(top_path.c_str(), nullptr, boxtop_, &natoms, nullptr, nullptr, mtop_); if (no_pbc_) - { pbc_ = nullptr; - } else { pbc_ = (t_pbc*)malloc(sizeof(t_pbc)); @@ -293,14 +222,6 @@ class CMData ~CMData() { - // Shut down the persistent molecule thread pool cleanly before freeing data. - { - std::unique_lock lk(pool_.mtx); - pool_.stop = true; - pool_.cv_work.notify_all(); - } - for (auto &w : pool_.workers) w.join(); - free(frame_->x); free(frame_->offsets); free(frame_); @@ -309,91 +230,53 @@ class CMData } void initAnalysis() - /** - * @brief Initializes the analysis by setting up the molecule partitioning and the mode selection - * - * @todo Check if molecule block is empty - */ { n_x_ = 0; - // get the number of atoms per molecule - // equivalent to mols_ = gmx:gmx_mtop_molecules(*top.mtop()); + // build molecule blocks; equivalent to mols_ = gmx::gmx_mtop_molecules(*top.mtop()) for (const gmx_molblock_t &molb : mtop_->molblock) { int natm_per_mol = mtop_->moltype[molb.type].atoms.nr; for (int i = 0; i < molb.nmol; i++) mols_.appendBlock(natm_per_mol); num_mol_unique_.push_back(molb.nmol); } - // number of molecules nindex_ = mols_.numBlocks(); - if (num_threads_ > std::thread::hardware_concurrency()) - { - num_threads_ = std::thread::hardware_concurrency(); - std::cout << "Maximum thread number surpassed. Scaling num_threads down to " << num_threads_ << std::endl; - } - if (num_mol_threads_ > std::thread::hardware_concurrency()) - { - num_mol_threads_ = std::thread::hardware_concurrency(); - std::cout << "Maximum thread number surpassed. Scaling num_mol_threads down to " << num_mol_threads_ << std::endl; - } - if (num_mol_threads_ > nindex_) - { - num_mol_threads_ = nindex_; - std::cout << "Number of molecule threads surpassed number of molecules. Setting num_mol_threads to " << num_mol_threads_ << std::endl; - } - threads_.resize(num_threads_); - std::cout << "Using " << num_threads_ << " threads and " << num_mol_threads_ << " molecule threads" << std::endl; - - // Pool workers are spawned at the end of initAnalysis(), after mode flags - // are finalised, so each worker captures the correct concrete function type. - - printf("Evaluating mode selection:\n"); - std::string tmp_mode; - std::stringstream modestream{ mode_ }; - while (std::getline(modestream, tmp_mode, '+')) + // parse mode string (e.g. "intra+same+cross") + printf("\nEvaluating mode selection:\n"); + std::string token; + std::stringstream ss{ mode_ }; + while (std::getline(ss, token, '+')) { - if (tmp_mode == std::string("intra")) - { - intra_ = true; - } - else if (tmp_mode == std::string("same")) - { - same_ = true; - } - else if (tmp_mode == std::string("cross")) - { - cross_ = true; - } + if (token == "intra") intra_ = true; + else if (token == "same") same_ = true; + else if (token == "cross") cross_ = true; else { - printf("Wrong mode: %s\nMode must be one from: intra, same, cross. Use + to concatenate more than one, i.e. intra+cross\n", tmp_mode.c_str()); + printf("Wrong mode: %s\nMode must be one from: intra, same, cross. Use + to concatenate more than one, i.e. intra+cross\n", token.c_str()); exit(1); } - printf(" - found %s\n", tmp_mode.c_str()); + printf(" - found %s\n", token.c_str()); } - int mol_id = 0; - int molb_index = 0; - for ( auto i : num_mol_unique_ ) + // build per-molecule lookup tables + int mol_id = 0, molb_index = 0; + for (int n : num_mol_unique_) { natmol2_.push_back(mols_.block(molb_index).end() - mols_.block(molb_index).begin()); - inv_num_mol_unique_.push_back(1. / static_cast(i)); - for ( int j = 0; j < i; j++ ) + inv_num_mol_unique_.push_back(1.f / static_cast(n)); + for (int j = 0; j < n; j++) { mol_id_.push_back(mol_id); - inv_num_mol_.push_back(1. / static_cast(i)); + inv_num_mol_.push_back(1.f / static_cast(n)); } mol_id++; - molb_index += i; + molb_index += n; } - // Precompute per-molecule-type atom activity mask. - // Walk the first molecule of each type and record which atoms to include. + // precompute per-type atom activity mask (skip non-backbone hydrogens) { - int molb = 0; - int mol_first = 0; // global index of the first molecule of each type + int molb = 0, mol_first = 0; atom_active_.resize(natmol2_.size()); for (std::size_t mt = 0; mt < natmol2_.size(); mt++) { @@ -403,317 +286,91 @@ class CMData { int global_atom = mols_.block(mol_first).begin() + a; mtopGetAtomAndResidueName(*mtop_, global_atom, &molb, &atomname, nullptr, nullptr, nullptr); - // Skip non-backbone hydrogens. "H" (AMBER/GROMACS) and "HN" (CHARMM) - // are always treated as backbone H; --bkbn_H adds a third custom name. - { - const std::string aname(atomname); - const bool is_H = (aname[0] == 'H'); - const bool is_bkbn = (aname == "H" || aname == "HN" || - (!bkbn_H_.empty() && aname == bkbn_H_)); - atom_active_[mt][a] = !(is_H && !is_bkbn); - } + const std::string aname(atomname); + const bool is_H = (aname[0] == 'H'); + const bool is_bkbn = (aname == "H" || aname == "HN" || (!bkbn_H_.empty() && aname == bkbn_H_)); + atom_active_[mt][a] = !(is_H && !is_bkbn); } - mol_first += num_mol_unique_[mt]; // advance to first mol of next type + mol_first += num_mol_unique_[mt]; } } printf("Number of different molecules %lu\n", natmol2_.size()); bool check_same = false; - for(std::size_t i=0; i1) check_same = true; + for (std::size_t i = 0; i < natmol2_.size(); i++) + { + printf("mol %zu num %d size %d\n", i, num_mol_unique_[i], natmol2_[i]); + if (num_mol_unique_[i] > 1) check_same = true; } - if(!check_same && same_) same_ = false; - if(natmol2_.size()<2) cross_ = false; - if(nindex_>1 && (same_ || cross_)) + if (!check_same && same_) same_ = false; + if (natmol2_.size() < 2) cross_ = false; + if (nindex_ > 1 && (same_ || cross_)) { printf("\n\n::::::::::::WARNING::::::::::::\nMore than 1 molecule found in the system.\nFix pbc before running cmdata using pbc mol\n"); printf(":::::::::::::::::::::::::::::::\n\n"); } - if (same_) - { - std::cout << ":: activating intermat same calculations" << std::endl; - interm_same_mat_density_.resize(natmol2_.size()); - interm_same_maxcdf_mol_.resize(natmol2_.size()); - } - if (cross_) - { - std::cout << ":: activating intermat cross calculations" << std::endl; - interm_cross_mat_density_.resize((natmol2_.size() * (natmol2_.size() - 1)) / 2); - interm_cross_maxcdf_mol_.resize((natmol2_.size() * (natmol2_.size() - 1)) / 2); - } - if (intra_) - { - std::cout << " :: activating intramat calculations" << std::endl; - intram_mat_density_.resize(natmol2_.size()); - } - - density_bins_.resize(cmdata::indexing::n_bins(cutoff_)); - for (std::size_t i = 0; i < density_bins_.size(); i++) - density_bins_[i] = cutoff_ / static_cast(density_bins_.size()) * static_cast(i) + cutoff_ / static_cast(density_bins_.size() * 2); + if (same_) std::cout << ":: activating intermat same calculations" << std::endl; + if (cross_) std::cout << ":: activating intermat cross calculations" << std::endl; + if (intra_) std::cout << ":: activating intramat calculations" << std::endl; + // set up density bins + n_bins_ = cmdata::indexing::n_bins(cutoff_); + dx_ = cutoff_ / static_cast(n_bins_); + cut_sig_2_ = (cutoff_ + 0.02f) * (cutoff_ + 0.02f); + density_bins_.resize(n_bins_); + for (std::size_t i = 0; i < n_bins_; i++) + density_bins_[i] = (cutoff_ / static_cast(n_bins_)) * (static_cast(i) + 0.5f); + + // allocate density matrices and cross index + const std::size_t n_mol_types = natmol2_.size(); + const std::size_t n_cross = n_mol_types * (n_mol_types - 1) / 2; + if (same_) { interm_same_mat_density_.resize(n_mol_types); interm_same_maxcdf_mol_.resize(n_mol_types); } + if (cross_) { interm_cross_mat_density_.resize(n_cross); interm_cross_maxcdf_mol_.resize(n_cross); } + if (intra_) intram_mat_density_.resize(n_mol_types); + + if (cross_) cross_index_.resize(n_mol_types, std::vector(n_mol_types, 0)); int cross_count = 0; - if (cross_) cross_index_.resize(natmol2_.size(), std::vector(natmol2_.size(), 0)); - for ( std::size_t i = 0; i < natmol2_.size(); i++ ) + for (std::size_t i = 0; i < n_mol_types; i++) { + const auto mat_row = std::vector>(natmol2_[i], std::vector(n_bins_, 0.f)); if (same_) { - interm_same_mat_density_[i].resize(natmol2_[i], std::vector>(natmol2_[i], std::vector(cmdata::indexing::n_bins(cutoff_), 0))); - interm_same_maxcdf_mol_[i].resize(natmol2_[i], std::vector>(natmol2_[i], std::vector(cmdata::indexing::n_bins(cutoff_), 0))); + interm_same_mat_density_[i].assign(natmol2_[i], mat_row); + interm_same_maxcdf_mol_[i].assign(natmol2_[i], mat_row); } - if (intra_) intram_mat_density_[i].resize(natmol2_[i], std::vector>(natmol2_[i], std::vector(cmdata::indexing::n_bins(cutoff_), 0))); - for ( std::size_t j = i + 1; j < natmol2_.size() && cross_; j++ ) + if (intra_) intram_mat_density_[i].assign(natmol2_[i], mat_row); + for (std::size_t j = i + 1; j < n_mol_types && cross_; j++) { - interm_cross_mat_density_[cross_count].resize(natmol2_[i], std::vector>(natmol2_[j], std::vector(cmdata::indexing::n_bins(cutoff_), 0))); - interm_cross_maxcdf_mol_[cross_count].resize(natmol2_[i], std::vector>(natmol2_[j], std::vector(cmdata::indexing::n_bins(cutoff_), 0))); - cross_index_[i][j] = cross_count; - cross_count++; + const auto cross_row = std::vector>(natmol2_[j], std::vector(n_bins_, 0.f)); + interm_cross_mat_density_[cross_count].assign(natmol2_[i], cross_row); + interm_cross_maxcdf_mol_[cross_count].assign(natmol2_[i], cross_row); + cross_index_[i][j] = cross_count++; } } - n_bins_ = cmdata::indexing::n_bins(cutoff_); - dx_ = cutoff_ / static_cast(n_bins_); - - mcut2_ = mol_cutoff_ * mol_cutoff_; - cut_sig_2_ = (cutoff_ + 0.02) * (cutoff_ + 0.02); + // allocate per-frame minimum-distance work buffers xcm_ = (rvec*)malloc(nindex_ * sizeof(rvec)); - - if (same_) frame_same_mat_.resize(natmol2_.size()); - if (intra_ || same_) frame_same_mutex_.resize(natmol2_.size()); - if (cross_) frame_cross_mat_.resize(cross_index_.size()); - if (cross_) frame_cross_mutex_.resize(cross_index_.size()); - for ( std::size_t i = 0; i < natmol2_.size(); i++ ) + if (same_) { - if (same_) frame_same_mat_[i].resize(natmol2_[i] * natmol2_[i] * num_mol_unique_[i], 0); - if (intra_ || same_) frame_same_mutex_[i] = std::vector(natmol2_[i] * natmol2_[i]); - for ( std::size_t j = i+1; j < natmol2_.size() && cross_; j++ ) - { - frame_cross_mat_[cross_index_[i][j]].resize(natmol2_[i] * natmol2_[j] * num_mol_unique_[i] * num_mol_unique_[j], 0); - frame_cross_mutex_[cross_index_[i][j]] = std::vector(natmol2_[i] * natmol2_[j]); - } + frame_same_mat_.resize(n_mol_types); + for (std::size_t i = 0; i < n_mol_types; i++) + frame_same_mat_[i].resize(natmol2_[i] * natmol2_[i] * num_mol_unique_[i], 0.f); + } + if (cross_) + { + frame_cross_mat_.resize(n_cross); + for (std::size_t i = 0; i < n_mol_types; i++) + for (std::size_t j = i + 1; j < n_mol_types; j++) + frame_cross_mat_[cross_index_[i][j]].resize(natmol2_[i] * natmol2_[j] * num_mol_unique_[i] * num_mol_unique_[j], 0.f); } - if (weights_path_ != "") + if (!weights_path_.empty()) { printf("Weights file provided. Reading weights from %s\n", weights_path_.c_str()); weights_ = cmdata::io::read_weights_file(weights_path_); - printf("Found %li frame weights in file\n", weights_.size()); - float w_sum = std::accumulate(std::begin(weights_), std::end(weights_), 0.0, std::plus<>()); + printf("Found %zu frame weights in file\n", weights_.size()); + float w_sum = std::accumulate(weights_.begin(), weights_.end(), 0.f); printf("Sum of weights amounts to %lf\n", w_sum); - weights_sum_ = 0.; - } - - std::cout << "Calculating threading indices" << std::endl; - /* calculate the mindist accumulation indices */ - std::size_t num_ops_same = 0; - for ( std::size_t im = 0; im < natmol2_.size(); im++ ) num_ops_same += num_mol_unique_[im] * ( natmol2_[im] * ( natmol2_[im] + 1 ) ) / 2; - int n_per_thread_same = (same_) ? num_ops_same / num_threads_ : 0; - int n_threads_same_uneven = (same_) ? num_ops_same % num_threads_ : 0; - std::size_t start_mti_same = 0, start_im_same = 0, end_mti_same = 1, end_im_same = 1; - std::size_t start_i_same = 0, start_j_same = 0, end_i_same = 0, end_j_same = 0; - int num_ops_cross = 0; - for ( std::size_t im = 0; im < natmol2_.size(); im++ ) - { - for ( std::size_t jm = im + 1; jm < natmol2_.size(); jm++ ) - { - num_ops_cross += num_mol_unique_[im] * natmol2_[im] * num_mol_unique_[jm] * natmol2_[jm]; - } - } - int n_per_thread_cross = (cross_) ? num_ops_cross / num_threads_ : 0; - int n_threads_cross_uneven = (cross_) ? num_ops_cross % num_threads_ : 0; - - std::size_t start_mti_cross = 0, start_mtj_cross = 1, start_im_cross = 0, start_jm_cross = 0, start_i_cross = 0, start_j_cross = 0; - std::size_t end_mti_cross = 1, end_mtj_cross = 2, end_im_cross = 1, end_jm_cross = 1, end_i_cross = 0, end_j_cross = 0; - - for ( int tid = 0; tid < num_threads_; tid++ ) - { - /* calculate same indices */ - int n_loop_operations_same = n_per_thread_same + (tid < n_threads_same_uneven ? 1 : 0); - long int n_loop_operations_total_same = n_loop_operations_same; - while ( natmol2_[end_mti_same - 1] - static_cast(end_j_same) <= n_loop_operations_same ) - { - int sub_same = natmol2_[end_mti_same - 1] - static_cast(end_j_same); - n_loop_operations_same -= sub_same; - end_i_same++; - end_j_same = end_i_same; - if (static_cast(end_j_same) == natmol2_[end_mti_same - 1]) - { - end_im_same++; - end_i_same = 0; - end_j_same = 0; - } - if (static_cast(end_im_same) -1 == num_mol_unique_[end_mti_same - 1]) - { - end_mti_same++; - end_im_same = 1; - end_i_same = 0; - end_j_same = 0; - } - if (n_loop_operations_same == 0) break; - } - end_j_same += n_loop_operations_same; - /* calculate cross indices */ - int n_loop_operations_total_cross = n_per_thread_cross + ( tid < n_threads_cross_uneven ? 1 : 0 ); - if (natmol2_.size() > 1) - { - int n_loop_operations_cross = n_loop_operations_total_cross; - while ( natmol2_[end_mti_cross-1] * natmol2_[end_mtj_cross-1] - (natmol2_[end_mtj_cross-1] * static_cast(end_i_cross) + static_cast(end_j_cross)) <= n_loop_operations_cross ) - { - int sub_cross = natmol2_[end_mti_cross-1] * natmol2_[end_mtj_cross-1] - (natmol2_[end_mtj_cross-1] * static_cast(end_i_cross) + static_cast(end_j_cross)); - n_loop_operations_cross -= sub_cross; - - end_jm_cross++; - end_i_cross = 0; - end_j_cross = 0; - - // case jm is above max - if (end_jm_cross > num_mol_unique_[end_mtj_cross - 1]) - { - // end_im_cross++; - end_mtj_cross++; - end_jm_cross = 1; - end_i_cross = 0; - end_j_cross = 0; - } - if (end_mtj_cross > natmol2_.size()) - { - end_im_cross++; - end_mtj_cross = end_mti_cross + 1; - end_jm_cross = 1; - end_i_cross = 0; - end_j_cross = 0; - } - if (end_im_cross > num_mol_unique_[end_mti_cross - 1]) - { - end_mti_cross++; - end_mtj_cross = end_mti_cross + 1; - end_im_cross = 1; - end_jm_cross = 1; - end_i_cross = 0; - end_j_cross = 0; - } - if (end_mti_cross == natmol2_.size()) break; - if (n_loop_operations_cross == 0) break; - } - - // calculate overhangs and add them - if (end_mti_cross < natmol2_.size()) - { - end_i_cross += n_loop_operations_cross / natmol2_[end_mtj_cross-1]; - end_j_cross += n_loop_operations_cross % natmol2_[end_mtj_cross-1]; - end_i_cross += end_j_cross / natmol2_[end_mtj_cross-1]; - end_j_cross %= natmol2_[end_mtj_cross-1]; - } - } - - if (same_) - { - same_thread_indices_.push_back({ - start_mti_same, start_im_same, start_i_same, start_j_same, end_mti_same, - end_im_same, end_i_same, end_j_same, n_loop_operations_total_same - }); - } - else - { - same_thread_indices_.push_back({ - 0, 0, 0, 0, 0, 0, 0, 0, 0 - }); - } - if (cross_ && natmol2_.size() > 1) - { - cross_thread_indices_.push_back({ - start_mti_cross, start_mtj_cross, start_im_cross, start_jm_cross, start_i_cross, - start_j_cross, end_mti_cross, end_mtj_cross, end_im_cross, end_jm_cross, end_i_cross, - end_j_cross, n_loop_operations_total_cross - }); - } - else - { - cross_thread_indices_.push_back({ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }); - } - - /* set new starts */ - start_mti_same = end_mti_same - 1; - start_im_same = end_im_same - 1; - start_i_same = end_i_same; - start_j_same = end_j_same; - - start_mti_cross = end_mti_cross - 1; - start_mtj_cross = end_mtj_cross - 1; - start_im_cross = end_im_cross - 1; - start_jm_cross = end_jm_cross - 1; - start_i_cross = end_i_cross; - start_j_cross = end_j_cross; - } - - // Spawn persistent thread-pool workers now that mode flags (intra_/same_/cross_) - // are finalised. Each branch captures the concrete function type (function pointer - // or stateless lambda) so molecule_routine can be fully inlined. - { - // No-op callables used for disabled modes — stateless lambdas, zero overhead. - auto no_intra = [](auto&&...) {}; - auto no_same = [](auto&&...) {}; - auto no_cross = [](auto&&...) {}; - - // do_spawn creates the pool workers capturing concrete f_intra/f_same/f_cross. - auto do_spawn = [this](auto f_intra, auto f_same, auto f_cross) - { - pool_.num_workers = num_mol_threads_; - pool_.total_mol = nindex_; - pool_.workers.reserve(num_mol_threads_); - for (int t = 0; t < num_mol_threads_; t++) - { - pool_.workers.emplace_back([this, f_intra, f_same, f_cross]() - { - int my_gen = 0; - while (true) - { - { - std::unique_lock lk(pool_.mtx); - pool_.cv_work.wait(lk, [this, my_gen]{ return pool_.generation != my_gen || pool_.stop; }); - if (pool_.stop) return; - my_gen = pool_.generation; - } - while (true) - { - int i = pool_.next_mol.fetch_add(1, std::memory_order_relaxed); - if (i >= pool_.total_mol) break; - molecule_routine(i, nindex_, pool_.pbc, pool_.x, inv_num_mol_, - cut_sig_2_, natmol2_, num_mol_unique_, mol_id_, cross_index_, - density_bins_, mcut2_, xcm_, mols_, atom_active_, - frame_same_mat_, frame_same_mutex_, - intram_mat_density_, interm_same_mat_density_, frame_cross_mat_, - frame_cross_mutex_, interm_cross_mat_density_, - f_intra, f_same, f_cross, pool_.weight); - } - if (pool_.done_count.fetch_add(1, std::memory_order_acq_rel) + 1 == pool_.num_workers) - { - std::unique_lock lk(pool_.mtx); - pool_.cv_done.notify_one(); - } - } - }); - } - }; - - // Select the right template instantiation based on the three mode flags. - using I = decltype(cmdata::density::intra_mol_routine)*; - using S = decltype(cmdata::density::inter_mol_same_routine)*; - using C = decltype(cmdata::density::inter_mol_cross_routine)*; - I fi = cmdata::density::intra_mol_routine; - S fs = cmdata::density::inter_mol_same_routine; - C fc = cmdata::density::inter_mol_cross_routine; - - if ( intra_ && same_ && cross_) do_spawn(fi, fs, fc ); - else if ( intra_ && same_ && !cross_) do_spawn(fi, fs, no_cross); - else if ( intra_ && !same_ && cross_) do_spawn(fi, no_same, fc ); - else if ( intra_ && !same_ && !cross_) do_spawn(fi, no_same, no_cross); - else if (!intra_ && same_ && cross_) do_spawn(no_intra, fs, fc ); - else if (!intra_ && same_ && !cross_) do_spawn(no_intra, fs, no_cross); - else if (!intra_ && !same_ && cross_) do_spawn(no_intra, no_same, fc ); - else do_spawn(no_intra, no_same, no_cross); } printf("Finished preprocessing.\nStarting frame-by-frame analysis.\n"); @@ -723,102 +380,89 @@ class CMData { std::cout << "Running frame-by-frame analysis" << std::endl; - int frnr = 0; - float progress = 0.0, new_progress = 0.0; - cmdata::io::print_progress_bar(progress); - while (frame_->read_next_frame(trj_, no_pbc_, pbcType_, pbc_) == exdrOK) + // No-op callables for disabled modes — stateless lambdas, zero overhead. + auto no_op = [](auto&&...) {}; + + using I = decltype(cmdata::density::intra_mol_routine)*; + using S = decltype(cmdata::density::inter_mol_same_routine)*; + using C = decltype(cmdata::density::inter_mol_cross_routine)*; + I fi = cmdata::density::intra_mol_routine; + S fs = cmdata::density::inter_mol_same_routine; + C fc = cmdata::density::inter_mol_cross_routine; + + // do_run instantiates molecule_routine with the correct concrete function types + // so the compiler can inline all three paths. + auto do_run = [this](auto f_intra, auto f_same, auto f_cross) { - new_progress = static_cast(frnr) / static_cast(frame_->nframe); - if (new_progress - progress > 0.01) + int frnr = 0; + float progress = 0.f; + cmdata::io::print_progress_bar(progress); + while (frame_->read_next_frame(trj_, no_pbc_, pbcType_, pbc_) == exdrOK) { - progress = new_progress; - cmdata::io::print_progress_bar(progress); - } - if ((frame_->time >= t_begin_ && (t_end_ < 0 || frame_->time <= t_end_ )) && // within time borders - ( dt_ == 0 || std::fmod(frame_->time, dt_) == 0) && (nskip_ == 0 || std::fmod(frnr, nskip_) == 0)) // skip frames - { - float weight = 1.0; - if (!weights_.empty()) - { - weight = weights_[frnr]; - weights_sum_ += weight; - } - // TODO: replace magic sentinel 100.f (nm) with a named constant, e.g.: - // static constexpr float LARGE_DIST = 100.f; - /* resetting the per-frame minimum-distance matrix to a large sentinel value */ - for ( std::size_t i = 0; i < frame_same_mat_.size(); i++ ) - { - #pragma omp parallel for num_threads(std::min(num_threads_, static_cast(frame_same_mat_[i].size()))) - for ( std::size_t j = 0; j < frame_same_mat_[i].size(); j++ ) frame_same_mat_[i][j] = 100.f; - } - for ( std::size_t i = 0; i < frame_cross_mat_.size(); i++ ) - { - #pragma omp parallel for num_threads(std::min(num_threads_, static_cast(frame_cross_mat_[i].size()))) - for ( std::size_t j = 0; j < frame_cross_mat_[i].size(); j++ ) frame_cross_mat_[i][j] = 100.f; - } - #pragma omp parallel for num_threads(std::min(num_threads_, nindex_)) - for (int i = 0; i < nindex_; i++) + float new_progress = static_cast(frnr) / static_cast(frame_->nframe); + if (new_progress - progress > 0.01f) { progress = new_progress; cmdata::io::print_progress_bar(progress); } + + if ((frame_->time >= t_begin_ && (t_end_ < 0 || frame_->time <= t_end_)) && + (dt_ == 0 || std::fmod(frame_->time, dt_) == 0) && + (nskip_ == 0 || std::fmod(frnr, nskip_) == 0)) { - clear_rvec(xcm_[i]); - float tm = 0.; - for (int ii = mols_.block(i).begin(); ii < mols_.block(i).end(); ii++) + float weight = 1.f; + if (!weights_.empty()) { weight = weights_[frnr]; weights_sum_ += weight; } + + // reset per-frame minimum-distance matrices + for (auto &v : frame_same_mat_) std::fill(v.begin(), v.end(), LARGE_DIST); + for (auto &v : frame_cross_mat_) std::fill(v.begin(), v.end(), LARGE_DIST); + + // compute molecule centres of mass + for (int i = 0; i < nindex_; i++) { - for (int m = 0; (m < DIM); m++) + clear_rvec(xcm_[i]); + float tm = 0.f; + for (int ii = mols_.block(i).begin(); ii < mols_.block(i).end(); ii++) { - xcm_[i][m] += frame_->x[ii][m]; + for (int m = 0; m < DIM; m++) xcm_[i][m] += frame_->x[ii][m]; + tm += 1.f; } - tm += 1.0; + for (int m = 0; m < DIM; m++) xcm_[i][m] /= tm; } - for (int m = 0; (m < DIM); m++) - { - xcm_[i][m] /= tm; - } - } - /* dispatch frame to persistent thread pool */ - { - std::unique_lock lk(pool_.mtx); - pool_.pbc = pbc_; - pool_.x = frame_->x; - pool_.weight = weight; - pool_.next_mol.store(0, std::memory_order_relaxed); - pool_.done_count.store(0, std::memory_order_relaxed); - pool_.generation++; // wakes workers waiting on their old generation - pool_.cv_work.notify_all(); - pool_.cv_done.wait(lk, [this]{ return pool_.done_count == pool_.num_workers; }); - } - /* calculate the mindist accumulation indices */ - for ( int tid = 0; tid < num_threads_; tid++ ) - { - threads_[tid] = std::thread( - cmdata::mindist::mindist_kernel, std::cref(same_thread_indices_[tid]), std::cref(cross_thread_indices_[tid]), - weight, std::cref(natmol2_), std::cref(density_bins_), std::cref(num_mol_unique_), - std::cref(frame_same_mat_), std::ref(frame_same_mutex_), std::ref(interm_same_maxcdf_mol_), - std::cref(cross_index_), std::cref(frame_cross_mat_), std::ref(frame_cross_mutex_), std::ref(interm_cross_maxcdf_mol_) - ); + + // per-molecule pairwise distance computation + #pragma omp parallel for schedule(dynamic) + for (int i = 0; i < nindex_; i++) + molecule_routine(i, pbc_, frame_->x, f_intra, f_same, f_cross, weight); + + // accumulate per-frame minimum distances into KDE histograms + if (same_) + cmdata::mindist::mindist_same(density_bins_, num_mol_unique_, natmol2_, + frame_same_mat_, interm_same_maxcdf_mol_, weight); + if (cross_) + cmdata::mindist::mindist_cross(natmol2_, cross_index_, density_bins_, num_mol_unique_, + frame_cross_mat_, interm_cross_maxcdf_mol_, weight); + + ++n_x_; } - for ( auto &thread : threads_ ) thread.join(); - ++n_x_; + ++frnr; } - ++frnr; - } - - cmdata::io::print_progress_bar(1.0); + cmdata::io::print_progress_bar(1.f); + }; + + if ( intra_ && same_ && cross_) do_run(fi, fs, fc ); + else if ( intra_ && same_ && !cross_) do_run(fi, fs, no_op ); + else if ( intra_ && !same_ && cross_) do_run(fi, no_op, fc ); + else if ( intra_ && !same_ && !cross_) do_run(fi, no_op, no_op ); + else if (!intra_ && same_ && cross_) do_run(no_op, fs, fc ); + else if (!intra_ && same_ && !cross_) do_run(no_op, fs, no_op ); + else if (!intra_ && !same_ && cross_) do_run(no_op, no_op, fc ); + else do_run(no_op, no_op, no_op ); } void process_data() { - std::cout << "\nFinished frame-by-frame analysis\n"; + std::cout << "Finished frame-by-frame analysis\n"; std::cout << "Analyzed " << n_x_ << " frames\n"; std::cout << "Normalizing data... " << std::endl; - // normalisations - float norm = ( weights_.empty() ) ? 1. / n_x_ : 1. / weights_sum_; - - using ftype_norm = cmdata::ftypes::function_traits; - std::function f_empty = cmdata::ftypes::do_nothing(); - std::function normalize_intra = (intra_) ? cmdata::density::normalize_histo : f_empty; - std::function normalize_inter_same = (same_) ? cmdata::density::normalize_histo : f_empty; - std::function normalize_inter_cross = (cross_) ? cmdata::density::normalize_histo : f_empty; + float norm = weights_.empty() ? 1.f / n_x_ : 1.f / weights_sum_; for (std::size_t i = 0; i < natmol2_.size(); i++) { @@ -826,119 +470,92 @@ class CMData { for (int jj = ii; jj < natmol2_[i]; jj++) { - float inv_num_mol_same = inv_num_mol_unique_[i]; - normalize_inter_same(i, ii, jj, norm, inv_num_mol_same, interm_same_maxcdf_mol_); - normalize_inter_same(i, ii, jj, norm, 1.0, interm_same_mat_density_); - normalize_intra(i, ii, jj, norm, 1.0, intram_mat_density_); - - float sum = 0.0; - for ( std::size_t k = (same_) ? 0 : cmdata::indexing::n_bins(cutoff_); k < cmdata::indexing::n_bins(cutoff_); k++ ) + if (same_) + { + cmdata::density::normalize_histo(i, ii, jj, norm, inv_num_mol_unique_[i], interm_same_maxcdf_mol_); + cmdata::density::normalize_histo(i, ii, jj, norm, 1.f, interm_same_mat_density_); + float sum = 0.f; + for (std::size_t k = 0; k < n_bins_; k++) + { + sum += dx_ * interm_same_maxcdf_mol_[i][ii][jj][k]; + if (sum > 1.f) sum = 1.f; + interm_same_maxcdf_mol_[i][ii][jj][k] = sum; + } + interm_same_mat_density_[i][jj][ii] = interm_same_mat_density_[i][ii][jj]; + interm_same_maxcdf_mol_[i][jj][ii] = interm_same_maxcdf_mol_[i][ii][jj]; + } + if (intra_) { - sum+= dx_ * interm_same_maxcdf_mol_[i][ii][jj][k]; - if (sum > 1.0) sum=1.0; - interm_same_maxcdf_mol_[i][ii][jj][k] = sum; + cmdata::density::normalize_histo(i, ii, jj, norm, 1.f, intram_mat_density_); + intram_mat_density_[i][jj][ii] = intram_mat_density_[i][ii][jj]; } - if (same_) interm_same_mat_density_[i][jj][ii] = interm_same_mat_density_[i][ii][jj]; - if (same_) interm_same_maxcdf_mol_[i][jj][ii] = interm_same_maxcdf_mol_[i][ii][jj]; - if (intra_) intram_mat_density_[i][jj][ii] = intram_mat_density_[i][ii][jj]; } } - for (std::size_t j = i + 1; j < natmol2_.size() && cross_; j++) + if (cross_) { - for (int ii = 0; ii < natmol2_[i]; ii++) + for (std::size_t j = i + 1; j < natmol2_.size(); j++) { - for (int jj = 0; jj < natmol2_[j]; jj++) + for (int ii = 0; ii < natmol2_[i]; ii++) { - float inv_num_mol_cross = inv_num_mol_unique_[i]; - normalize_inter_cross(cross_index_[i][j], ii, jj, norm, 1.0, interm_cross_mat_density_); - normalize_inter_cross(cross_index_[i][j], ii, jj, norm, inv_num_mol_cross, interm_cross_maxcdf_mol_); - - float sum = 0.0; - for ( std::size_t k = (cross_) ? 0 : cmdata::indexing::n_bins(cutoff_); k < cmdata::indexing::n_bins(cutoff_); k++ ) + for (int jj = 0; jj < natmol2_[j]; jj++) { - sum += dx_ * interm_cross_maxcdf_mol_[cross_index_[i][j]][ii][jj][k]; - if (sum > 1.0) sum = 1.0; - interm_cross_maxcdf_mol_[cross_index_[i][j]][ii][jj][k] = sum; + cmdata::density::normalize_histo(cross_index_[i][j], ii, jj, norm, 1.f, interm_cross_mat_density_); + cmdata::density::normalize_histo(cross_index_[i][j], ii, jj, norm, inv_num_mol_unique_[i], interm_cross_maxcdf_mol_); + float sum = 0.f; + for (std::size_t k = 0; k < n_bins_; k++) + { + sum += dx_ * interm_cross_maxcdf_mol_[cross_index_[i][j]][ii][jj][k]; + if (sum > 1.f) sum = 1.f; + interm_cross_maxcdf_mol_[cross_index_[i][j]][ii][jj][k] = sum; + } } } } } } } - - void write_output( const std::string &output_prefix ) + + void write_output(const std::string &output_prefix) { std::cout << "Writing data... " << std::endl; - using ftype_write_intra = cmdata::ftypes::function_traits; - using ftype_write_inter_same = cmdata::ftypes::function_traits; - using ftype_write_inter_cross = cmdata::ftypes::function_traits; - std::function write_intra = cmdata::ftypes::do_nothing(); - std::function write_inter_same = cmdata::ftypes::do_nothing(); - std::function write_inter_cross = cmdata::ftypes::do_nothing(); - - if (intra_) - { - #ifdef USE_HDF5 - if(h5_) write_intra = cmdata::io::f_write_intra_HDF5; - else write_intra = cmdata::io::f_write_intra; - #else - write_intra = cmdata::io::f_write_intra; - #endif - } - if (same_) - { - #ifdef USE_HDF5 - if(h5_) write_inter_same = cmdata::io::f_write_inter_same_HDF5; - else write_inter_same = cmdata::io::f_write_inter_same; - #else - write_inter_same = cmdata::io::f_write_inter_same; - #endif - } - if (cross_) + + // select write functions (text or HDF5) once up front + auto* wf_intra = &cmdata::io::f_write_intra; + auto* wf_same = &cmdata::io::f_write_inter_same; + auto* wf_cross = &cmdata::io::f_write_inter_cross; + #ifdef USE_HDF5 + if (h5_) { - #ifdef USE_HDF5 - if(h5_) write_inter_cross = cmdata::io::f_write_inter_cross_HDF5; - else write_inter_cross = cmdata::io::f_write_inter_cross; - #else - write_inter_cross = cmdata::io::f_write_inter_cross; - #endif + wf_intra = &cmdata::io::f_write_intra_HDF5; + wf_same = &cmdata::io::f_write_inter_same_HDF5; + wf_cross = &cmdata::io::f_write_inter_cross_HDF5; } + #endif for (std::size_t i = 0; i < natmol2_.size(); i++) { - std::cout << "Writing data for molecule " << i << "..." << std::endl; - cmdata::io::print_progress_bar(0.0); - float progress = 0.0, new_progress = 0.0; - + std::cout << "Molecule " << i << ":" << std::endl; + float progress = 0.f; + cmdata::io::print_progress_bar(0.f); for (int ii = 0; ii < natmol2_[i]; ii++) { - new_progress = static_cast(ii) / static_cast(natmol2_[i]); - if (new_progress - progress > 0.01) - { - progress = new_progress; - cmdata::io::print_progress_bar(progress); - } - write_intra(output_prefix, i, ii, density_bins_, natmol2_, intram_mat_density_); - write_inter_same(output_prefix, i, ii, density_bins_, natmol2_, interm_same_mat_density_, interm_same_maxcdf_mol_); + float new_progress = static_cast(ii + 1) / static_cast(natmol2_[i]); + if (new_progress - progress > 0.01f) { progress = new_progress; cmdata::io::print_progress_bar(progress); } + if (intra_) wf_intra(output_prefix, i, ii, density_bins_, natmol2_, intram_mat_density_); + if (same_) wf_same (output_prefix, i, ii, density_bins_, natmol2_, interm_same_mat_density_, interm_same_maxcdf_mol_); } - if(cross_) + if (cross_) { for (std::size_t j = i + 1; j < natmol2_.size(); j++) - { for (int ii = 0; ii < natmol2_[i]; ii++) - { - write_inter_cross(output_prefix, i, j, ii, density_bins_, natmol2_, cross_index_, interm_cross_mat_density_, interm_cross_maxcdf_mol_); - } - } + wf_cross(output_prefix, i, j, ii, density_bins_, natmol2_, cross_index_, interm_cross_mat_density_, interm_cross_maxcdf_mol_); } + cmdata::io::print_progress_bar(1.f); // closes the bar line with \n } - - cmdata::io::print_progress_bar(1.0); - std::cout << "\nFinished!" << std::endl; + std::cout << "Finished!" << std::endl; } }; - } // namespace cmdata -#endif // _CM_DATA_HPP +#endif // _CMDATA_CMDATA_HPP diff --git a/tools/cmdata/src/cmdata/density.hpp b/tools/cmdata/src/cmdata/density.hpp index e7dce036..e4c6fdd0 100644 --- a/tools/cmdata/src/cmdata/density.hpp +++ b/tools/cmdata/src/cmdata/density.hpp @@ -1,8 +1,8 @@ #ifndef _CMDATA_DENSITY_HPP #define _CMDATA_DENSITY_HPP +#include #include -#include #include #include "indexing.hpp" @@ -40,54 +40,46 @@ void kernel_density_estimator(std::vector::iterator x, const std::vector< for (int i = from; i < to; i++) { float f = (mu - bins[i]) / h; - float kernel = std::exp(-0.5f * f * f); - x[i] += scale * (kernel - KDE_TAIL_SHIFT); + float contrib = scale * (std::exp(-0.5f * f * f) - KDE_TAIL_SHIFT); + #pragma omp atomic + x[i] += contrib; } } -void intra_mol_routine( +void intra_mol_routine( int i, std::size_t a_i, std::size_t a_j, float dx2, float weight, const std::vector &mol_id_, const std::vector &natmol2_, const std::vector &density_bins_, - const std::vector &inv_num_mol_, std::vector> &frame_same_mutex_, + const std::vector &inv_num_mol_, std::vector>>> &intram_mat_density_ ) { - std::size_t same_mutex_index = cmdata::indexing::mutex_access(mol_id_[i], a_i, a_j, natmol2_); - std::unique_lock lock(frame_same_mutex_[mol_id_[i]][same_mutex_index]); kernel_density_estimator(std::begin(intram_mat_density_[mol_id_[i]][a_i][a_j]), density_bins_, std::sqrt(dx2), weight*inv_num_mol_[i]); - lock.unlock(); } void inter_mol_same_routine( - int i, std::size_t mol_i, std::size_t a_i, std::size_t a_j, float dx2, float weight, + int i, std::size_t mol_i, std::size_t a_i, std::size_t a_j, float dx2, float weight, const std::vector &mol_id_, const std::vector &natmol2_, const std::vector &density_bins_, - std::vector> &frame_same_mutex_, std::vector> &frame_same_mat_, + std::vector> &frame_same_mat_, std::vector>>> &interm_same_mat_density_ ) { float dist = std::sqrt(dx2); std::size_t same_access_index = cmdata::indexing::offset_same(mol_id_[i], mol_i, a_i, a_j, natmol2_); - std::size_t same_mutex_index = cmdata::indexing::mutex_access(mol_id_[i], a_i, a_j, natmol2_); - std::unique_lock lock(frame_same_mutex_[mol_id_[i]][same_mutex_index]); kernel_density_estimator(std::begin(interm_same_mat_density_[mol_id_[i]][a_i][a_j]), density_bins_, dist, weight); frame_same_mat_[mol_id_[i]][same_access_index] = std::min(dist, frame_same_mat_[mol_id_[i]][same_access_index]); - lock.unlock(); } void inter_mol_cross_routine( int i, int j, std::size_t mol_i, std::size_t mol_j, std::size_t a_i, std::size_t a_j, float dx2, float weight, const std::vector &mol_id_, const std::vector &natmol2_, const std::vector> &cross_index_, - const std::vector &density_bins_, const std::vector &num_mol_unique, std::vector> &frame_cross_mutex_, + const std::vector &density_bins_, const std::vector &num_mol_unique, std::vector> &frame_cross_mat_, std::vector>>> &interm_cross_mat_density_ ) { float dist = std::sqrt(dx2); std::size_t cross_access_index = cmdata::indexing::offset_cross(mol_id_[i], mol_id_[j], mol_i, mol_j, a_i, a_j, natmol2_, num_mol_unique[mol_id_[j]]); - std::size_t cross_mutex_index = cmdata::indexing::mutex_access(mol_id_[j], a_i, a_j, natmol2_); - std::unique_lock lock(frame_cross_mutex_[cross_index_[mol_id_[i]][mol_id_[j]]][cross_mutex_index]); kernel_density_estimator(std::begin(interm_cross_mat_density_[cross_index_[mol_id_[i]][mol_id_[j]]][a_i][a_j]), density_bins_, dist, weight); frame_cross_mat_[cross_index_[mol_id_[i]][mol_id_[j]]][cross_access_index] = std::min(dist, frame_cross_mat_[cross_index_[mol_id_[i]][mol_id_[j]]][cross_access_index]); - lock.unlock(); } void normalize_histo( @@ -96,7 +88,7 @@ void normalize_histo( ) { std::transform( - std::begin(data[i][ii][jj]), std::end(data[i][ii][jj]), std::begin(data[i][ii][jj]), + std::begin(data[i][ii][jj]), std::end(data[i][ii][jj]), std::begin(data[i][ii][jj]), [norm, inv_num_mol_same]( auto &c ){ return c * norm * inv_num_mol_same; } ); } diff --git a/tools/cmdata/src/cmdata/function_types.hpp b/tools/cmdata/src/cmdata/function_types.hpp deleted file mode 100644 index 8ac48766..00000000 --- a/tools/cmdata/src/cmdata/function_types.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _CMDATA_FUNCTION_TYPES_HPP -#define _CMDATA_FUNCTION_TYPES_HPP - -namespace cmdata::ftypes -{ - template - struct function_traits; - - template - struct function_traits { - using return_type = Ret; - using args_tuple = std::tuple; - // Define a type for the function signature - using signature = Ret(Args...); - }; - - // does nothing while taking the same arguments as the function - template - auto do_nothing() { - return [](auto&&... args) -> typename function_traits::return_type {}; - } -} - -#endif // _CMDATA_FUNCTION_TYPES_HPP \ No newline at end of file diff --git a/tools/cmdata/src/cmdata/indexing.hpp b/tools/cmdata/src/cmdata/indexing.hpp index 26fbac15..4ffc3087 100644 --- a/tools/cmdata/src/cmdata/indexing.hpp +++ b/tools/cmdata/src/cmdata/indexing.hpp @@ -11,11 +11,6 @@ int n_bins(const float cut, const float factor = 4.0) return cut / (0.01 / factor); } -std::size_t mutex_access( std::size_t i, std::size_t a_i, std::size_t a_j, const std::vector &natmol2 ) -{ - return a_i * natmol2[i] + a_j; -} - static std::size_t offset_same( std::size_t i, std::size_t mol_i, std::size_t a_i, std::size_t a_j, const std::vector &natmol2 ) { return mol_i * (natmol2[i] * natmol2[i]) + a_i * natmol2[i] + a_j; @@ -29,34 +24,6 @@ static std::size_t offset_cross( return mol_i * (num_mol_j * natmol2[i] * natmol2[j]) + mol_j * (natmol2[i] * natmol2[j]) + a_i * natmol2[j] + a_j; } -struct SameThreadIndices { - std::size_t start_mti_same; - std::size_t start_im_same; - std::size_t start_i_same; - std::size_t start_j_same; - std::size_t end_mti_same; - std::size_t end_im_same; - std::size_t end_i_same; - std::size_t end_j_same; - long int n_loop_operations_same; -}; - -struct CrossThreadIndices { - std::size_t start_mti_cross; - std::size_t start_mtj_cross; - std::size_t start_im_cross; - std::size_t start_jm_cross; - std::size_t start_i_cross; - std::size_t start_j_cross; - std::size_t end_mti_cross; - std::size_t end_mtj_cross; - std::size_t end_im_cross; - std::size_t end_jm_cross; - std::size_t end_i_cross; - std::size_t end_j_cross; - int n_loop_operations_cross; -}; - } // namespace cmdata::indexing #endif // _CMDATA_INDEXING_HPP diff --git a/tools/cmdata/src/cmdata/io.hpp b/tools/cmdata/src/cmdata/io.hpp index 8585b3b3..893fb831 100644 --- a/tools/cmdata/src/cmdata/io.hpp +++ b/tools/cmdata/src/cmdata/io.hpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include #define COUT_FLOAT_PREC std::fixed << std::setprecision(5) @@ -211,7 +213,7 @@ void f_write_intra_HDF5(const std::string &output_prefix, for (size_t k = 0; k < density_bins.size(); ++k) { flat_data.push_back(density_bins[k]); // Then iterate over jj dimension - for (size_t jj = 0; jj < natmol2[i]; ++jj) { + for (size_t jj = 0; jj < static_cast(natmol2[i]); ++jj) { flat_data.push_back(intram_mat_density[i][ii][jj][k]); } } @@ -279,7 +281,7 @@ void f_write_inter_same_HDF5(const std::string &output_prefix, flat_data.push_back(density_bins[k]); flat_data_c.push_back(density_bins[k]); // Then iterate over jj dimension - for (size_t jj = 0; jj < natmol2[i]; ++jj) { + for (size_t jj = 0; jj < static_cast(natmol2[i]); ++jj) { flat_data.push_back(interm_same_mat_density[i][ii][jj][k]); flat_data_c.push_back(interm_same_maxcdf_mol[i][ii][jj][k]); } @@ -355,7 +357,7 @@ void f_write_inter_cross_HDF5(const std::string &output_prefix, flat_data.push_back(density_bins[k]); flat_data_c.push_back(density_bins[k]); // Then iterate over jj dimension - for (size_t jj = 0; jj < natmol2[j]; ++jj) { + for (size_t jj = 0; jj < static_cast(natmol2[j]); ++jj) { flat_data.push_back(interm_cross_mat_density[cross_index[i][j]][ii][jj][k]); flat_data_c.push_back(interm_cross_maxcdf_mol[cross_index[i][j]][ii][jj][k]); } @@ -449,14 +451,51 @@ std::vector read_selection( const std::string &path, const std::string &se */ void print_progress_bar(float percentage) { - constexpr std::size_t PROGRESS_BAR_LENGTH = 60; - constexpr char PROGRESS_BAR[] = "############################################################"; - - int val = (int) (percentage * 100); - int lpad = (int) (percentage * PROGRESS_BAR_LENGTH); - int rpad = PROGRESS_BAR_LENGTH - lpad; - - printf("\r%3d%% [%.*s%*s]", val, lpad, PROGRESS_BAR, rpad, ""); + static const auto t0 = std::chrono::steady_clock::now(); + constexpr int BAR_WIDTH = 50; + + const bool tty = isatty(fileno(stdout)); + const int pct = static_cast(percentage * 100.f); + const int filled = static_cast(percentage * BAR_WIDTH); + + auto elapsed_s = std::chrono::duration( + std::chrono::steady_clock::now() - t0).count(); + + // Build time suffix: elapsed + ETA once we have some progress + char time_buf[32]; + if (percentage <= 0.f || elapsed_s < 0.5) + { + snprintf(time_buf, sizeof(time_buf), " %4.0fs", elapsed_s); + } + else if (percentage >= 1.f) + { + snprintf(time_buf, sizeof(time_buf), " %4.0fs", elapsed_s); + } + else + { + double eta = elapsed_s * (1.0 - percentage) / percentage; + snprintf(time_buf, sizeof(time_buf), " %4.0fs ETA %4.0fs", elapsed_s, eta); + } + + if (tty) + { + // Coloured bar: green brackets, cyan fill, dark empty + printf("\r\033[32m%3d%%\033[0m [\033[36m", pct); + for (int i = 0; i < filled; i++) printf("\xe2\x96\x88"); // █ + printf("\033[90m"); + for (int i = filled; i < BAR_WIDTH; i++) printf("\xe2\x96\x91"); // ░ + printf("\033[0m]%s", time_buf); + } + else + { + // Plain ASCII fallback for pipes / log files + printf("\r%3d%% [", pct); + for (int i = 0; i < filled; i++) putchar('#'); + for (int i = filled; i < BAR_WIDTH; i++) putchar(' '); + printf("]%s", time_buf); + } + + if (percentage >= 1.f) putchar('\n'); fflush(stdout); } diff --git a/tools/cmdata/src/cmdata/mindist.hpp b/tools/cmdata/src/cmdata/mindist.hpp index 7e0b8ddb..8c89cfd1 100644 --- a/tools/cmdata/src/cmdata/mindist.hpp +++ b/tools/cmdata/src/cmdata/mindist.hpp @@ -2,7 +2,6 @@ #define _CMDATA_MINDIST_HPP #include -#include #include #include "cmdata/indexing.hpp" @@ -12,139 +11,44 @@ namespace cmdata::mindist { static void mindist_same( - std::size_t start_mti_same, std::size_t start_im_same, std::size_t start_i_same, - std::size_t start_j_same, long int n_loop_operations_same, const std::vector &density_bins, + const std::vector &density_bins, const std::vector &num_mol_unique, const std::vector &natmol2, const std::vector> &frame_same_mat, - std::vector> &frame_same_mutex, std::vector>>> &interm_same_maxcdf_mol, float weight ) { - bool first_im_same = true, first_i_same = true, first_j_same = true; - unsigned counter_same = 0; - for (std::size_t mt_i = start_mti_same; mt_i < natmol2.size(); mt_i++) - { - for (std::size_t im = (first_im_same) ? start_im_same : 0; im < num_mol_unique[mt_i]; im++) - { - for (std::size_t i = (first_i_same) ? start_i_same : 0; i < natmol2[mt_i]; i++) - { - for (std::size_t j = (first_j_same) ? start_j_same : i; j < natmol2[mt_i]; j++) + for (std::size_t mt_i = 0; mt_i < natmol2.size(); mt_i++) + for (std::size_t im = 0; im < static_cast(num_mol_unique[mt_i]); im++) + for (std::size_t i = 0; i < static_cast(natmol2[mt_i]); i++) + for (std::size_t j = i; j < static_cast(natmol2[mt_i]); j++) { std::size_t offset = cmdata::indexing::offset_same(mt_i, im, i, j, natmol2); - std::size_t mutex_j = cmdata::indexing::mutex_access(mt_i, i, j, natmol2); float mindist = frame_same_mat[mt_i][offset]; - - std::unique_lock lock(frame_same_mutex[mt_i][mutex_j]); cmdata::density::kernel_density_estimator(std::begin(interm_same_maxcdf_mol[mt_i][i][j]), density_bins, mindist, weight); interm_same_maxcdf_mol[mt_i][j][i] = interm_same_maxcdf_mol[mt_i][i][j]; - lock.unlock(); - - ++counter_same; - if (counter_same == n_loop_operations_same) return; } - first_j_same = false; - } - first_i_same = false; - } - first_im_same = false; - } } static void mindist_cross( - std::size_t start_mti_cross, std::size_t start_mtj_cross, std::size_t start_im_cross, std::size_t start_jm_cross, - std::size_t start_i_cross, std::size_t start_j_cross, int n_loop_operations_cross, const std::vector &natmol2, const std::vector> &cross_index, const std::vector &density_bins, const std::vector &num_mol_unique, const std::vector> &frame_cross_mat, - std::vector> &frame_cross_mutex, std::vector>>> &interm_cross_maxcdf_mol, float weight ) { - bool first_im_cross = true, first_mtj_cross = true, first_jm_cross = true, first_i_cross = true, first_j_cross = true; - unsigned counter_cross = 0; - - for (std::size_t mt_i = start_mti_cross; mt_i < natmol2.size(); mt_i++) - { - for (std::size_t mt_j = (first_mtj_cross) ? start_mtj_cross : mt_i + 1; mt_j < natmol2.size(); mt_j++) - { - for (std::size_t im = (first_im_cross) ? start_im_cross : 0; im < num_mol_unique[mt_i]; im++) - { - for (std::size_t jm = (first_jm_cross) ? start_jm_cross : 0; jm < num_mol_unique[mt_j]; jm++) - { - for (std::size_t i = (first_i_cross) ? start_i_cross : 0; i < natmol2[mt_i]; i++) - { - for (std::size_t j = (first_j_cross) ? start_j_cross : 0; j < natmol2[mt_j]; j++) + for (std::size_t mt_i = 0; mt_i < natmol2.size(); mt_i++) + for (std::size_t mt_j = mt_i + 1; mt_j < natmol2.size(); mt_j++) + for (std::size_t im = 0; im < static_cast(num_mol_unique[mt_i]); im++) + for (std::size_t jm = 0; jm < static_cast(num_mol_unique[mt_j]); jm++) + for (std::size_t i = 0; i < static_cast(natmol2[mt_i]); i++) + for (std::size_t j = 0; j < static_cast(natmol2[mt_j]); j++) { std::size_t offset = cmdata::indexing::offset_cross(mt_i, mt_j, im, jm, i, j, natmol2, num_mol_unique[mt_j]); - std::size_t mutex_j = cmdata::indexing::mutex_access(mt_j, i, j, natmol2); float mindist = frame_cross_mat[cross_index[mt_i][mt_j]][offset]; - - std::unique_lock lock(frame_cross_mutex[cross_index[mt_i][mt_j]][mutex_j]); cmdata::density::kernel_density_estimator(std::begin(interm_cross_maxcdf_mol[cross_index[mt_i][mt_j]][i][j]), density_bins, mindist, weight); - lock.unlock(); - - ++counter_cross; - if (counter_cross == n_loop_operations_cross) return; } - first_j_cross = false; - } - first_i_cross = false; - } - first_jm_cross = false; - } - first_im_cross = false; - } - first_mtj_cross = false; - } -} - -static void mindist_kernel( // indices - const cmdata::indexing::SameThreadIndices &same_thread_indices, - const cmdata::indexing::CrossThreadIndices &cross_thread_indices, - const float weight, // common parameters - const std::vector &natmol2, - const std::vector &density_bins, - const std::vector &num_mol_unique, - const std::vector> &frame_same_mat, - std::vector> &frame_same_mutex, - std::vector>>> &interm_same_maxcdf_mol, - const std::vector> &cross_index, - const std::vector> &frame_cross_mat, - std::vector> &frame_cross_mutex, - std::vector>>> &interm_cross_maxcdf_mol -) -{ - const std::size_t start_mti_same = same_thread_indices.start_mti_same; - const std::size_t start_im_same = same_thread_indices.start_im_same; - const std::size_t start_i_same = same_thread_indices.start_i_same; - const std::size_t start_j_same = same_thread_indices.start_j_same; - const long int n_loop_operations_same = same_thread_indices.n_loop_operations_same; - - const std::size_t start_mti_cross = cross_thread_indices.start_mti_cross; - const std::size_t start_mtj_cross = cross_thread_indices.start_mtj_cross; - const std::size_t start_im_cross = cross_thread_indices.start_im_cross; - const std::size_t start_jm_cross = cross_thread_indices.start_jm_cross; - const std::size_t start_i_cross = cross_thread_indices.start_i_cross; - const std::size_t start_j_cross = cross_thread_indices.start_j_cross; - const int n_loop_operations_cross = cross_thread_indices.n_loop_operations_cross; - - if (n_loop_operations_same != 0) - { - mindist_same( - start_mti_same, start_im_same, start_i_same, start_j_same, n_loop_operations_same, density_bins, - num_mol_unique, natmol2, frame_same_mat, frame_same_mutex, interm_same_maxcdf_mol, weight - ); - } - - if (n_loop_operations_cross != 0) - { - mindist_cross( - start_mti_cross, start_mtj_cross, start_im_cross, start_jm_cross, start_i_cross, start_j_cross, n_loop_operations_cross, - natmol2, cross_index, density_bins, num_mol_unique, frame_cross_mat, frame_cross_mutex, interm_cross_maxcdf_mol, weight - ); - } } } // namespace cmdata::mindist diff --git a/tools/cmdata/test/CMakeLists.txt b/tools/cmdata/test/CMakeLists.txt new file mode 100644 index 00000000..5ad5a2e7 --- /dev/null +++ b/tools/cmdata/test/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.16) + +# Test targets — intentionally have NO dependency on GROMACS or any external +# library. Only indexing.hpp, density.hpp, and mindist.hpp are needed; all +# three are pure C++17 header-only code with no GROMACS includes. + +add_executable(test_cmdata test_cmdata.cpp) +target_include_directories(test_cmdata PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src) +target_compile_features(test_cmdata PRIVATE cxx_std_17) +target_compile_options(test_cmdata PRIVATE -Wall -Wextra -Wno-unused-parameter) +add_test(NAME cmdata_tests COMMAND test_cmdata) diff --git a/tools/cmdata/test/test_cmdata.cpp b/tools/cmdata/test/test_cmdata.cpp new file mode 100644 index 00000000..6c9cb5bc --- /dev/null +++ b/tools/cmdata/test/test_cmdata.cpp @@ -0,0 +1,463 @@ +// Unit tests for cmdata headers that have no GROMACS dependency: +// indexing.hpp — n_bins, offset_same, offset_cross +// density.hpp — kernel_density_estimator, normalize_histo +// mindist.hpp — mindist_same, mindist_cross +// +// Build (no CMake needed): +// g++ -std=c++17 -I../src test_cmdata.cpp -o test_cmdata && ./test_cmdata + +#include +#include +#include +#include +#include + +#include "cmdata/indexing.hpp" +#include "cmdata/density.hpp" +#include "cmdata/mindist.hpp" + +// ── minimal test harness ───────────────────────────────────────────────────── +static int g_pass = 0, g_fail = 0; + +#define CHECK(expr) \ + do { \ + if (expr) { ++g_pass; } \ + else { ++g_fail; fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, #expr); } \ + } while (0) + +#define CHECK_MSG(expr, msg) \ + do { \ + if (expr) { ++g_pass; } \ + else { ++g_fail; fprintf(stderr, "FAIL %s:%d %s (%s)\n", __FILE__, __LINE__, #expr, msg); } \ + } while (0) + +#define CHECK_NEAR(a, b, tol) \ + do { \ + double _a = (a), _b = (b), _t = (tol); \ + if (std::abs(_a - _b) <= _t) { ++g_pass; } \ + else { ++g_fail; fprintf(stderr, "FAIL %s:%d |%.6g - %.6g| = %.6g > %.6g\n", __FILE__, __LINE__, _a, _b, std::abs(_a - _b), _t); } \ + } while (0) + +// ── helpers ────────────────────────────────────────────────────────────────── + +// Build a uniform density_bins vector of length n for the given cutoff. +static std::vector make_bins(float cutoff) +{ + int n = cmdata::indexing::n_bins(cutoff); + std::vector bins(n); + float dx = cutoff / static_cast(n); + for (int i = 0; i < n; i++) bins[i] = dx * (static_cast(i) + 0.5f); + return bins; +} + +// Sum a histogram, multiplied by dx, to approximate the integral. +static double integrate(const std::vector &h, float dx) +{ + double s = 0.0; + for (float v : h) s += v; + return s * dx; +} + +// ============================================================================ +// indexing tests +// ============================================================================ + +static void test_n_bins() +{ + // formula: cut / (0.01 / factor) = cut * factor / 0.01 + CHECK(cmdata::indexing::n_bins(0.75f) == 300); + CHECK(cmdata::indexing::n_bins(1.00f) == 400); + CHECK(cmdata::indexing::n_bins(0.50f) == 200); + CHECK(cmdata::indexing::n_bins(0.75f, 4.0f) == 300); + CHECK(cmdata::indexing::n_bins(0.75f, 2.0f) == 150); // factor=2 → 0.75*2/0.01 +} + +static void test_offset_same_uniqueness() +{ + // For each topology (natmol2, num_mol_unique), all tuples + // (mol_i, a_i, a_j) must map to distinct offsets. + struct Case { std::vector natmol2; std::vector nmol; }; + std::vector cases = { + {{ 2 }, { 1 }}, + {{ 3 }, { 1 }}, + {{ 3 }, { 2 }}, + {{ 5 }, { 3 }}, + {{ 2 }, { 4 }}, + }; + for (auto &c : cases) + { + std::set seen; + bool ok = true; + for (std::size_t mt = 0; mt < c.natmol2.size(); mt++) + for (int im = 0; im < c.nmol[mt]; im++) + for (int ai = 0; ai < c.natmol2[mt]; ai++) + for (int aj = 0; aj < c.natmol2[mt]; aj++) + { + auto off = cmdata::indexing::offset_same(mt, im, ai, aj, c.natmol2); + ok = ok && seen.insert(off).second; // insert returns false on duplicate + } + CHECK_MSG(ok, "offset_same: duplicate offset found"); + } +} + +static void test_offset_same_formula() +{ + std::vector nat = { 3 }; + // offset_same = mol_i * N*N + a_i * N + a_j + CHECK(cmdata::indexing::offset_same(0, 0, 0, 0, nat) == 0); + CHECK(cmdata::indexing::offset_same(0, 0, 0, 2, nat) == 2); + CHECK(cmdata::indexing::offset_same(0, 0, 1, 0, nat) == 3); + CHECK(cmdata::indexing::offset_same(0, 1, 0, 0, nat) == 9); // mol_i=1 → +1*9 + CHECK(cmdata::indexing::offset_same(0, 1, 2, 1, nat) == 9 + 6 + 1); // 16 +} + +static void test_offset_cross_uniqueness() +{ + struct Case { + std::vector natmol2; + std::vector nmol; + std::size_t mt_i, mt_j; + }; + std::vector cases = { + {{ 2, 3 }, { 1, 1 }, 0, 1}, + {{ 2, 3 }, { 2, 1 }, 0, 1}, + {{ 2, 3 }, { 1, 3 }, 0, 1}, + {{ 2, 3 }, { 2, 3 }, 0, 1}, + {{ 3, 3 }, { 2, 2 }, 0, 1}, + {{ 4, 2 }, { 3, 4 }, 0, 1}, + }; + for (auto &c : cases) + { + std::set seen; + bool ok = true; + for (int im = 0; im < c.nmol[c.mt_i]; im++) + for (int jm = 0; jm < c.nmol[c.mt_j]; jm++) + for (int ai = 0; ai < c.natmol2[c.mt_i]; ai++) + for (int aj = 0; aj < c.natmol2[c.mt_j]; aj++) + { + auto off = cmdata::indexing::offset_cross(c.mt_i, c.mt_j, im, jm, ai, aj, c.natmol2, c.nmol[c.mt_j]); + ok = ok && seen.insert(off).second; + } + char buf[128]; + snprintf(buf, sizeof(buf), "offset_cross: mt_i=%zu mt_j=%zu ni=%d nj=%d", + c.mt_i, c.mt_j, c.natmol2[c.mt_i], c.natmol2[c.mt_j]); + CHECK_MSG(ok, buf); + } +} + +static void test_offset_cross_formula() +{ + // offset_cross = mol_i * (num_mol_j * Ni * Nj) + mol_j * (Ni * Nj) + ai * Nj + aj + std::vector nat = { 2, 3 }; + std::size_t num_mol_j = 2; + // mol_i=0, mol_j=0, ai=0, aj=0 → 0 + CHECK(cmdata::indexing::offset_cross(0, 1, 0, 0, 0, 0, nat, num_mol_j) == 0); + // mol_i=0, mol_j=0, ai=0, aj=2 → 2 + CHECK(cmdata::indexing::offset_cross(0, 1, 0, 0, 0, 2, nat, num_mol_j) == 2); + // mol_i=0, mol_j=0, ai=1, aj=0 → Nj=3 + CHECK(cmdata::indexing::offset_cross(0, 1, 0, 0, 1, 0, nat, num_mol_j) == 3); + // mol_i=0, mol_j=1, ai=0, aj=0 → Ni*Nj = 2*3 = 6 + CHECK(cmdata::indexing::offset_cross(0, 1, 0, 1, 0, 0, nat, num_mol_j) == 6); + // mol_i=1, mol_j=0, ai=0, aj=0 → num_mol_j * Ni*Nj = 2*6 = 12 + CHECK(cmdata::indexing::offset_cross(0, 1, 1, 0, 0, 0, nat, num_mol_j) == 12); +} + +// ============================================================================ +// density tests +// ============================================================================ + +static void test_kde_normalization() +{ + // A single sample with weight=1 should integrate to ~1. + const float cutoff = 0.75f; + auto bins = make_bins(cutoff); + float dx = cutoff / static_cast(bins.size()); + + // interior point (mu well inside range, no boundary effect) + { + std::vector h(bins.size(), 0.f); + cmdata::density::kernel_density_estimator(h.begin(), bins, 0.40f, 1.f); + double integral = integrate(h, dx); + CHECK_NEAR(integral, 1.0, 0.01); // 1% tolerance for discrete approximation + } + // point near zero (mu < h=0.01): the implementation doubles the scale as a + // boundary reflection approximation. The approximation is exact at mu=0 but + // degrades for mu in (0, h). We only check that the KDE is non-zero and that + // the integral stays in a physically reasonable range (0.8 – 1.5). + { + std::vector hv(bins.size(), 0.f); + cmdata::density::kernel_density_estimator(hv.begin(), bins, 0.005f, 1.f); + double integral = integrate(hv, dx); + CHECK_MSG(integral > 0.8 && integral < 1.6, "KDE near-zero: integral out of reasonable range"); + } + // weight=2: integral should be ~2 + { + std::vector h(bins.size(), 0.f); + cmdata::density::kernel_density_estimator(h.begin(), bins, 0.40f, 2.f); + double integral = integrate(h, dx); + CHECK_NEAR(integral, 2.0, 0.02); + } +} + +static void test_kde_support() +{ + // Bins outside [mu-2h, mu+2h] (h=0.01) must be exactly zero. + const float cutoff = 0.75f; + const float h = 0.01f; + auto bins = make_bins(cutoff); + float dx = cutoff / static_cast(bins.size()); + + const float mu = 0.40f; + std::vector hist(bins.size(), 0.f); + cmdata::density::kernel_density_estimator(hist.begin(), bins, mu, 1.f); + + for (std::size_t k = 0; k < bins.size(); k++) + { + if (bins[k] < mu - 2.f * h - dx || bins[k] > mu + 2.f * h + dx) + CHECK_MSG(hist[k] == 0.f, "KDE nonzero outside support"); + } + // peak should be near mu + std::size_t peak = std::max_element(hist.begin(), hist.end()) - hist.begin(); + CHECK_MSG(std::abs(bins[peak] - mu) < 3.f * dx, "KDE peak not near mu"); +} + +static void test_kde_accumulation() +{ + // Adding two samples produces the sum of individual KDEs. + const float cutoff = 0.75f; + auto bins = make_bins(cutoff); + float dx = cutoff / static_cast(bins.size()); + + std::vector h1(bins.size(), 0.f), h2(bins.size(), 0.f), h12(bins.size(), 0.f); + cmdata::density::kernel_density_estimator(h1.begin(), bins, 0.30f, 1.f); + cmdata::density::kernel_density_estimator(h2.begin(), bins, 0.50f, 1.f); + cmdata::density::kernel_density_estimator(h12.begin(), bins, 0.30f, 1.f); + cmdata::density::kernel_density_estimator(h12.begin(), bins, 0.50f, 1.f); + + bool ok = true; + for (std::size_t k = 0; k < bins.size(); k++) + if (std::abs(h12[k] - h1[k] - h2[k]) > 1e-6f) { ok = false; break; } + CHECK_MSG(ok, "KDE: two-sample sum != sum of individual KDEs"); + + CHECK_NEAR(integrate(h12, dx), 2.0, 0.02); +} + +static void test_normalize_histo() +{ + // normalize_histo multiplies every element by norm * inv_num_mol_same. + std::vector>>> data(1); + data[0].resize(3, std::vector>(3, std::vector(10, 2.f))); + + cmdata::density::normalize_histo(0, 1, 2, 0.5f, 0.25f, data); + // expected: 2.0 * 0.5 * 0.25 = 0.25 + for (float v : data[0][1][2]) CHECK_NEAR(v, 0.25, 1e-6); + // other elements untouched + for (float v : data[0][0][0]) CHECK_NEAR(v, 2.0, 1e-6); + + // zero stays zero + std::vector>>> z(1); + z[0].resize(2, std::vector>(2, std::vector(5, 0.f))); + cmdata::density::normalize_histo(0, 0, 1, 1.f, 1.f, z); + for (float v : z[0][0][1]) CHECK(v == 0.f); +} + +// ============================================================================ +// mindist tests +// ============================================================================ + +// Helper: build cross_index table for a given natmol2. +static std::vector> make_cross_index(std::size_t n_types) +{ + std::vector> ci(n_types, std::vector(n_types, 0)); + int count = 0; + for (std::size_t i = 0; i < n_types; i++) + for (std::size_t j = i + 1; j < n_types; j++) + ci[i][j] = count++; + return ci; +} + +static void test_mindist_same_single_mol() +{ + // One molecule type, one molecule, 2 atoms. + // frame_same_mat[0][offset] = known distance. + // After mindist_same, interm_same_maxcdf_mol[0][i][j] should be non-zero + // near that distance and its integral should be ~1. + std::vector natmol2 = { 2 }; + std::vector nmol = { 1 }; + + const float cutoff = 0.75f; + auto bins = make_bins(cutoff); + float dx = cutoff / static_cast(bins.size()); + + // frame_same_mat[mt][offset] — size = nmol * N * N = 1*2*2 = 4 + std::vector> fsm(1, std::vector(4, 100.f)); + // set distance for (im=0, a_i=0, a_j=1) + float test_dist = 0.30f; + fsm[0][cmdata::indexing::offset_same(0, 0, 0, 1, natmol2)] = test_dist; + // set diagonal (a_i==a_j) to some value too + fsm[0][cmdata::indexing::offset_same(0, 0, 0, 0, natmol2)] = 0.10f; + fsm[0][cmdata::indexing::offset_same(0, 0, 1, 1, natmol2)] = 0.20f; + + // interm_same_maxcdf_mol[mt][a_i][a_j] — start at zero + std::vector>>> cdf(1); + cdf[0].resize(2, std::vector>(2, std::vector(bins.size(), 0.f))); + + cmdata::mindist::mindist_same(bins, nmol, natmol2, fsm, cdf, 1.f); + + // (0,0) and (1,1) should have non-zero KDE + CHECK(integrate(cdf[0][0][0], dx) > 0.5); + CHECK(integrate(cdf[0][1][1], dx) > 0.5); + + // (0,1) should have KDE peaking near test_dist + double integral_01 = integrate(cdf[0][0][1], dx); + CHECK_NEAR(integral_01, 1.0, 0.02); + + // mindist_same mirrors: cdf[0][1][0] should equal cdf[0][0][1] + bool mirrored = true; + for (std::size_t k = 0; k < bins.size(); k++) + if (std::abs(cdf[0][0][1][k] - cdf[0][1][0][k]) > 1e-6f) { mirrored = false; break; } + CHECK_MSG(mirrored, "mindist_same: [i][j] != [j][i] after mirror copy"); +} + +static void test_mindist_same_multi_mol() +{ + // One type, 3 molecules, 2 atoms each. + // All (im, a_i, a_j) slots must be visited. + // We count how many slots received a KDE contribution. + std::vector natmol2 = { 2 }; + std::vector nmol = { 3 }; + const int N = natmol2[0], M = nmol[0]; + + const float cutoff = 0.75f; + auto bins = make_bins(cutoff); + + // fill every slot with a known distance + std::vector> fsm(1, std::vector(M * N * N, 0.40f)); + + std::vector>>> cdf(1); + cdf[0].resize(N, std::vector>(N, std::vector(bins.size(), 0.f))); + + cmdata::mindist::mindist_same(bins, nmol, natmol2, fsm, cdf, 1.f); + + // Each (a_i, a_j >= a_i) bin should have been accumulated M times. + // integral ≈ M (weight=1 per accumulation). + float dx = cutoff / static_cast(bins.size()); + for (int ai = 0; ai < N; ai++) + for (int aj = ai; aj < N; aj++) + CHECK_NEAR(integrate(cdf[0][ai][aj], dx), static_cast(M), 0.05 * M); +} + +static void test_mindist_cross_basic() +{ + // Two molecule types: type 0 has 2 atoms / 1 mol, type 1 has 3 atoms / 1 mol. + // cross_index[0][1] = 0 + // frame_cross_mat[0] has size 1*2*1*3 = 6. + // After mindist_cross, interm_cross_maxcdf_mol[0][a_i][a_j] should carry + // a normalized KDE (integral ~1). + std::vector natmol2 = { 2, 3 }; + std::vector nmol = { 1, 1 }; + auto ci = make_cross_index(2); + + const float cutoff = 0.75f; + auto bins = make_bins(cutoff); + float dx = cutoff / static_cast(bins.size()); + + // Set every entry in the cross mat to a known distance. + const float test_dist = 0.45f; + std::vector> fcm(1, std::vector(2 * 3 * 1 * 1, test_dist)); + + std::vector>>> cdf(1); + cdf[0].resize(2, std::vector>(3, std::vector(bins.size(), 0.f))); + + cmdata::mindist::mindist_cross(natmol2, ci, bins, nmol, fcm, cdf, 1.f); + + for (int ai = 0; ai < 2; ai++) + for (int aj = 0; aj < 3; aj++) + CHECK_NEAR(integrate(cdf[0][ai][aj], dx), 1.0, 0.02); +} + +static void test_mindist_cross_multi_mol() +{ + // Type 0: 2 atoms, 2 mols. Type 1: 2 atoms, 3 mols. + // Each (ai, aj) pair is visited im*jm = 2*3 = 6 times. + std::vector natmol2 = { 2, 2 }; + std::vector nmol = { 2, 3 }; + auto ci = make_cross_index(2); + std::size_t buf_size = natmol2[0] * natmol2[1] * nmol[0] * nmol[1]; // 2*2*2*3=24 + + const float cutoff = 0.75f; + auto bins = make_bins(cutoff); + float dx = cutoff / static_cast(bins.size()); + + std::vector> fcm(1, std::vector(buf_size, 0.40f)); + + std::vector>>> cdf(1); + cdf[0].resize(2, std::vector>(2, std::vector(bins.size(), 0.f))); + + cmdata::mindist::mindist_cross(natmol2, ci, bins, nmol, fcm, cdf, 1.f); + + const double expected = static_cast(nmol[0]) * static_cast(nmol[1]); + for (int ai = 0; ai < 2; ai++) + for (int aj = 0; aj < 2; aj++) + CHECK_NEAR(integrate(cdf[0][ai][aj], dx), expected, 0.05 * expected); +} + +static void test_mindist_cross_uses_correct_offset() +{ + // Verify that each (im, jm) slot in frame_cross_mat contributes to the + // KDE at the correct distance. We place a unique distance in each slot + // and verify that the combined KDE integrates to im_count * jm_count. + std::vector natmol2 = { 1, 1 }; // one atom each — simplifies offset to mol index + std::vector nmol = { 2, 2 }; + auto ci = make_cross_index(2); + // offset_cross(mt_i=0, mt_j=1, im, jm, 0, 0, nat, num_mol_j=2) + // = im*(2*1*1) + jm*(1*1) + 0 + 0 = 2*im + jm + // 4 slots: (0,0)->0, (0,1)->1, (1,0)->2, (1,1)->3 + std::vector> fcm(1, std::vector(4, 0.f)); + fcm[0][0] = 0.30f; // (im=0, jm=0) + fcm[0][1] = 0.35f; // (im=0, jm=1) + fcm[0][2] = 0.40f; // (im=1, jm=0) + fcm[0][3] = 0.45f; // (im=1, jm=1) + + const float cutoff = 0.75f; + auto bins = make_bins(cutoff); + float dx = cutoff / static_cast(bins.size()); + + std::vector>>> cdf(1); + cdf[0].resize(1, std::vector>(1, std::vector(bins.size(), 0.f))); + + cmdata::mindist::mindist_cross(natmol2, ci, bins, nmol, fcm, cdf, 1.f); + + // All four contributions land on cdf[0][0][0] → integral ≈ 4. + CHECK_NEAR(integrate(cdf[0][0][0], dx), 4.0, 0.05); +} + +// ============================================================================ +// main +// ============================================================================ + +int main() +{ + printf("=== indexing ===\n"); + test_n_bins(); + test_offset_same_uniqueness(); + test_offset_same_formula(); + test_offset_cross_uniqueness(); + test_offset_cross_formula(); + + printf("=== density ===\n"); + test_kde_normalization(); + test_kde_support(); + test_kde_accumulation(); + test_normalize_histo(); + + printf("=== mindist ===\n"); + test_mindist_same_single_mol(); + test_mindist_same_multi_mol(); + test_mindist_cross_basic(); + test_mindist_cross_multi_mol(); + test_mindist_cross_uses_correct_offset(); + + printf("\n%d PASS %d FAIL\n", g_pass, g_fail); + return g_fail > 0 ? 1 : 0; +} diff --git a/tools/cmdata/test/test_partition.cpp b/tools/cmdata/test/test_partition.cpp new file mode 100644 index 00000000..0e995517 --- /dev/null +++ b/tools/cmdata/test/test_partition.cpp @@ -0,0 +1,452 @@ +// Unit tests for cmdata::partition and cmdata::indexing::offset_cross. +// +// No GROMACS, no external test framework — plain C++17 with assertions and +// stderr reporting. Exit code 0 = all tests pass. +// +// Build standalone (no cmake required): +// g++ -std=c++17 -Wall -I../src test_partition.cpp -o test_partition && ./test_partition + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cmdata/indexing.hpp" +#include "cmdata/partition.hpp" + +// ───────────────────────────────────────────────────────────────────────────── +// Minimal test harness +// ───────────────────────────────────────────────────────────────────────────── + +static int g_pass = 0, g_fail = 0; + +#define CHECK(expr) \ + do { \ + if (!(expr)) { \ + std::fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, #expr); \ + ++g_fail; \ + } else { \ + ++g_pass; \ + } \ + } while (0) + +#define CHECK_MSG(expr, msg) \ + do { \ + if (!(expr)) { \ + std::fprintf(stderr, "FAIL %s:%d %s (%s)\n", \ + __FILE__, __LINE__, #expr, (msg).c_str()); \ + ++g_fail; \ + } else { \ + ++g_pass; \ + } \ + } while (0) + +// ───────────────────────────────────────────────────────────────────────────── +// Tuple helpers +// ───────────────────────────────────────────────────────────────────────────── + +using Op6 = std::tuple; // mt_i, mt_j, im, jm, i, j +using Op4 = std::tuple; // mt_i, im, i, j + +// ───────────────────────────────────────────────────────────────────────────── +// Enumerate every cross op in canonical order (mt_i, mt_j>mt_i, im, jm, i, j) +// This is the order mindist_cross iterates. +// ───────────────────────────────────────────────────────────────────────────── +static std::vector all_cross_ops( + const std::vector &natmol2, + const std::vector &num_mol_unique +) +{ + std::vector ops; + for (int mt_i = 0; mt_i < (int)natmol2.size(); mt_i++) + for (int mt_j = mt_i + 1; mt_j < (int)natmol2.size(); mt_j++) + for (int im = 0; im < num_mol_unique[mt_i]; im++) + for (int jm = 0; jm < num_mol_unique[mt_j]; jm++) + for (int i = 0; i < natmol2[mt_i]; i++) + for (int j = 0; j < natmol2[mt_j]; j++) + ops.emplace_back(mt_i, mt_j, im, jm, i, j); + return ops; +} + +// ───────────────────────────────────────────────────────────────────────────── +// Enumerate every same op in canonical order (mt_i, im, i, j>=i) +// This is the order mindist_same iterates. +// ───────────────────────────────────────────────────────────────────────────── +static std::vector all_same_ops( + const std::vector &natmol2, + const std::vector &num_mol_unique +) +{ + std::vector ops; + for (int mt_i = 0; mt_i < (int)natmol2.size(); mt_i++) + for (int im = 0; im < num_mol_unique[mt_i]; im++) + for (int i = 0; i < natmol2[mt_i]; i++) + for (int j = i; j < natmol2[mt_i]; j++) + ops.emplace_back(mt_i, im, i, j); + return ops; +} + +// ───────────────────────────────────────────────────────────────────────────── +// Simulate mindist_cross for a single thread — returns the ops it would process +// ───────────────────────────────────────────────────────────────────────────── +static std::vector simulate_cross( + const cmdata::indexing::CrossThreadIndices &idx, + const std::vector &natmol2, + const std::vector &num_mol_unique +) +{ + std::vector ops; + if (idx.n_loop_operations_cross == 0) return ops; + + bool first_mtj = true, first_im = true, first_jm = true; + bool first_i = true, first_j = true; + int counter = 0; + + for (int mt_i = (int)idx.start_mti_cross; mt_i < (int)natmol2.size(); mt_i++) + { + for (int mt_j = first_mtj ? (int)idx.start_mtj_cross : mt_i + 1; + mt_j < (int)natmol2.size(); mt_j++) + { + for (int im = first_im ? (int)idx.start_im_cross : 0; + im < num_mol_unique[mt_i]; im++) + { + for (int jm = first_jm ? (int)idx.start_jm_cross : 0; + jm < num_mol_unique[mt_j]; jm++) + { + for (int i = first_i ? (int)idx.start_i_cross : 0; + i < natmol2[mt_i]; i++) + { + for (int j = first_j ? (int)idx.start_j_cross : 0; + j < natmol2[mt_j]; j++) + { + ops.emplace_back(mt_i, mt_j, im, jm, i, j); + ++counter; + if (counter == idx.n_loop_operations_cross) return ops; + first_j = false; + } + first_j = false; + } + first_i = false; + } + first_jm = false; + } + first_im = false; + } + first_mtj = false; + } + return ops; +} + +// ───────────────────────────────────────────────────────────────────────────── +// Simulate mindist_same for a single thread +// ───────────────────────────────────────────────────────────────────────────── +static std::vector simulate_same( + const cmdata::indexing::SameThreadIndices &idx, + const std::vector &natmol2, + const std::vector &num_mol_unique +) +{ + std::vector ops; + if (idx.n_loop_operations_same == 0) return ops; + + bool first_im = true, first_i = true, first_j = true; + long int counter = 0; + + for (int mt_i = (int)idx.start_mti_same; mt_i < (int)natmol2.size(); mt_i++) + { + for (int im = first_im ? (int)idx.start_im_same : 0; + im < num_mol_unique[mt_i]; im++) + { + for (int i = first_i ? (int)idx.start_i_same : 0; i < natmol2[mt_i]; i++) + { + for (int j = first_j ? (int)idx.start_j_same : i; j < natmol2[mt_i]; j++) + { + ops.emplace_back(mt_i, im, i, j); + ++counter; + if (counter == idx.n_loop_operations_same) return ops; + first_j = false; + } + first_j = false; + } + first_i = false; + } + first_im = false; + } + return ops; +} + +// ───────────────────────────────────────────────────────────────────────────── +// Core checker: partition coverage and no-overlap for cross case +// ───────────────────────────────────────────────────────────────────────────── +static void check_cross_partition( + const std::string &label, + const std::vector &natmol2, + const std::vector &num_mol_unique, + int num_threads +) +{ + if (natmol2.size() < 2) return; // no cross case possible + + auto result = cmdata::partition::compute_thread_indices( + natmol2, num_mol_unique, num_threads, /*do_same=*/false, /*do_cross=*/true + ); + + const auto expected = all_cross_ops(natmol2, num_mol_unique); + const int total_ops = (int)expected.size(); + + // Build a map from op → thread index that claimed it, to detect duplicates + std::map op_to_thread; + std::vector all_claimed; + + for (int tid = 0; tid < num_threads; tid++) + { + auto ops = simulate_cross(result.cross[tid], natmol2, num_mol_unique); + + // n_loop_operations == claimed count + CHECK_MSG( + (int)ops.size() == result.cross[tid].n_loop_operations_cross, + label + " tid=" + std::to_string(tid) + " ops.size() vs n_loop" + ); + + for (auto &op : ops) + { + auto [it, inserted] = op_to_thread.emplace(op, tid); + CHECK_MSG(inserted, + label + " duplicate op (" + + std::to_string(std::get<0>(op)) + "," + + std::to_string(std::get<1>(op)) + "," + + std::to_string(std::get<2>(op)) + "," + + std::to_string(std::get<3>(op)) + "," + + std::to_string(std::get<4>(op)) + "," + + std::to_string(std::get<5>(op)) + ") claimed by tid=" + + std::to_string(it->second) + " AND tid=" + std::to_string(tid) + ); + all_claimed.push_back(op); + } + } + + // Total claimed == expected total + CHECK_MSG( + (int)all_claimed.size() == total_ops, + label + " total ops: got " + std::to_string(all_claimed.size()) + + " expected " + std::to_string(total_ops) + ); + + // Claimed set == expected set (i.e. no gaps) + std::set claimed_set(all_claimed.begin(), all_claimed.end()); + std::set expected_set(expected.begin(), expected.end()); + CHECK_MSG(claimed_set == expected_set, label + " claimed set != expected set"); + + // Verify the ops are delivered in the same ORDER as the canonical enumeration + // when all thread op-lists are concatenated in tid order. + std::vector concat; + for (int tid = 0; tid < num_threads; tid++) + { + auto ops = simulate_cross(result.cross[tid], natmol2, num_mol_unique); + concat.insert(concat.end(), ops.begin(), ops.end()); + } + CHECK_MSG(concat == expected, + label + " concatenated order does not match canonical order" + ); +} + +// ───────────────────────────────────────────────────────────────────────────── +// Core checker: partition coverage for same case +// ───────────────────────────────────────────────────────────────────────────── +static void check_same_partition( + const std::string &label, + const std::vector &natmol2, + const std::vector &num_mol_unique, + int num_threads +) +{ + // same mode only makes sense when at least one type has >1 molecule + bool any_multi = false; + for (auto n : num_mol_unique) if (n > 1) { any_multi = true; break; } + if (!any_multi) return; + + auto result = cmdata::partition::compute_thread_indices( + natmol2, num_mol_unique, num_threads, /*do_same=*/true, /*do_cross=*/false + ); + + const auto expected = all_same_ops(natmol2, num_mol_unique); + const int total_ops = (int)expected.size(); + + std::map op_to_thread; + std::vector all_claimed; + + for (int tid = 0; tid < num_threads; tid++) + { + auto ops = simulate_same(result.same[tid], natmol2, num_mol_unique); + CHECK_MSG( + (int)ops.size() == result.same[tid].n_loop_operations_same, + label + " same tid=" + std::to_string(tid) + " ops.size() vs n_loop" + ); + for (auto &op : ops) + { + auto [it, inserted] = op_to_thread.emplace(op, tid); + CHECK_MSG(inserted, label + " same: duplicate op claimed by tid=" + + std::to_string(it->second) + " AND tid=" + std::to_string(tid)); + all_claimed.push_back(op); + } + } + + CHECK_MSG( + (int)all_claimed.size() == total_ops, + label + " same total ops: got " + std::to_string(all_claimed.size()) + + " expected " + std::to_string(total_ops) + ); + + std::vector concat; + for (int tid = 0; tid < num_threads; tid++) + { + auto ops = simulate_same(result.same[tid], natmol2, num_mol_unique); + concat.insert(concat.end(), ops.begin(), ops.end()); + } + CHECK_MSG(concat == expected, label + " same order mismatch"); +} + +// ───────────────────────────────────────────────────────────────────────────── +// Check offset_cross uniqueness: every (mol_i, mol_j, a_i, a_j) must map to a +// distinct slot in [0, num_mol_unique[i]*num_mol_unique[j]*natmol2[i]*natmol2[j]) +// ───────────────────────────────────────────────────────────────────────────── +static void check_offset_cross( + const std::string &label, + const std::vector &natmol2, + const std::vector &num_mol_unique +) +{ + for (int mt_i = 0; mt_i < (int)natmol2.size(); mt_i++) + { + for (int mt_j = mt_i + 1; mt_j < (int)natmol2.size(); mt_j++) + { + const std::size_t capacity = + static_cast(num_mol_unique[mt_i]) + * static_cast(num_mol_unique[mt_j]) + * static_cast(natmol2[mt_i]) + * static_cast(natmol2[mt_j]); + + std::set seen_offsets; + bool all_unique = true, all_in_bounds = true; + + for (int im = 0; im < num_mol_unique[mt_i]; im++) + { + for (int jm = 0; jm < num_mol_unique[mt_j]; jm++) + { + for (int a_i = 0; a_i < natmol2[mt_i]; a_i++) + { + for (int a_j = 0; a_j < natmol2[mt_j]; a_j++) + { + std::size_t off = cmdata::indexing::offset_cross( + mt_i, mt_j, im, jm, a_i, a_j, natmol2 + ); + if (off >= capacity) all_in_bounds = false; + if (!seen_offsets.insert(off).second) all_unique = false; + } + } + } + } + + std::string pair = label + " cross(" + std::to_string(mt_i) + "," + + std::to_string(mt_j) + ") nmu=[" + + std::to_string(num_mol_unique[mt_i]) + "," + + std::to_string(num_mol_unique[mt_j]) + "]"; + + CHECK_MSG(all_in_bounds, pair + ": offset out of bounds"); + CHECK_MSG(all_unique, pair + ": aliased offsets (bug in formula)"); + } + } +} + +// ───────────────────────────────────────────────────────────────────────────── +// Test topologies +// ───────────────────────────────────────────────────────────────────────────── + +struct Topo { + std::string label; + std::vector natmol2; + std::vector num_mol_unique; +}; + +static const std::vector TOPOS = { + // trivial: 1 type, single molecule — no cross, no same + {"1t_1mol", {5}, {1}}, + + // 1 type, multi-molecule: same only + {"1t_3mol", {4}, {3}}, + {"1t_1mol_big", {172}, {1}}, + {"1t_2mol_big", {172}, {2}}, + + // 2 types, 1 molecule each: minimal cross + {"2t_1each", {3,4}, {1,1}}, + + // 2 types, multi-molecule: cross + same + {"2t_2each", {3,4}, {2,2}}, + {"2t_3_2", {5,3}, {3,2}}, + + // 3 types — the case that exposed the bug + {"3t_1each", {2,2,2}, {1,1,1}}, + {"3t_3_1_2", {2,2,2}, {3,1,2}}, // concrete example from the bug trace + {"3t_vary", {4,3,2}, {2,3,1}}, + {"3t_protein_lg", {172,6,3}, {2,4,8}}, // protein + ligand + water (scaled) + {"3t_1multi", {3,3,3}, {1,3,1}}, // only middle type has multi-mol + + // 4 types: stress test + {"4t_all1", {2,3,4,2}, {1,1,1,1}}, + {"4t_mixed", {4,2,3,2}, {2,1,3,2}}, +}; + +static const std::vector THREAD_COUNTS = {1, 2, 3, 4, 7, 8, 16}; + +// ───────────────────────────────────────────────────────────────────────────── +// main +// ───────────────────────────────────────────────────────────────────────────── + +int main() +{ + std::printf("Running partition unit tests...\n\n"); + + // ── cross partition tests ────────────────────────────────────────────────── + std::printf("=== Cross partition coverage tests ===\n"); + for (const auto &topo : TOPOS) + { + if (topo.natmol2.size() < 2) continue; + for (int nt : THREAD_COUNTS) + { + std::string lbl = topo.label + "/threads=" + std::to_string(nt); + check_cross_partition(lbl, topo.natmol2, topo.num_mol_unique, nt); + } + } + + // ── same partition tests ─────────────────────────────────────────────────── + std::printf("=== Same partition coverage tests ===\n"); + for (const auto &topo : TOPOS) + { + for (int nt : THREAD_COUNTS) + { + std::string lbl = topo.label + "/threads=" + std::to_string(nt); + check_same_partition(lbl, topo.natmol2, topo.num_mol_unique, nt); + } + } + + // ── offset_cross uniqueness tests ───────────────────────────────────────── + std::printf("=== offset_cross uniqueness tests ===\n"); + for (const auto &topo : TOPOS) + { + if (topo.natmol2.size() < 2) continue; + check_offset_cross(topo.label, topo.natmol2, topo.num_mol_unique); + } + + // ── summary ─────────────────────────────────────────────────────────────── + std::printf("\n"); + if (g_fail == 0) + std::printf("ALL %d checks PASSED.\n", g_pass); + else + std::printf("%d FAILED / %d PASSED (total %d)\n", g_fail, g_pass, g_pass + g_fail); + + return g_fail > 0 ? 1 : 0; +}