Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-01 22:34:26

0001 #include <cuda_runtime.h>
0002 
0003 #include "CUDADataFormats/Common/interface/Product.h"
0004 #include "CUDADataFormats/Common/interface/HostProduct.h"
0005 #include "CUDADataFormats/Track/interface/PixelTrackHeterogeneous.h"
0006 #include "DataFormats/Common/interface/Handle.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/EventSetup.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010 #include "FWCore/Framework/interface/stream/EDProducer.h"
0011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0014 #include "FWCore/PluginManager/interface/ModuleDef.h"
0015 #include "FWCore/Utilities/interface/EDGetToken.h"
0016 #include "FWCore/Utilities/interface/InputTag.h"
0017 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0018 
0019 // Switch on to enable checks and printout for found tracks
0020 // #define PIXEL_DEBUG_PRODUCE
0021 
0022 class PixelTrackSoAFromCUDA : public edm::stream::EDProducer<edm::ExternalWork> {
0023 public:
0024   explicit PixelTrackSoAFromCUDA(const edm::ParameterSet& iConfig);
0025   ~PixelTrackSoAFromCUDA() override = default;
0026 
0027   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0028 
0029 private:
0030   void acquire(edm::Event const& iEvent,
0031                edm::EventSetup const& iSetup,
0032                edm::WaitingTaskWithArenaHolder waitingTaskHolder) override;
0033   void produce(edm::Event& iEvent, edm::EventSetup const& iSetup) override;
0034 
0035   edm::EDGetTokenT<cms::cuda::Product<PixelTrackHeterogeneous>> tokenCUDA_;
0036   edm::EDPutTokenT<PixelTrackHeterogeneous> tokenSOA_;
0037 
0038   cms::cuda::host::unique_ptr<pixelTrack::TrackSoA> soa_;
0039 };
0040 
0041 PixelTrackSoAFromCUDA::PixelTrackSoAFromCUDA(const edm::ParameterSet& iConfig)
0042     : tokenCUDA_(consumes<cms::cuda::Product<PixelTrackHeterogeneous>>(iConfig.getParameter<edm::InputTag>("src"))),
0043       tokenSOA_(produces<PixelTrackHeterogeneous>()) {}
0044 
0045 void PixelTrackSoAFromCUDA::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0046   edm::ParameterSetDescription desc;
0047 
0048   desc.add<edm::InputTag>("src", edm::InputTag("pixelTracksCUDA"));
0049   descriptions.add("pixelTracksSoA", desc);
0050 }
0051 
0052 void PixelTrackSoAFromCUDA::acquire(edm::Event const& iEvent,
0053                                     edm::EventSetup const& iSetup,
0054                                     edm::WaitingTaskWithArenaHolder waitingTaskHolder) {
0055   cms::cuda::Product<PixelTrackHeterogeneous> const& inputDataWrapped = iEvent.get(tokenCUDA_);
0056   cms::cuda::ScopedContextAcquire ctx{inputDataWrapped, std::move(waitingTaskHolder)};
0057   auto const& inputData = ctx.get(inputDataWrapped);
0058 
0059   soa_ = inputData.toHostAsync(ctx.stream());
0060 }
0061 
0062 void PixelTrackSoAFromCUDA::produce(edm::Event& iEvent, edm::EventSetup const& iSetup) {
0063   // check that the fixed-size SoA does not overflow
0064   auto const& tsoa = *soa_;
0065   auto maxTracks = tsoa.stride();
0066   auto nTracks = tsoa.nTracks();
0067   assert(nTracks < maxTracks);
0068   if (nTracks == maxTracks - 1) {
0069     edm::LogWarning("PixelTracks") << "Unsorted reconstructed pixel tracks truncated to " << maxTracks - 1
0070                                    << " candidates";
0071   }
0072 
0073 #ifdef PIXEL_DEBUG_PRODUCE
0074   std::cout << "size of SoA " << sizeof(tsoa) << " stride " << maxTracks << std::endl;
0075   std::cout << "found " << nTracks << " tracks in cpu SoA at " << &tsoa << std::endl;
0076 
0077   int32_t nt = 0;
0078   for (int32_t it = 0; it < maxTracks; ++it) {
0079     auto nHits = tsoa.nHits(it);
0080     assert(nHits == int(tsoa.hitIndices.size(it)));
0081     if (nHits == 0)
0082       break;  // this is a guard: maybe we need to move to nTracks...
0083     nt++;
0084   }
0085   assert(nTracks == nt);
0086 #endif
0087 
0088   // DO NOT  make a copy  (actually TWO....)
0089   iEvent.emplace(tokenSOA_, std::move(soa_));
0090 
0091   assert(!soa_);
0092 }
0093 
0094 DEFINE_FWK_MODULE(PixelTrackSoAFromCUDA);