CMS 3D CMS Logo

CUDATestOpaqueAdditionModule.cc
Go to the documentation of this file.
1 #include <cstddef>
2 #include <cstdint>
3 #include <iostream>
4 #include <random>
5 #include <vector>
6 
16 
18 public:
20  ~CUDATestOpaqueAdditionModule() override = default;
21 
22  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
23 
24  void analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const& setup) const override;
25 
26 private:
27  const uint32_t size_;
28 };
29 
31  : size_(config.getParameter<uint32_t>("size")) {}
32 
35  desc.add<uint32_t>("size", 1024 * 1024);
36  descriptions.addWithDefaultLabel(desc);
37 }
38 
40  // require CUDA for running
42  if (not cuda or not cuda->enabled()) {
43  std::cout << "The CUDAService is not available or disabled, the test will be skipped.\n";
44  return;
45  }
46 
47  // random number generator with a gaussian distribution
48  std::random_device rd{};
49  std::default_random_engine rand{rd()};
50  std::normal_distribution<float> dist{0., 1.};
51 
52  // tolerance
53  constexpr float epsilon = 0.000001;
54 
55  // allocate input and output host buffers
56  std::vector<float> in1(size_);
57  std::vector<float> in2(size_);
58  std::vector<float> out(size_);
59 
60  // fill the input buffers with random data, and the output buffer with zeros
61  for (size_t i = 0; i < size_; ++i) {
62  in1[i] = dist(rand);
63  in2[i] = dist(rand);
64  out[i] = 0.;
65  }
66 
67  // launch the 1-dimensional kernel for vector addition
68  cms::cudatest::opaque_add_vectors_f(in1.data(), in2.data(), out.data(), size_);
69 
70  // check the results
71  for (size_t i = 0; i < size_; ++i) {
72  float sum = in1[i] + in2[i];
73  assert(out[i] < sum + epsilon);
74  assert(out[i] > sum - epsilon);
75  }
76 
77  std::cout << "All tests passed.\n";
78 }
79 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
CUDATestOpaqueAdditionModule(edm::ParameterSet const &config)
Definition: config.py:1
assert(be >=bs)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
~CUDATestOpaqueAdditionModule() override=default
void analyze(edm::StreamID, edm::Event const &event, edm::EventSetup const &setup) const override
void opaque_add_vectors_f(const float *in1, const float *in2, float *out, size_t size)
Definition: event.py:1