Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-26 08:14:18

0001 #include <cassert>
0002 
0003 #include "CUDADataFormats/PortableTestObjects/interface/TestHostCollection.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/EventSetup.h"
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/Framework/interface/stream/EDAnalyzer.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0012 #include "FWCore/Utilities/interface/EDGetToken.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014 
0015 namespace {
0016 
0017   template <typename T>
0018   class Column {
0019   public:
0020     Column(T const* data, size_t size) : data_(data), size_(size) {}
0021 
0022     void print(std::ostream& out) const {
0023       std::stringstream buffer;
0024       buffer << "{ ";
0025       if (size_ > 0) {
0026         buffer << data_[0];
0027       }
0028       if (size_ > 1) {
0029         buffer << ", " << data_[1];
0030       }
0031       if (size_ > 2) {
0032         buffer << ", " << data_[2];
0033       }
0034       if (size_ > 3) {
0035         buffer << ", ...";
0036       }
0037       buffer << '}';
0038       out << buffer.str();
0039     }
0040 
0041   private:
0042     T const* const data_;
0043     size_t const size_;
0044   };
0045 
0046   template <typename T>
0047   std::ostream& operator<<(std::ostream& out, Column<T> const& column) {
0048     column.print(out);
0049     return out;
0050   }
0051 }  // namespace
0052 
0053 class TestPortableAnalyzer : public edm::stream::EDAnalyzer<> {
0054 public:
0055   TestPortableAnalyzer(edm::ParameterSet const& config)
0056       : source_{config.getParameter<edm::InputTag>("source")}, token_{consumes(source_)} {}
0057 
0058   void analyze(edm::Event const& event, edm::EventSetup const&) override {
0059     cudatest::TestHostCollection const& product = event.get(token_);
0060 
0061     auto const& view = product.const_view();
0062     for (int32_t i = 0; i < view.metadata().size(); ++i) {
0063       assert(view[i].id() == i);
0064     }
0065 
0066     edm::LogInfo msg("TestPortableAnalyzer");
0067     msg << source_.encode() << ".size() = " << view.metadata().size() << '\n';
0068     msg << "  data  @ " << product.buffer().get() << ",\n"
0069         << "  x     @ " << view.metadata().addressOf_x() << " = " << Column(view.x(), view.metadata().size()) << ",\n"
0070         << "  y     @ " << view.metadata().addressOf_y() << " = " << Column(view.y(), view.metadata().size()) << ",\n"
0071         << "  z     @ " << view.metadata().addressOf_z() << " = " << Column(view.z(), view.metadata().size()) << ",\n"
0072         << "  id    @ " << view.metadata().addressOf_id() << " = " << Column(view.id(), view.metadata().size()) << ",\n"
0073         << "  r     @ " << view.metadata().addressOf_r() << " = " << view.r() << '\n'
0074         << "  flags @ " << view.metadata().addressOf_flags() << " = " << Column(view.flags(), view.metadata().size())
0075         << ",\n"
0076         << "  m     @ " << view.metadata().addressOf_m() << " = { ... {" << view[1].m()(1, Eigen::indexing::all)
0077         << " } ... } \n";
0078     msg << std::hex << "  [y - x] = 0x"
0079         << reinterpret_cast<intptr_t>(view.metadata().addressOf_y()) -
0080                reinterpret_cast<intptr_t>(view.metadata().addressOf_x())
0081         << "  [z - y] = 0x"
0082         << reinterpret_cast<intptr_t>(view.metadata().addressOf_z()) -
0083                reinterpret_cast<intptr_t>(view.metadata().addressOf_y())
0084         << "  [id - z] = 0x"
0085         << reinterpret_cast<intptr_t>(view.metadata().addressOf_id()) -
0086                reinterpret_cast<intptr_t>(view.metadata().addressOf_z())
0087         << "  [r - id] = 0x"
0088         << reinterpret_cast<intptr_t>(view.metadata().addressOf_r()) -
0089                reinterpret_cast<intptr_t>(view.metadata().addressOf_id())
0090         << "  [flags - r] = 0x"
0091         << reinterpret_cast<intptr_t>(view.metadata().addressOf_flags()) -
0092                reinterpret_cast<intptr_t>(view.metadata().addressOf_r())
0093         << "  [m - flags] = 0x"
0094         << reinterpret_cast<intptr_t>(view.metadata().addressOf_m()) -
0095                reinterpret_cast<intptr_t>(view.metadata().addressOf_flags());
0096 
0097     const portabletest::Matrix matrix{{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 18}};
0098     const portabletest::Array flags = {{6, 4, 2, 0}};
0099 
0100     assert(view.r() == 1.);
0101     for (int32_t i = 0; i < view.metadata().size(); ++i) {
0102       auto vi = view[i];
0103       assert(vi.x() == 0.);
0104       assert(vi.y() == 0.);
0105       assert(vi.z() == 0.);
0106       assert(vi.id() == i);
0107       assert(vi.flags() == flags);
0108       assert(vi.m() == matrix * i);
0109     }
0110   }
0111 
0112   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0113     edm::ParameterSetDescription desc;
0114     desc.add<edm::InputTag>("source");
0115     descriptions.addWithDefaultLabel(desc);
0116   }
0117 
0118 private:
0119   const edm::InputTag source_;
0120   const edm::EDGetTokenT<cudatest::TestHostCollection> token_;
0121 };
0122 
0123 #include "FWCore/Framework/interface/MakerMacros.h"
0124 DEFINE_FWK_MODULE(TestPortableAnalyzer);