Repository navigation
DPL Analysis: add CompactTHnSparse, a THnSparse with compact bin storage - #16198
Merged
Merged
Conversation
THnSparse keeps a TExMap from bin coordinates to bin index, at 2.5-3 slots
of 24 bytes per filled bin, next to the 4-byte content and the packed
coordinates: 67-74 B per filled bin live, a peak of 2.7x that while the map
grows, and 128-174 B per bin after a merge. In train 813131 the two
lf-lambda-two-part-polarization wagons hold ten 7-dimensional THnSparseF
with millions of filled bins each; the grid jobs reach 11.6 GB RSS (median)
and 15% of them expire.
CompactTHnSparseT<T> (CompactTHnSparseF/D) derives from THnBase and keeps the
filled bins in a BlockSparseArray keyed by their linear bin index: blocks of
2^12 consecutive bins, each either sorted 16-bit offsets with the contents
alongside (sizeof(T) + 2 B per filled bin, +8 with Sumw2) or, once at most 4x
larger, dense arrays with a fill bitmap; the blocks in use are found through
one open-addressing hash table. With bins scattered one per block (the worst
case) it costs 56-69 B per filled bin, about what THnSparse costs live.
Fill, Add, Merge, Scale, projections, axis ranges and the statistics come
from THnBase; contents are summed in double and stored as T as in THnSparse,
so bins and statistics are identical. Errors are too, except where the
compiler fuses w * w + sumw2 into one FMA in one fill path and not in the
other (GCC on aarch64): there they differ in the last bit. Bin numbers are
linear indices over all cells (as in THn) and GetNbins() counts the filled
bins (as in THnSparse); the THnBase functions that assume THnSparse's
numbering of the filled bins (ComputeIntegral, GetRandom, PrintEntries,
Print("c"), Fit) throw instead of using wrong bins. The type is meant to be
filled and written out; in O2Physics the registry THnSparse are only filled
(Fill, Sumw2, GetAxis, GetNdimensions, SetMinimum). Bin indices and
coordinates are range-checked; a custom streamer writes the filled bins as
(index, content, sumw2) arrays.
In the HistogramRegistry it is HistType::kCompactTHnSparseF/D, a one-word
change in histos.add(). At the end of the stream such a histogram is sent as
the equivalent THnSparseF/D, one at a time and after freeing its compact
storage, so output files and whatever reads or merges them are unchanged.
toTHnSparse() writes the filled bins straight into the THnSparse chunks, in
THnSparse's packed coordinate format (that of fCoordinates on file), without
building its transient hash table: THnSparse builds it on the first lookup, as
for a histogram read from a file. Going through THnBase::Add instead cost the
table, ~85 B per bin, at the end of the stream (about 1.1 GB for the largest
femto histogram below).
Measured (macOS):
- Replaying the real fills of the six AnaHL histograms of wagon 69897 from the
813131 wagon test (1.1-2.7 M filled bins, 11-12 M fills each): 9.3-12.0 B
per filled bin and 85-110 ns per fill, against 67-69 B and 144-191 ns for
THnSparseF; peak RSS 12-34 MB against 203-462 MB; every bin identical.
- Uniform random fills over the same binning, the worst case for inserting
new bins: at 0.5/4.8/18% of the cells filled, 106/213/385 ns per fill and
11/8/9 B per bin, against 180/229/258 ns and 61-69 B for THnSparseF.
Blocks of 2^16 bins save little memory but were up to 4.4x slower here.
- Peak RSS of the devices of three heavy wagon tests, kTHnSparseF against
kCompactTHnSparseF, same O2Physics, one test input each, end of stream
included; AnalysisResults.root identical bin by bin (errors, statistics):
- 813131, the two lf-lambda-two-part-polarization wagons, first 21 DFs:
3025 / 2265 MB against 872 / 765 MB, same run time (71 objects).
- 813409, femto-pair-track-track (20.5 M filled bins): 2440 MB against
950 MB (183 objects).
- 812749, f0phiproxy, three devices (13.3 M filled bins): 1595 / 1617 /
1455 MB against 1105 / 1097 / 1151 MB (116 objects).
Tests: 11 Catch2 cases against a THnSparseF filled with the same data
(weights, Sumw2 switched on part way, under/overflow, variable binning, dense
and sparse blocks, conversion including zero-weight fills, Merge, streamer,
Clone, projections with axis ranges, out-of-range bins, the refused reads,
registry fill/get/addClone/size/output; the conversion with THnSparse
coordinates wider than 8 bytes and more than one chunk, then looked up,
streamed and filled again), and bins scattered over 30- to
62-bit key spaces with a memory bound.
Member
Author
|
New feature. Nothing uses it. Merging. |
This was referenced Nov 26, 2026
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.
THnSparse keeps a TExMap from bin coordinates to bin index, at 2.5-3 slots
of 24 bytes per filled bin, next to the 4-byte content and the packed
coordinates: 67-74 B per filled bin live, a peak of 2.7x that while the map
grows, and 128-174 B per bin after a merge. In train 813131 the two
lf-lambda-two-part-polarization wagons hold ten 7-dimensional THnSparseF
with millions of filled bins each; the grid jobs reach 11.6 GB RSS (median)
and 15% of them expire.
CompactTHnSparseT (CompactTHnSparseF/D) derives from THnBase and keeps the
filled bins in a BlockSparseArray keyed by their linear bin index: blocks of
2^12 consecutive bins, each either sorted 16-bit offsets with the contents
alongside (sizeof(T) + 2 B per filled bin, +8 with Sumw2) or, once at most 4x
larger, dense arrays with a fill bitmap; the blocks in use are found through
one open-addressing hash table. With bins scattered one per block (the worst
case) it costs 56-69 B per filled bin, about what THnSparse costs live.
Fill, Add, Merge, Scale, projections, axis ranges and the statistics come
from THnBase; contents are summed in double and stored as T as in THnSparse,
so bins and statistics are identical. Errors are too, except where the
compiler fuses w * w + sumw2 into one FMA in one fill path and not in the
other (GCC on aarch64): there they differ in the last bit. Bin numbers are
linear indices over all cells (as in THn) and GetNbins() counts the filled
bins (as in THnSparse); the THnBase functions that assume THnSparse's
numbering of the filled bins (ComputeIntegral, GetRandom, PrintEntries,
Print("c"), Fit) throw instead of using wrong bins. The type is meant to be
filled and written out; in O2Physics the registry THnSparse are only filled
(Fill, Sumw2, GetAxis, GetNdimensions, SetMinimum). Bin indices and
coordinates are range-checked; a custom streamer writes the filled bins as
(index, content, sumw2) arrays.
In the HistogramRegistry it is HistType::kCompactTHnSparseF/D, a one-word
change in histos.add(). At the end of the stream such a histogram is sent as
the equivalent THnSparseF/D, one at a time and after freeing its compact
storage, so output files and whatever reads or merges them are unchanged.
toTHnSparse() writes the filled bins straight into the THnSparse chunks, in
THnSparse's packed coordinate format (that of fCoordinates on file), without
building its transient hash table: THnSparse builds it on the first lookup, as
for a histogram read from a file. Going through THnBase::Add instead cost the
table, ~85 B per bin, at the end of the stream (about 1.1 GB for the largest
femto histogram below).
Measured (macOS):
813131 wagon test (1.1-2.7 M filled bins, 11-12 M fills each): 9.3-12.0 B
per filled bin and 85-110 ns per fill, against 67-69 B and 144-191 ns for
THnSparseF; peak RSS 12-34 MB against 203-462 MB; every bin identical.
new bins: at 0.5/4.8/18% of the cells filled, 106/213/385 ns per fill and
11/8/9 B per bin, against 180/229/258 ns and 61-69 B for THnSparseF.
Blocks of 2^16 bins save little memory but were up to 4.4x slower here.
kCompactTHnSparseF, same O2Physics, one test input each, end of stream
included; AnalysisResults.root identical bin by bin (errors, statistics):
3025 / 2265 MB against 872 / 765 MB, same run time (71 objects).
950 MB (183 objects).
1455 MB against 1105 / 1097 / 1151 MB (116 objects).
Tests: 11 Catch2 cases against a THnSparseF filled with the same data
(weights, Sumw2 switched on part way, under/overflow, variable binning, dense
and sparse blocks, conversion including zero-weight fills, Merge, streamer,
Clone, projections with axis ranges, out-of-range bins, the refused reads,
registry fill/get/addClone/size/output; the conversion with THnSparse
coordinates wider than 8 bytes and more than one chunk, then looked up,
streamed and filled again), and bins scattered over 30- to
62-bit key spaces with a memory bound.