Repository navigation
DistanceOracle(): one-to-many distances against prepared trees (#229) - #232
Merged
Merged
Conversation
- `.SplitDistanceOneMany()` and `.FastManyManyPath()` route a single tree versus many through the batched cross-pairs kernels; the kernel dispatch is shared via `.BatchCrossPairs()`. - Cross-pairs kernels hold the smaller tree set in memory and stream the larger through a reused pool of SplitLists, avoiding a ~520 KB heap allocation per tree. ClusteringInfoDistance(tree, trees), 2000 trees of 183 leaves: 622 -> 144 ms (phylo), 466 -> 73 ms (Splits). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A split's clustering entropy depends only on its size, so compute it once per size rather than calling log()/log1p() for every split of every tree. Bit-identical results; 2000 trees of 183 leaves: 24.1 -> 10.2 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Lists of Splits already on the target leaf labels skip TipLabels() and as.Splits(), which cost more than scoring the pairs. - Cross-pairs kernels can return each tree's information content, so .FastManyManyPath() no longer parses every tree a second time. - build_canon() sorts canonical forms directly when a split needs at most four bins. ClusteringInfoDistance(sp[[1]], sp), 10 000 trees of 183 leaves: 258 -> 108 ms. Outputs are bit-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Normalized MutualClusteringInfo(), SharedPhylogeneticInfo(), MatchingSplitInfo() and InfoRobinsonFoulds() take each tree's information from the kernel that scores it. - RobinsonFoulds() scores one-to-many and Splits comparisons with a batch kernel. - TransferDist() cross pairs stream through cross_pairs() and reuse labelled Splits. - PathDist() and KendallColijn() score one-to-many in one pass; KCVector() finds each pair's MRCA depth in O(1). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Parse a tree set once into compact split rows and canonical forms held behind an external pointer; each column re-aims SplitList shells at the stored rows and scores with the same scorers as cross_pairs(). Supports CID, IRF, RF, MSD, Nye and JRF; falls back to Func(trees[[i]], trees) where trees cannot be prepared. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e-e0fdc0 # Conflicts: # tests/testthat/test-pairwise_distances.R
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Performance benchmark results
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #232 +/- ##
==========================================
+ Coverage 97.77% 97.83% +0.06%
==========================================
Files 60 61 +1
Lines 6832 7033 +201
==========================================
+ Hits 6680 6881 +201
Misses 152 152 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
DistanceOracle(trees, Func, ...), which prepares a set of trees once and returnsfunction(i)givingFunc(trees[[i]], trees, ...). This is the shapeCoreset::FarFirst(d = )accepts. Addresses the remaining cost in #229.Builds on #230 (
one-many-batch). This branch contains #230's commits, so until #230 merges the diff below includes them. The new work is the last three commits.What it does
src/pairwise_distances.cpp).cpp_oracle_prepare()parses every tree once into anOracleStorebehind an external pointer.in_split,CanonSplits, and per-tree information (clustering entropy, splitwise info orNSplits).cpp_oracle_column()points a fewSplitListshells at the stored rows, asPrunedTreealready does, and scores the query against every tree in one OpenMP loop. It touches no R objects.pruned_pair()is nowmetric_score(), which both the pruned-pairs path and the oracle call. They use the same scorers ascross_pairs(), and the query is always argumenta, so raw scores match the one-to-many kernels exactly.R/DistanceOracle.R). EachFunc's one-to-many post-processing is reproduced as written:.FloorNumericalNoise(),.InfoToNormalize()/NormalizeInfo()semantics, and names.Funcs:ClusteringInfoDistance,InfoRobinsonFoulds,RobinsonFoulds,MatchingSplitDistance,NyeSimilarityandJaccardRobinsonFoulds, withnormalizeas FALSE, TRUE, numeric or a function, plussimilarity,k,allowConflictandnormalizeMax.Func,reportMatching = TRUEor an unknown argument is an error.MatchingSplitDistance(normalize = TRUE)errors, as it does today.NyeSimilarityreturns a similarity by default; the documentation says so.saveRDS()/readRDS(), the oracle rebuilds it on the next call.oracle(i)callsFunc(trees[[i]], trees, ...)when the trees do not share a set of four or more leaves, or when the estimated store would exceedmaxBytes(2 GiB by default; about 11.5 KB per tree at 183 leaves).ireturns an N × length(i) matrix.Equality: across all metrics, every
normalizevariant and 9, 70, 140 and 330 leaves, the largest |oracle − Func| was exactly 0 (NaN on both sides for normalized star trees). The tests cover 1, 2, 3 and 6 bins (6 exercises the generic canonical-form path), and includes duplicate trees, an unresolved tree, a star tree, permuted leaf order,mc.cores = 2and anreadRDS()round trip.Benchmark
Release build installed to a per-agent library, R-devel on Windows. Each figure is the mean time for one column (one tree against all), in ms, cycling through random query trees for at least 1 s (so the slowest cells average only 3–4 columns). Repeat runs differ by up to about ±20% (e.g. the 1-thread CID baseline was 147 ms here and 178 ms in the thread study below), so compare within a row. The baseline is
Func(sp[[i]], sp)on this branch, withspa list ofSplits, which is what WideSample passes. Leaves: 183.as.phylo(sample.int(1e6, N), 183); pairs differ in only 2 to 7 splits.10 000 trees, 1 thread (one-off preparation 200–250 ms)
10 000 trees, 2 threads
2 000 trees
The absolute saving per 10k-tree column ranges from about 90 ms (CID on nni trees) to 240 ms (JRF on nni trees). The ratio is largest where scoring is cheap: near-identical trees, and IRF/RF on any trees. On nni trees, CID is dominated by its LAP, so preparation buys only about 1.35×.
Implication for WideSample, estimated from these timings (TreeSearch was not run): 48 CID columns of 10 000 ws trees take about 48 × 147 ms ≈ 7.1 s now, against 0.21 s preparation + 48 × 29.4 ms ≈ 1.6 s with the oracle at 1 thread, or ≈ 1.1 s at 2 threads.
Why 2 threads didn't speed up the existing one-to-many CID call
Timed directly on 10k ws trees:
ClusteringInfoDistance(sp[[7]], sp)cpp_mutual_clustering_cross_pairs, 1 × 10kcpp_clustering_entropy_batch)OpenMP is active. In
cross_pairs(), each 16-tree chunk is first refilled serially on the main thread (Rcpp::as<RawMatrix>+refill_split_list, about 55 ms per 10k trees). Only then does a parallel region build canonical forms and score 16 pairs. The serial parse explains about a third of the cost: it alone predicts about 107 ms at 2 threads, against 142 ms measured. I attribute the rest to the ~625 small fork/join regions with a dynamic, chunk-1 schedule, but did not measure that separately. With 8 queries the scoring share grows, and 2 threads help (49 → 33 ms per column). The oracle parses once, so its single loop scales almost linearly (1.9×).Proposed TreeSearch change (not made here)
In
R/WideSample.R,.WideSampleColumnOracle()could use the oracle when it is available.NyeSimilarityis excluded because it defaults to a similarity.Checks
NOT_CRAN=true(396 on CRAN, where only the 9-leaf grid runs), all passing against the release install. The rest of the local suite passed excepttest-parallel.R:21(NNIDist through a PSOCK cluster); CI on this PR should settle that.stop()incpp_oracle_prepare(), which the R layer cannot reach, is marked// #nocov; it still has a direct test.spelling::spell_check_package()finds no errors.🤖 Generated with Claude Code