Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:00

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     auto pset_auto = edm::readConfig(baseConfig_auto);
0132     auto pset_test1 = edm::readConfig(baseConfig_test1);
0133     auto pset_test2 = edm::readConfig(baseConfig_test2);
0134     auto psetTest2Disabled_auto = edm::readConfig(baseConfigTest2Disabled_auto);
0135     auto psetTest2Disabled_test1 = edm::readConfig(baseConfigTest2Disabled_test1);
0136     auto psetTest2Disabled_test2 = edm::readConfig(baseConfigTest2Disabled_test2);
0137     pset_auto->registerIt();
0138     pset_test1->registerIt();
0139     pset_test2->registerIt();
0140     psetTest2Disabled_auto->registerIt();
0141     psetTest2Disabled_test1->registerIt();
0142     psetTest2Disabled_test2->registerIt();
0143     REQUIRE(pset_auto->id() == pset_test1->id());
0144     REQUIRE(pset_auto->id() == pset_test2->id());
0145     REQUIRE(pset_auto->id() == psetTest2Disabled_auto->id());
0146     REQUIRE(pset_auto->id() == psetTest2Disabled_test1->id());
0147     REQUIRE(pset_auto->id() == psetTest2Disabled_test2->id());
0148   }
0149 
0150   edm::test::TestProcessor::Config config_auto{baseConfig_auto};
0151   edm::test::TestProcessor::Config config_test1{baseConfig_test1};
0152   edm::test::TestProcessor::Config config_test2{baseConfig_test2};
0153   edm::test::TestProcessor::Config configTest2Disabled_auto{baseConfigTest2Disabled_auto};
0154   edm::test::TestProcessor::Config configTest2Disabled_test1{baseConfigTest2Disabled_test1};
0155   edm::test::TestProcessor::Config configTest2Disabled_test2{baseConfigTest2Disabled_test2};
0156 
0157   SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config_auto)); }
0158 
0159   SECTION("No event data") {
0160     edm::test::TestProcessor tester(config_auto);
0161     REQUIRE_NOTHROW(tester.test());
0162   }
0163 
0164   SECTION("beginJob and endJob only") {
0165     edm::test::TestProcessor tester(config_auto);
0166     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0167   }
0168 
0169   SECTION("Run with no LuminosityBlocks") {
0170     edm::test::TestProcessor tester(config_auto);
0171     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0172   }
0173 
0174   SECTION("LuminosityBlock with no Events") {
0175     edm::test::TestProcessor tester(config_auto);
0176     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0177   }
0178 
0179   SECTION("Test2 enabled, acclerators=*") {
0180     edm::test::TestProcessor tester(config_auto);
0181     auto event = tester.test();
0182     REQUIRE(event.get<edmtest::IntProduct>()->value == 2);
0183   }
0184 
0185   SECTION("Test2 enabled, acclerators=test1") {
0186     edm::test::TestProcessor tester(config_test1);
0187     auto event = tester.test();
0188     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0189   }
0190 
0191   SECTION("Test2 enabled, acclerators=test2") {
0192     edm::test::TestProcessor tester(config_test2);
0193     auto event = tester.test();
0194     REQUIRE(event.get<edmtest::IntProduct>()->value == 2);
0195   }
0196 
0197   SECTION("Test2 disabled, accelerators=*") {
0198     edm::test::TestProcessor tester(configTest2Disabled_auto);
0199     auto event = tester.test();
0200     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0201   }
0202 
0203   SECTION("Test2 disabled, accelerators=test1") {
0204     edm::test::TestProcessor tester(configTest2Disabled_test1);
0205     auto event = tester.test();
0206     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0207   }
0208 
0209   SECTION("Test2 disabled, accelerators=test2") {
0210     REQUIRE_THROWS_WITH(
0211         edm::test::TestProcessor(configTest2Disabled_test2),
0212         Catch::Contains("The system has no compute accelerators that match the patterns") && Catch::Contains("test1"));
0213   }
0214 }
0215 
0216 TEST_CASE("Configuration with ModuleTypeResolver", s_tag) {
0217   const std::string baseConfig_auto = makeResolverConfig(true, "*", "");
0218   const std::string baseConfig_cpu = makeResolverConfig(true, "cpu", "");
0219   const std::string baseConfig_other = makeResolverConfig(true, "other", "");
0220   const std::string baseConfig_cpuExplicit = makeResolverConfig(true, "*", "cpu");
0221   const std::string baseConfig_otherExplicit = makeResolverConfig(true, "*", "other");
0222   const std::string baseConfigOtherDisabled_auto = makeResolverConfig(false, "*", "");
0223   const std::string baseConfigOtherDisabled_cpu = makeResolverConfig(false, "cpu", "");
0224   const std::string baseConfigOtherDisabled_other = makeResolverConfig(false, "other", "");
0225 
0226   SECTION("Configuration hash is not changed") {
0227     auto pset_auto = edm::readConfig(baseConfig_auto);
0228     auto pset_cpu = edm::readConfig(baseConfig_cpu);
0229     auto pset_other = edm::readConfig(baseConfig_other);
0230     auto pset_cpuExplicit = edm::readConfig(baseConfig_cpuExplicit);
0231     auto pset_otherExplicit = edm::readConfig(baseConfig_otherExplicit);
0232     auto psetOtherDisabled_auto = edm::readConfig(baseConfigOtherDisabled_auto);
0233     auto psetOtherDisabled_cpu = edm::readConfig(baseConfigOtherDisabled_cpu);
0234     REQUIRE_THROWS_WITH(edm::readConfig(baseConfigOtherDisabled_other), Catch::Contains("UnavailableAccelerator"));
0235     pset_auto->registerIt();
0236     pset_cpu->registerIt();
0237     pset_other->registerIt();
0238     pset_cpuExplicit->registerIt();
0239     pset_otherExplicit->registerIt();
0240     psetOtherDisabled_auto->registerIt();
0241     psetOtherDisabled_cpu->registerIt();
0242     REQUIRE(pset_auto->id() == pset_cpu->id());
0243     REQUIRE(pset_auto->id() == pset_other->id());
0244     REQUIRE(pset_auto->id() == pset_cpuExplicit->id());
0245     REQUIRE(pset_auto->id() == pset_otherExplicit->id());
0246     REQUIRE(pset_auto->id() == psetOtherDisabled_auto->id());
0247     REQUIRE(pset_auto->id() == psetOtherDisabled_cpu->id());
0248   }
0249 
0250   edm::test::TestProcessor::Config config_auto{baseConfig_auto};
0251   edm::test::TestProcessor::Config config_cpu{baseConfig_cpu};
0252   edm::test::TestProcessor::Config config_other{baseConfig_other};
0253   edm::test::TestProcessor::Config config_cpuExplicit{baseConfig_cpuExplicit};
0254   edm::test::TestProcessor::Config config_otherExplicit{baseConfig_otherExplicit};
0255   edm::test::TestProcessor::Config configOtherDisabled_auto{baseConfigOtherDisabled_auto};
0256   edm::test::TestProcessor::Config configOtherDisabled_cpu{baseConfigOtherDisabled_cpu};
0257 
0258   SECTION("Base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config_auto)); }
0259 
0260   SECTION("No event data") {
0261     edm::test::TestProcessor tester(config_auto);
0262     REQUIRE_NOTHROW(tester.test());
0263   }
0264 
0265   SECTION("beginJob and endJob only") {
0266     edm::test::TestProcessor tester(config_auto);
0267     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0268   }
0269 
0270   SECTION("Run with no LuminosityBlocks") {
0271     edm::test::TestProcessor tester(config_auto);
0272     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0273   }
0274 
0275   SECTION("LuminosityBlock with no Events") {
0276     edm::test::TestProcessor tester(config_auto);
0277     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0278   }
0279 
0280   SECTION("Other enabled, acclerators=*") {
0281     edm::test::TestProcessor tester(config_auto);
0282     auto event = tester.test();
0283     REQUIRE(event.get<edmtest::IntProduct>()->value == 2);
0284   }
0285 
0286   SECTION("Other enabled, acclerators=cpu") {
0287     edm::test::TestProcessor tester(config_cpu);
0288     auto event = tester.test();
0289     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0290   }
0291 
0292   SECTION("Other enabled, acclerators=other") {
0293     edm::test::TestProcessor tester(config_other);
0294     auto event = tester.test();
0295     REQUIRE(event.get<edmtest::IntProduct>()->value == 2);
0296   }
0297 
0298   SECTION("Other disabled, accelerators=*") {
0299     edm::test::TestProcessor tester(configOtherDisabled_auto);
0300     auto event = tester.test();
0301     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0302   }
0303 
0304   SECTION("Other disabled, accelerators=cpu") {
0305     edm::test::TestProcessor tester(configOtherDisabled_cpu);
0306     auto event = tester.test();
0307     REQUIRE(event.get<edmtest::IntProduct>()->value == 1);
0308   }
0309 }