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