Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-11 23:28:09

0001 #include "CondFormats/DataRecord/interface/EcalMultifitConditionsRcd.h"
0002 #include "CondFormats/DataRecord/interface/EcalMultifitParametersRcd.h"
0003 #include "CondFormats/EcalObjects/interface/alpaka/EcalMultifitConditionsDevice.h"
0004 #include "CondFormats/EcalObjects/interface/alpaka/EcalMultifitParametersDevice.h"
0005 #include "DataFormats/EcalDigi/interface/alpaka/EcalDigiDeviceCollection.h"
0006 #include "DataFormats/EcalRecHit/interface/alpaka/EcalUncalibratedRecHitDeviceCollection.h"
0007 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0010 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/EDGetToken.h"
0011 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/EDPutToken.h"
0012 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/Event.h"
0013 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/EventSetup.h"
0014 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/stream/SynchronizingEDProducer.h"
0015 
0016 #include "DeclsForKernels.h"
0017 #include "EcalUncalibRecHitMultiFitAlgoPortable.h"
0018 
0019 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0020 
0021   class EcalUncalibRecHitProducerPortable : public stream::SynchronizingEDProducer<> {
0022   public:
0023     explicit EcalUncalibRecHitProducerPortable(edm::ParameterSet const& ps);
0024     ~EcalUncalibRecHitProducerPortable() override = default;
0025     static void fillDescriptions(edm::ConfigurationDescriptions&);
0026 
0027     void acquire(device::Event const&, device::EventSetup const&) override;
0028     void produce(device::Event&, device::EventSetup const&) override;
0029 
0030   private:
0031     using InputProduct = EcalDigiDeviceCollection;
0032     const device::EDGetToken<InputProduct> digisTokenEB_;
0033     const device::EDGetToken<InputProduct> digisTokenEE_;
0034     using OutputProduct = EcalUncalibratedRecHitDeviceCollection;
0035     const device::EDPutToken<OutputProduct> uncalibRecHitsTokenEB_;
0036     const device::EDPutToken<OutputProduct> uncalibRecHitsTokenEE_;
0037 
0038     // conditions tokens
0039     const device::ESGetToken<EcalMultifitConditionsDevice, EcalMultifitConditionsRcd> multifitConditionsToken_;
0040     const device::ESGetToken<EcalMultifitParametersDevice, EcalMultifitParametersRcd> multifitParametersToken_;
0041 
0042     // configuration parameters
0043     ecal::multifit::ConfigurationParameters configParameters_;
0044 
0045     cms::alpakatools::host_buffer<uint32_t> ebDigisSizeHostBuf_;
0046     cms::alpakatools::host_buffer<uint32_t> eeDigisSizeHostBuf_;
0047   };
0048 
0049   void EcalUncalibRecHitProducerPortable::fillDescriptions(edm::ConfigurationDescriptions& confDesc) {
0050     edm::ParameterSetDescription desc;
0051 
0052     desc.add<edm::InputTag>("digisLabelEB", edm::InputTag("ecalRawToDigiPortable", "ebDigis"));
0053     desc.add<edm::InputTag>("digisLabelEE", edm::InputTag("ecalRawToDigiPortable", "eeDigis"));
0054 
0055     desc.add<std::string>("recHitsLabelEB", "EcalUncalibRecHitsEB");
0056     desc.add<std::string>("recHitsLabelEE", "EcalUncalibRecHitsEE");
0057 
0058     desc.add<double>("EBtimeFitLimits_Lower", 0.2);
0059     desc.add<double>("EBtimeFitLimits_Upper", 1.4);
0060     desc.add<double>("EEtimeFitLimits_Lower", 0.2);
0061     desc.add<double>("EEtimeFitLimits_Upper", 1.4);
0062     desc.add<double>("EBtimeConstantTerm", .6);
0063     desc.add<double>("EEtimeConstantTerm", 1.0);
0064     desc.add<double>("EBtimeNconst", 28.5);
0065     desc.add<double>("EEtimeNconst", 31.8);
0066     desc.add<double>("outOfTimeThresholdGain12pEB", 5);
0067     desc.add<double>("outOfTimeThresholdGain12mEB", 5);
0068     desc.add<double>("outOfTimeThresholdGain61pEB", 5);
0069     desc.add<double>("outOfTimeThresholdGain61mEB", 5);
0070     desc.add<double>("outOfTimeThresholdGain12pEE", 1000);
0071     desc.add<double>("outOfTimeThresholdGain12mEE", 1000);
0072     desc.add<double>("outOfTimeThresholdGain61pEE", 1000);
0073     desc.add<double>("outOfTimeThresholdGain61mEE", 1000);
0074     desc.add<double>("amplitudeThresholdEB", 10);
0075     desc.add<double>("amplitudeThresholdEE", 10);
0076     desc.addUntracked<std::vector<uint32_t>>("kernelMinimizeThreads", {32, 1, 1});
0077     desc.add<bool>("shouldRunTimingComputation", true);
0078     confDesc.addWithDefaultLabel(desc);
0079   }
0080 
0081   EcalUncalibRecHitProducerPortable::EcalUncalibRecHitProducerPortable(const edm::ParameterSet& ps)
0082       : digisTokenEB_{consumes(ps.getParameter<edm::InputTag>("digisLabelEB"))},
0083         digisTokenEE_{consumes(ps.getParameter<edm::InputTag>("digisLabelEE"))},
0084         uncalibRecHitsTokenEB_{produces(ps.getParameter<std::string>("recHitsLabelEB"))},
0085         uncalibRecHitsTokenEE_{produces(ps.getParameter<std::string>("recHitsLabelEE"))},
0086         multifitConditionsToken_{esConsumes()},
0087         multifitParametersToken_{esConsumes()},
0088         ebDigisSizeHostBuf_{cms::alpakatools::make_host_buffer<uint32_t>()},
0089         eeDigisSizeHostBuf_{cms::alpakatools::make_host_buffer<uint32_t>()} {
0090     // Workaround until the ProductID problem in issue https://github.com/cms-sw/cmssw/issues/44643 is fixed
0091 #ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
0092     producesTemporarily("edm::DeviceProduct<alpaka_cuda_async::EcalUncalibratedRecHitDeviceCollection>",
0093                         ps.getParameter<std::string>("recHitsLabelEB"));
0094     producesTemporarily("edm::DeviceProduct<alpaka_cuda_async::EcalUncalibratedRecHitDeviceCollection>",
0095                         ps.getParameter<std::string>("recHitsLabelEE"));
0096 #endif
0097 
0098     std::pair<double, double> EBtimeFitLimits, EEtimeFitLimits;
0099     EBtimeFitLimits.first = ps.getParameter<double>("EBtimeFitLimits_Lower");
0100     EBtimeFitLimits.second = ps.getParameter<double>("EBtimeFitLimits_Upper");
0101     EEtimeFitLimits.first = ps.getParameter<double>("EEtimeFitLimits_Lower");
0102     EEtimeFitLimits.second = ps.getParameter<double>("EEtimeFitLimits_Upper");
0103 
0104     auto EBtimeConstantTerm = ps.getParameter<double>("EBtimeConstantTerm");
0105     auto EEtimeConstantTerm = ps.getParameter<double>("EEtimeConstantTerm");
0106     auto EBtimeNconst = ps.getParameter<double>("EBtimeNconst");
0107     auto EEtimeNconst = ps.getParameter<double>("EEtimeNconst");
0108 
0109     auto outOfTimeThreshG12pEB = ps.getParameter<double>("outOfTimeThresholdGain12pEB");
0110     auto outOfTimeThreshG12mEB = ps.getParameter<double>("outOfTimeThresholdGain12mEB");
0111     auto outOfTimeThreshG61pEB = ps.getParameter<double>("outOfTimeThresholdGain61pEB");
0112     auto outOfTimeThreshG61mEB = ps.getParameter<double>("outOfTimeThresholdGain61mEB");
0113     auto outOfTimeThreshG12pEE = ps.getParameter<double>("outOfTimeThresholdGain12pEE");
0114     auto outOfTimeThreshG12mEE = ps.getParameter<double>("outOfTimeThresholdGain12mEE");
0115     auto outOfTimeThreshG61pEE = ps.getParameter<double>("outOfTimeThresholdGain61pEE");
0116     auto outOfTimeThreshG61mEE = ps.getParameter<double>("outOfTimeThresholdGain61mEE");
0117     auto amplitudeThreshEB = ps.getParameter<double>("amplitudeThresholdEB");
0118     auto amplitudeThreshEE = ps.getParameter<double>("amplitudeThresholdEE");
0119 
0120     // switch to run timing computation kernels
0121     configParameters_.shouldRunTimingComputation = ps.getParameter<bool>("shouldRunTimingComputation");
0122 
0123     // minimize kernel launch conf
0124     auto threadsMinimize = ps.getUntrackedParameter<std::vector<uint32_t>>("kernelMinimizeThreads");
0125     configParameters_.kernelMinimizeThreads[0] = threadsMinimize[0];
0126     configParameters_.kernelMinimizeThreads[1] = threadsMinimize[1];
0127     configParameters_.kernelMinimizeThreads[2] = threadsMinimize[2];
0128 
0129     //
0130     // configuration and physics parameters: done once
0131     // assume there is a single device
0132     // use sync copying
0133     //
0134 
0135     // time fit parameters and limits
0136     configParameters_.timeFitLimitsFirstEB = EBtimeFitLimits.first;
0137     configParameters_.timeFitLimitsSecondEB = EBtimeFitLimits.second;
0138     configParameters_.timeFitLimitsFirstEE = EEtimeFitLimits.first;
0139     configParameters_.timeFitLimitsSecondEE = EEtimeFitLimits.second;
0140 
0141     // time constant terms
0142     configParameters_.timeConstantTermEB = EBtimeConstantTerm;
0143     configParameters_.timeConstantTermEE = EEtimeConstantTerm;
0144 
0145     // time N const
0146     configParameters_.timeNconstEB = EBtimeNconst;
0147     configParameters_.timeNconstEE = EEtimeNconst;
0148 
0149     // amplitude threshold for time flags
0150     configParameters_.amplitudeThreshEB = amplitudeThreshEB;
0151     configParameters_.amplitudeThreshEE = amplitudeThreshEE;
0152 
0153     // out of time thresholds gain-dependent
0154     configParameters_.outOfTimeThreshG12pEB = outOfTimeThreshG12pEB;
0155     configParameters_.outOfTimeThreshG12pEE = outOfTimeThreshG12pEE;
0156     configParameters_.outOfTimeThreshG61pEB = outOfTimeThreshG61pEB;
0157     configParameters_.outOfTimeThreshG61pEE = outOfTimeThreshG61pEE;
0158     configParameters_.outOfTimeThreshG12mEB = outOfTimeThreshG12mEB;
0159     configParameters_.outOfTimeThreshG12mEE = outOfTimeThreshG12mEE;
0160     configParameters_.outOfTimeThreshG61mEB = outOfTimeThreshG61mEB;
0161     configParameters_.outOfTimeThreshG61mEE = outOfTimeThreshG61mEE;
0162   }
0163 
0164   void EcalUncalibRecHitProducerPortable::acquire(device::Event const& event, device::EventSetup const& setup) {
0165     auto& queue = event.queue();
0166 
0167     // get device collections from event
0168     auto const& ebDigisDev = event.get(digisTokenEB_);
0169     auto const& eeDigisDev = event.get(digisTokenEE_);
0170 
0171     // copy the actual numbers of digis in the collections to host
0172     auto ebDigisSizeDevConstView =
0173         cms::alpakatools::make_device_view<const uint32_t>(alpaka::getDev(queue), ebDigisDev.const_view().size());
0174     auto eeDigisSizeDevConstView =
0175         cms::alpakatools::make_device_view<const uint32_t>(alpaka::getDev(queue), eeDigisDev.const_view().size());
0176     alpaka::memcpy(queue, ebDigisSizeHostBuf_, ebDigisSizeDevConstView);
0177     alpaka::memcpy(queue, eeDigisSizeHostBuf_, eeDigisSizeDevConstView);
0178   }
0179 
0180   void EcalUncalibRecHitProducerPortable::produce(device::Event& event, device::EventSetup const& setup) {
0181     auto& queue = event.queue();
0182 
0183     // get device collections from event
0184     auto const& ebDigisDev = event.get(digisTokenEB_);
0185     auto const& eeDigisDev = event.get(digisTokenEE_);
0186 
0187     // get the actual numbers of digis in the collections
0188     auto const ebDigisSize = static_cast<int>(*ebDigisSizeHostBuf_.data());
0189     auto const eeDigisSize = static_cast<int>(*eeDigisSizeHostBuf_.data());
0190 
0191     // output device collections
0192     OutputProduct uncalibRecHitsDevEB{ebDigisSize, queue};
0193     OutputProduct uncalibRecHitsDevEE{eeDigisSize, queue};
0194     // reset the size scalar of the SoA
0195     // memset takes an alpaka view that is created from the scalar in a view to the portable device collection
0196     auto uncalibRecHitSizeViewEB =
0197         cms::alpakatools::make_device_view<uint32_t>(alpaka::getDev(queue), uncalibRecHitsDevEB.view().size());
0198     auto uncalibRecHitSizeViewEE =
0199         cms::alpakatools::make_device_view<uint32_t>(alpaka::getDev(queue), uncalibRecHitsDevEE.view().size());
0200     alpaka::memset(queue, uncalibRecHitSizeViewEB, 0);
0201     alpaka::memset(queue, uncalibRecHitSizeViewEE, 0);
0202 
0203     // stop here if there are no digis
0204     if (ebDigisSize + eeDigisSize > 0) {
0205       // conditions
0206       auto const& multifitConditionsDev = setup.getData(multifitConditionsToken_);
0207       auto const& multifitParametersDev = setup.getData(multifitParametersToken_);
0208 
0209       //
0210       // schedule algorithms
0211       //
0212       ecal::multifit::launchKernels(queue,
0213                                     ebDigisDev,
0214                                     eeDigisDev,
0215                                     uncalibRecHitsDevEB,
0216                                     uncalibRecHitsDevEE,
0217                                     multifitConditionsDev,
0218                                     multifitParametersDev,
0219                                     configParameters_);
0220     }
0221 
0222     // put into the event
0223     event.emplace(uncalibRecHitsTokenEB_, std::move(uncalibRecHitsDevEB));
0224     event.emplace(uncalibRecHitsTokenEE_, std::move(uncalibRecHitsDevEE));
0225   }
0226 
0227 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0228 
0229 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
0230 DEFINE_FWK_ALPAKA_MODULE(EcalUncalibRecHitProducerPortable);