Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-07 02:51:59

0001 #include <alpaka/alpaka.hpp>
0002 
0003 #include "DataFormats/Portable/interface/Product.h"
0004 #include "DataFormats/PortableTestObjects/interface/TestHostCollection.h"
0005 #include "DataFormats/PortableTestObjects/interface/alpaka/TestDeviceCollection.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 #include "FWCore/Framework/interface/stream/EDProducer.h"
0010 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0013 #include "FWCore/ServiceRegistry/interface/Service.h"
0014 #include "FWCore/Utilities/interface/EDGetToken.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 #include "FWCore/Utilities/interface/StreamID.h"
0017 #include "HeterogeneousCore/AlpakaCore/interface/ScopedContext.h"
0018 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0019 #include "HeterogeneousCore/AlpakaServices/interface/alpaka/AlpakaService.h"
0020 
0021 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0022 
0023   class TestAlpakaTranscriber : public edm::stream::EDProducer<edm::ExternalWork> {
0024   public:
0025     TestAlpakaTranscriber(edm::ParameterSet const& config)
0026         : deviceToken_{consumes(config.getParameter<edm::InputTag>("source"))}, hostToken_{produces()} {}
0027 
0028     void beginStream(edm::StreamID) override {
0029       edm::Service<ALPAKA_TYPE_ALIAS(AlpakaService)> service;
0030       if (not service->enabled()) {
0031         throw cms::Exception("Configuration") << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " is disabled.";
0032       }
0033     }
0034 
0035     void acquire(edm::Event const& event, edm::EventSetup const& setup, edm::WaitingTaskWithArenaHolder task) override {
0036       // create a context reusing the same device and queue as the producer of the input collection
0037       auto const& input = event.get(deviceToken_);
0038       cms::alpakatools::ScopedContextAcquire ctx{input, std::move(task)};
0039 
0040       portabletest::TestDeviceCollection const& deviceProduct = ctx.get(input);
0041 
0042       // allocate a host product based on the metadata of the device product
0043       hostProduct_ = portabletest::TestHostCollection{deviceProduct->metadata().size(), ctx.queue()};
0044 
0045       // FIXME find a way to avoid the copy when the device product is actually a wrapped host prodict
0046 
0047       // copy the content of the device product to the host product
0048       alpaka::memcpy(ctx.queue(), hostProduct_.buffer(), deviceProduct.const_buffer());
0049 
0050       // do not wait for the asynchronous operation to complete
0051     }
0052 
0053     void produce(edm::Event& event, edm::EventSetup const&) override {
0054       // produce() is called once the asynchronous operation has completed, so there is no need for an explicit wait
0055       event.emplace(hostToken_, std::move(hostProduct_));
0056     }
0057 
0058     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0059       edm::ParameterSetDescription desc;
0060       desc.add<edm::InputTag>("source");
0061       descriptions.addWithDefaultLabel(desc);
0062     }
0063 
0064   private:
0065     const edm::EDGetTokenT<cms::alpakatools::Product<Queue, portabletest::TestDeviceCollection>> deviceToken_;
0066     const edm::EDPutTokenT<portabletest::TestHostCollection> hostToken_;
0067 
0068     // hold the output product between acquire() and produce()
0069     portabletest::TestHostCollection hostProduct_;
0070   };
0071 
0072 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0073 
0074 #include "HeterogeneousCore/AlpakaCore/interface/MakerMacros.h"
0075 DEFINE_FWK_ALPAKA_MODULE(TestAlpakaTranscriber);