Skip to content

Commit

Permalink
Implement AlpakaBackendProducer and AlpakaBackendFilter
Browse files Browse the repository at this point in the history
Implement AlpakaBackendProducer, an empty alpaka-based EDProducer whose
only purpose is to save in the event what alpaka backend has been used.

Implement AlpakaBackendFilter, an EDFilter that selects events based on
the alpaka backend used to run a previous producer.

Implement a unit test for both modules.
  • Loading branch information
fwyzard committed Mar 13, 2024
1 parent b9bd7bb commit 8d6395c
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
48 changes: 48 additions & 0 deletions HeterogeneousCore/AlpakaCore/plugins/AlpakaBackendFilter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <array>
#include <string>
#include <vector>

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/global/EDFilter.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "HeterogeneousCore/AlpakaInterface/interface/Backend.h"

class AlpakaBackendFilter : public edm::global::EDFilter<> {
public:
explicit AlpakaBackendFilter(edm::ParameterSet const& config)
: producer_(consumes<unsigned short>(config.getParameter<edm::InputTag>("producer"))), backends_{} {
for (auto const& backend : config.getParameter<std::vector<std::string>>("backends")) {
backends_[static_cast<unsigned short>(cms::alpakatools::toBackend(backend))] = true;
}
}

bool filter(edm::StreamID sid, edm::Event& event, edm::EventSetup const& setup) const final {
return backends_[event.get(producer_)];
}

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
const edm::EDGetTokenT<unsigned short> producer_;
std::array<bool, static_cast<short>(cms::alpakatools::Backend::size)> backends_;
};

void AlpakaBackendFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<edm::InputTag>("producer", edm::InputTag("alpakaBackendProducer", "backend"))
->setComment(
"Use the 'backend' instance label to read the backend indicator that is implicitly produced by every alpaka "
"EDProducer.");
desc.add<std::vector<std::string>>("backends", {"SerialSync"})
->setComment("Valid backends are 'SerialSync', 'CudaAsync', 'ROCmAsync', and 'TbbAsync'.");
descriptions.addWithDefaultLabel(desc);
descriptions.setComment(
"This EDFilter accepts events if the alpaka EDProducer 'producer' was run on a backend among those listed by the "
"'backends' parameter.");
}

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(AlpakaBackendFilter);
12 changes: 12 additions & 0 deletions HeterogeneousCore/AlpakaCore/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,17 @@
<use name="FWCore/MessageLogger"/>
<use name="FWCore/ParameterSet"/>
<use name="FWCore/Utilities"/>
<use name="HeterogeneousCore/AlpakaInterface"/>
<flags EDM_PLUGIN="1"/>
</library>

<library file="alpaka/*.cc" name="HeterogeneousCoreAlpakaTestPluginsPortable">
<use name="alpaka"/>
<use name="FWCore/Framework"/>
<use name="FWCore/ParameterSet"/>
<use name="FWCore/Utilities"/>
<use name="HeterogeneousCore/AlpakaCore"/>
<use name="HeterogeneousCore/AlpakaInterface"/>
<flags ALPAKA_BACKENDS="1"/>
<flags EDM_PLUGIN="1"/>
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/Event.h"
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/EventSetup.h"
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/global/EDProducer.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"

namespace ALPAKA_ACCELERATOR_NAMESPACE {

class AlpakaBackendProducer : public global::EDProducer<> {
public:
AlpakaBackendProducer(edm::ParameterSet const& config){};

void produce(edm::StreamID sid, device::Event& event, device::EventSetup const&) const override {}

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
descriptions.addWithDefaultLabel(desc);
descriptions.setComment(
"The alpaka EDProducer does not have any explicit products. "
"Its only purpose is to produce a 'backend' value.");
}
};

} // namespace ALPAKA_ACCELERATOR_NAMESPACE

#include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
DEFINE_FWK_ALPAKA_MODULE(AlpakaBackendProducer);
3 changes: 3 additions & 0 deletions HeterogeneousCore/AlpakaCore/test/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<test name="testAlpakaBackendFilter" command="cmsRun ${LOCALTOP}/src/HeterogeneousCore/AlpakaCore/test/testAlpakaBackendFilter.py">
<flags ALPAKA_BACKENDS="1"/>
</test>
41 changes: 41 additions & 0 deletions HeterogeneousCore/AlpakaCore/test/testAlpakaBackendFilter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import sys

# choose a different alpaka backend depending on the SCRAM test being run
try:
backend = os.environ['SCRAM_TEST_NAME'].replace('testAlpakaBackendFilter', '')
except:
backend = 'SerialSync'

# map the alpaka backends to the process accelerators
accelerators = {
'SerialSync': 'cpu',
'CudaAsync': 'gpu-nvidia',
'ROCmAsync': 'gpu-amd'
}

import FWCore.ParameterSet.Config as cms
from HeterogeneousCore.AlpakaCore.functions import *

process = cms.Process('Test')

process.options.accelerators = [ accelerators[backend] ]

process.maxEvents.input = 10

process.source = cms.Source('EmptySource')

process.load('Configuration.StandardSequences.Accelerators_cff')
process.load('HeterogeneousCore.AlpakaCore.ProcessAcceleratorAlpaka_cfi')

process.alpakaBackendProducer = cms.EDProducer('AlpakaBackendProducer@alpaka')

process.alpakaBackendFilter = cms.EDFilter('AlpakaBackendFilter',
producer = cms.InputTag('alpakaBackendProducer', 'backend'),
backends = cms.vstring(backend)
)

process.SelectedBackend = cms.Path(process.alpakaBackendProducer + process.alpakaBackendFilter)
process.AnyOtherBackend = cms.Path(process.alpakaBackendProducer + ~process.alpakaBackendFilter)

process.options.wantSummary = True

0 comments on commit 8d6395c

Please sign in to comment.