Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:16

0001 #ifndef DataFormats_SoATemplate_test_FakeSoA_h
0002 #define DataFormats_SoATemplate_test_FakeSoA_h
0003 
0004 // A SoA-like class (with fake alignment (padding))
0005 #include <cstddef>
0006 #include <iostream>
0007 #include <memory>
0008 
0009 #define myassert(A)                                                                            \
0010   if (not(A)) {                                                                                \
0011     std::cerr << "Failed assertion: " #A " at  " __FILE__ "(" << __LINE__ << ")" << std::endl; \
0012     abort();                                                                                   \
0013   }
0014 
0015 class FakeSoA {
0016 public:
0017   static constexpr size_t padding_ = 128;
0018 
0019   // A fake SoA with 2 columns of uint16_t and uin32_t, plus fake padding.
0020   static size_t computeBufferSize(size_t nElements) {
0021     return nElements * (sizeof(uint16_t) + sizeof(uint32_t)) + padding_ + 0x400;
0022   }
0023 
0024   FakeSoA(std::byte *buffer, size_t nElements) { constFromBufferImpl(buffer, nElements); }
0025 
0026   FakeSoA() : size_(0) { std::cout << "At end of FakeSoA::FakeSoA()" << std::endl; }
0027 
0028   template <typename T>
0029   void allocateAndIoRead(T &onfile) {
0030     std::cout << "allocateAndIoRead begin" << std::endl;
0031     auto buffSize = FakeSoA::computeBufferSize(onfile.size_);
0032     auto buffer = new std::byte[buffSize];
0033     std::cout << "Buffer first byte after (alloc) =" << buffer + buffSize << std::endl;
0034     constFromBufferImpl(buffer, onfile.size_);
0035     memcpy(a16_, onfile.a16_, sizeof(uint16_t) * onfile.size_);
0036     memcpy(b32_, onfile.b32_, sizeof(uint32_t) * onfile.size_);
0037     std::cout << "allocateAndIoRead end" << std::endl;
0038   }
0039 
0040   void dump() {
0041     std::cout << "size=" << size_ << " buffer=" << buffer_.get() << " a16=" << a16_ << " b32=" << b32_
0042               << " (b32 - a16)="
0043               << reinterpret_cast<std::byte *>(reinterpret_cast<intptr_t>(b32_) - reinterpret_cast<intptr_t>(a16_))
0044               << " buffer size=" << computeBufferSize(size_) << "(" << std::hex << computeBufferSize(size_) << ")"
0045               << std::endl;
0046   }
0047 
0048   void dumpData() { std::cout << "a16_[0]=" << a16_[0] << " b32_[0]=" << b32_[0] << std::endl; }
0049 
0050   void fill() {
0051     for (int i = 0; i < size_; i++) {
0052       a16_[i] = 42 + i;
0053       b32_[i] = 24 + i;
0054     }
0055   }
0056 
0057   bool check() {
0058     bool result = true;
0059     for (int i = 0; i < size_; i++) {
0060       if (a16_[i] != 42 + i) {
0061         std::cout << "a16 mismatch at i=" << i << "(" << a16_[i] << "/" << 42 + i << ")" << std::endl;
0062         result = false;
0063       }
0064       if (b32_[i] != 24 + (uint32_t)i) {
0065         std::cout << "b32 mismatch at i=" << i << "(" << b32_[i] << "/" << 24 + i << ")" << std::endl;
0066         result = false;
0067       }
0068     }
0069     return result;
0070   }
0071 
0072 private:
0073   void constFromBufferImpl(std::byte *buffer, size_t nElements) {
0074     buffer_.reset(buffer);
0075     size_ = nElements;
0076     a16_ = reinterpret_cast<uint16_t *>(buffer);
0077     buffer += nElements * sizeof(uint16_t) + padding_;
0078     b32_ = reinterpret_cast<uint32_t *>(buffer);
0079     buffer += nElements * sizeof(uint32_t);
0080     std::cout << "Buffer first byte after (const) =" << buffer << std::endl;
0081     std::cout << "At end of FakeSoA::constFromBufferImpl(std::byte * buffer, size_t nElements): ";
0082     dump();
0083   }
0084 
0085   int size_;
0086   uint16_t *a16_ = nullptr;                      //[size_]
0087   uint32_t *b32_ = nullptr;                      //[size_]
0088   std::unique_ptr<std::byte> buffer_ = nullptr;  //!
0089 };
0090 
0091 #endif  // DataFormats_SoATemplate_test_FakeSoA_h