|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +#include "Framework/InputSpec.h" |
| 12 | +#include "Framework/DataProcessorSpec.h" |
| 13 | +#include "Framework/DataSpecUtils.h" |
| 14 | +#include "Framework/ParallelContext.h" |
| 15 | +#include "Framework/runDataProcessing.h" |
| 16 | +#include "Framework/ControlService.h" |
| 17 | +#include "Framework/ParallelContext.h" |
| 18 | +#include <iostream> |
| 19 | +#include <algorithm> |
| 20 | +#include <memory> |
| 21 | +#include <unordered_map> |
| 22 | + |
| 23 | +#define ASSERT_ERROR(condition) \ |
| 24 | + if ((condition) == false) { \ |
| 25 | + LOG(ERROR) << R"(Test condition ")" #condition R"(" failed)"; \ |
| 26 | + } |
| 27 | + |
| 28 | +using DataHeader = o2::header::DataHeader; |
| 29 | +using namespace o2::framework; |
| 30 | + |
| 31 | +size_t nPipelines = 4; |
| 32 | +size_t nParallelChannels = 6; |
| 33 | +size_t nRolls = 1; |
| 34 | + |
| 35 | +std::vector<DataProcessorSpec> defineDataProcessing(ConfigContext const&) |
| 36 | +{ |
| 37 | + // define a template workflow with processors to be executed in a pipeline |
| 38 | + std::vector<DataProcessorSpec> workflowSpecs{ |
| 39 | + { "processor1", |
| 40 | + Inputs{ |
| 41 | + { "input", "TST", "TRIGGER", 0, Lifetime::Timeframe } }, |
| 42 | + Outputs{ |
| 43 | + { { "output" }, "TST", "PREPROC", 0, Lifetime::Timeframe } }, |
| 44 | + AlgorithmSpec{ [](ProcessingContext& ctx) { |
| 45 | + for (auto const& input : ctx.inputs()) { |
| 46 | + auto const& parallelContext = ctx.services().get<ParallelContext>(); |
| 47 | + std::cout << "instance " << parallelContext.index1D() << " of " << parallelContext.index1DSize() << ": " |
| 48 | + << *input.spec << ": " << *((int*)input.payload) << std::endl; |
| 49 | + auto const* dataheader = DataRefUtils::getHeader<o2::header::DataHeader*>(input); |
| 50 | + //auto data& = ctx.outputs().make<int>(OutputRef{"output", dataheader->subSpecification}); |
| 51 | + auto& data = ctx.outputs().make<int>(Output{ "TST", "PREPROC", dataheader->subSpecification, Lifetime::Timeframe }); |
| 52 | + ASSERT_ERROR(ctx.inputs().get<int>(input.spec->binding.c_str()) == parallelContext.index1D()); |
| 53 | + data = parallelContext.index1D(); |
| 54 | + } |
| 55 | + } } }, |
| 56 | + { "processor2", |
| 57 | + Inputs{ |
| 58 | + { "input", "TST", "PREPROC", 0, Lifetime::Timeframe } }, |
| 59 | + Outputs{ |
| 60 | + { { "output" }, "TST", "DATA", 0, Lifetime::Timeframe }, |
| 61 | + { { "metadt" }, "TST", "META", 0, Lifetime::Timeframe } }, |
| 62 | + AlgorithmSpec{ [](ProcessingContext& ctx) { |
| 63 | + for (auto const& input : ctx.inputs()) { |
| 64 | + auto const& parallelContext = ctx.services().get<ParallelContext>(); |
| 65 | + std::cout << "instance " << parallelContext.index1D() << " of " << parallelContext.index1DSize() << ": " |
| 66 | + << *input.spec << ": " << *((int*)input.payload) << std::endl; |
| 67 | + ASSERT_ERROR(ctx.inputs().get<int>(input.spec->binding.c_str()) == parallelContext.index1D()); |
| 68 | + auto const* dataheader = DataRefUtils::getHeader<o2::header::DataHeader*>(input); |
| 69 | + // TODO: there is a bug in the API for using OutputRef, returns an rvalue which can not be bound to |
| 70 | + // lvalue reference |
| 71 | + //auto& data = ctx.outputs().make<int>(OutputRef{"output", dataheader->subSpecification}); |
| 72 | + auto& data = ctx.outputs().make<int>(Output{ "TST", "DATA", dataheader->subSpecification, Lifetime::Timeframe }); |
| 73 | + data = ctx.inputs().get<int>(input.spec->binding.c_str()); |
| 74 | + //auto meta& = ctx.outputs().make<int>(OutputRef{"metadt", dataheader->subSpecification}); |
| 75 | + auto& meta = ctx.outputs().make<int>(Output{ "TST", "META", dataheader->subSpecification, Lifetime::Timeframe }); |
| 76 | + meta = dataheader->subSpecification; |
| 77 | + } |
| 78 | + } } }, |
| 79 | + }; |
| 80 | + |
| 81 | + // create parallel pipelines from the template workflow, the number of parallel channel is defined by |
| 82 | + // nParallelChannels and is distributed among the pipelines |
| 83 | + std::vector<o2::header::DataHeader::SubSpecificationType> subspecs(nParallelChannels); |
| 84 | + std::generate(subspecs.begin(), subspecs.end(), [counter = std::make_shared<int>(0)]() { return 0x1 << (*counter)++; }); |
| 85 | + workflowSpecs = parallelPipeline(workflowSpecs, nPipelines, |
| 86 | + [&subspecs]() { return subspecs.size(); }, |
| 87 | + [&subspecs](size_t index) { return subspecs[index]; }); |
| 88 | + |
| 89 | + // define a producer process with outputs for all subspecs |
| 90 | + auto producerOutputs = [&subspecs]() { |
| 91 | + Outputs outputs; |
| 92 | + for (auto const& subspec : subspecs) { |
| 93 | + outputs.emplace_back("TST", "TRIGGER", subspec, Lifetime::Timeframe); |
| 94 | + } |
| 95 | + return outputs; |
| 96 | + }; |
| 97 | + |
| 98 | + // we keep the correspondence between the subspec and the instance which serves this particular subspec |
| 99 | + // this is checked in the final consumer |
| 100 | + auto checkMap = std::make_shared<std::unordered_map<o2::header::DataHeader::SubSpecificationType, int>>(); |
| 101 | + workflowSpecs.emplace_back(DataProcessorSpec{ |
| 102 | + "trigger", |
| 103 | + Inputs{}, |
| 104 | + producerOutputs(), |
| 105 | + AlgorithmSpec{ [subspecs, checkMap, counter = std::make_shared<int>(0)](ProcessingContext& ctx) { |
| 106 | + if (*counter < nRolls) { |
| 107 | + size_t multiplicity = subspecs.size() / nPipelines; |
| 108 | + if (subspecs.size() % nPipelines) { |
| 109 | + multiplicity++; |
| 110 | + } |
| 111 | + size_t instance = 0; |
| 112 | + size_t inputRank = 0; |
| 113 | + for (size_t index = 0, end = subspecs.size(); index < end; index++) { |
| 114 | + ctx.outputs().make<int>(Output{ "TST", "TRIGGER", subspecs[index], Lifetime::Timeframe }) = instance; |
| 115 | + (*checkMap)[subspecs[index]] = instance; |
| 116 | + if (++inputRank == multiplicity) { |
| 117 | + inputRank = 0; |
| 118 | + instance++; |
| 119 | + if (instance < nPipelines && ((subspecs.size() - index - 1) % (nPipelines - instance)) == 0) { |
| 120 | + multiplicity = subspecs.size() / nPipelines; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + (*counter)++; |
| 125 | + } |
| 126 | + if (*counter == nRolls) { |
| 127 | + ctx.services().get<ControlService>().readyToQuit(false); |
| 128 | + } |
| 129 | + } } }); |
| 130 | + |
| 131 | + // the final consumer |
| 132 | + workflowSpecs.emplace_back(DataProcessorSpec{ |
| 133 | + "consumer", |
| 134 | + mergeInputs({ { "datain", "TST", "DATA", 0, Lifetime::Timeframe }, |
| 135 | + { "metain", "TST", "META", 0, Lifetime::Timeframe } }, |
| 136 | + subspecs.size(), |
| 137 | + [&subspecs](InputSpec& input, size_t index) { |
| 138 | + DataSpecUtils::updateMatchingSubspec(input, subspecs[index]); |
| 139 | + }), |
| 140 | + Outputs(), |
| 141 | + AlgorithmSpec{ [checkMap](ProcessingContext& ctx) { |
| 142 | + for (auto const& input : ctx.inputs()) { |
| 143 | + std::cout << "consuming : " << *input.spec << ": " << *((int*)input.payload) << std::endl; |
| 144 | + auto const* dataheader = DataRefUtils::getHeader<o2::header::DataHeader*>(input); |
| 145 | + if (input.spec->binding.compare(0, 6, "datain") == 0) { |
| 146 | + ASSERT_ERROR((*checkMap)[dataheader->subSpecification] == ctx.inputs().get<int>(input.spec->binding.c_str())); |
| 147 | + } |
| 148 | + } |
| 149 | + ctx.services().get<ControlService>().readyToQuit(true); |
| 150 | + } } }); |
| 151 | + |
| 152 | + return workflowSpecs; |
| 153 | +} |
0 commit comments