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