Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AlpakaBackendProducer and AlpakaBackendFilter #44387

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem lies here, duplicate plugin name wrt

<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>
46 changes: 46 additions & 0 deletions HeterogeneousCore/AlpakaCore/test/testAlpakaBackendFilter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
import sys

# choose a different alpaka backend depending on the SCRAM test being run
try:
backend = os.environ['SCRAM_ALPAKA_BACKEND']
except:
backend = 'SerialSync'

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

print(f"Testing the alpaka backend {backend} using the process accelerator {accelerators[backend]}")

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.mustRun = cms.EDProducer("edmtest::MustRunIntProducer", ivalue=cms.int32(1))
process.mustNotRun = cms.EDProducer("FailingProducer")

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

process.options.wantSummary = True