Project
Loading...
Searching...
No Matches
CompletionPolicyHelpers.cxx
Go to the documentation of this file.
1// Copyright 2019-2026 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15#include "Framework/InputSpan.h"
18#include "Framework/Logger.h"
21#include "DecongestionService.h"
22#include "Framework/Signpost.h"
23#include <fairmq/shmem/Message.h>
24
25#include <cassert>
26#include <regex>
27
29
30namespace o2::framework
31{
32
34{
35 auto matcher = [name](DeviceSpec const& device) -> bool {
36 return std::regex_match(device.name.begin(), device.name.end(), std::regex(name));
37 };
38
39 auto originReceived = std::make_shared<std::vector<uint64_t>>();
40
41 auto callback = [originReceived, origin, op](InputSpan const& inputRefs, std::vector<InputSpec> const&, ServiceRegistryRef&) -> CompletionPolicy::CompletionOp {
42 // update list of the start times of inputs with origin @origin
43 for (auto& ref : inputRefs) {
44 if (ref.header != nullptr) {
45 auto header = CompletionPolicyHelpers::getHeader<o2::header::DataHeader>(ref);
46 if (header->dataOrigin.str == origin) {
47 auto startTime = DataRefUtils::getHeader<DataProcessingHeader*>(ref)->startTime;
48 auto it = std::find(originReceived->begin(), originReceived->end(), startTime);
49 if (it == originReceived->end()) {
50 originReceived->emplace_back(startTime);
51 }
52 }
53 }
54 }
55
56 // find out if all inputs which are not of origin @origin have a corresponding entry in originReceived
57 // if one is missing then we have to wait
58 for (auto& ref : inputRefs) {
59 if (ref.header != nullptr) {
60 auto header = CompletionPolicyHelpers::getHeader<o2::header::DataHeader>(ref);
61 if (header->dataOrigin.str != origin) {
62 auto startTime = DataRefUtils::getHeader<DataProcessingHeader*>(ref)->startTime;
63 auto it = std::find(originReceived->begin(), originReceived->end(), startTime);
64 if (it == originReceived->end()) {
65 LOGP(info, "Have to wait until message of origin {} with startTime {} has arrived.", origin, startTime);
67 }
68 }
69 }
70 }
71 return op;
72 };
73 return CompletionPolicy{"wait-origin", matcher, callback};
74
76}
77
79{
80 auto matcher = [name](DeviceSpec const& device) -> bool {
81 return std::regex_match(device.name.begin(), device.name.end(), std::regex(name));
82 };
83 auto callback = [op](InputSpan const&, std::vector<InputSpec> const& specs, ServiceRegistryRef& ref) -> CompletionPolicy::CompletionOp {
84 return op;
85 };
86 switch (op) {
88 return consumeWhenAny(name.c_str(), matcher);
89 break;
91 return CompletionPolicy{"consume-existing", matcher, callback};
92 break;
94 return CompletionPolicy{"always-process", matcher, callback};
95 break;
97 return CompletionPolicy{"always-wait", matcher, callback};
98 break;
100 return CompletionPolicy{"always-discard", matcher, callback, false};
101 break;
103 return CompletionPolicy{"always-rescan", matcher, callback};
104 break;
106 return CompletionPolicy{"retry", matcher, callback};
107 break;
108 }
110}
111
113{
114 auto callback = [](InputSpan const& inputs, std::vector<InputSpec> const& specs, ServiceRegistryRef& ref) -> CompletionPolicy::CompletionOp {
115 assert(inputs.size() == specs.size());
116 O2_SIGNPOST_ID_GENERATE(sid, completion);
117 O2_SIGNPOST_START(completion, sid, "consumeWhenAll", "Completion policy invoked");
118
119 size_t si = 0;
120 int sporadicCount = 0;
121 int timeframeCount = 0;
122 int missingSporadicCount = 0;
123 bool needsProcessing = false;
124 size_t currentTimeslice = -1;
125 for (auto& input : inputs) {
126 assert(si < specs.size());
127 auto& spec = specs[si++];
128 sporadicCount += spec.lifetime == Lifetime::Sporadic ? 1 : 0;
129 timeframeCount += spec.lifetime == Lifetime::Timeframe ? 1 : 0;
130 // If we are missing something which is not sporadic, we wait.
131 if (input.header == nullptr && spec.lifetime != Lifetime::Sporadic) {
132 O2_SIGNPOST_END(completion, sid, "consumeWhenAll", "Completion policy returned %{public}s due to missing input %lu", "Wait", si);
134 }
135 // If we are missing something which is sporadic, we wait until we are sure it will not come.
136 if (input.header == nullptr && spec.lifetime == Lifetime::Sporadic) {
137 O2_SIGNPOST_EVENT_EMIT(completion, sid, "consumeWhenAll", "Missing sporadic found for route index %lu", si);
138 missingSporadicCount += 1;
139 }
140 // If we have a header, we use it to determine the current timesliceIsTimer
141 // (unless this is a timer which does not enter the oldest possible timeslice).
142 if (input.header != nullptr && currentTimeslice == -1) {
143 auto const* dph = framework::DataRefUtils::getHeader<o2::framework::DataProcessingHeader*>(input);
144 if (dph && !TimingInfo::timesliceIsTimer(dph->startTime)) {
145 currentTimeslice = dph->startTime;
146 O2_SIGNPOST_EVENT_EMIT(completion, sid, "consumeWhenAll", "currentTimeslice %lu from route index %lu", currentTimeslice, si);
147 }
148 }
149 // If we have a header, we need to process it if it is not a condition object.
150 if (input.header != nullptr && spec.lifetime != Lifetime::Condition) {
151 needsProcessing = true;
152 }
153 }
154 // If some sporadic inputs are missing, we wait for them util we are sure they will not come,
155 // i.e. until the oldest possible timeslice is beyond the timeslice of the input.
156 auto& timesliceIndex = ref.get<TimesliceIndex>();
157 auto oldestPossibleTimeslice = timesliceIndex.getOldestPossibleInput().timeslice.value;
158
159 if (missingSporadicCount && currentTimeslice >= oldestPossibleTimeslice) {
160 O2_SIGNPOST_END(completion, sid, "consumeWhenAll", "Completion policy returned %{public}s for timeslice %lu > oldestPossibleTimeslice %lu", "Retry", currentTimeslice, oldestPossibleTimeslice);
162 }
163
164 // No need to process if we have only sporadic inputs and they are all missing.
165 if (needsProcessing && (sporadicCount > 0) && (missingSporadicCount == sporadicCount) && (timeframeCount == 0)) {
166 O2_SIGNPOST_END(completion, sid, "consumeWhenAll", "Completion policy returned %{public}s for timeslice %lu", "Discard", currentTimeslice);
168 }
169 auto consumes = (needsProcessing || sporadicCount == 0);
170 O2_SIGNPOST_END(completion, sid, "consumeWhenAll", "Completion policy returned %{public}s for timeslice %lu", consumes ? "Consume" : "Discard", currentTimeslice);
172 };
173 return CompletionPolicy{name, matcher, callback};
174}
175
177{
178 auto callbackFull = [](InputSpan const& inputs, std::vector<InputSpec> const&, ServiceRegistryRef& ref) -> CompletionPolicy::CompletionOp {
179 auto& decongestionService = ref.get<DecongestionService>();
180 decongestionService.orderedCompletionPolicyActive = true;
181 for (auto& input : inputs) {
182 if (input.header == nullptr) {
184 }
185 long int startTime = framework::DataRefUtils::getHeader<o2::framework::DataProcessingHeader*>(input)->startTime;
186 if (startTime == 0) {
187 LOGP(debug, "startTime is 0, which means we have the first message, so we can process it.");
188 decongestionService.nextTimeslice = 0;
189 }
190 if (framework::DataRefUtils::isValid(input) && startTime != decongestionService.nextTimeslice) {
192 }
193 }
194 decongestionService.nextTimeslice++;
196 };
197 return CompletionPolicy{name, matcher, callbackFull};
198}
199
201{
202 auto matcher = [matchName](DeviceSpec const& device) -> bool {
203 return std::regex_match(device.name.begin(), device.name.end(), std::regex(matchName));
204 };
205 return consumeWhenAllOrdered(matcher);
206}
207
209{
210 return CompletionPolicy{
211 name,
212 matcher,
213 [](InputSpan const& inputs, std::vector<InputSpec> const& specs, ServiceRegistryRef&) -> CompletionPolicy::CompletionOp {
214 size_t present = 0;
215 size_t current = 0;
216 size_t withPayload = 0;
217 size_t sporadic = 0;
218 size_t maxSporadic = 0;
219 size_t i = 0;
220 for (auto& input : inputs) {
221 auto& spec = specs[i++];
222 if (spec.lifetime == Lifetime::Sporadic) {
223 maxSporadic++;
224 }
225 if (input.header != nullptr) {
226 present++;
227 if (spec.lifetime == Lifetime::Sporadic) {
228 sporadic++;
229 }
230 }
231 if (input.payload != nullptr) {
232 withPayload++;
233 }
234 current++;
235 }
236 // * In case we have all inputs but the sporadic ones: Consume, since we do not know if the sporadic ones
237 // will ever come.
238 // * In case we have only sporadic inputs: Consume, since we do not know if we already Consumed
239 // the non sporadic ones above.
240 // * In case we do not have payloads: Wait
241 // * In all other cases we consume what is there, but we wait for the non sporadic ones to be complete
242 // (i.e. we wait for present + maxSporadic).
243 if (present - sporadic + maxSporadic == inputs.size()) {
245 } else if (present - sporadic == 0) {
247 } else if (withPayload == 0) {
249 }
251 }};
252}
253
255{
256 auto callback = [](InputSpan const& inputs, std::vector<InputSpec> const&, ServiceRegistryRef& ref) -> CompletionPolicy::CompletionOp {
257 for (size_t i = 0; i < inputs.size(); ++i) {
258 if (inputs.get(i).header != nullptr && inputs.getRefCount(i) == 1) {
260 }
261 }
263 };
264 return CompletionPolicy{name, matcher, callback, false};
265}
266
268{
269 auto callback = [](InputSpan const& inputs, std::vector<InputSpec> const&, ServiceRegistryRef& ref) -> CompletionPolicy::CompletionOp {
270 auto& decongestionService = ref.get<DecongestionService>();
271 decongestionService.consumeWhenPastOldestPossibleTimeframeActive = true;
272 size_t currentTimeslice = -1;
273 for (auto& input : inputs) {
274 if (input.header == nullptr) {
275 continue;
276 }
277 o2::framework::DataProcessingHeader const* dph = o2::header::get<o2::framework::DataProcessingHeader*>(input.header);
278 if (dph && !TimingInfo::timesliceIsTimer(dph->startTime)) {
279 currentTimeslice = dph->startTime;
280 break;
281 }
282 }
283
284 auto& timesliceIndex = ref.get<TimesliceIndex>();
285 auto oldestPossibleTimeslice = timesliceIndex.getOldestPossibleInput().timeslice.value;
286
287 if (currentTimeslice >= oldestPossibleTimeslice) {
289 }
291 };
292 return CompletionPolicy{name, matcher, callback, false};
293}
294
296{
297 auto callback = [](InputSpan const& inputs, std::vector<InputSpec> const&, ServiceRegistryRef& ref) -> CompletionPolicy::CompletionOp {
298 for (auto& input : inputs) {
299 if (input.header != nullptr) {
301 }
302 }
304 };
305 return CompletionPolicy{name, matcher, callback, false};
306}
307
309{
310 auto matcher = [matchName](DeviceSpec const& device) -> bool {
311 return std::regex_match(device.name.begin(), device.name.end(), std::regex(matchName));
312 };
313 return consumeWhenAny(matcher);
314}
315
317{
318 auto callback = [](InputSpan const& inputs, std::vector<InputSpec> const& specs, ServiceRegistryRef&) -> CompletionPolicy::CompletionOp {
319 bool canConsume = false;
320 bool hasConditions = false;
321 bool conditionMissing = false;
322 size_t timeslice = -1;
323 static size_t timesliceOK = -1; // FIXME: This breaks start/stop/start, since it must be reset!
324 // FIXME: Also, this just checks the max timeslice that was already consumed.
325 // In case timeslices do not come in order, we might have consumed a later
326 // condition object, but not the one for the current time slice.
327 // But I don't see any possibility to handle this in a better way.
328
329 // Iterate on all specs and all inputs simultaneously
330 for (auto it = inputs.begin(), end = inputs.end(); it != end; ++it) {
331 char const* header = (*it).header;
332 auto& spec = specs[it.position()];
333 // In case a condition object is not there, we need to wait.
334 if (header != nullptr) {
335 canConsume = true;
336 }
337 if (spec.lifetime == Lifetime::Condition) {
338 hasConditions = true;
339 if (header == nullptr) {
340 conditionMissing = true;
341 }
342 }
343 }
344 if (canConsume || conditionMissing) {
345 for (auto it = inputs.begin(), end = inputs.end(); it != end; ++it) {
346 for (auto const& ref : it.parts()) {
348 continue;
349 }
350 auto const* dph = framework::DataRefUtils::getHeader<o2::framework::DataProcessingHeader*>(ref);
351 if (dph && !TimingInfo::timesliceIsTimer(dph->startTime)) {
352 timeslice = dph->startTime;
353 break;
354 }
355 }
356 if (timeslice != -1) {
357 break;
358 }
359 }
360 }
361
362 // If there are no conditions, just consume.
363 if (!hasConditions) {
364 canConsume = true;
365 } else if (conditionMissing && (timeslice == -1 || timesliceOK == -1 || timeslice > timesliceOK)) {
367 }
368
369 if (canConsume && timeslice != -1 && (timeslice > timesliceOK || timesliceOK == -1)) {
370 timesliceOK = timeslice;
371 }
373 };
374 return CompletionPolicy{name, matcher, callback, false};
375}
376
378{
379 auto matcher = [matchName](DeviceSpec const& device) -> bool {
380 return std::regex_match(device.name.begin(), device.name.end(), std::regex(matchName));
381 };
382 return consumeWhenAnyWithAllConditions(matcher);
383}
384
386{
387 auto callback = [](InputSpan const& inputs, std::vector<InputSpec> const&, ServiceRegistryRef& ref) -> CompletionPolicy::CompletionOp {
388 size_t present = 0;
389 for (auto& input : inputs) {
390 if (input.header != nullptr) {
391 present++;
392 }
393 }
394 if (present == inputs.size()) {
396 } else if (present == 0) {
398 }
400 };
401 return CompletionPolicy{name, matcher, callback};
402}
403
404} // namespace o2::framework
header::DataOrigin origin
#define O2_BUILTIN_UNREACHABLE
std::ostringstream debug
std::vector< std::string > header
int32_t i
uint32_t op
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this Except pad from cluster finding if number of consecutive charges in a fragment is above this Threshold where a timebin is considered disabling the noisy pad check for that pad Treat signal above this minimum as saturated Smoothing factor for the exponential Highly Ionising Particle tail filter Number of timebins to use for the averaging Add additional custom track errors when crossing relative as fraction of Do N fit passes in final fit of< 0:reject at max -n hits") AddOptionRTC(rejectIFCLowRadiusCluster, uint8_t, 1, "", 0, "Reject clusters that get the IFC mask error during refit") AddOptionRTC(dEdxTruncLow, uint8_t, 2, "", 0, "Low truncation threshold, fraction of 128") AddOptionRTC(dEdxTruncHigh, uint8_t, 77, "", 0, "High truncation threshold, fraction of 128") AddOptionRTC(extrapolationTracking, int8_t, 1, "", 0, "Enable Extrapolation Tracking(prolong tracks to adjacent sectors to find short segments)") AddOptionRTC(disableRefitAttachment, uint8_t, 0, "", 0, "Bitmask to disable certain attachment steps during refit(1:attachment, 2:propagation, 4:loop following, 8:mirroring)") AddOptionRTC(rejectionStrategy, uint8_t, o2::gpu::GPUSettings::RejectionStrategyA, "", 0, "Enable rejection of TPC clusters for compression(0=no, 1=strategy A, 2=strategy B)") AddOptionRTC(mergeLoopersAfterburner, uint8_t, 1, "", 0, "Run afterburner for additional looper merging") AddOptionRTC(compressionTypeMask, uint8_t, o2::gpu::GPUSettings::CompressionFull, "", 0, "TPC Compression mode bits(1=truncate charge/width LSB, 2=differences, 4=track-model)") AddOptionRTC(compressionSortOrder, uint8_t, o2::gpu::GPUSettings::SortTime, "", 0, "Sort order of TPC compression(0=time, 1=pad, 2=Z-time-pad, 3=Z-pad-time, 4=no sorting(use incoming order))") AddOptionRTC(sigBitsCharge, uint8_t, 4, "", 0, "Number of significant bits for TPC cluster charge in compression mode 1") AddOptionRTC(sigBitsWidth, uint8_t, 3, "", 0, "Number of significant bits for TPC cluster width in compression mode 1") AddOptionRTC(dropLoopers, uint8_t, 0, "", 0, "Drop looping tracks starting from second loop") AddOptionRTC(mergerInterpolateErrors, uint8_t, 1, "", 0, "Use interpolation instead of extrapolation for chi2 based cluster rejection") AddOptionRTC(mergerInterpolateRejectAlsoOnCurrentPosition, uint8_t, 1, "", 0, "When using mergerInterpolateErrors, reject based on chi2 twice computed with interpolated and current track position starting from NDF > mergerNonInterpolateRejectMinNDF Merge tracks accross the central electrode Enable PID response Add sub threshold clusters in TPC dEdx computation OR mask of TPC flags that will reject the cluster in alternative dEdx Reject edge clusters based on uncorrected track Y during track fit Increase decrease removal by when on this number of pads from the edge h configStandalone Reconstruction rec_trd Max chi2 for TRD tracklets to be matched to a track Chi2 cut for strict matching mode Number of sigmas for ITS TPC track time error estimate Addition to search road around track prolongation along Y in cm Optimal tracklet rphi resolution in maximum chi2 per attached tracklet for TRD tracks take into account the full likelihood of the slope including exponential tails Tracks with less attached tracklets are discarded after the tracking Post trigger Pile up integration time in BCs fits all iterations but the last one in projections Use material lookup table for TPC refit Apply workarounds to avoid cyclic merge should not be needed tpc dyn configStandalone Processing proc_rtc Replace constant variables static by constexpr expressions Compile RTC in deterministic with NO_FAST_MATH flags and GPUCA_DETERMINISTIC_MODE define Use RTC to optimize GPU code h configStandalone Processing proc_rtctech Use a file lock to serialize access to the cache folder Print launch bounds used for RTC code as debugging for exit after Allow RTC with slave GPUReconstruction instances with optConstexpr and optSpecialcode Maximum number of RTC cache files per backend in the cache the oldest are Prepend RTC compilation commands by this string Load a parameter object containing the launch bounds from a file h configStandalone Processing proc_param TPC Cluster Z Error Parameterization h configStandalone Processing settings for neural network proc_nn std::string Specify inference device(cpu(default), rocm, cuda)") AddOption(nnInferenceDeviceId
#define O2_DECLARE_DYNAMIC_LOG(name)
Definition Signpost.h:490
#define O2_SIGNPOST_END(log, id, name, format,...)
Definition Signpost.h:609
#define O2_SIGNPOST_ID_GENERATE(name, log)
Definition Signpost.h:507
#define O2_SIGNPOST_EVENT_EMIT(log, id, name, format,...)
Definition Signpost.h:523
#define O2_SIGNPOST_START(log, id, name, format,...)
Definition Signpost.h:603
const_iterator end() const
Definition InputSpan.h:239
const_iterator begin() const
Definition InputSpan.h:234
size_t size() const
Number of elements in the InputSpan.
Definition InputSpan.h:102
DataRef get(size_t i, size_t partidx=0) const
i-th element of the InputSpan (O(partidx) sequential scan via indices protocol)
Definition InputSpan.h:47
int getRefCount(size_t i) const
Definition InputSpan.h:90
OldestInputInfo getOldestPossibleInput() const
GLuint GLuint end
Definition glcorearb.h:469
GLuint const GLchar * name
Definition glcorearb.h:781
GLint ref
Definition glcorearb.h:291
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
static CompletionPolicy consumeWhenPastOldestPossibleTimeframe(const char *name, CompletionPolicy::Matcher matcher)
static CompletionPolicy processWhenAny(const char *name, CompletionPolicy::Matcher matcher)
static CompletionPolicy consumeWhenAllOrdered(const char *name, CompletionPolicy::Matcher matcher)
as consumeWhenAll, but ensures that records are processed with incremental timeSlice (DataHeader::sta...
static CompletionPolicy defineByName(std::string const &name, CompletionPolicy::CompletionOp op)
static CompletionPolicy consumeExistingWhenAny(const char *name, CompletionPolicy::Matcher matcher)
static CompletionPolicy consumeWhenAll(const char *name, CompletionPolicy::Matcher matcher)
Default Completion policy. When all the parts of a record have arrived, consume them.
static CompletionPolicy consumeWhenAnyWithAllConditions(const char *name, CompletionPolicy::Matcher matcher)
When any of the parts of the record have been received, consume them.
static CompletionPolicy consumeWhenAnyZeroCount(const char *name, CompletionPolicy::Matcher matcher)
When any of the parts which has arrived has a refcount of 1.
static CompletionPolicy defineByNameOrigin(std::string const &name, std::string const &origin, CompletionPolicy::CompletionOp op)
static CompletionPolicy consumeWhenAny(const char *name, CompletionPolicy::Matcher matcher)
When any of the parts of the record have been received, consume them.
CompletionOp
Action to take with the InputRecord:
@ Retry
Like Wait but mark the cacheline as dirty.
std::function< bool(DeviceSpec const &device)> Matcher
static bool isValid(DataRef const &ref)
const char * header
Definition DataRef.h:28
bool orderedCompletionPolicyActive
Ordered completion policy is active.
bool consumeWhenPastOldestPossibleTimeframeActive
consumeWhenPastOldestPossibleTimeframe completion policy is active.
static bool timesliceIsTimer(size_t timeslice)
Definition TimingInfo.h:46