Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-11-24 00:02:23

0001 #include "FWCore/Framework/interface/ESTransientHandle.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include "FWCore/Utilities/interface/ESGetToken.h"
0004 #include "FWCore/Utilities/interface/typelookup.h"
0005 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/ESProducer.h"
0006 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/ModuleFactory.h"
0007 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0008 #include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
0009 #include "HeterogeneousCore/AlpakaTest/interface/AlpakaESTestRecords.h"
0010 #include "HeterogeneousCore/AlpakaTest/interface/ESTestData.h"
0011 #include "HeterogeneousCore/AlpakaTest/interface/alpaka/AlpakaESTestData.h"
0012 
0013 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0014   /**
0015    * This class demonstrates and ESProducer on the data model 1 that
0016    * - consumes a standard host ESProduct and converts the data into an Alpaka buffer
0017    * - transfers the buffer contents to the device of the backend
0018    */
0019   class TestAlpakaESProducerA : public ESProducer {
0020   public:
0021     class HostBuffer {
0022     public:
0023       using Buffer = cms::alpakatools::device_buffer<DevHost, int[]>;
0024       using ConstBuffer = cms::alpakatools::const_device_buffer<DevHost, int[]>;
0025 
0026       HostBuffer(Buffer buffer) : buffer_(std::move(buffer)) {}
0027 
0028       ConstBuffer buffer() const { return buffer_; }
0029 
0030     private:
0031       Buffer buffer_;
0032     };
0033 
0034     TestAlpakaESProducerA(edm::ParameterSet const& iConfig) {
0035       {
0036         auto cc = setWhatProduced(this, &TestAlpakaESProducerA::produceHost);
0037         token_ = cc.consumes();
0038       }
0039       {
0040         auto cc = setWhatProduced(this, &TestAlpakaESProducerA::produceDevice);
0041         hostToken_ = cc.consumes();
0042       }
0043     }
0044 
0045     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0046       edm::ParameterSetDescription desc;
0047       descriptions.addWithDefaultLabel(desc);
0048     }
0049 
0050     std::optional<HostBuffer> produceHost(AlpakaESTestRecordA const& iRecord) {
0051       auto const& input = iRecord.get(token_);
0052 
0053       int const size = 10;
0054       // TODO: cached allocation?
0055       auto buffer = cms::alpakatools::make_host_buffer<int[], Platform>(size);
0056       for (int i = 0; i < size; ++i) {
0057         buffer[i] = i * input.value();
0058       }
0059       return HostBuffer(std::move(buffer));
0060     }
0061 
0062     std::optional<AlpakaESTestDataA> produceDevice(device::Record<AlpakaESTestRecordA> const& iRecord) {
0063       auto hostHandle = iRecord.getTransientHandle(hostToken_);
0064       // TODO: In principle associating the allocation to a queue is
0065       // incorrect. Framework will keep the memory alive until the IOV
0066       // ends. By that point all asynchronous activity using that
0067       // memory has finished, and the memory could be marked as "free"
0068       // in the allocator already by the host-side release of the
0069       // memory. There could also be other, independent asynchronous
0070       // activity going on that uses the same queue (since we don't
0071       // retain the queue here), and at the time of host-side release
0072       // the device-side release gets associated to the complemention
0073       // of that activity (which has nothing to do with the memory here).
0074       auto buffer =
0075           cms::alpakatools::make_device_buffer<int[]>(iRecord.queue(), alpaka::getExtentProduct(hostHandle->buffer()));
0076       alpaka::memcpy(iRecord.queue(), buffer, hostHandle->buffer());
0077       return AlpakaESTestDataA(std::move(buffer));
0078     }
0079 
0080   private:
0081     edm::ESGetToken<cms::alpakatest::ESTestDataA, AlpakaESTestRecordA> token_;
0082     edm::ESGetToken<HostBuffer, AlpakaESTestRecordA> hostToken_;
0083   };
0084 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0085 
0086 DEFINE_FWK_EVENTSETUP_ALPAKA_MODULE(TestAlpakaESProducerA);
0087 // for the temporary host data
0088 TYPELOOKUP_DATA_REG(ALPAKA_ACCELERATOR_NAMESPACE::TestAlpakaESProducerA::HostBuffer);