CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TestCUDAProducerGPUtoCPU.cc
Go to the documentation of this file.
7 
13 
15 
16 class TestCUDAProducerGPUtoCPU : public edm::stream::EDProducer<edm::ExternalWork> {
17 public:
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(),
64  TestCUDAProducerGPUKernel::NUM_VALUES * sizeof(float),
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 
Log< level::Info, true > LogVerbatim
EventNumber_t event() const
Definition: EventID.h:40
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
const float * get() const
Definition: Thing.h:13
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
cms::cuda::host::unique_ptr< float[]> buffer_
int iEvent
Definition: GenABIO.cc:224
TestCUDAProducerGPUtoCPU(edm::ParameterSet const &iConfig)
def move
Definition: eostools.py:511
edm::EDPutTokenT< int > const dstToken_
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void setComment(std::string const &value)
OrphanHandle< PROD > emplace(EDPutTokenT< PROD > token, Args &&...args)
puts a new product
Definition: Event.h:433
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::EDGetTokenT< cms::cuda::Product< cms::cudatest::Thing > > const srcToken_
void acquire(edm::Event const &iEvent, edm::EventSetup const &iSetup, edm::WaitingTaskWithArenaHolder waitingTaskHolder) override
edm::EventID id() const
Definition: EventBase.h:59
static std::atomic< unsigned int > counter
StreamID streamID() const
Definition: Event.h:98
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