Skip to content

DPL Analysis: add CompactTHnSparse, a THnSparse with compact bin storage - #16198

Merged
ktf merged 1 commit into
AliceO2Group:devfrom
ktf:pr16198
Nov 26, 2026
Merged

ktf merged 1 commit into
AliceO2Group:devfrom
ktf:pr16198

Conversation

@ktf

@ktf ktf commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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):

  • 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.

@ktf
ktf requested a review from a team as a code owner November 25, 2026 21:44
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.
@ktf

ktf commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

New feature. Nothing uses it. Merging.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant