Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-05 03:15:03

0001 #include "DataFormats/PortableTestObjects/interface/alpaka/TestDeviceCollection.h"
0002 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0005 #include "FWCore/Utilities/interface/InputTag.h"
0006 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/global/EDProducer.h"
0007 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/EDPutToken.h"
0008 #include "HeterogeneousCore/AlpakaCore/interface/CopyToDeviceCache.h"
0009 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0010 
0011 #include "TestAlgo.h"
0012 
0013 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0014   /**
0015    * This class demonstrates a global EDProducer that
0016    * - uses a CopyToDeviceCache to copy some host-side data to the devices of the backend.
0017    * - produces a device EDProduct (that can get transferred to host automatically)
0018    */
0019   class TestAlpakaGlobalProducerCopyToDeviceCache : public global::EDProducer<> {
0020   public:
0021     TestAlpakaGlobalProducerCopyToDeviceCache(edm::ParameterSet const& config)
0022         : EDProducer(config),
0023           getToken_(consumes(config.getParameter<edm::InputTag>("source"))),
0024           getTokenMulti2_(consumes(config.getParameter<edm::InputTag>("source"))),
0025           getTokenMulti3_(consumes(config.getParameter<edm::InputTag>("source"))),
0026           putToken_{produces()},
0027           putTokenMulti2_{produces()},
0028           putTokenMulti3_{produces()},
0029           // create host-side object that gets implicitly copied to all devices of the backend
0030           deviceCache_{[&config]() {
0031             auto buffer = cms::alpakatools::make_host_buffer<TestAlgo::UpdateInfo>();
0032             *buffer = TestAlgo::UpdateInfo{config.getParameter<int32_t>("x"),
0033                                            config.getParameter<int32_t>("y"),
0034                                            config.getParameter<int32_t>("z")};
0035             return buffer;
0036           }()} {}
0037 
0038     void produce(edm::StreamID, device::Event& iEvent, device::EventSetup const& iSetup) const override {
0039       auto const& input = iEvent.get(getToken_);
0040       auto const& inputMulti2 = iEvent.get(getTokenMulti2_);
0041       auto const& inputMulti3 = iEvent.get(getTokenMulti3_);
0042 
0043       // get the object corresponding to the Device the Event is being processed on
0044       auto const& infoBuffer = deviceCache_.get(iEvent.queue());
0045 
0046       // run the algorithm, potentially asynchronously
0047       auto deviceProduct = algo_.update(iEvent.queue(), input, infoBuffer.data());
0048       auto deviceProductMulti2 = algo_.updateMulti2(iEvent.queue(), inputMulti2, infoBuffer.data());
0049       auto deviceProductMulti3 = algo_.updateMulti3(iEvent.queue(), inputMulti3, infoBuffer.data());
0050 
0051       iEvent.emplace(putToken_, std::move(deviceProduct));
0052       iEvent.emplace(putTokenMulti2_, std::move(deviceProductMulti2));
0053       iEvent.emplace(putTokenMulti3_, std::move(deviceProductMulti3));
0054     }
0055 
0056     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0057       edm::ParameterSetDescription desc;
0058 
0059       desc.add("source", edm::InputTag{});
0060       desc.add<int32_t>("x", 0);
0061       desc.add<int32_t>("y", 1);
0062       desc.add<int32_t>("z", 2);
0063 
0064       descriptions.addWithDefaultLabel(desc);
0065     }
0066 
0067   private:
0068     const device::EDGetToken<portabletest::TestDeviceCollection> getToken_;
0069     const device::EDGetToken<portabletest::TestDeviceMultiCollection2> getTokenMulti2_;
0070     const device::EDGetToken<portabletest::TestDeviceMultiCollection3> getTokenMulti3_;
0071     const device::EDPutToken<portabletest::TestDeviceCollection> putToken_;
0072     const device::EDPutToken<portabletest::TestDeviceMultiCollection2> putTokenMulti2_;
0073     const device::EDPutToken<portabletest::TestDeviceMultiCollection3> putTokenMulti3_;
0074 
0075     // implementation of the algorithm
0076     TestAlgo algo_;
0077 
0078     cms::alpakatools::CopyToDeviceCache<Device, cms::alpakatools::host_buffer<TestAlgo::UpdateInfo>> deviceCache_;
0079   };
0080 
0081 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0082 
0083 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
0084 DEFINE_FWK_ALPAKA_MODULE(TestAlpakaGlobalProducerCopyToDeviceCache);