Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CUDADataFormats/Common/interface/Product.h"
0002 #include "CUDADataFormats/SiPixelDigi/interface/SiPixelDigiErrorsCUDA.h"
0003 #include "DataFormats/SiPixelRawData/interface/SiPixelErrorsSoA.h"
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/Framework/interface/stream/EDProducer.h"
0008 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0012 #include "HeterogeneousCore/CUDAUtilities/interface/host_unique_ptr.h"
0013 
0014 class SiPixelDigiErrorsSoAFromCUDA : public edm::stream::EDProducer<edm::ExternalWork> {
0015 public:
0016   explicit SiPixelDigiErrorsSoAFromCUDA(const edm::ParameterSet& iConfig);
0017   ~SiPixelDigiErrorsSoAFromCUDA() override = default;
0018 
0019   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0020 
0021 private:
0022   void acquire(const edm::Event& iEvent,
0023                const edm::EventSetup& iSetup,
0024                edm::WaitingTaskWithArenaHolder waitingTaskHolder) override;
0025   void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0026 
0027   edm::EDGetTokenT<cms::cuda::Product<SiPixelDigiErrorsCUDA>> digiErrorGetToken_;
0028   edm::EDPutTokenT<SiPixelErrorsSoA> digiErrorPutToken_;
0029 
0030   cms::cuda::host::unique_ptr<SiPixelErrorCompact[]> data_;
0031   cms::cuda::SimpleVector<SiPixelErrorCompact> error_ = cms::cuda::make_SimpleVector<SiPixelErrorCompact>(0, nullptr);
0032   const SiPixelFormatterErrors* formatterErrors_ = nullptr;
0033 };
0034 
0035 SiPixelDigiErrorsSoAFromCUDA::SiPixelDigiErrorsSoAFromCUDA(const edm::ParameterSet& iConfig)
0036     : digiErrorGetToken_(
0037           consumes<cms::cuda::Product<SiPixelDigiErrorsCUDA>>(iConfig.getParameter<edm::InputTag>("src"))),
0038       digiErrorPutToken_(produces<SiPixelErrorsSoA>()) {}
0039 
0040 void SiPixelDigiErrorsSoAFromCUDA::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0041   edm::ParameterSetDescription desc;
0042   desc.add<edm::InputTag>("src", edm::InputTag("siPixelClustersCUDA"));
0043   descriptions.addWithDefaultLabel(desc);
0044 }
0045 
0046 void SiPixelDigiErrorsSoAFromCUDA::acquire(const edm::Event& iEvent,
0047                                            const edm::EventSetup& iSetup,
0048                                            edm::WaitingTaskWithArenaHolder waitingTaskHolder) {
0049   // Do the transfer in a CUDA stream parallel to the computation CUDA stream
0050   cms::cuda::ScopedContextAcquire ctx{iEvent.streamID(), std::move(waitingTaskHolder)};
0051   const auto& gpuDigiErrors = ctx.get(iEvent, digiErrorGetToken_);
0052   formatterErrors_ = &(gpuDigiErrors.formatterErrors());
0053 
0054   if (gpuDigiErrors.nErrorWords() == 0)
0055     return;
0056 
0057   auto tmp = gpuDigiErrors.dataErrorToHostAsync(ctx.stream());
0058   error_ = tmp.first;
0059   data_ = std::move(tmp.second);
0060 }
0061 
0062 void SiPixelDigiErrorsSoAFromCUDA::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0063   // The following line copies the data from the pinned host memory to
0064   // regular host memory. In principle that feels unnecessary (why not
0065   // just use the pinned host memory?). There are a few arguments for
0066   // doing it though
0067   // - Now can release the pinned host memory back to the (caching) allocator
0068   //   * if we'd like to keep the pinned memory, we'd need to also
0069   //     keep the CUDA stream around as long as that, or allow pinned
0070   //     host memory to be allocated without a CUDA stream
0071   // - What if a CPU algorithm would produce the same SoA? We can't
0072   //   use cudaMallocHost without a GPU...
0073   iEvent.emplace(digiErrorPutToken_, error_.size(), error_.data(), formatterErrors_);
0074   error_ = cms::cuda::make_SimpleVector<SiPixelErrorCompact>(0, nullptr);
0075   data_.reset();
0076   formatterErrors_ = nullptr;
0077 }
0078 
0079 // define as framework plugin
0080 DEFINE_FWK_MODULE(SiPixelDigiErrorsSoAFromCUDA);