Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:30:33

0001 //#define ECAL_RECO_CUDA_DEBUG
0002 
0003 #ifdef ECAL_RECO_CUDA_DEBUG
0004 #include <iostream>
0005 #endif
0006 
0007 // framework
0008 #include "FWCore/Framework/interface/stream/EDProducer.h"
0009 
0010 #include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/Framework/interface/MakerMacros.h"
0015 
0016 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0017 
0018 // algorithm specific
0019 
0020 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0021 
0022 #include "CUDADataFormats/EcalRecHitSoA/interface/EcalRecHit.h"
0023 
0024 class EcalCPURecHitProducer : public edm::stream::EDProducer<edm::ExternalWork> {
0025 public:
0026   explicit EcalCPURecHitProducer(edm::ParameterSet const& ps);
0027   ~EcalCPURecHitProducer() override = default;
0028   static void fillDescriptions(edm::ConfigurationDescriptions&);
0029 
0030 private:
0031   void acquire(edm::Event const&, edm::EventSetup const&, edm::WaitingTaskWithArenaHolder) override;
0032   void produce(edm::Event&, edm::EventSetup const&) override;
0033 
0034 private:
0035   using InputProduct = cms::cuda::Product<ecal::RecHit<calo::common::DevStoragePolicy>>;
0036   edm::EDGetTokenT<InputProduct> recHitsInEBToken_, recHitsInEEToken_;
0037   using OutputProduct = ecal::RecHit<calo::common::VecStoragePolicy<calo::common::CUDAHostAllocatorAlias>>;
0038   edm::EDPutTokenT<OutputProduct> recHitsOutEBToken_, recHitsOutEEToken_;
0039 
0040   OutputProduct recHitsEB_, recHitsEE_;
0041   bool containsTimingInformation_;
0042 };
0043 
0044 void EcalCPURecHitProducer::fillDescriptions(edm::ConfigurationDescriptions& confDesc) {
0045   edm::ParameterSetDescription desc;
0046 
0047   desc.add<edm::InputTag>("recHitsInLabelEB", edm::InputTag{"ecalRecHitProducerGPU", "EcalRecHitsEB"});
0048   desc.add<edm::InputTag>("recHitsInLabelEE", edm::InputTag{"ecalRecHitProducerGPU", "EcalRecHitsEE"});
0049   desc.add<std::string>("recHitsOutLabelEB", "EcalRecHitsEB");
0050   desc.add<std::string>("recHitsOutLabelEE", "EcalRecHitsEE");
0051   desc.add<bool>("containsTimingInformation", false);
0052 
0053   confDesc.addWithDefaultLabel(desc);
0054 }
0055 
0056 EcalCPURecHitProducer::EcalCPURecHitProducer(const edm::ParameterSet& ps)
0057     : recHitsInEBToken_{consumes<InputProduct>(ps.getParameter<edm::InputTag>("recHitsInLabelEB"))},
0058       recHitsInEEToken_{consumes<InputProduct>(ps.getParameter<edm::InputTag>("recHitsInLabelEE"))},
0059       recHitsOutEBToken_{produces<OutputProduct>(ps.getParameter<std::string>("recHitsOutLabelEB"))},
0060       recHitsOutEEToken_{produces<OutputProduct>(ps.getParameter<std::string>("recHitsOutLabelEE"))},
0061       containsTimingInformation_{ps.getParameter<bool>("containsTimingInformation")} {}
0062 
0063 void EcalCPURecHitProducer::acquire(edm::Event const& event,
0064                                     edm::EventSetup const& setup,
0065                                     edm::WaitingTaskWithArenaHolder taskHolder) {
0066   // retrieve data/ctx
0067   auto const& ebRecHitsProduct = event.get(recHitsInEBToken_);
0068   auto const& eeRecHitsProduct = event.get(recHitsInEEToken_);
0069   cms::cuda::ScopedContextAcquire ctx{ebRecHitsProduct, std::move(taskHolder)};
0070   auto const& ebRecHits = ctx.get(ebRecHitsProduct);
0071   auto const& eeRecHits = ctx.get(eeRecHitsProduct);
0072 
0073   // resize the output buffers
0074   recHitsEB_.resize(ebRecHits.size);
0075   recHitsEE_.resize(eeRecHits.size);
0076 
0077 #ifdef ECAL_RECO_CUDA_DEBUG
0078   std::cout << " [EcalCPURecHitProducer::acquire] ebRecHits.size = " << ebRecHits.size << std::endl;
0079   std::cout << " [EcalCPURecHitProducer::acquire] eeRecHits.size = " << eeRecHits.size << std::endl;
0080 #endif
0081 
0082   // enqeue transfers
0083   cudaCheck(cudaMemcpyAsync(recHitsEB_.did.data(),
0084                             ebRecHits.did.get(),
0085                             recHitsEB_.did.size() * sizeof(uint32_t),
0086                             cudaMemcpyDeviceToHost,
0087                             ctx.stream()));
0088   cudaCheck(cudaMemcpyAsync(recHitsEE_.did.data(),
0089                             eeRecHits.did.get(),
0090                             recHitsEE_.did.size() * sizeof(uint32_t),
0091                             cudaMemcpyDeviceToHost,
0092                             ctx.stream()));
0093   //
0094   //     ./CUDADataFormats/EcalRecHitSoA/interface/RecoTypes.h:using StorageScalarType = float;
0095   //
0096 
0097   cudaCheck(cudaMemcpyAsync(recHitsEB_.energy.data(),
0098                             ebRecHits.energy.get(),
0099                             recHitsEB_.energy.size() * sizeof(::ecal::reco::StorageScalarType),
0100                             cudaMemcpyDeviceToHost,
0101                             ctx.stream()));
0102   cudaCheck(cudaMemcpyAsync(recHitsEE_.energy.data(),
0103                             eeRecHits.energy.get(),
0104                             recHitsEE_.energy.size() * sizeof(::ecal::reco::StorageScalarType),
0105                             cudaMemcpyDeviceToHost,
0106                             ctx.stream()));
0107 
0108   cudaCheck(cudaMemcpyAsync(recHitsEB_.chi2.data(),
0109                             ebRecHits.chi2.get(),
0110                             recHitsEB_.chi2.size() * sizeof(::ecal::reco::StorageScalarType),
0111                             cudaMemcpyDeviceToHost,
0112                             ctx.stream()));
0113   cudaCheck(cudaMemcpyAsync(recHitsEE_.chi2.data(),
0114                             eeRecHits.chi2.get(),
0115                             recHitsEE_.chi2.size() * sizeof(::ecal::reco::StorageScalarType),
0116                             cudaMemcpyDeviceToHost,
0117                             ctx.stream()));
0118 
0119   cudaCheck(cudaMemcpyAsync(recHitsEB_.extra.data(),
0120                             ebRecHits.extra.get(),
0121                             recHitsEB_.extra.size() * sizeof(uint32_t),
0122                             cudaMemcpyDeviceToHost,
0123                             ctx.stream()));
0124   cudaCheck(cudaMemcpyAsync(recHitsEE_.extra.data(),
0125                             eeRecHits.extra.get(),
0126                             recHitsEE_.extra.size() * sizeof(uint32_t),
0127                             cudaMemcpyDeviceToHost,
0128                             ctx.stream()));
0129 
0130   cudaCheck(cudaMemcpyAsync(recHitsEB_.flagBits.data(),
0131                             ebRecHits.flagBits.get(),
0132                             recHitsEB_.flagBits.size() * sizeof(uint32_t),
0133                             cudaMemcpyDeviceToHost,
0134                             ctx.stream()));
0135   cudaCheck(cudaMemcpyAsync(recHitsEE_.flagBits.data(),
0136                             eeRecHits.flagBits.get(),
0137                             recHitsEE_.flagBits.size() * sizeof(uint32_t),
0138                             cudaMemcpyDeviceToHost,
0139                             ctx.stream()));
0140 
0141 #ifdef ECAL_RECO_CUDA_DEBUG
0142   for (unsigned int ieb = 0; ieb < ebRecHits.size; ieb++) {
0143     if (recHitsEB_.extra[ieb] != 0)
0144       std::cout << " [ " << ieb << " :: " << ebRecHits.size << " ] [ " << recHitsEB_.did[ieb]
0145                 << " ] eb extra = " << recHitsEB_.extra[ieb] << std::endl;
0146   }
0147 
0148   for (unsigned int ieb = 0; ieb < ebRecHits.size; ieb++) {
0149     if (recHitsEB_.energy[ieb] != 0)
0150       std::cout << " [ " << ieb << " :: " << ebRecHits.size << " ] [ " << recHitsEB_.did[ieb]
0151                 << " ] eb energy = " << recHitsEB_.energy[ieb] << std::endl;
0152   }
0153 
0154   for (unsigned int iee = 0; iee < eeRecHits.size; iee++) {
0155     if (recHitsEE_.energy[iee] != 0)
0156       std::cout << " [ " << iee << " :: " << eeRecHits.size << " ] [ " << recHitsEE_.did[iee]
0157                 << " ] ee energy = " << recHitsEE_.energy[iee] << std::endl;
0158   }
0159 #endif
0160 }
0161 
0162 void EcalCPURecHitProducer::produce(edm::Event& event, edm::EventSetup const& setup) {
0163   // put into event
0164   event.emplace(recHitsOutEBToken_, std::move(recHitsEB_));
0165   event.emplace(recHitsOutEEToken_, std::move(recHitsEE_));
0166 }
0167 
0168 DEFINE_FWK_MODULE(EcalCPURecHitProducer);