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 "HeterogeneousCore/AlpakaCore/interface/alpaka/ESProducer.h"
0005 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/ModuleFactory.h"
0006 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0007 #include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
0008 #include "HeterogeneousCore/AlpakaTest/interface/AlpakaESTestRecords.h"
0009 #include "HeterogeneousCore/AlpakaTest/interface/ESTestData.h"
0010 #include "HeterogeneousCore/AlpakaTest/interface/AlpakaESTestData.h"
0011 
0012 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0013   /**
0014    * This class demonstrates and ESProducer on the data model 2 that
0015    * - consumes a standard host ESProduct and converts the data into an Alpaka buffer
0016    * - transfers the buffer contents to the device of the backend
0017    */
0018   class TestAlpakaESProducerB : public ESProducer {
0019   public:
0020     TestAlpakaESProducerB(edm::ParameterSet const& iConfig) {
0021       {
0022         auto cc = setWhatProduced(this, &TestAlpakaESProducerB::produceHost);
0023         token_ = cc.consumes();
0024       }
0025       {
0026         auto cc = setWhatProduced(this, &TestAlpakaESProducerB::produceDevice);
0027         hostToken_ = cc.consumes();
0028       }
0029     }
0030 
0031     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0032       edm::ParameterSetDescription desc;
0033       descriptions.addWithDefaultLabel(desc);
0034     }
0035 
0036     std::optional<cms::alpakatest::AlpakaESTestDataB<DevHost>> produceHost(AlpakaESTestRecordB const& iRecord) {
0037       auto const& input = iRecord.get(token_);
0038 
0039       int const size = 5;
0040       // TODO: cached allocation?
0041       auto buffer = cms::alpakatools::make_host_buffer<int[], Platform>(size);
0042       for (int i = 0; i < size; ++i) {
0043         buffer[i] = i + input.value();
0044       }
0045       return cms::alpakatest::AlpakaESTestDataB<DevHost>(std::move(buffer));
0046     }
0047 
0048     // TODO: in principle in this model the transfer to device could be automated
0049     std::optional<cms::alpakatest::AlpakaESTestDataB<Device>> produceDevice(
0050         device::Record<AlpakaESTestRecordB> const& iRecord) {
0051       auto hostHandle = iRecord.getTransientHandle(hostToken_);
0052       auto buffer = cms::alpakatools::make_device_buffer<int[]>(iRecord.queue(), hostHandle->size());
0053       alpaka::memcpy(iRecord.queue(), buffer, hostHandle->buffer());
0054       return cms::alpakatest::AlpakaESTestDataB<Device>(std::move(buffer));
0055     }
0056 
0057   private:
0058     edm::ESGetToken<cms::alpakatest::ESTestDataB, AlpakaESTestRecordB> token_;
0059     edm::ESGetToken<cms::alpakatest::AlpakaESTestDataB<DevHost>, AlpakaESTestRecordB> hostToken_;
0060   };
0061 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0062 
0063 DEFINE_FWK_EVENTSETUP_ALPAKA_MODULE(TestAlpakaESProducerB);