Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-29 11:58:11

0001 #include <utility>
0002 
0003 #include <alpaka/alpaka.hpp>
0004 
0005 #include "DataFormats/Common/interface/SortedCollection.h"
0006 #include "DataFormats/ParticleFlowReco/interface/CaloRecHitHostCollection.h"
0007 #include "DataFormats/ParticleFlowReco/interface/alpaka/CaloRecHitDeviceCollection.h"
0008 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0011 #include "FWCore/Utilities/interface/EDGetToken.h"
0012 #include "FWCore/Utilities/interface/InputTag.h"
0013 #include "FWCore/Utilities/interface/StreamID.h"
0014 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/global/EDProducer.h"
0015 #include "CalorimeterDefinitions.h"
0016 
0017 #define DEBUG false
0018 
0019 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0020   using namespace particleFlowRecHitProducer;
0021 
0022   template <typename CAL>
0023   class CaloRecHitSoAProducer : public global::EDProducer<> {
0024   public:
0025     CaloRecHitSoAProducer(edm::ParameterSet const& config)
0026         : recHitsToken_(consumes(config.getParameter<edm::InputTag>("src"))),
0027           deviceToken_(produces()),
0028           synchronise_(config.getUntrackedParameter<bool>("synchronise")) {}
0029 
0030     void produce(edm::StreamID sid, device::Event& event, device::EventSetup const&) const override {
0031       const edm::SortedCollection<typename CAL::CaloRecHitType>& recHits = event.get(recHitsToken_);
0032       const int32_t num_recHits = recHits.size();
0033       if (DEBUG)
0034         printf("Found %d recHits\n", num_recHits);
0035 
0036       reco::CaloRecHitHostCollection hostProduct{num_recHits, event.queue()};
0037       auto& view = hostProduct.view();
0038 
0039       for (int i = 0; i < num_recHits; i++) {
0040         convertRecHit(view[i], recHits[i]);
0041 
0042         if (DEBUG && i < 10)
0043           printf("recHit %4d %u %f %f %08x\n", i, view.detId(i), view.energy(i), view.time(i), view.flags(i));
0044       }
0045 
0046       reco::CaloRecHitDeviceCollection deviceProduct{num_recHits, event.queue()};
0047       alpaka::memcpy(event.queue(), deviceProduct.buffer(), hostProduct.buffer());
0048       if (synchronise_)
0049         alpaka::wait(event.queue());
0050       event.emplace(deviceToken_, std::move(deviceProduct));
0051     }
0052 
0053     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0054       edm::ParameterSetDescription desc;
0055       desc.add<edm::InputTag>("src")->setComment("Input calorimeter rec hit collection");
0056       desc.addUntracked<bool>("synchronise", false)
0057           ->setComment("Add synchronisation point after execution (for benchmarking asynchronous execution)");
0058       descriptions.addWithDefaultLabel(desc);
0059     }
0060 
0061   private:
0062     const edm::EDGetTokenT<edm::SortedCollection<typename CAL::CaloRecHitType>> recHitsToken_;
0063     const device::EDPutToken<reco::CaloRecHitDeviceCollection> deviceToken_;
0064     const bool synchronise_;
0065 
0066     static void convertRecHit(reco::CaloRecHitHostCollection::View::element to,
0067                               const typename CAL::CaloRecHitType& from);
0068   };
0069 
0070   template <>
0071   void CaloRecHitSoAProducer<HCAL>::convertRecHit(reco::CaloRecHitHostCollection::View::element to,
0072                                                   const HCAL::CaloRecHitType& from) {
0073     // Fill SoA from HCAL rec hit
0074     to.detId() = from.id().rawId();
0075     to.energy() = from.energy();
0076     to.time() = from.time();
0077     to.flags() = from.flags();
0078   }
0079 
0080   template <>
0081   void CaloRecHitSoAProducer<ECAL>::convertRecHit(reco::CaloRecHitHostCollection::View::element to,
0082                                                   const ECAL::CaloRecHitType& from) {
0083     // Fill SoA from ECAL rec hit
0084     to.detId() = from.id().rawId();
0085     to.energy() = from.energy();
0086     to.time() = from.time();
0087     to.flags() = from.flagsBits();
0088   }
0089 
0090   using HCALRecHitSoAProducer = CaloRecHitSoAProducer<HCAL>;
0091   using ECALRecHitSoAProducer = CaloRecHitSoAProducer<ECAL>;
0092 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0093 
0094 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
0095 DEFINE_FWK_ALPAKA_MODULE(HCALRecHitSoAProducer);
0096 DEFINE_FWK_ALPAKA_MODULE(ECALRecHitSoAProducer);