CMS 3D CMS Logo

TestCUDAProducerGPUtoCPU.cc
Go to the documentation of this file.
7 
13 
15 
16 class TestCUDAProducerGPUtoCPU : public edm::stream::EDProducer<edm::ExternalWork> {
17 public:
18  explicit TestCUDAProducerGPUtoCPU(edm::ParameterSet const& iConfig);
19  ~TestCUDAProducerGPUtoCPU() override = default;
20 
21  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
22 
23  void acquire(edm::Event const& iEvent,
24  edm::EventSetup const& iSetup,
25  edm::WaitingTaskWithArenaHolder waitingTaskHolder) override;
26 
27  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
28 
29 private:
34 };
35 
37  : label_{iConfig.getParameter<std::string>("@module_label")},
38  srcToken_{consumes<cms::cuda::Product<cms::cudatest::Thing>>(iConfig.getParameter<edm::InputTag>("src"))},
39  dstToken_{produces<int>()} {}
40 
43  desc.add<edm::InputTag>("src", edm::InputTag())->setComment("Source for cms::cuda::Product<cms::cudatest::Thing>.");
44  descriptions.addWithDefaultLabel(desc);
45  descriptions.setComment(
46  "This EDProducer is part of the TestCUDAProducer* family. It models the GPU->CPU data transfer and formatting of "
47  "the data to legacy data format. Produces int, to be compatible with TestCUDAProducerCPU.");
48 }
49 
51  edm::EventSetup const& iSetup,
52  edm::WaitingTaskWithArenaHolder waitingTaskHolder) {
53  edm::LogVerbatim("TestCUDAProducerGPUtoCPU") << label_ << " TestCUDAProducerGPUtoCPU::acquire begin event "
54  << iEvent.id().event() << " stream " << iEvent.streamID();
55 
56  auto const& in = iEvent.get(srcToken_);
57  cms::cuda::ScopedContextAcquire ctx{in, std::move(waitingTaskHolder)};
58  cms::cudatest::Thing const& device = ctx.get(in);
59 
60  buffer_ = cms::cuda::make_host_unique<float[]>(TestCUDAProducerGPUKernel::NUM_VALUES, ctx.stream());
61  // Enqueue async copy, continue in produce once finished
62  cudaCheck(cudaMemcpyAsync(buffer_.get(),
63  device.get(),
65  cudaMemcpyDeviceToHost,
66  ctx.stream()));
67 
68  edm::LogVerbatim("TestCUDAProducerGPUtoCPU") << label_ << " TestCUDAProducerGPUtoCPU::acquire end event "
69  << iEvent.id().event() << " stream " << iEvent.streamID();
70 }
71 
73  edm::LogVerbatim("TestCUDAProducerGPUtoCPU") << label_ << " TestCUDAProducerGPUtoCPU::produce begin event "
74  << iEvent.id().event() << " stream " << iEvent.streamID();
75 
76  int counter = 0;
77  for (int i = 0; i < TestCUDAProducerGPUKernel::NUM_VALUES; ++i) {
78  counter += buffer_[i];
79  }
80  buffer_.reset(); // not so nice, but no way around?
81 
82  iEvent.emplace(dstToken_, counter);
83 
84  edm::LogVerbatim("TestCUDAProducerGPUtoCPU")
85  << label_ << " TestCUDAProducerGPUtoCPU::produce end event " << iEvent.id().event() << " stream "
86  << iEvent.streamID() << " result " << counter;
87 }
88 
const float * get() const
Definition: Thing.h:13
Log< level::Info, true > LogVerbatim
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
cms::cuda::host::unique_ptr< float[]> buffer_
int iEvent
Definition: GenABIO.cc:224
TestCUDAProducerGPUtoCPU(edm::ParameterSet const &iConfig)
edm::EDPutTokenT< int > const dstToken_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void setComment(std::string const &value)
edm::EDGetTokenT< cms::cuda::Product< cms::cudatest::Thing > > const srcToken_
void acquire(edm::Event const &iEvent, edm::EventSetup const &iSetup, edm::WaitingTaskWithArenaHolder waitingTaskHolder) override
static std::atomic< unsigned int > counter
std::unique_ptr< T, impl::HostDeleter > unique_ptr
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69
~TestCUDAProducerGPUtoCPU() override=default
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
def move(src, dest)
Definition: eostools.py:511