54 ,
fPool(2 * clusterBunchSize)
65 std::unique_lock<std::mutex> lock(fLockWorkQueue);
67 fCvHasReadWork.notify_one();
73 std::unique_lock<std::mutex> lock(fLockUnzipQueue);
75 fCvHasUnzipWork.notify_one();
86 std::deque<RUnzipItem> unzipItems;
89 std::unique_lock<std::mutex> lock(fLockUnzipQueue);
90 fCvHasUnzipWork.wait(lock, [&]{
return !fUnzipQueue.empty(); });
91 std::swap(unzipItems, fUnzipQueue);
94 for (
auto &item : unzipItems) {
98 fPageSource.UnzipCluster(item.fCluster.get());
101 item.fPromise.set_value(std::move(item.fCluster));
109 std::deque<RReadItem> readItems;
112 std::unique_lock<std::mutex> lock(fLockWorkQueue);
113 fCvHasReadWork.wait(lock, [&]{
return !fReadQueue.empty(); });
114 std::swap(readItems, fReadQueue);
117 while (!readItems.empty()) {
118 std::vector<RCluster::RKey> clusterKeys;
119 std::int64_t bunchId = -1;
120 for (
unsigned i = 0; i < readItems.size(); ++i) {
121 const auto &item = readItems[i];
128 if ((bunchId >= 0) && (item.fBunchId != bunchId))
130 bunchId = item.fBunchId;
131 clusterKeys.emplace_back(item.fClusterKey);
134 auto clusters = fPageSource.LoadClusters(clusterKeys);
135 bool unzipQueueDirty =
false;
136 for (std::size_t i = 0; i < clusters.size(); ++i) {
141 std::unique_lock<std::mutex> lock(fLockWorkQueue);
142 discard = std::any_of(fInFlightClusters.begin(), fInFlightClusters.end(),
143 [thisClusterId = clusters[i]->GetId()](
auto &inFlight) {
144 return inFlight.fClusterKey.fClusterId == thisClusterId && inFlight.fIsExpired;
149 readItems[i].fPromise.set_value(std::move(clusters[i]));
152 std::unique_lock<std::mutex> lock(fLockUnzipQueue);
153 fUnzipQueue.emplace_back(
RUnzipItem{std::move(clusters[i]), std::move(readItems[i].fPromise)});
154 unzipQueueDirty =
true;
157 readItems.erase(readItems.begin(), readItems.begin() + clusters.size());
159 fCvHasUnzipWork.notify_one();
167 for (
const auto &cptr : fPool) {
168 if (cptr && (cptr->GetId() == clusterId))
176 auto N = fPool.size();
177 for (
unsigned i = 0; i <
N; ++i) {
196 std::int64_t fBunchId = -1;
198 ColumnSet_t fPhysicalColumnSet;
201 static constexpr std::int64_t kFlagRequired = 0x01;
202 static constexpr std::int64_t kFlagLast = 0x02;
205 std::map<DescriptorId_t, RInfo> fMap;
210 fMap.emplace(clusterId, info);
213 bool Contains(DescriptorId_t clusterId) {
214 return fMap.count(clusterId) > 0;
217 std::size_t GetSize()
const {
return fMap.size(); }
219 void Erase(DescriptorId_t clusterId,
const ColumnSet_t &physicalColumns)
221 auto itr = fMap.find(clusterId);
222 if (itr == fMap.end())
225 std::copy_if(itr->second.fPhysicalColumnSet.begin(), itr->second.fPhysicalColumnSet.end(),
226 std::inserter(
d,
d.end()),
227 [&physicalColumns](DescriptorId_t needle) { return physicalColumns.count(needle) == 0; });
231 itr->second.fPhysicalColumnSet =
d;
235 decltype(fMap)::iterator begin() {
return fMap.begin(); }
236 decltype(fMap)::iterator
end() {
return fMap.end(); }
245 std::set<DescriptorId_t> keep;
248 auto descriptorGuard = fPageSource.GetSharedDescriptorGuard();
251 auto prev = clusterId;
252 for (
unsigned int i = 0; i < fWindowPre; ++i) {
253 prev = descriptorGuard->FindPrevClusterId(prev);
260 RProvides::RInfo provideInfo;
261 provideInfo.fPhysicalColumnSet = physicalColumns;
262 provideInfo.fBunchId = fBunchId;
263 provideInfo.fFlags = RProvides::kFlagRequired;
264 for (
DescriptorId_t i = 0, next = clusterId; i < 2 * fClusterBunchSize; ++i) {
265 if (i == fClusterBunchSize)
266 provideInfo.fBunchId = ++fBunchId;
269 next = descriptorGuard->FindNextClusterId(cid);
271 provideInfo.fFlags |= RProvides::kFlagLast;
273 provide.Insert(cid, provideInfo);
277 provideInfo.fFlags = 0;
282 for (
auto &cptr : fPool) {
285 if (provide.Contains(cptr->GetId()) > 0)
287 if (keep.count(cptr->GetId()) > 0)
299 std::lock_guard<std::mutex> lockGuard(fLockWorkQueue);
301 for (
auto itr = fInFlightClusters.begin(); itr != fInFlightClusters.end(); ) {
304 !provide.Contains(itr->fClusterKey.fClusterId) && (keep.count(itr->fClusterKey.fClusterId) == 0);
306 if (itr->fFuture.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
308 provide.Erase(itr->fClusterKey.fClusterId, itr->fClusterKey.fPhysicalColumnSet);
313 auto cptr = itr->fFuture.get();
315 if (!cptr || itr->fIsExpired) {
317 itr = fInFlightClusters.erase(itr);
322 auto existingCluster = FindInPool(cptr->GetId());
323 if (existingCluster) {
324 existingCluster->Adopt(std::move(*cptr));
326 auto idxFreeSlot = FindFreeSlot();
327 fPool[idxFreeSlot] = std::move(cptr);
329 itr = fInFlightClusters.erase(itr);
333 for (
auto &cptr : fPool) {
336 provide.Erase(cptr->GetId(), cptr->GetAvailPhysicalColumns());
340 bool skipPrefetch =
false;
341 if (provide.GetSize() < fClusterBunchSize) {
343 for (
const auto &kv : provide) {
344 if ((kv.second.fFlags & (RProvides::kFlagRequired | RProvides::kFlagLast)) == 0)
346 skipPrefetch =
false;
356 for (
const auto &kv : provide) {
357 R__ASSERT(!kv.second.fPhysicalColumnSet.empty());
361 readItem.
fBunchId = kv.second.fBunchId;
368 fInFlightClusters.emplace_back(std::move(inFlightCluster));
370 fReadQueue.emplace_back(std::move(readItem));
372 if (fReadQueue.size() > 0)
373 fCvHasReadWork.notify_one();
377 return WaitFor(clusterId, physicalColumns);
386 auto result = FindInPool(clusterId);
388 bool hasMissingColumn =
false;
389 for (
auto cid : physicalColumns) {
390 if (
result->ContainsColumn(cid))
393 hasMissingColumn =
true;
396 if (!hasMissingColumn)
401 decltype(fInFlightClusters)::iterator itr;
403 std::lock_guard<std::mutex> lockGuardInFlightClusters(fLockWorkQueue);
404 itr = fInFlightClusters.begin();
405 for (; itr != fInFlightClusters.end(); ++itr) {
406 if (itr->fClusterKey.fClusterId == clusterId)
409 R__ASSERT(itr != fInFlightClusters.end());
416 auto cptr = itr->fFuture.get();
418 result->Adopt(std::move(*cptr));
420 auto idxFreeSlot = FindFreeSlot();
421 fPool[idxFreeSlot] = std::move(cptr);
424 std::lock_guard<std::mutex> lockGuardInFlightClusters(fLockWorkQueue);
425 fInFlightClusters.erase(itr);
433 decltype(fInFlightClusters)::iterator itr;
435 std::lock_guard<std::mutex> lockGuardInFlightClusters(fLockWorkQueue);
436 itr = fInFlightClusters.begin();
437 if (itr == fInFlightClusters.end())
443 std::lock_guard<std::mutex> lockGuardInFlightClusters(fLockWorkQueue);
444 fInFlightClusters.erase(itr);
#define R__unlikely(expr)
std::ios_base::fmtflags fFlags
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Managed a set of clusters containing compressed and packed pages.
RCluster * FindInPool(DescriptorId_t clusterId) const
Every cluster id has at most one corresponding RCluster pointer in the pool.
std::vector< std::unique_ptr< RCluster > > fPool
The cache of clusters around the currently active cluster.
void ExecUnzipClusters()
The unzip thread routine which takes a loaded cluster and passes it to fPageSource....
size_t FindFreeSlot() const
Returns an index of an unused element in fPool; callers of this function (GetCluster() and WaitFor())...
RCluster * GetCluster(DescriptorId_t clusterId, const RCluster::ColumnSet_t &physicalColumns)
Returns the requested cluster either from the pool or, in case of a cache miss, lets the I/O thread l...
void WaitForInFlightClusters()
Used by the unit tests to drain the queue of clusters to be preloaded.
RClusterPool(RPageSource &pageSource, unsigned int clusterBunchSize)
void ExecReadClusters()
The I/O thread routine, there is exactly one I/O thread in-flight for every cluster pool.
std::thread fThreadUnzip
The unzip thread takes a loaded cluster and passes it to fPageSource->UnzipCluster() on it.
RPageSource & fPageSource
Every cluster pool is responsible for exactly one page source that triggers loading of the clusters (...
std::thread fThreadIo
The I/O thread calls RPageSource::LoadClusters() asynchronously.
unsigned int fClusterBunchSize
The number of clusters that are being read in a single vector read.
RCluster * WaitFor(DescriptorId_t clusterId, const RCluster::ColumnSet_t &physicalColumns)
Returns the given cluster from the pool, which needs to contain at least the columns physicalColumns.
An in-memory subset of the packed and compressed pages of a cluster.
std::unordered_set< DescriptorId_t > ColumnSet_t
Abstract interface to read data from an ntuple.
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
constexpr DescriptorId_t kInvalidDescriptorId
void Erase(const T &that, std::vector< T > &v)
Erase that element from vector v
Clusters that are currently being processed by the pipeline.
bool operator<(const RInFlightCluster &other) const
First order by cluster id, then by number of columns, than by the column ids in fColumns.
RCluster::RKey fClusterKey
std::future< std::unique_ptr< RCluster > > fFuture
Request to load a subset of the columns of a particular cluster.
std::int64_t fBunchId
Items with different bunch ids are scheduled for different vector reads.
std::promise< std::unique_ptr< RCluster > > fPromise
RCluster::RKey fClusterKey
Request to decompress and if necessary unpack compressed pages.
DescriptorId_t fClusterId
ColumnSet_t fPhysicalColumnSet