Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:49:25

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/Frameworkfwd.h"
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0007 #include "FWCore/ServiceRegistry/interface/Service.h"
0008 
0009 #include "CUDADataFormats/Common/interface/Product.h"
0010 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0011 #include "HeterogeneousCore/CUDAServices/interface/CUDAService.h"
0012 #include "HeterogeneousCore/CUDATest/interface/Thing.h"
0013 #include "HeterogeneousCore/CUDAUtilities/interface/StreamCache.h"
0014 
0015 #include "TestCUDAAnalyzerGPUKernel.h"
0016 
0017 class TestCUDAAnalyzerGPU : public edm::global::EDAnalyzer<> {
0018 public:
0019   explicit TestCUDAAnalyzerGPU(edm::ParameterSet const& iConfig);
0020   ~TestCUDAAnalyzerGPU() override = default;
0021 
0022   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0023 
0024   void analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const& iSetup) const override;
0025   void endJob() override;
0026 
0027 private:
0028   std::string const label_;
0029   edm::EDGetTokenT<cms::cuda::Product<cms::cudatest::Thing>> const srcToken_;
0030   double const minValue_;
0031   double const maxValue_;
0032   // the public interface is thread safe
0033   CMS_THREAD_SAFE mutable std::unique_ptr<TestCUDAAnalyzerGPUKernel> gpuAlgo_;
0034 };
0035 
0036 TestCUDAAnalyzerGPU::TestCUDAAnalyzerGPU(edm::ParameterSet const& iConfig)
0037     : label_(iConfig.getParameter<std::string>("@module_label")),
0038       srcToken_(consumes<cms::cuda::Product<cms::cudatest::Thing>>(iConfig.getParameter<edm::InputTag>("src"))),
0039       minValue_(iConfig.getParameter<double>("minValue")),
0040       maxValue_(iConfig.getParameter<double>("maxValue")) {
0041   edm::Service<CUDAService> cs;
0042   if (cs->enabled()) {
0043     auto streamPtr = cms::cuda::getStreamCache().get();
0044     gpuAlgo_ = std::make_unique<TestCUDAAnalyzerGPUKernel>(streamPtr.get());
0045   }
0046 }
0047 
0048 void TestCUDAAnalyzerGPU::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0049   edm::ParameterSetDescription desc;
0050   desc.add<edm::InputTag>("src", edm::InputTag())->setComment("Source of cms::cuda::Product<cms::cudatest::Thing>.");
0051   desc.add<double>("minValue", -1e308);
0052   desc.add<double>("maxValue", 1e308);
0053   descriptions.addWithDefaultLabel(desc);
0054   descriptions.setComment("This EDAnalyzer is part of the TestCUDAProducer* family. It models a GPU analyzer.");
0055 }
0056 
0057 void TestCUDAAnalyzerGPU::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const& iSetup) const {
0058   edm::LogVerbatim("TestCUDAAnalyzerGPU") << label_ << " TestCUDAAnalyzerGPU::analyze begin event "
0059                                           << iEvent.id().event() << " stream " << iEvent.streamID();
0060 
0061   auto const& in = iEvent.get(srcToken_);
0062   cms::cuda::ScopedContextAnalyze ctx{in};
0063   cms::cudatest::Thing const& input = ctx.get(in);
0064   gpuAlgo_->analyzeAsync(input.get(), ctx.stream());
0065 
0066   edm::LogVerbatim("TestCUDAAnalyzerGPU")
0067       << label_ << " TestCUDAAnalyzerGPU::analyze end event " << iEvent.id().event() << " stream " << iEvent.streamID();
0068 }
0069 
0070 void TestCUDAAnalyzerGPU::endJob() {
0071   edm::LogVerbatim("TestCUDAAnalyzerGPU") << label_ << " TestCUDAAnalyzerGPU::endJob begin";
0072 
0073   auto streamPtr = cms::cuda::getStreamCache().get();
0074   auto value = gpuAlgo_->value(streamPtr.get());
0075   edm::LogVerbatim("TestCUDAAnalyzerGPU") << label_ << "  accumulated value " << value;
0076   assert(minValue_ <= value && value <= maxValue_);
0077 
0078   edm::LogVerbatim("TestCUDAAnalyzerGPU") << label_ << " TestCUDAAnalyzerGPU::endJob end";
0079 }
0080 
0081 DEFINE_FWK_MODULE(TestCUDAAnalyzerGPU);