Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:44

0001 #include "FWCore/Framework/interface/global/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/Framework/interface/Frameworkfwd.h"
0004 #include "FWCore/Framework/interface/MakerMacros.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0007 
0008 #include <chrono>
0009 #include <random>
0010 #include <thread>
0011 
0012 class TestCUDAProducerCPU : public edm::global::EDProducer<> {
0013 public:
0014   explicit TestCUDAProducerCPU(edm::ParameterSet const& iConfig);
0015   ~TestCUDAProducerCPU() override = default;
0016 
0017   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0018 
0019   void produce(edm::StreamID id, edm::Event& iEvent, edm::EventSetup const& iSetup) const override;
0020 
0021 private:
0022   std::string const label_;
0023   edm::EDGetTokenT<int> srcToken_;
0024   edm::EDPutTokenT<int> const dstToken_;
0025 };
0026 
0027 TestCUDAProducerCPU::TestCUDAProducerCPU(edm::ParameterSet const& iConfig)
0028     : label_{iConfig.getParameter<std::string>("@module_label")}, dstToken_{produces<int>()} {
0029   auto srcTag = iConfig.getParameter<edm::InputTag>("src");
0030   if (!srcTag.label().empty()) {
0031     srcToken_ = consumes<int>(srcTag);
0032   }
0033 }
0034 
0035 void TestCUDAProducerCPU::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0036   edm::ParameterSetDescription desc;
0037   desc.add<edm::InputTag>("src", edm::InputTag())->setComment("Optional source of another TestCUDAProducerCPU.");
0038   descriptions.addWithDefaultLabel(desc);
0039   descriptions.setComment("This EDProducer is part of the TestCUDAProducer* family. It models a CPU algorithm.");
0040 }
0041 
0042 void TestCUDAProducerCPU::produce(edm::StreamID id, edm::Event& iEvent, edm::EventSetup const& iSetup) const {
0043   edm::LogVerbatim("TestCUDAProducerCPU")
0044       << label_ << " TestCUDAProducerCPU::produce begin event " << iEvent.id().event() << " stream " << id;
0045 
0046   int input = 0;
0047   if (!srcToken_.isUninitialized()) {
0048     input = iEvent.get(srcToken_);
0049   }
0050 
0051   std::random_device r;
0052   std::mt19937 gen(r());
0053   auto dist = std::uniform_real_distribution<>(0.2, 1.5);
0054   auto dur = dist(gen);
0055   edm::LogVerbatim("TestCUDAProducerCPU")
0056       << " Task (CPU) for event " << iEvent.id().event() << " in stream " << id << " will take " << dur << " seconds";
0057   std::this_thread::sleep_for(std::chrono::seconds(1) * dur);
0058 
0059   unsigned int const output = input + id * 100 + iEvent.id().event();
0060 
0061   iEvent.emplace(dstToken_, output);
0062 
0063   edm::LogVerbatim("TestCUDAProducerCPU") << label_ << " TestCUDAProducerCPU::produce end event " << iEvent.id().event()
0064                                           << " stream " << id << " result " << output;
0065 }
0066 
0067 DEFINE_FWK_MODULE(TestCUDAProducerCPU);