CMS 3D CMS Logo

AlpakaTestOpaqueAdditionModule.cc
Go to the documentation of this file.
1 #include <cstdint>
2 #include <iostream>
3 #include <random>
4 #include <vector>
5 
16 
18 
20  public:
22  ~AlpakaTestOpaqueAdditionModule() override = default;
23 
24  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
25 
26  void analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const& setup) const override;
27 
28  private:
29  const uint32_t size_;
30  };
31 
33  : size_(config.getParameter<uint32_t>("size")) {}
34 
37  desc.add<uint32_t>("size", 1024 * 1024);
38 
39  // ignore the alpaka = cms.untracked.PSet(...) injected by the framework
41  alpaka.setAllowAnything();
42  desc.addUntracked<edm::ParameterSetDescription>("alpaka", alpaka);
43 
44  descriptions.addWithDefaultLabel(desc);
45  }
46 
48  edm::Event const& event,
49  edm::EventSetup const& setup) const {
50  // require a valid Alpaka backend for running
52  if (not service or not service->enabled()) {
53  std::cout << "The " << ALPAKA_TYPE_ALIAS_NAME(AlpakaService)
54  << " is not available or disabled, the test will be skipped.\n";
55  return;
56  }
57 
58  // random number generator with a gaussian distribution
59  std::random_device rd{};
60  std::default_random_engine rand{rd()};
61  std::normal_distribution<float> dist{0., 1.};
62 
63  // tolerance
64  constexpr float epsilon = 0.000001;
65 
66  // allocate input and output host buffers
67  std::vector<float> in1(size_);
68  std::vector<float> in2(size_);
69  std::vector<float> out(size_);
70 
71  // fill the input buffers with random data, and the output buffer with zeros
72  for (uint32_t i = 0; i < size_; ++i) {
73  in1[i] = dist(rand);
74  in2[i] = dist(rand);
75  out[i] = 0.;
76  }
77 
78  // launch the 1-dimensional kernel for vector addition on the first available device
79  test::opaque_add_vectors_f(in1.data(), in2.data(), out.data(), size_);
80 
81  // check the results
82  for (uint32_t i = 0; i < size_; ++i) {
83  float sum = in1[i] + in2[i];
84  assert(out[i] < sum + epsilon);
85  assert(out[i] > sum - epsilon);
86  }
87 
88  std::cout << "All tests passed.\n";
89  }
90 
91 } // namespace ALPAKA_ACCELERATOR_NAMESPACE
92 
94 DEFINE_FWK_ALPAKA_MODULE(AlpakaTestOpaqueAdditionModule);
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void analyze(edm::StreamID, edm::Event const &event, edm::EventSetup const &setup) const override
Definition: config.py:1
assert(be >=bs)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
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
void opaque_add_vectors_f(const float *in1, const float *in2, float *out, uint32_t size)
#define DEFINE_FWK_ALPAKA_MODULE(name)
Definition: MakerMacros.h:16
Definition: event.py:1