-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathDeviceSpecHelpers.cxx
More file actions
1507 lines (1373 loc) · 68.3 KB
/
DeviceSpecHelpers.cxx
File metadata and controls
1507 lines (1373 loc) · 68.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "DeviceSpecHelpers.h"
#include "ChannelSpecHelpers.h"
#include <wordexp.h>
#include <algorithm>
#include <boost/program_options.hpp>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unordered_set>
#include <vector>
#include "Framework/ChannelConfigurationPolicy.h"
#include "Framework/ChannelMatching.h"
#include "Framework/ConfigParamsHelper.h"
#include "Framework/ConfigParamRegistry.h"
#include "Framework/DeviceControl.h"
#include "Framework/DeviceSpec.h"
#include "Framework/DeviceState.h"
#include "Framework/Lifetime.h"
#include "Framework/LifetimeHelpers.h"
#include "Framework/ProcessingPolicies.h"
#include "Framework/OutputRoute.h"
#include "Framework/WorkflowSpec.h"
#include "Framework/ComputingResource.h"
#include "Framework/Logger.h"
#include "Framework/RuntimeError.h"
#include "Framework/RawDeviceService.h"
#include "ProcessingPoliciesHelpers.h"
#include "WorkflowHelpers.h"
#include <uv.h>
#include <iostream>
#include <fmt/format.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <csignal>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
namespace bpo = boost::program_options;
using namespace o2::framework;
namespace o2::framework
{
namespace detail
{
void timer_callback(uv_timer_t* handle)
{
// We simply wake up the event loop. Nothing to be done here.
auto* state = (DeviceState*)handle->data;
state->loopReason |= DeviceState::TIMER_EXPIRED;
state->loopReason |= DeviceState::DATA_INCOMING;
}
void signal_callback(uv_signal_t* handle, int)
{
// We simply wake up the event loop. Nothing to be done here.
auto* state = (DeviceState*)handle->data;
state->loopReason |= DeviceState::SIGNAL_ARRIVED;
state->loopReason |= DeviceState::DATA_INCOMING;
}
} // namespace detail
struct ExpirationHandlerHelpers {
static RouteConfigurator::CreationConfigurator dataDrivenConfigurator()
{
return [](DeviceState&, ServiceRegistry&, ConfigParamRegistry const&) { return LifetimeHelpers::dataDrivenCreation(); };
}
static RouteConfigurator::CreationConfigurator timeDrivenConfigurator(InputSpec const& matcher)
{
return [matcher](DeviceState& state, ServiceRegistry&, ConfigParamRegistry const& options) {
std::string rateName = std::string{"period-"} + matcher.binding;
auto period = options.get<int>(rateName.c_str());
// We create a timer to wake us up. Notice the actual
// timeslot creation and record expiration still happens
// in a synchronous way.
auto* timer = (uv_timer_t*)(malloc(sizeof(uv_timer_t)));
timer->data = &state;
uv_timer_init(state.loop, timer);
uv_timer_start(timer, detail::timer_callback, period / 1000, period / 1000);
state.activeTimers.push_back(timer);
return LifetimeHelpers::timeDrivenCreation(std::chrono::microseconds(period));
};
}
static RouteConfigurator::CreationConfigurator loopEventDrivenConfigurator(InputSpec const& matcher)
{
return [matcher](DeviceState& state, ServiceRegistry&, ConfigParamRegistry const&) {
return LifetimeHelpers::uvDrivenCreation(DeviceState::LoopReason::OOB_ACTIVITY, state);
};
}
static RouteConfigurator::CreationConfigurator signalDrivenConfigurator(InputSpec const& matcher, size_t inputTimeslice, size_t maxInputTimeslices)
{
return [matcher, inputTimeslice, maxInputTimeslices](DeviceState& state, ServiceRegistry&, ConfigParamRegistry const& options) {
std::string startName = std::string{"start-value-"} + matcher.binding;
std::string endName = std::string{"end-value-"} + matcher.binding;
std::string stepName = std::string{"step-value-"} + matcher.binding;
auto start = options.get<int64_t>(startName.c_str());
auto stop = options.get<int64_t>(endName.c_str());
auto step = options.get<int64_t>(stepName.c_str());
// We create a timer to wake us up. Notice the actual
// timeslot creation and record expiration still happens
// in a synchronous way.
auto* sh = (uv_signal_t*)(malloc(sizeof(uv_signal_t)));
uv_signal_init(state.loop, sh);
sh->data = &state;
uv_signal_start(sh, detail::signal_callback, SIGUSR1);
state.activeSignals.push_back(sh);
return LifetimeHelpers::enumDrivenCreation(start, stop, step, inputTimeslice, maxInputTimeslices, 1);
};
}
static RouteConfigurator::CreationConfigurator oobDrivenConfigurator()
{
return [](DeviceState& state, ServiceRegistry&, ConfigParamRegistry const&) {
return LifetimeHelpers::uvDrivenCreation(DeviceState::LoopReason::OOB_ACTIVITY, state);
};
}
static RouteConfigurator::CreationConfigurator enumDrivenConfigurator(InputSpec const& matcher, size_t inputTimeslice, size_t maxInputTimeslices)
{
return [matcher, inputTimeslice, maxInputTimeslices](DeviceState&, ServiceRegistry&, ConfigParamRegistry const& options) {
std::string startName = std::string{"start-value-"} + matcher.binding;
std::string endName = std::string{"end-value-"} + matcher.binding;
std::string stepName = std::string{"step-value-"} + matcher.binding;
auto start = options.get<int64_t>(startName.c_str());
auto stop = options.get<int64_t>(endName.c_str());
auto step = options.get<int64_t>(stepName.c_str());
auto repetitions = 1;
for (auto& meta : matcher.metadata) {
if (meta.name == "repetitions") {
repetitions = meta.defaultValue.get<int64_t>();
break;
}
}
return LifetimeHelpers::enumDrivenCreation(start, stop, step, inputTimeslice, maxInputTimeslices, repetitions);
};
}
static RouteConfigurator::DanglingConfigurator danglingTimeframeConfigurator()
{
return [](DeviceState&, ConfigParamRegistry const&) { return LifetimeHelpers::expireNever(); };
}
static RouteConfigurator::ExpirationConfigurator expiringTimeframeConfigurator()
{
return [](DeviceState&, ConfigParamRegistry const&) { return LifetimeHelpers::doNothing(); };
}
static RouteConfigurator::DanglingConfigurator danglingConditionConfigurator()
{
return [](DeviceState&, ConfigParamRegistry const& options) {
auto serverUrl = options.get<std::string>("condition-backend");
return LifetimeHelpers::expectCTP(serverUrl, true);
};
}
static RouteConfigurator::ExpirationConfigurator expiringConditionConfigurator(InputSpec const& spec, std::string const& sourceChannel)
{
return [spec, sourceChannel](DeviceState&, ConfigParamRegistry const& options) {
auto serverUrl = options.get<std::string>("condition-backend");
auto forceTimestamp = options.get<std::string>("condition-timestamp");
return LifetimeHelpers::fetchFromCCDBCache(spec, serverUrl, forceTimestamp, sourceChannel);
};
}
static RouteConfigurator::CreationConfigurator fairmqDrivenConfiguration(InputSpec const& spec, int inputTimeslice, int maxInputTimeslices)
{
return [spec, inputTimeslice, maxInputTimeslices](DeviceState& state, ServiceRegistry& services, ConfigParamRegistry const&) {
// std::string channelNameOption = std::string{"out-of-band-channel-name-"} + spec.binding;
// auto channelName = options.get<std::string>(channelNameOption.c_str());
std::string channelName = "upstream";
for (auto& meta : spec.metadata) {
if (meta.name != "channel-name") {
continue;
}
channelName = meta.defaultValue.get<std::string>();
}
auto device = services.get<RawDeviceService>().device();
auto& channel = device->fChannels[channelName];
// We assume there is always a ZeroMQ socket behind.
int zmq_fd = 0;
size_t zmq_fd_len = sizeof(zmq_fd);
auto* poller = (uv_poll_t*)malloc(sizeof(uv_poll_t));
channel[0].GetSocket().GetOption("fd", &zmq_fd, &zmq_fd_len);
if (zmq_fd == 0) {
throw runtime_error_f("Cannot get file descriptor for channel %s", channelName.c_str());
}
LOG(debug) << "Polling socket for " << channel[0].GetName();
state.activeOutOfBandPollers.push_back(poller);
// We always create entries whenever we get invoked.
// Notice this works only if we are the only input.
// Otherwise we should check the channel for new data,
// before we create an entry.
return LifetimeHelpers::enumDrivenCreation(0, -1, 1, inputTimeslice, maxInputTimeslices, 1);
};
}
static RouteConfigurator::DanglingConfigurator danglingOutOfBandConfigurator()
{
return [](DeviceState&, ConfigParamRegistry const&) {
return LifetimeHelpers::expireAlways();
};
}
static RouteConfigurator::ExpirationConfigurator expiringOutOfBandConfigurator(InputSpec const& spec)
{
return [spec](DeviceState&, ConfigParamRegistry const& options) {
std::string channelNameOption = std::string{"out-of-band-channel-name-"} + spec.binding;
auto channelName = options.get<std::string>(channelNameOption.c_str());
return LifetimeHelpers::fetchFromFairMQ(spec, channelName);
};
}
static RouteConfigurator::DanglingConfigurator danglingQAConfigurator()
{
// FIXME: this should really be expireAlways. However, since we do not have
// a proper backend for conditions yet, I keep it behaving like it was
// before.
return [](DeviceState&, ConfigParamRegistry const&) { return LifetimeHelpers::expireNever(); };
}
static RouteConfigurator::ExpirationConfigurator expiringQAConfigurator()
{
return [](DeviceState&, ConfigParamRegistry const&) { return LifetimeHelpers::fetchFromQARegistry(); };
}
static RouteConfigurator::DanglingConfigurator danglingTimerConfigurator(InputSpec const& matcher)
{
return [matcher](DeviceState&, ConfigParamRegistry const&) {
return LifetimeHelpers::expireAlways();
};
}
static RouteConfigurator::DanglingConfigurator danglingEnumerationConfigurator(InputSpec const& matcher)
{
return [matcher](DeviceState&, ConfigParamRegistry const&) {
return LifetimeHelpers::expireAlways();
};
}
static RouteConfigurator::ExpirationConfigurator expiringTimerConfigurator(InputSpec const& spec, std::string const& sourceChannel)
{
auto m = std::get_if<ConcreteDataMatcher>(&spec.matcher);
if (m == nullptr) {
throw runtime_error("InputSpec for Timers must be fully qualified");
}
// We copy the matcher to avoid lifetime issues.
return [matcher = *m, sourceChannel](DeviceState&, ConfigParamRegistry const&) {
// Timers do not have any orbit associated to them
return LifetimeHelpers::enumerate(matcher, sourceChannel, 0, 0);
};
}
static RouteConfigurator::ExpirationConfigurator expiringOOBConfigurator(InputSpec const& spec, std::string const& sourceChannel)
{
auto m = std::get_if<ConcreteDataMatcher>(&spec.matcher);
if (m == nullptr) {
throw runtime_error("InputSpec for OOB must be fully qualified");
}
// We copy the matcher to avoid lifetime issues.
return [matcher = *m, sourceChannel](DeviceState&, ConfigParamRegistry const&) {
// Timers do not have any orbit associated to them
return LifetimeHelpers::enumerate(matcher, sourceChannel, 0, 0);
};
}
static RouteConfigurator::ExpirationConfigurator expiringEnumerationConfigurator(InputSpec const& spec, std::string const& sourceChannel)
{
auto m = std::get_if<ConcreteDataMatcher>(&spec.matcher);
if (m == nullptr) {
throw runtime_error("InputSpec for Enumeration must be fully qualified");
}
// We copy the matcher to avoid lifetime issues.
return [matcher = *m, sourceChannel](DeviceState&, ConfigParamRegistry const& config) {
size_t orbitOffset = config.get<int64_t>("orbit-offset-enumeration");
size_t orbitMultiplier = config.get<int64_t>("orbit-multiplier-enumeration");
return LifetimeHelpers::enumerate(matcher, sourceChannel, orbitOffset, orbitMultiplier);
};
}
static RouteConfigurator::DanglingConfigurator danglingTransientConfigurator()
{
// FIXME: this should really be expireAlways. However, since we do not have
// a proper backend for conditions yet, I keep it behaving like it was
// before.
return [](DeviceState&, ConfigParamRegistry const&) { return LifetimeHelpers::expireNever(); };
}
static RouteConfigurator::ExpirationConfigurator expiringTransientConfigurator(InputSpec const&)
{
return [](DeviceState&, ConfigParamRegistry const&) { return LifetimeHelpers::fetchFromObjectRegistry(); };
}
/// This behaves as data. I.e. we never create it unless data arrives.
static RouteConfigurator::CreationConfigurator createOptionalConfigurator()
{
return [](DeviceState&, ServiceRegistry&, ConfigParamRegistry const&) { return LifetimeHelpers::dataDrivenCreation(); };
}
/// This will always exipire an optional record when no data is received.
static RouteConfigurator::DanglingConfigurator danglingOptionalConfigurator(std::vector<InputRoute> const& routes)
{
return [routes](DeviceState&, ConfigParamRegistry const&) { return LifetimeHelpers::expireIfPresent(routes, ConcreteDataMatcher{"FLP", "DISTSUBTIMEFRAME", 0}); };
}
/// When the record expires, simply create a dummy entry.
static RouteConfigurator::ExpirationConfigurator expiringOptionalConfigurator(InputSpec const& spec, std::string const& sourceChannel)
{
try {
ConcreteDataMatcher concrete = DataSpecUtils::asConcreteDataMatcher(spec);
return [concrete, sourceChannel](DeviceState&, ConfigParamRegistry const&) {
return LifetimeHelpers::dummy(concrete, sourceChannel);
};
} catch (...) {
ConcreteDataTypeMatcher dataType = DataSpecUtils::asConcreteDataTypeMatcher(spec);
ConcreteDataMatcher concrete{dataType.origin, dataType.description, 0xdeadbeef};
return [concrete, sourceChannel](DeviceState&, ConfigParamRegistry const&) {
return LifetimeHelpers::dummy(concrete, sourceChannel);
};
// We copy the matcher to avoid lifetime issues.
}
}
};
/// This creates a string to configure channels of a FairMQDevice
/// FIXME: support shared memory
std::string DeviceSpecHelpers::inputChannel2String(const InputChannelSpec& channel)
{
return fmt::format("{}type={},method={},address={},rateLogging={},rcvBufSize={},sndBufSize={}",
channel.name.empty() ? "" : "name=" + channel.name + ",",
ChannelSpecHelpers::typeAsString(channel.type),
ChannelSpecHelpers::methodAsString(channel.method),
ChannelSpecHelpers::channelUrl(channel),
channel.rateLogging,
channel.recvBufferSize,
channel.sendBufferSize);
}
std::string DeviceSpecHelpers::outputChannel2String(const OutputChannelSpec& channel)
{
return fmt::format("{}type={},method={},address={},rateLogging={},rcvBufSize={},sndBufSize={}",
channel.name.empty() ? "" : "name=" + channel.name + ",",
ChannelSpecHelpers::typeAsString(channel.type),
ChannelSpecHelpers::methodAsString(channel.method),
ChannelSpecHelpers::channelUrl(channel),
channel.rateLogging,
channel.recvBufferSize,
channel.sendBufferSize);
}
void DeviceSpecHelpers::processOutEdgeActions(std::vector<DeviceSpec>& devices,
std::vector<DeviceId>& deviceIndex,
std::vector<DeviceConnectionId>& connections,
ResourceManager& resourceManager,
const std::vector<size_t>& outEdgeIndex,
const std::vector<DeviceConnectionEdge>& logicalEdges,
const std::vector<EdgeAction>& actions, const WorkflowSpec& workflow,
const std::vector<OutputSpec>& outputsMatchers,
const std::vector<ChannelConfigurationPolicy>& channelPolicies,
std::string const& channelPrefix,
ComputingOffer const& defaultOffer)
{
// The topology cannot be empty or not connected. If that is the case, than
// something before this went wrong.
// FIXME: is that really true???
assert(!workflow.empty());
// Edges are navigated in order for each device, so the device associaited to
// an edge is always the last one created.
auto deviceForEdge = [&actions, &workflow, &devices,
&logicalEdges, &resourceManager,
&defaultOffer, &channelPrefix](size_t ei, ComputingOffer& acceptedOffer) {
auto& edge = logicalEdges[ei];
auto& action = actions[ei];
if (action.requiresNewDevice == false) {
assert(devices.empty() == false);
return devices.size() - 1;
}
if (acceptedOffer.hostname != "") {
resourceManager.notifyAcceptedOffer(acceptedOffer);
}
auto processor = workflow[edge.producer];
acceptedOffer.cpu = defaultOffer.cpu;
acceptedOffer.memory = defaultOffer.memory;
for (auto offer : resourceManager.getAvailableOffers()) {
if (offer.cpu < acceptedOffer.cpu) {
continue;
}
if (offer.memory < acceptedOffer.memory) {
continue;
}
acceptedOffer.hostname = offer.hostname;
acceptedOffer.startPort = offer.startPort;
acceptedOffer.rangeSize = 0;
break;
}
DeviceSpec device;
device.name = processor.name;
device.id = processor.name;
device.channelPrefix = channelPrefix;
if (processor.maxInputTimeslices != 1) {
device.id = processor.name + "_t" + std::to_string(edge.producerTimeIndex);
}
device.algorithm = processor.algorithm;
device.services = processor.requiredServices;
device.options = processor.options;
device.rank = processor.rank;
device.nSlots = processor.nSlots;
device.inputTimesliceId = edge.producerTimeIndex;
device.maxInputTimeslices = processor.maxInputTimeslices;
device.resource = {acceptedOffer};
device.labels = processor.labels;
/// If any of the inputs or outputs are "Lifetime::OutOfBand"
/// create the associated channels.
//
// for (auto& input : processor.inputs) {
// if (input.lifetime != Lifetime::OutOfBand) {
// continue;
// }
// InputChannelSpec extraInputChannelSpec{
// .name = "upstream",
// .type = ChannelType::Pair,
// .method = ChannelMethod::Bind,
// .hostname = "localhost",
// .port = 33000,
// .protocol = ChannelProtocol::IPC,
// };
// for (auto& meta : input.metadata) {
// if (meta.name == "name") {
// extraInputChannelSpec.name = meta.defaultValue.get<std::string>();
// }
// if (meta.name == "port") {
// extraInputChannelSpec.port = meta.defaultValue.get<int32_t>();
// }
// if (meta.name == "address") {
// extraInputChannelSpec.hostname = meta.defaultValue.get<std::string>();
// }
// }
// device.inputChannels.push_back(extraInputChannelSpec);
//}
for (auto& output : processor.outputs) {
if (output.lifetime != Lifetime::OutOfBand) {
continue;
}
OutputChannelSpec extraOutputChannelSpec{
.name = "downstream",
.type = ChannelType::Pair,
.method = ChannelMethod::Connect,
.hostname = "localhost",
.port = 33000,
.protocol = ChannelProtocol::IPC};
for (auto& meta : output.metadata) {
if (meta.name == "channel-name") {
extraOutputChannelSpec.name = meta.defaultValue.get<std::string>();
}
if (meta.name == "port") {
extraOutputChannelSpec.port = meta.defaultValue.get<int32_t>();
}
if (meta.name == "address") {
extraOutputChannelSpec.hostname = meta.defaultValue.get<std::string>();
}
}
device.outputChannels.push_back(extraOutputChannelSpec);
}
devices.push_back(device);
return devices.size() - 1;
};
auto channelFromDeviceEdgeAndPort = [&connections, &workflow, &channelPolicies](const DeviceSpec& device,
ComputingResource& deviceResource,
ComputingOffer& acceptedOffer,
const DeviceConnectionEdge& edge) {
OutputChannelSpec channel;
auto& consumer = workflow[edge.consumer];
std::string consumerDeviceId = consumer.name;
if (consumer.maxInputTimeslices != 1) {
consumerDeviceId += "_t" + std::to_string(edge.timeIndex);
}
channel.name = device.channelPrefix + "from_" + device.id + "_to_" + consumerDeviceId;
channel.port = acceptedOffer.startPort + acceptedOffer.rangeSize;
channel.hostname = acceptedOffer.hostname;
deviceResource.usedPorts += 1;
acceptedOffer.rangeSize += 1;
for (auto& policy : channelPolicies) {
if (policy.match(device.id, consumerDeviceId)) {
policy.modifyOutput(channel);
break;
}
}
DeviceConnectionId id{edge.producer, edge.consumer, edge.timeIndex, edge.producerTimeIndex, channel.port};
connections.push_back(id);
return channel;
};
auto isDifferentDestinationDeviceReferredBy = [&actions](size_t ei) { return actions[ei].requiresNewChannel; };
// This creates a new channel for a given edge, if needed. Notice that we
// navigate edges in a per device fashion (creating those if they are not
// alredy there) and create a new channel only if it connects two new
// devices. Whether or not this is the case was previously computed
// in the action.requiresNewChannel field.
auto createChannelForDeviceEdge = [&devices, &logicalEdges, &channelFromDeviceEdgeAndPort,
&deviceIndex](size_t di, size_t ei, ComputingOffer& offer) {
auto& device = devices[di];
auto& edge = logicalEdges[ei];
deviceIndex.emplace_back(DeviceId{edge.producer, edge.producerTimeIndex, di});
OutputChannelSpec channel = channelFromDeviceEdgeAndPort(device, device.resource, offer, edge);
device.outputChannels.push_back(channel);
return device.outputChannels.size() - 1;
};
// Notice how we need to behave in two different ways depending
// whether this is a real OutputRoute or if it's a forward from
// a previous consumer device.
// FIXME: where do I find the InputSpec for the forward?
auto appendOutputRouteToSourceDeviceChannel = [&outputsMatchers, &workflow, &devices, &logicalEdges](
size_t ei, size_t di, size_t ci) {
assert(ei < logicalEdges.size());
assert(di < devices.size());
assert(ci < devices[di].outputChannels.size());
auto& edge = logicalEdges[ei];
auto& device = devices[di];
assert(edge.consumer < workflow.size());
auto& consumer = workflow[edge.consumer];
auto& channel = devices[di].outputChannels[ci];
assert(edge.outputGlobalIndex < outputsMatchers.size());
if (edge.isForward == false) {
OutputRoute route{
edge.timeIndex,
consumer.maxInputTimeslices,
outputsMatchers[edge.outputGlobalIndex],
channel.name};
device.outputs.emplace_back(route);
} else {
ForwardRoute route{
edge.timeIndex,
consumer.maxInputTimeslices,
workflow[edge.consumer].inputs[edge.consumerInputIndex],
channel.name};
device.forwards.emplace_back(route);
}
};
auto sortDeviceIndex = [&deviceIndex]() { std::sort(deviceIndex.begin(), deviceIndex.end()); };
auto lastChannelFor = [&devices](size_t di) {
assert(di < devices.size());
assert(devices[di].outputChannels.empty() == false);
return devices[di].outputChannels.size() - 1;
};
//
// OUTER LOOP
//
// We need to create all the channels going out of a device, and associate
// routes to them for this reason
// we iterate over all the edges (which are per-datatype exchanged) and
// whenever we need to connect to a new device we create the channel. `device`
// here refers to the source device. This loop will therefore not create the
// devices which acts as sink, which are done in the preocessInEdgeActions
// function.
ComputingOffer acceptedOffer;
for (auto edge : outEdgeIndex) {
auto device = deviceForEdge(edge, acceptedOffer);
size_t channel = -1;
if (isDifferentDestinationDeviceReferredBy(edge)) {
channel = createChannelForDeviceEdge(device, edge, acceptedOffer);
} else {
channel = lastChannelFor(device);
}
appendOutputRouteToSourceDeviceChannel(edge, device, channel);
}
if (std::string(acceptedOffer.hostname) != "") {
resourceManager.notifyAcceptedOffer(acceptedOffer);
}
sortDeviceIndex();
}
void DeviceSpecHelpers::processInEdgeActions(std::vector<DeviceSpec>& devices,
std::vector<DeviceId>& deviceIndex,
const std::vector<DeviceConnectionId>& connections,
ResourceManager& resourceManager,
const std::vector<size_t>& inEdgeIndex,
const std::vector<DeviceConnectionEdge>& logicalEdges,
const std::vector<EdgeAction>& actions, const WorkflowSpec& workflow,
std::vector<LogicalForwardInfo> const& availableForwardsInfo,
std::vector<ChannelConfigurationPolicy> const& channelPolicies,
std::string const& channelPrefix,
ComputingOffer const& defaultOffer)
{
auto const& constDeviceIndex = deviceIndex;
auto findProducerForEdge = [&logicalEdges, &constDeviceIndex](size_t ei) {
auto& edge = logicalEdges[ei];
DeviceId pid{edge.producer, edge.producerTimeIndex, 0};
auto deviceIt = std::lower_bound(constDeviceIndex.cbegin(), constDeviceIndex.cend(), pid);
// By construction producer should always be there
assert(deviceIt != constDeviceIndex.end());
assert(deviceIt->processorIndex == pid.processorIndex && deviceIt->timeslice == pid.timeslice);
return deviceIt->deviceIndex;
};
auto findConsumerForEdge = [&logicalEdges, &constDeviceIndex](size_t ei) {
auto& edge = logicalEdges[ei];
if (!std::is_sorted(constDeviceIndex.cbegin(), constDeviceIndex.cend())) {
throw o2::framework::runtime_error("Needs a sorted vector to be correct");
}
DeviceId pid{edge.consumer, edge.timeIndex, 0};
auto deviceIt = std::lower_bound(constDeviceIndex.cbegin(), constDeviceIndex.cend(), pid);
// We search for a consumer only if we know it's is already there.
assert(deviceIt != constDeviceIndex.end());
assert(deviceIt->processorIndex == pid.processorIndex && deviceIt->timeslice == pid.timeslice);
return deviceIt->deviceIndex;
};
// Notice that to start with, consumer exists only if they also are
// producers, so we need to create one if it does not exist. Given this is
// stateful, we keep an eye on what edge was last searched to make sure we
// are not screwing up.
//
// Notice this is not thread safe.
decltype(deviceIndex.begin()) lastConsumerSearch;
size_t lastConsumerSearchEdge;
auto hasConsumerForEdge = [&lastConsumerSearch, &lastConsumerSearchEdge, &deviceIndex,
&logicalEdges](size_t ei) -> int {
auto& edge = logicalEdges[ei];
DeviceId cid{edge.consumer, edge.timeIndex, 0};
lastConsumerSearchEdge = ei; // This will invalidate the cache
lastConsumerSearch = std::lower_bound(deviceIndex.begin(), deviceIndex.end(), cid);
return lastConsumerSearch != deviceIndex.end() && cid.processorIndex == lastConsumerSearch->processorIndex &&
cid.timeslice == lastConsumerSearch->timeslice;
};
// The passed argument is there just to check. We do know that the last searched
// is the one we want.
auto getConsumerForEdge = [&lastConsumerSearch, &lastConsumerSearchEdge](size_t ei) {
assert(ei == lastConsumerSearchEdge);
return lastConsumerSearch->deviceIndex;
};
auto createNewDeviceForEdge = [&workflow, &logicalEdges, &devices,
&deviceIndex, &resourceManager, &defaultOffer,
&channelPrefix](size_t ei, ComputingOffer& acceptedOffer) {
auto& edge = logicalEdges[ei];
if (acceptedOffer.hostname != "") {
resourceManager.notifyAcceptedOffer(acceptedOffer);
}
auto& processor = workflow[edge.consumer];
acceptedOffer.cpu = defaultOffer.cpu;
acceptedOffer.memory = defaultOffer.memory;
for (auto offer : resourceManager.getAvailableOffers()) {
if (offer.cpu < acceptedOffer.cpu) {
continue;
}
if (offer.memory < acceptedOffer.memory) {
continue;
}
acceptedOffer.hostname = offer.hostname;
acceptedOffer.startPort = offer.startPort;
acceptedOffer.rangeSize = 0;
break;
}
DeviceSpec device;
device.name = processor.name;
device.id = processor.name;
device.channelPrefix = channelPrefix;
if (processor.maxInputTimeslices != 1) {
device.id += "_t" + std::to_string(edge.timeIndex);
}
device.algorithm = processor.algorithm;
device.services = processor.requiredServices;
device.options = processor.options;
device.rank = processor.rank;
device.nSlots = processor.nSlots;
device.inputTimesliceId = edge.timeIndex;
device.maxInputTimeslices = processor.maxInputTimeslices;
device.resource = {acceptedOffer};
device.labels = processor.labels;
// FIXME: maybe I should use an std::map in the end
// but this is really not performance critical
auto id = DeviceId{edge.consumer, edge.timeIndex, devices.size()};
devices.push_back(device);
deviceIndex.push_back(id);
std::sort(deviceIndex.begin(), deviceIndex.end());
return devices.size() - 1;
};
// We search for a preexisting outgoing connection associated to this edge.
// This is to retrieve the port of the source.
// This has to exists, because we already created all the outgoing connections
// so it's just a matter of looking it up.
auto findMatchingOutgoingPortForEdge = [&logicalEdges, &connections](size_t ei) {
auto const& edge = logicalEdges[ei];
DeviceConnectionId connectionId{edge.producer, edge.consumer, edge.timeIndex, edge.producerTimeIndex, 0};
auto it = std::lower_bound(connections.begin(), connections.end(), connectionId);
assert(it != connections.end());
assert(it->producer == connectionId.producer);
assert(it->consumer == connectionId.consumer);
assert(it->timeIndex == connectionId.timeIndex);
assert(it->producerTimeIndex == connectionId.producerTimeIndex);
return it->port;
};
auto checkNoDuplicatesFor = [](std::vector<InputChannelSpec> const& channels, const std::string& name) {
for (auto const& channel : channels) {
if (channel.name == name) {
return false;
}
}
return true;
};
auto appendInputChannelForConsumerDevice = [&devices, &connections, &checkNoDuplicatesFor, &channelPolicies](
size_t pi, size_t ci, unsigned short port) {
auto const& producerDevice = devices[pi];
auto& consumerDevice = devices[ci];
InputChannelSpec channel;
channel.name = producerDevice.channelPrefix + "from_" + producerDevice.id + "_to_" + consumerDevice.id;
channel.hostname = producerDevice.resource.hostname;
channel.port = port;
for (auto& policy : channelPolicies) {
if (policy.match(producerDevice.id, consumerDevice.id)) {
policy.modifyInput(channel);
break;
}
}
assert(checkNoDuplicatesFor(consumerDevice.inputChannels, channel.name));
consumerDevice.inputChannels.push_back(channel);
return consumerDevice.inputChannels.size() - 1;
};
// I think this is trivial, since I think it should always be the last one,
// in case it's not actually the case, I should probably do an actual lookup
// here.
auto getChannelForEdge = [&devices](size_t pi, size_t ci) {
auto& consumerDevice = devices[ci];
return consumerDevice.inputChannels.size() - 1;
};
// This is always called when adding a new channel, so we can simply refer
// to back. Notice also that this is the place where it makes sense to
// assign the forwarding, given that the forwarded stuff comes from some
// input.
auto appendInputRouteToDestDeviceChannel = [&devices, &logicalEdges, &workflow](size_t ei, size_t di, size_t ci) {
auto const& edge = logicalEdges[ei];
auto const& consumer = workflow[edge.consumer];
auto& consumerDevice = devices[di];
auto const& inputSpec = consumer.inputs[edge.consumerInputIndex];
auto const& sourceChannel = consumerDevice.inputChannels[ci].name;
InputRoute route{
inputSpec,
edge.consumerInputIndex,
sourceChannel,
edge.producerTimeIndex,
std::nullopt};
// In case we have wildcards, we must make sure that some other edge
// produced the same route, i.e. has the same matcher. Without this,
// otherwise, we would end up with as many input routes as the outputs that
// can be matched by the wildcard.
for (size_t iri = 0; iri < consumerDevice.inputs.size(); ++iri) {
auto& existingRoute = consumerDevice.inputs[iri];
if (existingRoute.timeslice != edge.producerTimeIndex) {
continue;
}
if (existingRoute.inputSpecIndex == edge.consumerInputIndex) {
return;
}
}
consumerDevice.inputs.push_back(route);
};
// Outer loop. A new device is needed for each
// of the sink data processors.
// New InputChannels need to refer to preexisting OutputChannels we create
// previously.
ComputingOffer acceptedOffer;
for (size_t edge : inEdgeIndex) {
auto& action = actions[edge];
size_t consumerDevice = -1;
if (action.requiresNewDevice) {
if (hasConsumerForEdge(edge)) {
consumerDevice = getConsumerForEdge(edge);
} else {
consumerDevice = createNewDeviceForEdge(edge, acceptedOffer);
}
} else {
consumerDevice = findConsumerForEdge(edge);
}
size_t producerDevice = findProducerForEdge(edge);
size_t channel = -1;
if (action.requiresNewChannel) {
int16_t port = findMatchingOutgoingPortForEdge(edge);
channel = appendInputChannelForConsumerDevice(producerDevice, consumerDevice, port);
} else {
channel = getChannelForEdge(producerDevice, consumerDevice);
}
appendInputRouteToDestDeviceChannel(edge, consumerDevice, channel);
}
// Bind the expiration mechanism to the input routes
for (auto& device : devices) {
for (auto& route : device.inputs) {
switch (route.matcher.lifetime) {
case Lifetime::OutOfBand:
route.configurator = {
.name = "oob",
.creatorConfigurator = ExpirationHandlerHelpers::loopEventDrivenConfigurator(route.matcher),
.danglingConfigurator = ExpirationHandlerHelpers::danglingOutOfBandConfigurator(),
.expirationConfigurator = ExpirationHandlerHelpers::expiringOOBConfigurator(route.matcher, route.sourceChannel)};
break;
// case Lifetime::Condition:
// route.configurator = {
// ExpirationHandlerHelpers::dataDrivenConfigurator(),
// ExpirationHandlerHelpers::danglingConditionConfigurator(),
// ExpirationHandlerHelpers::expiringConditionConfigurator(inputSpec, sourceChannel)};
// break;
case Lifetime::QA:
route.configurator = {
.name = "qa",
.creatorConfigurator = ExpirationHandlerHelpers::dataDrivenConfigurator(),
.danglingConfigurator = ExpirationHandlerHelpers::danglingQAConfigurator(),
.expirationConfigurator = ExpirationHandlerHelpers::expiringQAConfigurator()};
break;
case Lifetime::Timer:
route.configurator = {
.name = "timer",
.creatorConfigurator = ExpirationHandlerHelpers::timeDrivenConfigurator(route.matcher),
.danglingConfigurator = ExpirationHandlerHelpers::danglingTimerConfigurator(route.matcher),
.expirationConfigurator = ExpirationHandlerHelpers::expiringTimerConfigurator(route.matcher, route.sourceChannel)};
break;
case Lifetime::Enumeration:
route.configurator = {
.name = "enumeration",
.creatorConfigurator = ExpirationHandlerHelpers::enumDrivenConfigurator(route.matcher, device.inputTimesliceId, device.maxInputTimeslices),
.danglingConfigurator = ExpirationHandlerHelpers::danglingEnumerationConfigurator(route.matcher),
.expirationConfigurator = ExpirationHandlerHelpers::expiringEnumerationConfigurator(route.matcher, route.sourceChannel)};
break;
case Lifetime::Signal:
route.configurator = {
.name = "signal",
.creatorConfigurator = ExpirationHandlerHelpers::signalDrivenConfigurator(route.matcher, device.inputTimesliceId, device.maxInputTimeslices),
.danglingConfigurator = ExpirationHandlerHelpers::danglingEnumerationConfigurator(route.matcher),
.expirationConfigurator = ExpirationHandlerHelpers::expiringEnumerationConfigurator(route.matcher, route.sourceChannel)};
break;
case Lifetime::Transient:
route.configurator = {
.name = "transient",
.creatorConfigurator = ExpirationHandlerHelpers::dataDrivenConfigurator(),
.danglingConfigurator = ExpirationHandlerHelpers::danglingTransientConfigurator(),
.expirationConfigurator = ExpirationHandlerHelpers::expiringTransientConfigurator(route.matcher)};
break;
case Lifetime::Optional:
route.configurator = {
.name = "optional",
.creatorConfigurator = ExpirationHandlerHelpers::createOptionalConfigurator(),
.danglingConfigurator = ExpirationHandlerHelpers::danglingOptionalConfigurator(device.inputs),
.expirationConfigurator = ExpirationHandlerHelpers::expiringOptionalConfigurator(route.matcher, route.sourceChannel)};
break;
default:
break;
}
}
}
if (acceptedOffer.hostname != "") {
resourceManager.notifyAcceptedOffer(acceptedOffer);
}
}
// Construct the list of actual devices we want, given a workflow.
//
// FIXME: make start port configurable?
void DeviceSpecHelpers::dataProcessorSpecs2DeviceSpecs(const WorkflowSpec& workflow,
std::vector<ChannelConfigurationPolicy> const& channelPolicies,
std::vector<CompletionPolicy> const& completionPolicies,
std::vector<DispatchPolicy> const& dispatchPolicies,
std::vector<ResourcePolicy> const& resourcePolicies,
std::vector<CallbacksPolicy> const& callbacksPolicies,
std::vector<SendingPolicy> const& sendingPolicies,
std::vector<DeviceSpec>& devices,
ResourceManager& resourceManager,
std::string const& uniqueWorkflowId,
ConfigContext const& configContext,
bool optimizeTopology,
unsigned short resourcesMonitoringInterval,
std::string const& channelPrefix)
{
std::vector<LogicalForwardInfo> availableForwardsInfo;
std::vector<DeviceConnectionEdge> logicalEdges;
std::vector<DeviceConnectionId> connections;
std::vector<DeviceId> deviceIndex;
// This is a temporary store for inputs and outputs,
// including forwarded channels, so that we can construct
// them before assigning to a device.
std::vector<OutputSpec> outputs;
WorkflowHelpers::constructGraph(workflow, logicalEdges, outputs, availableForwardsInfo);
// We need to instanciate one device per (me, timeIndex) in the
// DeviceConnectionEdge. For each device we need one new binding
// server per (me, other) -> port Moreover for each (me, other,
// outputGlobalIndex) we need to insert either an output or a
// forward.
//
// We then sort by other. For each (other, me) we need to connect to
// port (me, other) and add an input.
// Fill an index to do the sorting
std::vector<size_t> inEdgeIndex;
std::vector<size_t> outEdgeIndex;
WorkflowHelpers::sortEdges(inEdgeIndex, outEdgeIndex, logicalEdges);
std::vector<EdgeAction> outActions = WorkflowHelpers::computeOutEdgeActions(logicalEdges, outEdgeIndex);
// Crete the connections on the inverse map for all of them
// lookup for port and add as input of the current device.
std::vector<EdgeAction> inActions = WorkflowHelpers::computeInEdgeActions(logicalEdges, inEdgeIndex);
size_t deviceCount = 0;
for (auto& action : outActions) {
deviceCount += action.requiresNewDevice ? 1 : 0;
}
for (auto& action : inActions) {
deviceCount += action.requiresNewDevice ? 1 : 0;
}
ComputingOffer defaultOffer;
for (auto& offer : resourceManager.getAvailableOffers()) {
defaultOffer.cpu += offer.cpu;
defaultOffer.memory += offer.memory;
}
/// For the moment lets play it safe and underestimate default needed resources.
defaultOffer.cpu /= deviceCount + 1;
defaultOffer.memory /= deviceCount + 1;
processOutEdgeActions(devices, deviceIndex, connections, resourceManager, outEdgeIndex, logicalEdges,
outActions, workflow, outputs, channelPolicies, channelPrefix, defaultOffer);
// FIXME: is this not the case???
std::sort(connections.begin(), connections.end());
processInEdgeActions(devices, deviceIndex, connections, resourceManager, inEdgeIndex, logicalEdges,
inActions, workflow, availableForwardsInfo, channelPolicies, channelPrefix, defaultOffer);
// We apply the completion policies here since this is where we have all the
// devices resolved.
for (auto& device : devices) {
for (auto& policy : completionPolicies) {
if (policy.matcher(device) == true) {
device.completionPolicy = policy;
break;
}
}
for (auto& policy : dispatchPolicies) {