Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:42

0001 #include <iostream>
0002 
0003 #include "CUDADataFormats/HcalDigi/interface/DigiCollection.h"
0004 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0005 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/Framework/interface/stream/EDProducer.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0012 #include "HeterogeneousCore/CUDAUtilities/interface/HostAllocator.h"
0013 #include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
0014 
0015 class HcalCPUDigisProducer : public edm::stream::EDProducer<edm::ExternalWork> {
0016 public:
0017   explicit HcalCPUDigisProducer(edm::ParameterSet const& ps);
0018   ~HcalCPUDigisProducer() override;
0019   static void fillDescriptions(edm::ConfigurationDescriptions&);
0020 
0021 private:
0022   void acquire(edm::Event const&, edm::EventSetup const&, edm::WaitingTaskWithArenaHolder) override;
0023   void produce(edm::Event&, edm::EventSetup const&) override;
0024 
0025 private:
0026   using IProductTypef01 = cms::cuda::Product<hcal::DigiCollection<hcal::Flavor1, calo::common::DevStoragePolicy>>;
0027   edm::EDGetTokenT<IProductTypef01> digisF01HETokenIn_;
0028   using IProductTypef5 = cms::cuda::Product<hcal::DigiCollection<hcal::Flavor5, calo::common::DevStoragePolicy>>;
0029   edm::EDGetTokenT<IProductTypef5> digisF5HBTokenIn_;
0030   using IProductTypef3 = cms::cuda::Product<hcal::DigiCollection<hcal::Flavor3, calo::common::DevStoragePolicy>>;
0031   edm::EDGetTokenT<IProductTypef3> digisF3HBTokenIn_;
0032 
0033   using OProductTypef01 =
0034       hcal::DigiCollection<hcal::Flavor1, calo::common::VecStoragePolicy<calo::common::CUDAHostAllocatorAlias>>;
0035   edm::EDPutTokenT<OProductTypef01> digisF01HETokenOut_;
0036   using OProductTypef5 =
0037       hcal::DigiCollection<hcal::Flavor5, calo::common::VecStoragePolicy<calo::common::CUDAHostAllocatorAlias>>;
0038   edm::EDPutTokenT<OProductTypef5> digisF5HBTokenOut_;
0039   using OProductTypef3 =
0040       hcal::DigiCollection<hcal::Flavor3, calo::common::VecStoragePolicy<calo::common::CUDAHostAllocatorAlias>>;
0041   edm::EDPutTokenT<OProductTypef3> digisF3HBTokenOut_;
0042 
0043   // needed to pass data from acquire to produce
0044   OProductTypef01 digisf01HE_;
0045   OProductTypef5 digisf5HB_;
0046   OProductTypef3 digisf3HB_;
0047 };
0048 
0049 void HcalCPUDigisProducer::fillDescriptions(edm::ConfigurationDescriptions& confDesc) {
0050   edm::ParameterSetDescription desc;
0051 
0052   desc.add<edm::InputTag>("digisLabelF01HEIn", edm::InputTag{"hcalRawToDigiGPU", "f01HEDigisGPU"});
0053   desc.add<edm::InputTag>("digisLabelF5HBIn", edm::InputTag{"hcalRawToDigiGPU", "f5HBDigisGPU"});
0054   desc.add<edm::InputTag>("digisLabelF3HBIn", edm::InputTag{"hcalRawToDigiGPU", "f3HBDigisGPU"});
0055   desc.add<std::string>("digisLabelF01HEOut", "f01HEDigis");
0056   desc.add<std::string>("digisLabelF5HBOut", "f5HBDigis");
0057   desc.add<std::string>("digisLabelF3HBOut", "f3HBDigis");
0058 
0059   confDesc.addWithDefaultLabel(desc);
0060 }
0061 
0062 HcalCPUDigisProducer::HcalCPUDigisProducer(const edm::ParameterSet& ps)
0063     : digisF01HETokenIn_{consumes<IProductTypef01>(ps.getParameter<edm::InputTag>("digisLabelF01HEIn"))},
0064       digisF5HBTokenIn_{consumes<IProductTypef5>(ps.getParameter<edm::InputTag>("digisLabelF5HBIn"))},
0065       digisF3HBTokenIn_{consumes<IProductTypef3>(ps.getParameter<edm::InputTag>("digisLabelF3HBIn"))},
0066       digisF01HETokenOut_{produces<OProductTypef01>(ps.getParameter<std::string>("digisLabelF01HEOut"))},
0067       digisF5HBTokenOut_{produces<OProductTypef5>(ps.getParameter<std::string>("digisLabelF5HBOut"))},
0068       digisF3HBTokenOut_{produces<OProductTypef3>(ps.getParameter<std::string>("digisLabelF3HBOut"))} {}
0069 
0070 HcalCPUDigisProducer::~HcalCPUDigisProducer() {}
0071 
0072 void HcalCPUDigisProducer::acquire(edm::Event const& event,
0073                                    edm::EventSetup const& setup,
0074                                    edm::WaitingTaskWithArenaHolder taskHolder) {
0075   // retrieve data/ctx
0076   auto const& f01HEProduct = event.get(digisF01HETokenIn_);
0077   auto const& f5HBProduct = event.get(digisF5HBTokenIn_);
0078   auto const& f3HBProduct = event.get(digisF3HBTokenIn_);
0079   cms::cuda::ScopedContextAcquire ctx{f01HEProduct, std::move(taskHolder)};
0080   auto const& f01HEDigis = ctx.get(f01HEProduct);
0081   auto const& f5HBDigis = ctx.get(f5HBProduct);
0082   auto const& f3HBDigis = ctx.get(f3HBProduct);
0083 
0084   // resize out tmp buffers
0085   digisf01HE_.stride = f01HEDigis.stride;
0086   digisf5HB_.stride = f5HBDigis.stride;
0087   digisf3HB_.stride = f3HBDigis.stride;
0088   digisf01HE_.resize(f01HEDigis.size);
0089   digisf5HB_.resize(f5HBDigis.size);
0090   digisf3HB_.resize(f3HBDigis.size);
0091 
0092   auto lambdaToTransfer = [&ctx](auto& dest, auto* src) {
0093     using vector_type = typename std::remove_reference<decltype(dest)>::type;
0094     using type = typename vector_type::value_type;
0095     using src_data_type = typename std::remove_pointer<decltype(src)>::type;
0096     static_assert(std::is_same<src_data_type, type>::value && "Dest and Src data types do not match");
0097     cudaCheck(cudaMemcpyAsync(dest.data(), src, dest.size() * sizeof(type), cudaMemcpyDeviceToHost, ctx.stream()));
0098   };
0099 
0100   lambdaToTransfer(digisf01HE_.data, f01HEDigis.data.get());
0101   lambdaToTransfer(digisf01HE_.ids, f01HEDigis.ids.get());
0102 
0103   lambdaToTransfer(digisf5HB_.data, f5HBDigis.data.get());
0104   lambdaToTransfer(digisf5HB_.ids, f5HBDigis.ids.get());
0105   lambdaToTransfer(digisf5HB_.npresamples, f5HBDigis.npresamples.get());
0106 
0107   lambdaToTransfer(digisf3HB_.data, f3HBDigis.data.get());
0108   lambdaToTransfer(digisf3HB_.ids, f3HBDigis.ids.get());
0109 }
0110 
0111 void HcalCPUDigisProducer::produce(edm::Event& event, edm::EventSetup const& setup) {
0112   event.emplace(digisF01HETokenOut_, std::move(digisf01HE_));
0113   event.emplace(digisF5HBTokenOut_, std::move(digisf5HB_));
0114   event.emplace(digisF3HBTokenOut_, std::move(digisf3HB_));
0115 }
0116 
0117 DEFINE_FWK_MODULE(HcalCPUDigisProducer);