Skip to content

feat: added support for getTotalSwapTime and getTimePerSwap#288

Open
amanbharti008 wants to merge 2 commits into
motiwari:mainfrom
amanbharti008:feature/swap_time
Open

feat: added support for getTotalSwapTime and getTimePerSwap#288
amanbharti008 wants to merge 2 commits into
motiwari:mainfrom
amanbharti008:feature/swap_time

Conversation

@amanbharti008
Copy link
Copy Markdown

Summary

Instruments swap phases so getTotalSwapTime() (total milliseconds) and getTimePerSwap() (average ms per timed swap iteration) reflect measured wall time.
getTimePerSwap() now divides by the number of timed swap iterations (swapTimingIterations), not the generic swap steps counter where those differ.
Adds a Python regression test on the MNIST_100 subset used elsewhere in the smaller test suite.


Motivation

  • totalSwapTime was not updated inside the swap loops.
  • getTimePerSwap() used totalSwapTime / steps, which does not match “average time per timed swap iteration” when steps counts something other than timed iterations.
  • Notably in FastPAM1, where steps is outer-loop iterations while timing sums inner swapFastPAM1 iterations.

Implementation

KMedoids

  • Introduces swapTimingIterations counter.

  • totalSwapTime reset along with swapTimingIterations at fit() start.

  • getTimePerSwap():

    • Returns 0 when there are no timed iterations.
    • Otherwise returns totalSwapTime / swapTimingIterations.
  • Changes mirrored in R_package/banditpam/src/ headers and sources.

Timing

Each timed swap loop uses:

  • std::chrono::steady_clock
  • std::chrono::duration_cast<std::chrono::milliseconds>

Applied in:

  • BanditPAM
  • BanditPAM_orig
  • FastPAM1::swapFastPAM1
  • PAM::fitPAM (when nMedoids > 1 in the main tree)

Tests

File: tests/test_smaller.py

test_swap_timing_stats

  • Fits k = 5 on small_mnist (data/MNIST_100.csv) for:

    • BanditPAM
    • BanditPAM_orig
    • FastPAM1
    • PAM

Assertions

  • Types are correct
  • Values are non-negative
  • time_per_swap == 0 when total_swap_time == 0

Additional Checks

  • For BanditPAM, BanditPAM_orig, and PAM:

    • time_per_swap * steps ≈ total_swap_time
    • (steps matches timed iterations)
  • For FastPAM1:

    • Skip identity check
    • (steps does not match inner timed swap count)

How to Verify

pytest tests/test_smaller.py::SmallerTests::test_swap_timing_stats -v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants