Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-09 02:22:22

0001 #include <cstddef>
0002 #include <cstdint>
0003 #include <iostream>
0004 #include <random>
0005 #include <vector>
0006 
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0010 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0013 #include "FWCore/ServiceRegistry/interface/Service.h"
0014 #include "HeterogeneousCore/ROCmServices/interface/ROCmInterface.h"
0015 #include "HeterogeneousTest/ROCmOpaque/interface/DeviceAdditionOpaque.h"
0016 
0017 class ROCmTestOpaqueAdditionModule : public edm::global::EDAnalyzer<> {
0018 public:
0019   explicit ROCmTestOpaqueAdditionModule(edm::ParameterSet const& config);
0020   ~ROCmTestOpaqueAdditionModule() override = default;
0021 
0022   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0023 
0024   void analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const& setup) const override;
0025 
0026 private:
0027   const uint32_t size_;
0028 };
0029 
0030 ROCmTestOpaqueAdditionModule::ROCmTestOpaqueAdditionModule(edm::ParameterSet const& config)
0031     : size_(config.getParameter<uint32_t>("size")) {}
0032 
0033 void ROCmTestOpaqueAdditionModule::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0034   edm::ParameterSetDescription desc;
0035   desc.add<uint32_t>("size", 1024 * 1024);
0036   descriptions.addWithDefaultLabel(desc);
0037 }
0038 
0039 void ROCmTestOpaqueAdditionModule::analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const& setup) const {
0040   // require ROCm for running
0041   edm::Service<ROCmInterface> rocm;
0042   if (not rocm or not rocm->enabled()) {
0043     std::cout << "The ROCmService is not available or disabled, the test will be skipped.\n";
0044     return;
0045   }
0046 
0047   // random number generator with a gaussian distribution
0048   std::random_device rd{};
0049   std::default_random_engine rand{rd()};
0050   std::normal_distribution<float> dist{0., 1.};
0051 
0052   // tolerance
0053   constexpr float epsilon = 0.000001;
0054 
0055   // allocate input and output host buffers
0056   std::vector<float> in1(size_);
0057   std::vector<float> in2(size_);
0058   std::vector<float> out(size_);
0059 
0060   // fill the input buffers with random data, and the output buffer with zeros
0061   for (size_t i = 0; i < size_; ++i) {
0062     in1[i] = dist(rand);
0063     in2[i] = dist(rand);
0064     out[i] = 0.;
0065   }
0066 
0067   // launch the 1-dimensional kernel for vector addition
0068   cms::rocmtest::opaque_add_vectors_f(in1.data(), in2.data(), out.data(), size_);
0069 
0070   // check the results
0071   for (size_t i = 0; i < size_; ++i) {
0072     float sum = in1[i] + in2[i];
0073     assert(out[i] < sum + epsilon);
0074     assert(out[i] > sum - epsilon);
0075   }
0076 
0077   std::cout << "All tests passed.\n";
0078 }
0079 
0080 #include "FWCore/Framework/interface/MakerMacros.h"
0081 DEFINE_FWK_MODULE(ROCmTestOpaqueAdditionModule);