File indexing completed on 2024-04-06 12:29:03
0001 #include <cuda_runtime.h>
0002
0003 #include "CUDADataFormats/BeamSpot/interface/BeamSpotCUDA.h"
0004 #include "CUDADataFormats/Common/interface/Product.h"
0005 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0006 #include "DataFormats/BeamSpot/interface/BeamSpotPOD.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/Framework/interface/global/EDProducer.h"
0010 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0013 #include "FWCore/ServiceRegistry/interface/Service.h"
0014 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0015 #include "HeterogeneousCore/CUDAServices/interface/CUDAInterface.h"
0016 #include "HeterogeneousCore/CUDAUtilities/interface/copyAsync.h"
0017 #include "HeterogeneousCore/CUDAUtilities/interface/host_noncached_unique_ptr.h"
0018
0019 namespace {
0020
0021 class BeamSpotHost {
0022 public:
0023 BeamSpotHost() : data_h_{cms::cuda::make_host_noncached_unique<BeamSpotPOD>(cudaHostAllocWriteCombined)} {}
0024
0025 BeamSpotHost(BeamSpotHost const&) = delete;
0026 BeamSpotHost(BeamSpotHost&&) = default;
0027
0028 BeamSpotHost& operator=(BeamSpotHost const&) = delete;
0029 BeamSpotHost& operator=(BeamSpotHost&&) = default;
0030
0031 BeamSpotPOD* data() { return data_h_.get(); }
0032 BeamSpotPOD const* data() const { return data_h_.get(); }
0033
0034 cms::cuda::host::noncached::unique_ptr<BeamSpotPOD>& ptr() { return data_h_; }
0035 cms::cuda::host::noncached::unique_ptr<BeamSpotPOD> const& ptr() const { return data_h_; }
0036
0037 private:
0038 cms::cuda::host::noncached::unique_ptr<BeamSpotPOD> data_h_;
0039 };
0040
0041 }
0042
0043 class BeamSpotToCUDA : public edm::global::EDProducer<edm::StreamCache<BeamSpotHost>> {
0044 public:
0045 explicit BeamSpotToCUDA(const edm::ParameterSet& iConfig);
0046 ~BeamSpotToCUDA() override = default;
0047
0048 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0049
0050 std::unique_ptr<BeamSpotHost> beginStream(edm::StreamID) const override {
0051 edm::Service<CUDAInterface> cuda;
0052 if (cuda and cuda->enabled()) {
0053 return std::make_unique<BeamSpotHost>();
0054 } else {
0055 return nullptr;
0056 }
0057 }
0058 void produce(edm::StreamID streamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0059
0060 private:
0061 const edm::EDGetTokenT<reco::BeamSpot> bsGetToken_;
0062 const edm::EDPutTokenT<cms::cuda::Product<BeamSpotCUDA>> bsPutToken_;
0063 };
0064
0065 BeamSpotToCUDA::BeamSpotToCUDA(const edm::ParameterSet& iConfig)
0066 : bsGetToken_{consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("src"))},
0067 bsPutToken_{produces<cms::cuda::Product<BeamSpotCUDA>>()} {}
0068
0069 void BeamSpotToCUDA::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0070 edm::ParameterSetDescription desc;
0071 desc.add<edm::InputTag>("src", edm::InputTag("offlineBeamSpot"));
0072 descriptions.add("offlineBeamSpotToCUDA", desc);
0073 }
0074
0075 void BeamSpotToCUDA::produce(edm::StreamID streamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0076 cms::cuda::ScopedContextProduce ctx{streamID};
0077
0078 const reco::BeamSpot& bs = iEvent.get(bsGetToken_);
0079
0080 auto& bsHost = streamCache(streamID)->ptr();
0081
0082 bsHost->x = bs.x0();
0083 bsHost->y = bs.y0();
0084 bsHost->z = bs.z0();
0085
0086 bsHost->sigmaZ = bs.sigmaZ();
0087 bsHost->beamWidthX = bs.BeamWidthX();
0088 bsHost->beamWidthY = bs.BeamWidthY();
0089 bsHost->dxdz = bs.dxdz();
0090 bsHost->dydz = bs.dydz();
0091 bsHost->emittanceX = bs.emittanceX();
0092 bsHost->emittanceY = bs.emittanceY();
0093 bsHost->betaStar = bs.betaStar();
0094
0095 BeamSpotCUDA bsDevice(ctx.stream());
0096 cms::cuda::copyAsync(bsDevice.ptr(), bsHost, ctx.stream());
0097
0098 ctx.emplace(iEvent, bsPutToken_, std::move(bsDevice));
0099 }
0100
0101 DEFINE_FWK_MODULE(BeamSpotToCUDA);