Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-04 22:45:23

0001 #include <iostream>
0002 
0003 // framework
0004 #include "FWCore/Framework/interface/stream/EDProducer.h"
0005 
0006 #include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
0007 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/EventSetup.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "FWCore/ParameterSet/interface/EmptyGroupDescription.h"
0013 
0014 // algorithm specific
0015 
0016 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0017 
0018 #include "CUDADataFormats/EcalRecHitSoA/interface/EcalUncalibratedRecHit.h"
0019 
0020 class EcalCPUUncalibRecHitProducer : public edm::stream::EDProducer<edm::ExternalWork> {
0021 public:
0022   explicit EcalCPUUncalibRecHitProducer(edm::ParameterSet const& ps);
0023   ~EcalCPUUncalibRecHitProducer() override;
0024   static void fillDescriptions(edm::ConfigurationDescriptions&);
0025 
0026 private:
0027   void acquire(edm::Event const&, edm::EventSetup const&, edm::WaitingTaskWithArenaHolder) override;
0028   void produce(edm::Event&, edm::EventSetup const&) override;
0029 
0030 private:
0031   const bool isPhase2_;
0032   using InputProduct = cms::cuda::Product<ecal::UncalibratedRecHit<calo::common::DevStoragePolicy>>;
0033   const edm::EDGetTokenT<InputProduct> recHitsInEBToken_, recHitsInEEToken_;
0034   using OutputProduct = ecal::UncalibratedRecHit<calo::common::VecStoragePolicy<calo::common::CUDAHostAllocatorAlias>>;
0035   const edm::EDPutTokenT<OutputProduct> recHitsOutEBToken_, recHitsOutEEToken_;
0036 
0037   OutputProduct recHitsEB_, recHitsEE_;
0038   const bool containsTimingInformation_;
0039 };
0040 
0041 void EcalCPUUncalibRecHitProducer::fillDescriptions(edm::ConfigurationDescriptions& confDesc) {
0042   edm::ParameterSetDescription desc;
0043 
0044   desc.add<edm::InputTag>("recHitsInLabelEB", edm::InputTag{"ecalUncalibRecHitProducerGPU", "EcalUncalibRecHitsEB"});
0045   desc.add<std::string>("recHitsOutLabelEB", "EcalUncalibRecHitsEB");
0046   desc.add<bool>("containsTimingInformation", false);
0047   desc.ifValue(
0048       edm::ParameterDescription<bool>("isPhase2", false, true),
0049       false >> (edm::ParameterDescription<edm::InputTag>(
0050                     "recHitsInLabelEE", edm::InputTag{"ecalUncalibRecHitProducerGPU", "EcalUncalibRecHitsEE"}, true) and
0051                 edm::ParameterDescription<std::string>("recHitsOutLabelEE", "EcalUncalibRecHitsEE", true)) or
0052           true >> edm::EmptyGroupDescription());
0053 
0054   confDesc.add("ecalCPUUncalibRecHitProducer", desc);
0055 }
0056 
0057 EcalCPUUncalibRecHitProducer::EcalCPUUncalibRecHitProducer(const edm::ParameterSet& ps)
0058     : isPhase2_{ps.getParameter<bool>("isPhase2")},
0059       recHitsInEBToken_{consumes<InputProduct>(ps.getParameter<edm::InputTag>("recHitsInLabelEB"))},
0060       recHitsInEEToken_{isPhase2_ ? edm::EDGetTokenT<InputProduct>{}
0061                                   : consumes<InputProduct>(ps.getParameter<edm::InputTag>("recHitsInLabelEE"))},
0062       recHitsOutEBToken_{produces<OutputProduct>(ps.getParameter<std::string>("recHitsOutLabelEB"))},
0063       recHitsOutEEToken_{isPhase2_ ? edm::EDPutTokenT<OutputProduct>{}
0064                                    : produces<OutputProduct>(ps.getParameter<std::string>("recHitsOutLabelEE"))},
0065       containsTimingInformation_{ps.getParameter<bool>("containsTimingInformation")} {}
0066 
0067 EcalCPUUncalibRecHitProducer::~EcalCPUUncalibRecHitProducer() {}
0068 
0069 void EcalCPUUncalibRecHitProducer::acquire(edm::Event const& event,
0070                                            edm::EventSetup const& setup,
0071                                            edm::WaitingTaskWithArenaHolder taskHolder) {
0072   // retrieve data/ctx
0073   auto const& ebRecHitsProduct = event.get(recHitsInEBToken_);
0074   cms::cuda::ScopedContextAcquire ctx{ebRecHitsProduct, std::move(taskHolder)};
0075   auto const& ebRecHits = ctx.get(ebRecHitsProduct);
0076 
0077   // resize the output buffers
0078   recHitsEB_.resize(ebRecHits.size);
0079 
0080   auto lambdaToTransfer = [&ctx](auto& dest, auto* src) {
0081     using vector_type = typename std::remove_reference<decltype(dest)>::type;
0082     using type = typename vector_type::value_type;
0083     using src_type = typename std::remove_pointer<decltype(src)>::type;
0084     static_assert(std::is_same<src_type, type>::value && "dst and src data types do not match");
0085     cudaCheck(cudaMemcpyAsync(dest.data(), src, dest.size() * sizeof(type), cudaMemcpyDeviceToHost, ctx.stream()));
0086   };
0087 
0088   // enqeue transfers
0089   lambdaToTransfer(recHitsEB_.did, ebRecHits.did.get());
0090   lambdaToTransfer(recHitsEB_.amplitudesAll, ebRecHits.amplitudesAll.get());
0091   lambdaToTransfer(recHitsEB_.amplitude, ebRecHits.amplitude.get());
0092   lambdaToTransfer(recHitsEB_.chi2, ebRecHits.chi2.get());
0093   lambdaToTransfer(recHitsEB_.pedestal, ebRecHits.pedestal.get());
0094   lambdaToTransfer(recHitsEB_.flags, ebRecHits.flags.get());
0095   if (containsTimingInformation_) {
0096     lambdaToTransfer(recHitsEB_.jitter, ebRecHits.jitter.get());
0097     lambdaToTransfer(recHitsEB_.jitterError, ebRecHits.jitterError.get());
0098   }
0099   if (isPhase2_) {
0100     lambdaToTransfer(recHitsEB_.amplitudeError, ebRecHits.amplitudeError.get());
0101 
0102   } else {
0103     auto const& eeRecHitsProduct = event.get(recHitsInEEToken_);
0104     auto const& eeRecHits = ctx.get(eeRecHitsProduct);
0105     recHitsEE_.resize(eeRecHits.size);
0106     lambdaToTransfer(recHitsEE_.did, eeRecHits.did.get());
0107     lambdaToTransfer(recHitsEE_.amplitudesAll, eeRecHits.amplitudesAll.get());
0108     lambdaToTransfer(recHitsEE_.amplitude, eeRecHits.amplitude.get());
0109     lambdaToTransfer(recHitsEE_.chi2, eeRecHits.chi2.get());
0110     lambdaToTransfer(recHitsEE_.pedestal, eeRecHits.pedestal.get());
0111     lambdaToTransfer(recHitsEE_.flags, eeRecHits.flags.get());
0112     if (containsTimingInformation_) {
0113       lambdaToTransfer(recHitsEE_.jitter, eeRecHits.jitter.get());
0114       lambdaToTransfer(recHitsEE_.jitterError, eeRecHits.jitterError.get());
0115     }
0116   }
0117 }
0118 
0119 void EcalCPUUncalibRecHitProducer::produce(edm::Event& event, edm::EventSetup const& setup) {
0120   // tmp vectors
0121   auto recHitsOutEB = std::make_unique<OutputProduct>(std::move(recHitsEB_));
0122   // put into event
0123   event.put(recHitsOutEBToken_, std::move(recHitsOutEB));
0124 
0125   if (!isPhase2_) {
0126     auto recHitsOutEE = std::make_unique<OutputProduct>(std::move(recHitsEE_));
0127     event.put(recHitsOutEEToken_, std::move(recHitsOutEE));
0128   }
0129 }
0130 
0131 DEFINE_FWK_MODULE(EcalCPUUncalibRecHitProducer);