Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-24 02:12:49

0001 #include "catch.hpp"
0002 #include "FWCore/TestProcessor/interface/TestProcessor.h"
0003 #include "FWCore/Utilities/interface/Exception.h"
0004 
0005 #include "CUDADataFormats/Common/interface/Product.h"
0006 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0007 #include "HeterogeneousCore/CUDATest/interface/Thing.h"
0008 #include "HeterogeneousCore/CUDAUtilities/interface/requireDevices.h"
0009 #include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
0010 
0011 #include <iostream>
0012 
0013 static constexpr auto s_tag = "[TestCUDAProducerGPUFirst]";
0014 
0015 TEST_CASE("Standard checks of TestCUDAProducerGPUFirst", s_tag) {
0016   const std::string baseConfig{
0017       R"_(from FWCore.TestProcessor.TestProcess import *
0018 process = TestProcess()
0019 process.load("HeterogeneousCore.CUDACore.ProcessAcceleratorCUDA_cfi")
0020 process.toTest = cms.EDProducer("TestCUDAProducerGPUFirst")
0021 process.moduleToTest(process.toTest)
0022 )_"};
0023 
0024   edm::test::TestProcessor::Config config{baseConfig};
0025   SECTION("base configuration is OK") { REQUIRE_NOTHROW(edm::test::TestProcessor(config)); }
0026 
0027   SECTION("No event data") {
0028     // Calls produce(), so don't call without a GPU
0029     if (not cms::cudatest::testDevices()) {
0030       return;
0031     }
0032     edm::test::TestProcessor tester(config);
0033 
0034     REQUIRE_NOTHROW(tester.test());
0035   }
0036 
0037   SECTION("beginJob and endJob only") {
0038     edm::test::TestProcessor tester(config);
0039 
0040     REQUIRE_NOTHROW(tester.testBeginAndEndJobOnly());
0041   }
0042 
0043   SECTION("Run with no LuminosityBlocks") {
0044     edm::test::TestProcessor tester(config);
0045 
0046     REQUIRE_NOTHROW(tester.testRunWithNoLuminosityBlocks());
0047   }
0048 
0049   SECTION("LuminosityBlock with no Events") {
0050     edm::test::TestProcessor tester(config);
0051 
0052     REQUIRE_NOTHROW(tester.testLuminosityBlockWithNoEvents());
0053   }
0054 }
0055 
0056 TEST_CASE("TestCUDAProducerGPUFirst operation", s_tag) {
0057   const std::string baseConfig{
0058       R"_(from FWCore.TestProcessor.TestProcess import *
0059 process = TestProcess()
0060 process.load("HeterogeneousCore.CUDACore.ProcessAcceleratorCUDA_cfi")
0061 process.toTest = cms.EDProducer("TestCUDAProducerGPUFirst")
0062 process.moduleToTest(process.toTest)
0063 )_"};
0064   edm::test::TestProcessor::Config config{baseConfig};
0065 
0066   if (not cms::cudatest::testDevices()) {
0067     return;
0068   }
0069 
0070   constexpr int defaultDevice = 0;
0071 
0072   SECTION("Produce") {
0073     edm::test::TestProcessor tester{config};
0074     auto event = tester.test();
0075     auto prod = event.get<cms::cuda::Product<cms::cudatest::Thing> >();
0076     REQUIRE(prod->device() == defaultDevice);
0077     auto ctx = cms::cuda::ScopedContextProduce(*prod);
0078     const cms::cudatest::Thing& thing = ctx.get(*prod);
0079     const float* data = thing.get();
0080     REQUIRE(data != nullptr);
0081 
0082     float firstElements[10];
0083     cudaCheck(cudaMemcpyAsync(firstElements, data, sizeof(float) * 10, cudaMemcpyDeviceToHost, prod->stream()));
0084 
0085     std::cout << "Synchronizing with CUDA stream" << std::endl;
0086     auto stream = prod->stream();
0087     cudaCheck(cudaStreamSynchronize(stream));
0088     std::cout << "Synchronized" << std::endl;
0089     REQUIRE(firstElements[0] == 0.f);
0090     REQUIRE(firstElements[1] == 1.f);
0091     REQUIRE(firstElements[9] == 9.f);
0092   }
0093 };