CMS 3D CMS Logo

TestAlpakaAnalyzer.cc
Go to the documentation of this file.
1 #include <cassert>
2 
3 #include "DataFormats/PortableTestObjects/interface/TestHostCollection.h"
14 
15 namespace {
16 
17  template <typename T>
18  class Column {
19  public:
20  Column(T const* data, size_t size) : data_(data), size_(size) {}
21 
22  void print(std::ostream& out) const {
23  std::stringstream buffer;
24  buffer << "{ ";
25  if (size_ > 0) {
26  buffer << data_[0];
27  }
28  if (size_ > 1) {
29  buffer << ", " << data_[1];
30  }
31  if (size_ > 2) {
32  buffer << ", " << data_[2];
33  }
34  if (size_ > 3) {
35  buffer << ", ...";
36  }
37  buffer << '}';
38  out << buffer.str();
39  }
40 
41  private:
42  T const* const data_;
43  size_t const size_;
44  };
45 
46  template <typename T>
47  std::ostream& operator<<(std::ostream& out, Column<T> const& column) {
48  column.print(out);
49  return out;
50  }
51 
52  template <typename T>
53  void checkViewAddresses(T const& view) {
54  // columns
55  assert(view.metadata().addressOf_x() == view.x());
56  assert(view.metadata().addressOf_x() == &view.x(0));
57  assert(view.metadata().addressOf_x() == &view[0].x());
58  assert(view.metadata().addressOf_y() == view.y());
59  assert(view.metadata().addressOf_y() == &view.y(0));
60  assert(view.metadata().addressOf_y() == &view[0].y());
61  assert(view.metadata().addressOf_z() == view.z());
62  assert(view.metadata().addressOf_z() == &view.z(0));
63  assert(view.metadata().addressOf_z() == &view[0].z());
64  assert(view.metadata().addressOf_id() == view.id());
65  assert(view.metadata().addressOf_id() == &view.id(0));
66  assert(view.metadata().addressOf_id() == &view[0].id());
67  // scalars
68  assert(view.metadata().addressOf_r() == &view.r());
69  //assert(view.metadata().addressOf_r() == &view.r(0)); // cannot access a scalar with an index
70  //assert(view.metadata().addressOf_r() == &view[0].r()); // cannot access a scalar via a SoA row-like accessor
71  // columns of arrays
72  assert(view.metadata().addressOf_flags() == view.flags());
73  assert(view.metadata().addressOf_flags() == &view.flags(0));
74  assert(view.metadata().addressOf_flags() == &view[0].flags());
75  // columns of Eigen matrices
76  assert(view.metadata().addressOf_m() == view.m());
77  assert(view.metadata().addressOf_m() == &view.m(0).coeffRef(0, 0));
78  assert(view.metadata().addressOf_m() == &view[0].m().coeffRef(0, 0));
79  }
80 
81  template <typename T>
82  void checkViewAddresses2(T const& view) {
83  assert(view.metadata().addressOf_x2() == view.x2());
84  assert(view.metadata().addressOf_x2() == &view.x2(0));
85  assert(view.metadata().addressOf_x2() == &view[0].x2());
86  assert(view.metadata().addressOf_y2() == view.y2());
87  assert(view.metadata().addressOf_y2() == &view.y2(0));
88  assert(view.metadata().addressOf_y2() == &view[0].y2());
89  assert(view.metadata().addressOf_z2() == view.z2());
90  assert(view.metadata().addressOf_z2() == &view.z2(0));
91  assert(view.metadata().addressOf_z2() == &view[0].z2());
92  assert(view.metadata().addressOf_id2() == view.id2());
93  assert(view.metadata().addressOf_id2() == &view.id2(0));
94  assert(view.metadata().addressOf_id2() == &view[0].id2());
95  assert(view.metadata().addressOf_m2() == view.m2());
96  assert(view.metadata().addressOf_m2() == &view.m2(0).coeffRef(0, 0));
97  assert(view.metadata().addressOf_m2() == &view[0].m2().coeffRef(0, 0));
98  assert(view.metadata().addressOf_r2() == &view.r2());
99  //assert(view.metadata().addressOf_r2() == &view.r2(0)); // cannot access a scalar with an index
100  //assert(view.metadata().addressOf_r2() == &view[0].r2()); // cannot access a scalar via a SoA row-like accessor
101  }
102 
103  template <typename T>
104  void checkViewAddresses3(T const& view) {
105  assert(view.metadata().addressOf_x3() == view.x3());
106  assert(view.metadata().addressOf_x3() == &view.x3(0));
107  assert(view.metadata().addressOf_x3() == &view[0].x3());
108  assert(view.metadata().addressOf_y3() == view.y3());
109  assert(view.metadata().addressOf_y3() == &view.y3(0));
110  assert(view.metadata().addressOf_y3() == &view[0].y3());
111  assert(view.metadata().addressOf_z3() == view.z3());
112  assert(view.metadata().addressOf_z3() == &view.z3(0));
113  assert(view.metadata().addressOf_z3() == &view[0].z3());
114  assert(view.metadata().addressOf_id3() == view.id3());
115  assert(view.metadata().addressOf_id3() == &view.id3(0));
116  assert(view.metadata().addressOf_id3() == &view[0].id3());
117  assert(view.metadata().addressOf_m3() == view.m3());
118  assert(view.metadata().addressOf_m3() == &view.m3(0).coeffRef(0, 0));
119  assert(view.metadata().addressOf_m3() == &view[0].m3().coeffRef(0, 0));
120  assert(view.metadata().addressOf_r3() == &view.r3());
121  //assert(view.metadata().addressOf_r3() == &view.r3(0)); // cannot access a scalar with an index
122  //assert(view.metadata().addressOf_r3() == &view[0].r3()); // cannot access a scalar via a SoA row-like accessor
123  }
124 
125 } // namespace
126 
128 public:
130  : source_{config.getParameter<edm::InputTag>("source")},
132  //tokenMulti_{consumes(source_)},
135  expectSize_{config.getParameter<int>("expectSize")},
136  expectXvalues_{config.getParameter<std::vector<double>>("expectXvalues")} {
137  if (std::string const& eb = config.getParameter<std::string>("expectBackend"); not eb.empty()) {
140  }
141  }
142 
143  void analyze(edm::StreamID sid, edm::Event const& event, edm::EventSetup const&) const override {
144  portabletest::TestHostCollection const& product = event.get(token_);
145  auto const& view = product.const_view();
146  auto& mview = product.view();
147  auto const& cmview = product.view();
148 
149  if (expectSize_ >= 0 and expectSize_ != view.metadata().size()) {
150  throw cms::Exception("Assert") << "Expected input collection size " << expectSize_ << ", got "
151  << view.metadata().size();
152  }
153 
154  {
155  edm::LogInfo msg("TestAlpakaAnalyzer");
156  msg << source_.encode() << ".size() = " << view.metadata().size() << '\n';
157  msg << " data @ " << product.buffer().data() << ",\n"
158  << " x @ " << view.metadata().addressOf_x() << " = " << Column(view.x(), view.metadata().size()) << ",\n"
159  << " y @ " << view.metadata().addressOf_y() << " = " << Column(view.y(), view.metadata().size()) << ",\n"
160  << " z @ " << view.metadata().addressOf_z() << " = " << Column(view.z(), view.metadata().size()) << ",\n"
161  << " id @ " << view.metadata().addressOf_id() << " = " << Column(view.id(), view.metadata().size())
162  << ",\n"
163  << " r @ " << view.metadata().addressOf_r() << " = " << view.r() << '\n'
164  << " flags @ " << view.metadata().addressOf_flags() << " = " << Column(view.flags(), view.metadata().size())
165  << ",\n"
166  << " m @ " << view.metadata().addressOf_m() << " = { ... {" << view[1].m()(1, Eigen::indexing::all)
167  << " } ... } \n";
168  msg << std::hex << " [y - x] = 0x"
169  << reinterpret_cast<intptr_t>(view.metadata().addressOf_y()) -
170  reinterpret_cast<intptr_t>(view.metadata().addressOf_x())
171  << " [z - y] = 0x"
172  << reinterpret_cast<intptr_t>(view.metadata().addressOf_z()) -
173  reinterpret_cast<intptr_t>(view.metadata().addressOf_y())
174  << " [id - z] = 0x"
175  << reinterpret_cast<intptr_t>(view.metadata().addressOf_id()) -
176  reinterpret_cast<intptr_t>(view.metadata().addressOf_z())
177  << " [r - id] = 0x"
178  << reinterpret_cast<intptr_t>(view.metadata().addressOf_r()) -
179  reinterpret_cast<intptr_t>(view.metadata().addressOf_id())
180  << " [flags - r] = 0x"
181  << reinterpret_cast<intptr_t>(view.metadata().addressOf_flags()) -
182  reinterpret_cast<intptr_t>(view.metadata().addressOf_r())
183  << " [m - flags] = 0x"
184  << reinterpret_cast<intptr_t>(view.metadata().addressOf_m()) -
185  reinterpret_cast<intptr_t>(view.metadata().addressOf_flags());
186  }
187 
188  checkViewAddresses(view);
189  checkViewAddresses(mview);
190  checkViewAddresses(cmview);
191 
192  const portabletest::Matrix matrix{{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 18}};
193  const portabletest::Array flags{{6, 4, 2, 0}};
194  assert(view.r() == 1.);
195  for (int32_t i = 0; i < view.metadata().size(); ++i) {
196  auto vi = view[i];
197  if (not expectXvalues_.empty() and vi.x() != expectXvalues_[i % expectXvalues_.size()]) {
198  throw cms::Exception("Assert") << "Index " << i << " expected value "
199  << expectXvalues_[i % expectXvalues_.size()] << ", got " << vi.x();
200  }
201  assert(vi.y() == 0.);
202  assert(vi.z() == 0.);
203  assert(vi.id() == i);
204  assert(vi.flags() == flags);
205  assert(vi.m() == matrix * i);
206  }
207 
208  if (expectBackend_) {
209  auto backend = static_cast<cms::alpakatools::Backend>(event.get(backendToken_));
210  if (expectBackend_ != backend) {
211  throw cms::Exception("Assert") << "Expected input backend " << cms::alpakatools::toString(*expectBackend_)
212  << ", got " << cms::alpakatools::toString(backend);
213  }
214  }
215 
216  // portabletest::TestHostMultiCollection const& productMulti = event.get(tokenMulti_);
217  // auto const& viewMulti0 = productMulti.const_view<0>();
218  // auto& mviewMulti0 = productMulti.view<0>();
219  // auto const& cmviewMulti0 = productMulti.view<0>();
220  // auto const& viewMulti1 = productMulti.const_view<1>();
221  // auto& mviewMulti1 = productMulti.view<1>();
222  // auto const& cmviewMulti1 = productMulti.view<1>();
223 
224  portabletest::TestHostMultiCollection2 const& productMulti2 = event.get(tokenMulti2_);
225  auto const& viewMulti2_0 = productMulti2.const_view<0>();
226  auto& mviewMulti2_0 = productMulti2.view<0>();
227  auto const& cmviewMulti2_0 = productMulti2.view<0>();
228  auto const& viewMulti2_1 = productMulti2.const_view<1>();
229  auto& mviewMulti2_1 = productMulti2.view<1>();
230  auto const& cmviewMulti2_1 = productMulti2.view<1>();
231 
232  checkViewAddresses(viewMulti2_0);
233  checkViewAddresses(mviewMulti2_0);
234  checkViewAddresses(cmviewMulti2_0);
235  checkViewAddresses2(viewMulti2_1);
236  checkViewAddresses2(mviewMulti2_1);
237  checkViewAddresses2(cmviewMulti2_1);
238 
239  assert(viewMulti2_0.r() == 1.);
240  for (int32_t i = 0; i < viewMulti2_0.metadata().size(); ++i) {
241  auto vi = viewMulti2_0[i];
242  // std::stringstream s;
243  // s << "i=" << i << " x=" << vi.x() << " y=" << vi.y() << " z=" << vi.z() << " id=" << vi.id() << "'\nm=" << vi.m();
244  // std::cout << s.str() << std::endl;
245  if (not expectXvalues_.empty() and vi.x() != expectXvalues_[i % expectXvalues_.size()]) {
246  throw cms::Exception("Assert") << "Index " << i << " expected value "
247  << expectXvalues_[i % expectXvalues_.size()] << ", got " << vi.x();
248  }
249  //assert(vi.x() == 0.);
250  assert(vi.y() == 0.);
251  assert(vi.z() == 0.);
252  assert(vi.id() == i);
253  assert(vi.m() == matrix * i);
254  }
255  assert(viewMulti2_1.r2() == 2.);
256  for (int32_t i = 0; i < viewMulti2_1.metadata().size(); ++i) {
257  auto vi = viewMulti2_1[i];
258  if (not expectXvalues_.empty() and vi.x2() != expectXvalues_[i % expectXvalues_.size()]) {
259  throw cms::Exception("Assert") << "Index " << i << " expected value "
260  << expectXvalues_[i % expectXvalues_.size()] << ", got " << vi.x2();
261  }
262  assert(vi.y2() == 0.);
263  assert(vi.z2() == 0.);
264  assert(vi.id2() == i);
265  assert(vi.m2() == matrix * i);
266  }
267 
268  portabletest::TestHostMultiCollection3 const& productMulti3 = event.get(tokenMulti3_);
269  auto const& viewMulti3_0 = productMulti3.const_view<0>();
270  auto& mviewMulti3_0 = productMulti3.view<0>();
271  auto const& cmviewMulti3_0 = productMulti3.view<0>();
272  auto const& viewMulti3_1 = productMulti3.const_view<1>();
273  auto& mviewMulti3_1 = productMulti3.view<1>();
274  auto const& cmviewMulti3_1 = productMulti3.view<1>();
275  auto const& viewMulti3_2 = productMulti3.const_view<2>();
276  auto& mviewMulti3_2 = productMulti3.view<2>();
277  auto const& cmviewMulti3_2 = productMulti3.view<2>();
278 
279  checkViewAddresses(viewMulti3_0);
280  checkViewAddresses(mviewMulti3_0);
281  checkViewAddresses(cmviewMulti3_0);
282  checkViewAddresses2(viewMulti3_1);
283  checkViewAddresses2(mviewMulti3_1);
284  checkViewAddresses2(cmviewMulti3_1);
285  checkViewAddresses3(viewMulti3_2);
286  checkViewAddresses3(mviewMulti3_2);
287  checkViewAddresses3(cmviewMulti3_2);
288 
289  assert(viewMulti3_0.r() == 1.);
290  for (int32_t i = 0; i < viewMulti3_0.metadata().size(); ++i) {
291  auto vi = viewMulti3_0[i];
292  if (not expectXvalues_.empty() and vi.x() != expectXvalues_[i % expectXvalues_.size()]) {
293  throw cms::Exception("Assert") << "Index " << i << " expected value "
294  << expectXvalues_[i % expectXvalues_.size()] << ", got " << vi.x();
295  }
296  assert(vi.y() == 0.);
297  assert(vi.z() == 0.);
298  assert(vi.id() == i);
299  assert(vi.m() == matrix * i);
300  }
301  assert(viewMulti3_1.r2() == 2.);
302  for (int32_t i = 0; i < viewMulti3_1.metadata().size(); ++i) {
303  auto vi = viewMulti3_1[i];
304  if (not expectXvalues_.empty() and vi.x2() != expectXvalues_[i % expectXvalues_.size()]) {
305  throw cms::Exception("Assert") << "Index " << i << " expected value "
306  << expectXvalues_[i % expectXvalues_.size()] << ", got " << vi.x2();
307  }
308  assert(vi.y2() == 0.);
309  assert(vi.z2() == 0.);
310  assert(vi.id2() == i);
311  assert(vi.m2() == matrix * i);
312  }
313 
314  assert(viewMulti3_2.r3() == 3.);
315  for (int32_t i = 0; i < viewMulti3_2.metadata().size(); ++i) {
316  auto vi = viewMulti3_2[i];
317  if (not expectXvalues_.empty() and vi.x3() != expectXvalues_[i % expectXvalues_.size()]) {
318  throw cms::Exception("Assert") << "Index " << i << " expected value "
319  << expectXvalues_[i % expectXvalues_.size()] << ", got " << vi.x3();
320  }
321  assert(vi.y3() == 0.);
322  assert(vi.z3() == 0.);
323  assert(vi.id3() == i);
324  assert(vi.m3() == matrix * i);
325  }
326  }
327 
330  desc.add<edm::InputTag>("source");
331  desc.add<int>("expectSize", -1)
332  ->setComment("Expected size of the input collection. Values < 0 mean the check is not performed. Default: -1");
333  desc.add<std::vector<double>>("expectXvalues", std::vector<double>(0.))
334  ->setComment(
335  "Expected values of the 'x' field in the input collection. Empty value means to not perform the check. If "
336  "input collection has more elements than this parameter, the parameter values are looped over. Default: "
337  "{0.}");
338  desc.add<std::string>("expectBackend", "")
339  ->setComment(
340  "Expected backend of the input collection. Empty value means to not perform the check. Default: empty "
341  "string");
342  descriptions.addWithDefaultLabel(desc);
343  }
344 
345 private:
349  std::optional<cms::alpakatools::Backend> expectBackend_;
350  //const edm::EDGetTokenT<portabletest::TestHostMultiCollection> tokenMulti_;
353  const int expectSize_;
354  const std::vector<double> expectXvalues_;
355 };
356 
size
Write out results.
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
const edm::EDGetTokenT< portabletest::TestHostCollection > token_
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
std::string encode() const
Definition: InputTag.cc:159
Eigen::Matrix< double, 3, 6 > Matrix
Definition: TestSoA.h:19
void analyze(edm::StreamID sid, edm::Event const &event, edm::EventSetup const &) const override
std::optional< cms::alpakatools::Backend > expectBackend_
Definition: config.py:1
std::string const & label() const
Definition: InputTag.h:36
assert(be >=bs)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ConstView const & const_view() const
const edm::InputTag source_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::string_view toString(Backend backend)
Definition: Backend.cc:24
const edm::EDGetTokenT< portabletest::TestHostMultiCollection2 > tokenMulti2_
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
edm::EDGetTokenT< unsigned short > backendToken_
Backend toBackend(std::string_view name)
Definition: Backend.cc:13
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const std::vector< double > expectXvalues_
tuple msg
Definition: mps_check.py:286
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
const edm::EDGetTokenT< portabletest::TestHostMultiCollection3 > tokenMulti3_
TestAlpakaAnalyzer(edm::ParameterSet const &config)
std::string const & process() const
Definition: InputTag.h:40
long double T
Definition: event.py:1
ConstView< Idx > const & const_view() const