CMS 3D CMS Logo

PixelTrackSoAFromCUDA.cc
Go to the documentation of this file.
1 #include <cuda_runtime.h>
2 #include <Eigen/Core> // needed here by soa layout
3 
21 
22 // Switch on to enable checks and printout for found tracks
23 // #define PIXEL_DEBUG_PRODUCE
24 
25 template <typename TrackerTraits>
26 class PixelTrackSoAFromCUDAT : public edm::stream::EDProducer<edm::ExternalWork> {
29 
30 public:
31  explicit PixelTrackSoAFromCUDAT(const edm::ParameterSet& iConfig);
32  ~PixelTrackSoAFromCUDAT() override = default;
33 
34  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
35 
36 private:
37  void acquire(edm::Event const& iEvent,
38  edm::EventSetup const& iSetup,
39  edm::WaitingTaskWithArenaHolder waitingTaskHolder) override;
40  void produce(edm::Event& iEvent, edm::EventSetup const& iSetup) override;
41 
44 
46 };
47 
48 template <typename TrackerTraits>
50  : tokenCUDA_(consumes(iConfig.getParameter<edm::InputTag>("src"))), tokenSOA_(produces<TrackSoAHost>()) {}
51 
52 template <typename TrackerTraits>
55 
56  desc.add<edm::InputTag>("src", edm::InputTag("pixelTracksCUDA"));
57  descriptions.addWithDefaultLabel(desc);
58 }
59 
60 template <typename TrackerTraits>
62  edm::EventSetup const& iSetup,
63  edm::WaitingTaskWithArenaHolder waitingTaskHolder) {
64  cms::cuda::Product<TrackSoADevice> const& inputDataWrapped = iEvent.get(tokenCUDA_);
65  cms::cuda::ScopedContextAcquire ctx{inputDataWrapped, std::move(waitingTaskHolder)};
66  auto const& tracks_d = ctx.get(inputDataWrapped); // Tracks on device
67  tracks_h_ = TrackSoAHost(ctx.stream()); // Create an instance of Tracks on Host, using the stream
68  cudaCheck(cudaMemcpyAsync(tracks_h_.buffer().get(),
69  tracks_d.const_buffer().get(),
70  tracks_d.bufferSize(),
71  cudaMemcpyDeviceToHost,
72  ctx.stream())); // Copy data from Device to Host
73 }
74 
75 template <typename TrackerTraits>
77  auto maxTracks = tracks_h_.view().metadata().size();
78  auto nTracks = tracks_h_.view().nTracks();
79 
81  if (nTracks == maxTracks - 1) {
82  edm::LogWarning("PixelTracks") << "Unsorted reconstructed pixel tracks truncated to " << maxTracks - 1
83  << " candidates";
84  }
85 
86 #ifdef PIXEL_DEBUG_PRODUCE
87  std::cout << "size of SoA " << sizeof(tsoa) << " stride " << maxTracks << std::endl;
88  std::cout << "found " << nTracks << " tracks in cpu SoA at " << &tsoa << std::endl;
89 
90  int32_t nt = 0;
91  for (int32_t it = 0; it < maxTracks; ++it) {
92  auto nHits = TracksUtilities<TrackerTraits>::nHits(tracks_h_.view(), it);
93  assert(nHits == int(tracks_h_.view().hitIndices().size(it)));
94  if (nHits == 0)
95  break; // this is a guard: maybe we need to move to nTracks...
96  nt++;
97  }
98  assert(nTracks == nt);
99 #endif
100 
101  // DO NOT make a copy (actually TWO....)
102  iEvent.emplace(tokenSOA_, std::move(tracks_h_));
103  assert(!tracks_h_.buffer());
104 }
105 
108 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void produce(edm::Event &iEvent, edm::EventSetup const &iSetup) override
edm::EDGetTokenT< cms::cuda::Product< TrackSoADevice > > tokenCUDA_
void acquire(edm::Event const &iEvent, edm::EventSetup const &iSetup, edm::WaitingTaskWithArenaHolder waitingTaskHolder) override
edm::EDPutTokenT< TrackSoAHost > tokenSOA_
assert(be >=bs)
PixelTrackSoAFromCUDAT(const edm::ParameterSet &iConfig)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~PixelTrackSoAFromCUDAT() override=default
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int nt
Definition: AMPTWrapper.h:42
static constexpr __host__ __device__ int nHits(const TrackSoAConstView &tracks, int i)
maxTracks
Definition: DMR_cfg.py:158
HLT enums.
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69
Log< level::Warning, false > LogWarning
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
def move(src, dest)
Definition: eostools.py:511