Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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