Skip to content

Commit ee2cc96

Browse files
committed
update FlatLutEntry
1 parent fc73bd9 commit ee2cc96

File tree

2 files changed

+110
-57
lines changed

2 files changed

+110
-57
lines changed

ALICE3/Core/FlatLutEntry.cxx

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "FlatLutEntry.h"
1313

1414
#include <Framework/Logger.h>
15+
#include <Framework/RuntimeError.h>
1516

1617
#include <cstring>
1718

@@ -89,9 +90,9 @@ void FlatLutData::initialize(const lutHeader_t& header)
8990
size_t totalSize = headerSize + entriesSize;
9091

9192
mData.resize(totalSize);
92-
9393
// Write header at the beginning
9494
std::memcpy(mData.data(), &header, headerSize);
95+
updateRef();
9596
}
9697

9798
size_t FlatLutData::getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
@@ -105,69 +106,95 @@ size_t FlatLutData::getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt
105106
return headerSize + linearIdx * sizeof(lutEntry_t);
106107
}
107108

108-
lutEntry_t* FlatLutData::getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin)
109+
const lutEntry_t* FlatLutData::getEntryRef(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
109110
{
110111
size_t offset = getEntryOffset(nch_bin, rad_bin, eta_bin, pt_bin);
111-
return reinterpret_cast<lutEntry_t*>(mData.data() + offset);
112+
return reinterpret_cast<const lutEntry_t*>(mDataRef.data() + offset);
112113
}
113114

114-
const lutEntry_t* FlatLutData::getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
115+
lutEntry_t* FlatLutData::getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin)
115116
{
116117
size_t offset = getEntryOffset(nch_bin, rad_bin, eta_bin, pt_bin);
117-
return reinterpret_cast<const lutEntry_t*>(mData.data() + offset);
118+
return reinterpret_cast<lutEntry_t*>(mData.data() + offset);
118119
}
119120

120-
FlatLutData FlatLutData::fromBuffer(const uint8_t* buffer, size_t size)
121+
const lutHeader_t& FlatLutData::getHeaderRef() const
121122
{
122-
FlatLutData data;
123-
// Validate buffer
124-
if (size < sizeof(lutHeader_t)) {
125-
LOG(fatal) << "Buffer too small for LUT header";
126-
}
123+
return *reinterpret_cast<const lutHeader_t*>(mDataRef.data());
124+
}
127125

128-
const auto* header = reinterpret_cast<const lutHeader_t*>(buffer);
129-
data.mNchBins = header->nchmap.nbins;
130-
data.mRadBins = header->radmap.nbins;
131-
data.mEtaBins = header->etamap.nbins;
132-
data.mPtBins = header->ptmap.nbins;
126+
lutHeader_t& FlatLutData::getHeader()
127+
{
128+
return *reinterpret_cast<lutHeader_t*>(mData.data());
129+
}
133130

134-
size_t expectedSize = sizeof(lutHeader_t) + static_cast<size_t>(data.mNchBins) * data.mRadBins * data.mEtaBins * data.mPtBins * sizeof(lutEntry_t);
131+
void FlatLutData::updateRef()
132+
{
133+
mDataRef = std::span{mData.data(), mData.size()};
134+
}
135135

136-
if (size < expectedSize) {
137-
LOG(fatal) << "Buffer size mismatch: expected " << expectedSize << ", got " << size;
138-
}
136+
void FlatLutData::cacheDimensions()
137+
{
138+
auto const& header = getHeaderRef();
139+
mNchBins = header.nchmap.nbins;
140+
mRadBins = header.radmap.nbins;
141+
mEtaBins = header.etamap.nbins;
142+
mPtBins = header.ptmap.nbins;
143+
}
139144

140-
// Copy buffer
141-
data.mData.resize(size);
142-
std::memcpy(data.mData.data(), buffer, size);
145+
void FlatLutData::adopt(const uint8_t* buffer, size_t size)
146+
{
147+
mData.resize(size);
148+
std::memcpy(mData.data(), buffer, size);
149+
updateRef();
150+
cacheDimensions();
151+
}
143152

144-
return data;
153+
void FlatLutData::view(const uint8_t* buffer, size_t size)
154+
{
155+
mData.clear();
156+
mDataRef = std::span{buffer, size};
157+
cacheDimensions();
145158
}
146159

147-
FlatLutData FlatLutData::fromExternalBuffer(uint8_t* buffer, size_t size)
160+
void FlatLutData::validateBuffer(const uint8_t* buffer, size_t size)
148161
{
149-
FlatLutData data;
150162
// Validate buffer
151163
if (size < sizeof(lutHeader_t)) {
152-
LOG(fatal) << "Buffer too small for LUT header";
164+
throw framework::runtime_error_f("Buffer too small for LUT header: expected at least %zu, got %zu", sizeof(lutHeader_t), size);
153165
}
154166

155167
const auto* header = reinterpret_cast<const lutHeader_t*>(buffer);
156-
data.mNchBins = header->nchmap.nbins;
157-
data.mRadBins = header->radmap.nbins;
158-
data.mEtaBins = header->etamap.nbins;
159-
data.mPtBins = header->ptmap.nbins;
168+
auto mNchBins = header->nchmap.nbins;
169+
auto mRadBins = header->radmap.nbins;
170+
auto mEtaBins = header->etamap.nbins;
171+
auto mPtBins = header->ptmap.nbins;
160172

161-
size_t expectedSize = sizeof(lutHeader_t) + static_cast<size_t>(data.mNchBins) * data.mRadBins * data.mEtaBins * data.mPtBins * sizeof(lutEntry_t);
173+
size_t expectedSize = sizeof(lutHeader_t) + static_cast<size_t>(mNchBins) * mRadBins * mEtaBins * mPtBins * sizeof(lutEntry_t);
162174

163175
if (size < expectedSize) {
164-
LOG(fatal) << "Buffer size mismatch: expected " << expectedSize << " got " << size;
176+
throw framework::runtime_error_f("Buffer size mismatch: expected %zu, got %zu", expectedSize, size);
165177
}
178+
}
166179

