Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:39

0001 #define CATCH_CONFIG_MAIN
0002 #include "catch.hpp"
0003 
0004 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/ParameterSetReader/interface/ParameterSetReader.h"
0007 #include "FWCore/TestProcessor/interface/TestProcessor.h"
0008 
0009 #include <fmt/format.h>
0010 
0011 #include <iostream>
0012 #include <string_view>
0013 
0014 static constexpr auto s_tag = "[ProcessAccelerator]";
0015 
0016 namespace {
0017   std::string makeSwitchConfig(bool test2Enabled,
0018                                std::string_view test1,
0019                                std::string_view test2,
0020                                std::string_view accelerator) {
0021     const std::string appendTest2 = test2Enabled ? "self._enabled.append('test2')" : "";
0022     return fmt::format(
0023         R"_(from FWCore.TestProcessor.TestProcess import *
0024 import FWCore.ParameterSet.Config as cms
0025 
0026 class ProcessAcceleratorTest(cms.ProcessAccelerator):
0027     def __init__(self):
0028         super(ProcessAcceleratorTest,self).__init__()
0029         self._labels = ["test1", "test2"]
0030         self._enabled = ["test1"]
0031         {}
0032     def labels(self):
0033         return self._labels
0034     def enabledLabels(self):
0035         return self._enabled
0036 
0037 class SwitchProducerTest(cms.SwitchProducer):
0038     def __init__(self, **kargs):
0039         super(SwitchProducerTest,self).__init__(
0040             dict(
0041                 cpu = cms.SwitchProducer.getCpu(),
0042                 test1 = lambda accelerators: ("test1" in accelerators, 2),
0043                 test2 = lambda accelerators: ("test2" in accelerators, 3),
0044             ), **kargs)
0045 
0046 process = TestProcess()
0047 process.options.accelerators = ["{}"]
0048 process.ProcessAcceleratorTest = ProcessAcceleratorTest()
0049 process.s = SwitchProducerTest(
0050    cpu = cms.EDProducer('IntProducer', ivalue = cms.int32(0)),
0051    test1 = {},
0052    test2 = {}
0053 )
0054 process.moduleToTest(process.s)
0055 )_",
0056         appendTest2,
0057         accelerator,
0058         test1,
0059         test2);
0060   }
0061 
0062   std::string makeResolverConfig(bool enableOther, std::string_view accelerator, std::string_view variant) {
0063     const std::string appendOther = enableOther ? "self._enabled.append('other')" : "";
0064     const std::string explicitVariant =
0065         variant.empty() ? std::string(variant) : fmt::format(", variant=cms.untracked.string('{}')", variant);
0066     return fmt::format(
0067         R"_(from FWCore.TestProcessor.TestProcess import *
0068 import FWCore.ParameterSet.Config as cms
0069 
0070 class ModuleTypeResolverTest:
0071     def __init__(self, accelerators):
0072         self._variants = []
0073         if "other" in accelerators:
0074             self._variants.append("other")
0075         if "cpu" in accelerators:
0076             self._variants.append("cpu")
0077         if len(self._variants) == 0:
0078             raise cms.EDMException(cms.edm.errors.UnavailableAccelerator, "No 'cpu' or 'other' accelerator available")
0079 
0080     def plugin(self):
0081         return "edm::test::ConfigurableTestTypeResolverMaker"
0082 
0083     def setModuleVariant(self, module):
0084         if "generic::" in module.type_():
0085             if hasattr(module, "variant"):
0086                 if module.variant.value() not in self._variants:
0087                     raise cms.EDMException(cms.edm.errors.UnavailableAccelerator, "Module {{}} has the Test variant set explicitly to {{}}, but its accelerator is not available for the job".format(module.label_(), module.variant.value()))
0088             else:
0089                 module.variant = cms.untracked.string(self._variants[0])
0090 
0091 class ProcessAcceleratorTest(cms.ProcessAccelerator):
0092     def __init__(self):
0093         super(ProcessAcceleratorTest,self).__init__()
0094         self._labels = ["other"]
0095         self._enabled = []
0096         {}
0097     def labels(self):
0098         return self._labels
0099     def enabledLabels(self):
0100         return self._enabled
0101     def moduleTypeResolver(self, accelerators):
0102         return ModuleTypeResolverTest(accelerators)
0103 
0104 process = TestProcess()
0105 process.options.accelerators = ["{}"]
0106 process.ProcessAcceleratorTest = ProcessAcceleratorTest()
0107 process.m = cms.EDProducer("generic::IntProducer",
0108     valueCpu = cms.int32(1), valueOther = cms.int32(2)
0109     {}
0110 )
0111 process.moduleToTest(process.m)
0112 )_",
0113         appendOther,
0114         accelerator,
0115         explicitVariant);
0116   }
0117 }  // namespace
0118 
0119 TEST_CASE("Configuration with SwitchProducer", s_tag) {
0120   const std::string test1{"cms.EDProducer('IntProducer', ivalue = cms.int32(1))"};
0121   const std::string test2{"cms.EDProducer('ManyIntProducer', ivalue = cms.int32(2), values = cms.VPSet())"};
0122 
0123   const std::string baseConfig_auto = makeSwitchConfig(true, test1, test2, "*");
0124   const std::string baseConfig_test1 = makeSwitchConfig(true, test1, test2, "test1");
0125   const std::string baseConfig_test2 = makeSwitchConfig(true, test1, test2, "test2");
0126   const std::string baseConfigTest2Disabled_auto = makeSwitchConfig(false, test1, test2, "*");
0127   const std::string baseConfigTest2Disabled_test1 = makeSwitchConfig(false, test1, test2, "test1");
0128   const std::string baseConfigTest2Disabled_test2 = makeSwitchConfig(false, test1, test2, "test2");
0129 
0130   SECTION("Configuration hash is not changed") {
0131     std::unique_ptr<edm::ParameterSet> pset_auto, pset_test1, pset_test2;
0132     std::unique_ptr<edm::ParameterSet> psetTest2Disabled_auto, psetTest2Disabled_test1, psetTest2Disabled_test2;
0133     edm::makeParameterSets(baseConfig_auto, pset_auto);
0134     edm::makeParameterSets(baseConfig_test1, pset_test1);
0135     edm::makeParameterSets(baseConfig_test2, pset_test2);
0136     edm::makeParameterSets(baseConfigTest2Disabled_auto, psetTest2Disabled_auto);
0137     edm::makeParameterSets(baseConfigTest2Disabled_test1, psetTest2Disabled_test1);
0138     edm::makeParameterSets(baseConfigTest2Disabled_test2, psetTest2Disabled_test2);
0139     pset_auto->registerIt();
0140     pset_test1->registerIt();
0141     pset_test2->registerIt();
0142     psetTest2Disabled_auto->registerIt();
0143     psetTest2Disabled_test1->registerIt();
0144     psetTest2Disabled_test2->registerIt();
0145     REQUIRE(pset_auto->id() == pset_test1->id());
0146     REQUIRE(pset_auto->id() == pset_test2->id());
0147     REQUIRE(pset_auto->id() == psetTest2Disabled_auto->id());
0148     REQUIRE(pset_auto->id() == psetTest2Disabled_test1->id());
0149     REQUIRE(pset_auto->id() == psetTest2Disabled_test2->id());
0150   }
0151 
0152   edm::test::TestProcessor::Config config_auto{baseConfig_auto};
0153   edm::test::TestProcessor::Config config_test1{baseConfig_test1};
0154   edm::test::TestProcessor::Config config_test2{baseConfig_test2};
0155   edm::test::TestProcessor::Config configTest2Disabled_auto{baseConfigTest2Disabled_auto};
0156   edm::test::TestProcessor::Config configTest2Disabled_test1{baseConfigTest2Disabled_test1};
0157   edm::test::TestProcessor::Config configTest2Disabled_test2{baseConfigTest2Disabled_test2};
0158 
0159   SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config_auto)); }
0160 
0161   SECTION("No event data") {
0162     edm::test::TestProcessor tester(config_auto);
0163     REQUIRE_NOTHROW(tester.test());
0164   }
0165 
0166   SECTION("beginJob and endJob only") {
0167     edm::test::TestProcessor tester(config_auto);
0168     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0169   }
0170 
0171   SECTION("Run with no LuminosityBlocks") {
0172     edm::test::TestProcessor tester(config_auto);
0173     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0174   }
0175 
0176   SECTION("LuminosityBlock with no Events") {
0177     edm::test::TestProcessor tester(config_auto);
0178     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0179   }
0180 
0181   SECTION("Test2 enabled, acclerators=*") {
0182     edm::test::TestProcessor tester(config_auto);
0183     auto event = tester.test();
0184     REQUIRE(event.get<edmtest::IntProduct>()->value == 2);
0185   }
0186 
0187   SECTION("Test2 enabled, acclerators=test1") {
0188     edm::test::TestProcessor tester(config_test1);
0189     auto event = tester.test();
0190     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0191   }
0192 
0193   SECTION("Test2 enabled, acclerators=test2") {
0194     edm::test::TestProcessor tester(config_test2);
0195     auto event = tester.test();
0196     REQUIRE(event.get<edmtest::IntProduct>()->value == 2);
0197   }
0198 
0199   SECTION("Test2 disabled, accelerators=*") {
0200     edm::test::TestProcessor tester(configTest2Disabled_auto);
0201     auto event = tester.test();
0202     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0203   }
0204 
0205   SECTION("Test2 disabled, accelerators=test1") {
0206     edm::test::TestProcessor tester(configTest2Disabled_test1);
0207     auto event = tester.test();
0208     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0209   }
0210 
0211   SECTION("Test2 disabled, accelerators=test2") {
0212     REQUIRE_THROWS_WITH(
0213         edm::test::TestProcessor(configTest2Disabled_test2),
0214         Catch::Contains("The system has no compute accelerators that match the patterns") && Catch::Contains("test1"));
0215   }
0216 }
0217 
0218 TEST_CASE("Configuration with ModuleTypeResolver", s_tag) {
0219   const std::string baseConfig_auto = makeResolverConfig(true, "*", "");
0220   const std::string baseConfig_cpu = makeResolverConfig(true, "cpu", "");
0221   const std::string baseConfig_other = makeResolverConfig(true, "other", "");
0222   const std::string baseConfig_cpuExplicit = makeResolverConfig(true, "*", "cpu");
0223   const std::string baseConfig_otherExplicit = makeResolverConfig(true, "*", "other");
0224   const std::string baseConfigOtherDisabled_auto = makeResolverConfig(false, "*", "");
0225   const std::string baseConfigOtherDisabled_cpu = makeResolverConfig(false, "cpu", "");
0226   const std::string baseConfigOtherDisabled_other = makeResolverConfig(false, "other", "");
0227 
0228   SECTION("Configuration hash is not changed") {
0229     std::unique_ptr<edm::ParameterSet> pset_auto, pset_cpu, pset_other, pset_cpuExplicit;
0230     std::unique_ptr<edm::ParameterSet> pset_otherExplicit, psetOtherDisabled_auto, psetOtherDisabled_cpu,
0231         psetOtherDisabled_other;
0232     edm::makeParameterSets(baseConfig_auto, pset_auto);
0233     edm::makeParameterSets(baseConfig_cpu, pset_cpu);
0234     edm::makeParameterSets(baseConfig_other, pset_other);
0235     edm::makeParameterSets(baseConfig_cpuExplicit, pset_cpuExplicit);
0236     edm::makeParameterSets(baseConfig_otherExplicit, pset_otherExplicit);
0237     edm::makeParameterSets(baseConfigOtherDisabled_auto, psetOtherDisabled_auto);
0238     edm::makeParameterSets(baseConfigOtherDisabled_cpu, psetOtherDisabled_cpu);
0239     REQUIRE_THROWS_WITH(edm::makeParameterSets(baseConfigOtherDisabled_other, psetOtherDisabled_other),
0240                         Catch::Contains("UnavailableAccelerator"));
0241     pset_auto->registerIt();
0242     pset_cpu->registerIt();
0243     pset_other->registerIt();
0244     pset_cpuExplicit->registerIt();
0245     pset_otherExplicit->registerIt();
0246     psetOtherDisabled_auto->registerIt();
0247     psetOtherDisabled_cpu->registerIt();
0248     REQUIRE(pset_auto->id() == pset_cpu->id());
0249     REQUIRE(pset_auto->id() == pset_other->id());
0250     REQUIRE(pset_auto->id() == pset_cpuExplicit->id());
0251     REQUIRE(pset_auto->id() == pset_otherExplicit->id());
0252     REQUIRE(pset_auto->id() == psetOtherDisabled_auto->id());
0253     REQUIRE(pset_auto->id() == psetOtherDisabled_cpu->id());
0254   }
0255 
0256   edm::test::TestProcessor::Config config_auto{baseConfig_auto};
0257   edm::test::TestProcessor::Config config_cpu{baseConfig_cpu};
0258   edm::test::TestProcessor::Config config_other{baseConfig_other};
0259   edm::test::TestProcessor::Config config_cpuExplicit{baseConfig_cpuExplicit};
0260   edm::test::TestProcessor::Config config_otherExplicit{baseConfig_otherExplicit};
0261   edm::test::TestProcessor::Config configOtherDisabled_auto{baseConfigOtherDisabled_auto};
0262   edm::test::TestProcessor::Config configOtherDisabled_cpu{baseConfigOtherDisabled_cpu};
0263 
0264   SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config_auto)); }
0265 
0266   SECTION("No event data") {
0267     edm::test::TestProcessor tester(config_auto);
0268     REQUIRE_NOTHROW(tester.test());
0269   }
0270 
0271   SECTION("beginJob and endJob only") {
0272     edm::test::TestProcessor tester(config_auto);
0273     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0274   }
0275 
0276   SECTION("Run with no LuminosityBlocks") {
0277     edm::test::TestProcessor tester(config_auto);
0278     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0279   }
0280 
0281   SECTION("LuminosityBlock with no Events") {
0282     edm::test::TestProcessor tester(config_auto);
0283     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0284   }
0285 
0286   SECTION("Other enabled, acclerators=*") {
0287     edm::test::TestProcessor tester(config_auto);
0288     auto event = tester.test();
0289     REQUIRE(event.get<edmtest::IntProduct>()->value == 2);
0290   }
0291 
0292   SECTION("Other enabled, acclerators=cpu") {
0293     edm::test::TestProcessor tester(config_cpu);
0294     auto event = tester.test();
0295     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0296   }
0297 
0298   SECTION("Other enabled, acclerators=other") {
0299     edm::test::TestProcessor tester(config_other);
0300     auto event = tester.test();
0301     REQUIRE(event.get<edmtest::IntProduct>()->value == 2);
0302   }
0303 
0304   SECTION("Other disabled, accelerators=*") {
0305     edm::test::TestProcessor tester(configOtherDisabled_auto);
0306     auto event = tester.test();
0307     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0308   }
0309 
0310   SECTION("Other disabled, accelerators=cpu") {
0311     edm::test::TestProcessor tester(configOtherDisabled_cpu);
0312     auto event = tester.test();
0313     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0314   }
0315 }