CMS 3D CMS Logo

SiPixelRawToClusterCUDA.cc
Go to the documentation of this file.
1 // C++ includes
2 #include <memory>
3 #include <string>
4 #include <vector>
5 
6 // CMSSW includes
39 
40 // local includes
43 
44 class SiPixelRawToClusterCUDA : public edm::stream::EDProducer<edm::ExternalWork> {
45 public:
46  explicit SiPixelRawToClusterCUDA(const edm::ParameterSet& iConfig);
47  ~SiPixelRawToClusterCUDA() override = default;
48 
49  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
50 
51 private:
52  void acquire(const edm::Event& iEvent,
53  const edm::EventSetup& iSetup,
54  edm::WaitingTaskWithArenaHolder waitingTaskHolder) override;
55  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
56 
58 
62 
64 
69 
70  std::unique_ptr<SiPixelFedCablingTree> cabling_;
71  std::vector<unsigned int> fedIds_;
73  std::unique_ptr<PixelUnpackingRegions> regions_;
74 
77 
78  const bool isRun2_;
79  const bool includeErrors_;
80  const bool useQuality_;
81  uint32_t nDigis_;
83 };
84 
86  : rawGetToken_(consumes<FEDRawDataCollection>(iConfig.getParameter<edm::InputTag>("InputLabel"))),
87  digiPutToken_(produces<cms::cuda::Product<SiPixelDigisCUDA>>()),
88  clusterPutToken_(produces<cms::cuda::Product<SiPixelClustersCUDA>>()),
92  edm::ESInputTag("", iConfig.getParameter<std::string>("CablingMapLabel")))),
93  isRun2_(iConfig.getParameter<bool>("isRun2")),
94  includeErrors_(iConfig.getParameter<bool>("IncludeErrors")),
95  useQuality_(iConfig.getParameter<bool>("UseQualityInfo")),
96  clusterThresholds_{iConfig.getParameter<int32_t>("clusterThreshold_layer1"),
97  iConfig.getParameter<int32_t>("clusterThreshold_otherLayers")} {
98  if (includeErrors_) {
99  digiErrorPutToken_ = produces<cms::cuda::Product<SiPixelDigiErrorsCUDA>>();
100  }
101 
102  // regions
103  if (!iConfig.getParameter<edm::ParameterSet>("Regions").getParameterNames().empty()) {
104  regions_ = std::make_unique<PixelUnpackingRegions>(iConfig, consumesCollector());
105  }
106 }
107 
110  desc.add<bool>("isRun2", true);
111  desc.add<bool>("IncludeErrors", true);
112  desc.add<bool>("UseQualityInfo", false);
113  // Note: this parameter is obsolete: it is ignored and will have no effect.
114  // It is kept to avoid breaking older configurations, and will not be printed in the generated cfi.py file.
115  desc.addOptionalNode(edm::ParameterDescription<uint32_t>("MaxFEDWords", 0, true), false)
116  ->setComment("This parameter is obsolete and will be ignored.");
117  desc.add<int32_t>("clusterThreshold_layer1", kSiPixelClusterThresholdsDefaultPhase1.layer1);
118  desc.add<int32_t>("clusterThreshold_otherLayers", kSiPixelClusterThresholdsDefaultPhase1.otherLayers);
119  desc.add<edm::InputTag>("InputLabel", edm::InputTag("rawDataCollector"));
120  {
122  psd0.addOptional<std::vector<edm::InputTag>>("inputs");
123  psd0.addOptional<std::vector<double>>("deltaPhi");
124  psd0.addOptional<std::vector<double>>("maxZ");
125  psd0.addOptional<edm::InputTag>("beamSpot");
126  desc.add<edm::ParameterSetDescription>("Regions", psd0)
127  ->setComment("## Empty Regions PSet means complete unpacking");
128  }
129  desc.add<std::string>("CablingMapLabel", "")->setComment("CablingMap label"); //Tav
130  descriptions.addWithDefaultLabel(desc);
131 }
132 
134  const edm::EventSetup& iSetup,
135  edm::WaitingTaskWithArenaHolder waitingTaskHolder) {
136  cms::cuda::ScopedContextAcquire ctx{iEvent.streamID(), std::move(waitingTaskHolder), ctxState_};
137 
138  auto hgpuMap = iSetup.getHandle(gpuMapToken_);
139  if (hgpuMap->hasQuality() != useQuality_) {
140  throw cms::Exception("LogicError")
141  << "UseQuality of the module (" << useQuality_
142  << ") differs the one from SiPixelROCsStatusAndMappingWrapper. Please fix your configuration.";
143  }
144  // get the GPU product already here so that the async transfer can begin
145  const auto* gpuMap = hgpuMap->getGPUProductAsync(ctx.stream());
146 
147  auto hgains = iSetup.getHandle(gainsToken_);
148  // get the GPU product already here so that the async transfer can begin
149  const auto* gpuGains = hgains->getGPUProductAsync(ctx.stream());
150 
151  cms::cuda::device::unique_ptr<unsigned char[]> modulesToUnpackRegional;
152  const unsigned char* gpuModulesToUnpack;
153 
154  if (regions_) {
155  regions_->run(iEvent, iSetup);
156  LogDebug("SiPixelRawToCluster") << "region2unpack #feds: " << regions_->nFEDs();
157  LogDebug("SiPixelRawToCluster") << "region2unpack #modules (BPIX,EPIX,total): " << regions_->nBarrelModules() << " "
158  << regions_->nForwardModules() << " " << regions_->nModules();
159  modulesToUnpackRegional = hgpuMap->getModToUnpRegionalAsync(*(regions_->modulesToUnpack()), ctx.stream());
160  gpuModulesToUnpack = modulesToUnpackRegional.get();
161  } else {
162  gpuModulesToUnpack = hgpuMap->getModToUnpAllAsync(ctx.stream());
163  }
164 
165  // initialize cabling map or update if necessary
166  if (recordWatcher_.check(iSetup)) {
167  // cabling map, which maps online address (fed->link->ROC->local pixel) to offline (DetId->global pixel)
168  auto cablingMap = iSetup.getTransientHandle(cablingMapToken_);
169  cablingMap_ = cablingMap.product();
170  fedIds_ = cablingMap->fedIds();
171  cabling_ = cablingMap->cablingTree();
172  LogDebug("map version:") << cabling_->version();
173  }
174 
175  const auto& buffers = iEvent.get(rawGetToken_);
176 
177  errors_.clear();
178 
179  // GPU specific: Data extraction for RawToDigi GPU
180  unsigned int wordCounter = 0;
181  unsigned int fedCounter = 0;
182  bool errorsInEvent = false;
183 
184  std::vector<unsigned int> index(fedIds_.size(), 0);
185  std::vector<cms_uint32_t const*> start(fedIds_.size(), nullptr);
186  std::vector<ptrdiff_t> words(fedIds_.size(), 0);
187 
188  // In CPU algorithm this loop is part of PixelDataFormatter::interpretRawData()
189  ErrorChecker errorcheck;
190  for (uint32_t i = 0; i < fedIds_.size(); ++i) {
191  const int fedId = fedIds_[i];
192  if (regions_ && !regions_->mayUnpackFED(fedId))
193  continue;
194 
195  // for GPU
196  // first 150 index stores the fedId and next 150 will store the
197  // start index of word in that fed
199  fedCounter++;
200 
201  // get event data for this fed
202  const FEDRawData& rawData = buffers.FEDData(fedId);
203 
204  // GPU specific
205  int nWords = rawData.size() / sizeof(cms_uint64_t);
206  if (nWords == 0) {
207  continue;
208  }
209 
210  // check CRC bit
211  const cms_uint64_t* trailer = reinterpret_cast<const cms_uint64_t*>(rawData.data()) + (nWords - 1);
212  if (not errorcheck.checkCRC(errorsInEvent, fedId, trailer, errors_)) {
213  continue;
214  }
215 
216  // check headers
217  const cms_uint64_t* header = reinterpret_cast<const cms_uint64_t*>(rawData.data());
218  header--;
219  bool moreHeaders = true;
220  while (moreHeaders) {
221  header++;
222  bool headerStatus = errorcheck.checkHeader(errorsInEvent, fedId, header, errors_);
223  moreHeaders = headerStatus;
224  }
225 
226  // check trailers
227  bool moreTrailers = true;
228  trailer++;
229  while (moreTrailers) {
230  trailer--;
231  bool trailerStatus = errorcheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors_);
232  moreTrailers = trailerStatus;
233  }
234 
235  const cms_uint32_t* bw = (const cms_uint32_t*)(header + 1);
236  const cms_uint32_t* ew = (const cms_uint32_t*)(trailer);
237 
238  assert(0 == (ew - bw) % 2);
239  index[i] = wordCounter;
240  start[i] = bw;
241  words[i] = (ew - bw);
242  wordCounter += (ew - bw);
243 
244  } // end of for loop
245 
246  nDigis_ = wordCounter;
247 
248  if (nDigis_ == 0)
249  return;
250 
251  // copy the FED data to a single cpu buffer
253  for (uint32_t i = 0; i < fedIds_.size(); ++i) {
254  wordFedAppender.initializeWordFed(fedIds_[i], index[i], start[i], words[i]);
255  }
256 
259  gpuMap,
260  gpuModulesToUnpack,
261  gpuGains,
262  wordFedAppender,
264  wordCounter,
265  fedCounter,
266  useQuality_,
268  edm::MessageDrop::instance()->debugEnabled,
269  ctx.stream());
270 }
271 
274 
275  if (nDigis_ == 0) {
276  // Cannot use the default constructor here, as it would not allocate memory.
277  // In the case of no digis, clusters_d are not being instantiated, but are
278  // still used downstream to initialize TrackingRecHitSoADevice. If there
279  // are no valid pointers to clusters' Collection columns, instantiation
280  // of TrackingRecHits fail. Example: workflow 11604.0
281  SiPixelDigisCUDA digis_d = SiPixelDigisCUDA(nDigis_, ctx.stream());
283  ctx.emplace(iEvent, digiPutToken_, std::move(digis_d));
284  ctx.emplace(iEvent, clusterPutToken_, std::move(clusters_d));
285  if (includeErrors_) {
287  }
288  return;
289  }
290 
291  auto tmp = gpuAlgo_.getResults();
292  ctx.emplace(iEvent, digiPutToken_, std::move(tmp.first));
293  ctx.emplace(iEvent, clusterPutToken_, std::move(tmp.second));
294  if (includeErrors_) {
295  ctx.emplace(iEvent, digiErrorPutToken_, gpuAlgo_.getErrors());
296  }
297 }
298 
299 // define as framework plugin
Definition: start.py:1
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
std::unique_ptr< SiPixelFedCablingTree > cabling_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
edm::ESWatcher< SiPixelFedCablingMapRcd > recordWatcher_
cms::cuda::ContextState ctxState_
void initializeWordFed(int fedId, unsigned int index, cms_uint32_t const *src, unsigned int length)
edm::EDPutTokenT< cms::cuda::Product< SiPixelDigiErrorsCUDA > > digiErrorPutToken_
std::pair< SiPixelDigisCUDA, SiPixelClustersCUDA > getResults()
edm::ESGetToken< SiPixelGainCalibrationForHLTGPU, SiPixelGainCalibrationForHLTGPURcd > gainsToken_
static MessageDrop * instance()
Definition: MessageDrop.cc:33
edm::EDPutTokenT< cms::cuda::Product< SiPixelClustersCUDA > > clusterPutToken_
const SiPixelFedCablingMap * cablingMap_
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
assert(be >=bs)
edm::EDPutTokenT< cms::cuda::Product< SiPixelDigisCUDA > > digiPutToken_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
unsigned int cms_uint32_t
Definition: typedefs.h:15
Namespace of DDCMS conversion namespace.
std::vector< unsigned int > fedIds_
constexpr SiPixelClusterThresholds kSiPixelClusterThresholdsDefaultPhase1
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
void acquire(const edm::Event &iEvent, const edm::EventSetup &iSetup, edm::WaitingTaskWithArenaHolder waitingTaskHolder) override
HLT enums.
const SiPixelClusterThresholds clusterThresholds_
ESTransientHandle< T > getTransientHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:141
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
pixelgpudetails::SiPixelRawToClusterGPUKernel gpuAlgo_
SiPixelRawToClusterCUDA(const edm::ParameterSet &iConfig)
void makeClustersAsync(bool isRun2, const SiPixelClusterThresholds clusterThresholds, const SiPixelROCsStatusAndMapping *cablingMap, const unsigned char *modToUnp, const SiPixelGainForHLTonGPU *gains, const WordFedAppender &wordFed, SiPixelFormatterErrors &&errors, const uint32_t wordCounter, const uint32_t fedCounter, bool useQualityInfo, bool includeErrors, bool debug, cudaStream_t stream)
unsigned long long cms_uint64_t
Definition: typedefs.h:17
~SiPixelRawToClusterCUDA() override=default
tmp
align.sh
Definition: createJobs.py:716
std::unique_ptr< PixelUnpackingRegions > regions_
edm::ESGetToken< SiPixelROCsStatusAndMappingWrapper, CkfComponentsRecord > gpuMapToken_
PixelDataFormatter::Errors errors_
edm::EDGetTokenT< FEDRawDataCollection > rawGetToken_
std::vector< std::string > getParameterNames() const
def move(src, dest)
Definition: eostools.py:511
std::map< cms_uint32_t, DetErrors > Errors
edm::ESGetToken< SiPixelFedCablingMap, SiPixelFedCablingMapRcd > cablingMapToken_
static constexpr uint16_t numberOfModules
#define LogDebug(id)