Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-02 22:46:44

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         << "  m    @ " << view.metadata().addressOf_m() << " = { ... {" << view[1].m()(1, Eigen::all) << " } ... } \n";
0075     msg << std::hex << "  [y - x] = 0x"
0076         << reinterpret_cast<intptr_t>(view.metadata().addressOf_y()) -
0077                reinterpret_cast<intptr_t>(view.metadata().addressOf_x())
0078         << "  [z - y] = 0x"
0079         << reinterpret_cast<intptr_t>(view.metadata().addressOf_z()) -
0080                reinterpret_cast<intptr_t>(view.metadata().addressOf_y())
0081         << "  [id - z] = 0x"
0082         << reinterpret_cast<intptr_t>(view.metadata().addressOf_id()) -
0083                reinterpret_cast<intptr_t>(view.metadata().addressOf_z())
0084         << "  [r - id] = 0x"
0085         << reinterpret_cast<intptr_t>(view.metadata().addressOf_r()) -
0086                reinterpret_cast<intptr_t>(view.metadata().addressOf_id())
0087         << "  [m - r] = 0x"
0088         << reinterpret_cast<intptr_t>(view.metadata().addressOf_m()) -
0089                reinterpret_cast<intptr_t>(view.metadata().addressOf_r());
0090 
0091     const portabletest::Matrix matrix{{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 18}};
0092     assert(view.r() == 1.);
0093     for (int32_t i = 0; i < view.metadata().size(); ++i) {
0094       auto vi = view[i];
0095       assert(vi.x() == 0.);
0096       assert(vi.y() == 0.);
0097       assert(vi.z() == 0.);
0098       assert(vi.id() == i);
0099       //assert(vi.m() == matrix * i);
0100     }
0101   }
0102 
0103   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0104     edm::ParameterSetDescription desc;
0105     desc.add<edm::InputTag>("source");
0106     descriptions.addWithDefaultLabel(desc);
0107   }
0108 
0109 private:
0110   const edm::InputTag source_;
0111   const edm::EDGetTokenT<cudatest::TestHostCollection> token_;
0112 };
0113 
0114 #include "FWCore/Framework/interface/MakerMacros.h"
0115 DEFINE_FWK_MODULE(TestPortableAnalyzer);