Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:42

0001 #include "DataFormats/PortableTestObjects/interface/TestHostCollection.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/Framework/interface/Frameworkfwd.h"
0004 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0009 #include "FWCore/Utilities/interface/EDGetToken.h"
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 
0013 #include <fmt/format.h>
0014 
0015 class TestAlpakaHostDeviceCompare : public edm::global::EDAnalyzer<> {
0016 public:
0017   TestAlpakaHostDeviceCompare(edm::ParameterSet const& iConfig)
0018       : hostToken_{consumes(iConfig.getUntrackedParameter<edm::InputTag>("srcHost"))},
0019         deviceToken_{consumes(iConfig.getUntrackedParameter<edm::InputTag>("srcDevice"))},
0020         expectedXdiff_{iConfig.getUntrackedParameter<double>("expectedXdiff")} {}
0021 
0022   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0023     edm::ParameterSetDescription desc;
0024     desc.addUntracked<edm::InputTag>("srcHost");
0025     desc.addUntracked<edm::InputTag>("srcDevice");
0026     desc.addUntracked<double>("expectedXdiff", 0.);
0027     descriptions.addWithDefaultLabel(desc);
0028   }
0029 
0030   template <typename T>
0031   static void require(T const& host, T const& device, T const& expectedDiff, std::string_view name) {
0032     T const diff = host - device;
0033     if (diff != expectedDiff) {
0034       throw cms::Exception("Assert") << "Comparison of " << name << " failed, expected difference " << expectedDiff
0035                                      << " but got " << diff << ", host value " << host << " device value " << device;
0036     }
0037   }
0038 
0039   void analyze(edm::StreamID iStream, edm::Event const& iEvent, edm::EventSetup const& iSetup) const override {
0040     auto const& hostData = iEvent.get(hostToken_);
0041     auto const& deviceData = iEvent.get(deviceToken_);
0042 
0043     require(hostData->metadata().size(), deviceData->metadata().size(), 0, "metadata().size()");
0044     auto const hostView = hostData.view();
0045     auto const deviceView = deviceData.view();
0046     for (int i = 0; i < hostData->metadata().size(); ++i) {
0047       require(hostView[i].x(), deviceView[i].x(), expectedXdiff_, fmt::format("view[{}].x()", i));
0048     }
0049   }
0050 
0051 private:
0052   edm::EDGetTokenT<portabletest::TestHostCollection> const hostToken_;
0053   edm::EDGetTokenT<portabletest::TestHostCollection> const deviceToken_;
0054   double const expectedXdiff_;
0055 };
0056 
0057 #include "FWCore/Framework/interface/MakerMacros.h"
0058 DEFINE_FWK_MODULE(TestAlpakaHostDeviceCompare);