167-
// Store reference to external buffer (no copy)
168-
// WARNING: Caller must ensure buffer lifetime exceeds FlatLutData usage
169-
data.mData.assign(buffer, buffer + size);
180+
FlatLutData FlatLutData::AdoptFromBuffer(const uint8_t* buffer, size_t size)
181+
{
182+
validateBuffer(buffer, size);
183+
FlatLutData data;
184+
185+
// Copy buffer
186+
data.adopt(buffer, size);
187+
return data;
188+
}
170189

190+
FlatLutData FlatLutData::ViewFromBuffer(const uint8_t* buffer, size_t size)
191+
{
192+
validateBuffer(buffer, size);
193+
FlatLutData data;
194+
195+
// Store reference to external buffer
196+
// WARNING: Caller must ensure buffer lifetime exceeds FlatLutData usage
197+
data.view(buffer, size);
171198
return data;
172199
}
173200

ALICE3/Core/FlatLutEntry.h

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <cmath>
1616
#include <cstdint>
17+
#include <span>
1718
#include <vector>
1819

1920
#define LUTCOVM_VERSION 20260408
@@ -100,51 +101,76 @@ class FlatLutData
100101
void initialize(const lutHeader_t& header);
101102

102103
/**
103-
* @brief Get LUT entry by bin indices
104-
* O(1) access via linear index calculation
104+
* @brief Get LUT entry by bin indices (view)
105+
*/
106+
const lutEntry_t* getEntryRef(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const;
107+
108+
/**
109+
* @brief Get LUT entry by bin indices (owned)
105110
*/
106111
lutEntry_t* getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin);
107-
const lutEntry_t* getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const;
108112

109113
/**
110-
* @brief Get raw data buffer (for serialization/shared memory)
114+
* @brief Get LUT header (view)
111115
*/
112-
uint8_t* data() { return mData.data(); }
113-
const uint8_t* data() const { return mData.data(); }
116+
const lutHeader_t& getHeaderRef() const;
114117

115118
/**
116-
* @brief Total size in bytes
119+
* @brief Get LUT header (owned)
117120
*/
118-
size_t bytes() const { return mData.size(); }
121+
lutHeader_t& getHeader();
119122

120123
/**
121-
* @brief Construct from external buffer (e.g., shared memory or file mapping)
124+
* @brief Get raw data buffer
122125
*/
123-
static FlatLutData fromBuffer(const uint8_t* buffer, size_t size);
126+
uint8_t* data() { return mData.data(); } // owned
127+
const uint8_t* data() const { return mDataRef.data(); } //view
124128

125129
/**
126-
* @brief Reference-based access without copying
127-
* Useful when data is already in shared memory
130+
* @brief Total size in bytes
128131
*/
129-
static FlatLutData fromExternalBuffer(uint8_t* buffer, size_t size);
132+
size_t bytes() const { return mDataRef.size(); }
130133

131-
const lutHeader_t& header() const
132-
{
133-
return *reinterpret_cast<const lutHeader_t*>(mData.data());
134-
}
134+
/**
135+
* @brief Construct a new FlatLutData from external buffer as a copy
136+
*/
137+
static FlatLutData AdoptFromBuffer(const uint8_t* buffer, size_t size);
135138

136-
lutHeader_t& header()
137-
{
138-
return *reinterpret_cast<lutHeader_t*>(mData.data());
139-
}
139+
/**
140+
* @brief Construct a new FlatLutData from external buffer as a view
141+
*/
142+
static FlatLutData ViewFromBuffer(const uint8_t* buffer, size_t size);
140143

141144
private:
142145
/**
143146
* @brief Linear index calculation for entry access
144147
*/
145148
size_t getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const;
146149

150+
/**
151+
* @brief Update dimensions from the current header
152+
*/
153+
void cacheDimensions();
154+
155+
/**
156+
* @brief Adopt a buffer by copying
157+
*/
158+
void adopt(const uint8_t* buffer, size_t size);
159+
/**
160+
* @brief Adopt a buffer as a view
161+
*/
162+
void view(const uint8_t* buffer, size_t size);
163+
/**
164+
* @brief Update mDataRef from mData
165+
*/
166+
void updateRef();
167+
/**
168+
* @brief Validate external buffer
169+
*/
170+
static void validateBuffer(const uint8_t* buffer, size_t size);
171+
147172
std::vector<uint8_t> mData;
173+
std::span<uint8_t const> mDataRef;
148174

149175
// Cache dimensions for quick access
150176
int mNchBins = 0;

0 commit comments

Comments
 (0)