Skip to content

Commit

Permalink
Require the AlpakaService to be enabled before running on a Device
Browse files Browse the repository at this point in the history
Move the check that the corresponding AlpakaService is enabled from the
beginStream() method of each EDProducer to thealpakatools::chooseDevice()
central function.

This replicates the behaviour currently used by the CUDA "framework".
  • Loading branch information
fwyzard committed Sep 25, 2022
1 parent 825e451 commit 3b56704
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
13 changes: 13 additions & 0 deletions HeterogeneousCore/AlpakaCore/interface/chooseDevice.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
#ifndef HeterogeneousCore_AlpakaCore_interface_chooseDevice_h
#define HeterogeneousCore_AlpakaCore_interface_chooseDevice_h

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/devices.h"
#include "HeterogeneousCore/AlpakaInterface/interface/traits.h"
#include "HeterogeneousCore/AlpakaServices/interface/alpaka/AlpakaService.h"

namespace cms::alpakatools {

template <typename TPlatform, typename = std::enable_if_t<is_platform_v<TPlatform>>>
alpaka::Dev<TPlatform> const& chooseDevice(edm::StreamID id) {
edm::Service<ALPAKA_TYPE_ALIAS(AlpakaService)> service;
if (not service->enabled()) {
cms::Exception ex("RuntimeError");
ex << "Unable to choose current device because " << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " is disabled.\n"
<< "If " << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " was not explicitly disabled in the configuration,\n"
<< "the probable cause is that there is no accelerator or there is some problem\n"
<< "with the accelerator runtime or drivers.";
ex.addContext("Calling cms::alpakatools::chooseDevice()");
throw ex;
}

// For startes we "statically" assign the device based on
// edm::Stream number. This is suboptimal if the number of
// edm::Streams is not a multiple of the number of devices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "HeterogeneousCore/AlpakaCore/interface/ScopedContext.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaServices/interface/alpaka/AlpakaService.h"

#include "TestAlgo.h"

Expand All @@ -24,13 +22,6 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
TestAlpakaProducer(edm::ParameterSet const& config)
: deviceToken_{produces()}, size_{config.getParameter<int32_t>("size")} {}

void beginStream(edm::StreamID) override {
edm::Service<ALPAKA_TYPE_ALIAS(AlpakaService)> service;
if (not service->enabled()) {
throw cms::Exception("Configuration") << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " is disabled.";
}
}

void produce(edm::Event& event, edm::EventSetup const&) override {
// create a context based on the EDM stream number
cms::alpakatools::ScopedContextProduce<Queue> ctx(event.streamID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "HeterogeneousCore/AlpakaCore/interface/ScopedContext.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaServices/interface/alpaka/AlpakaService.h"

namespace ALPAKA_ACCELERATOR_NAMESPACE {

Expand All @@ -25,13 +23,6 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
TestAlpakaTranscriber(edm::ParameterSet const& config)
: deviceToken_{consumes(config.getParameter<edm::InputTag>("source"))}, hostToken_{produces()} {}

void beginStream(edm::StreamID) override {
edm::Service<ALPAKA_TYPE_ALIAS(AlpakaService)> service;
if (not service->enabled()) {
throw cms::Exception("Configuration") << ALPAKA_TYPE_ALIAS_NAME(AlpakaService) << " is disabled.";
}
}

void acquire(edm::Event const& event, edm::EventSetup const& setup, edm::WaitingTaskWithArenaHolder task) override {
// create a context reusing the same device and queue as the producer of the input collection
auto const& input = event.get(deviceToken_);
Expand Down

0 comments on commit 3b56704

Please sign in to comment.