Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-11-18 03:07:26

0001 #include <memory>
0002 #include <tuple>
0003 
0004 #define CATCH_CONFIG_MAIN
0005 #include "catch.hpp"
0006 #include "DataFormats/SoATemplate/interface/SoALayout.h"
0007 
0008 // clang-format off
0009 GENERATE_SOA_LAYOUT(SimpleLayoutTemplate,
0010   SOA_COLUMN(float, x),
0011   SOA_COLUMN(float, y),
0012   SOA_COLUMN(float, z),
0013   SOA_COLUMN(float, t))
0014 // clang-format on
0015 
0016 using SimpleLayout = SimpleLayoutTemplate<>;
0017 
0018 TEST_CASE("SoATemplate") {
0019   const std::size_t slSize = 10;
0020   const std::size_t slBufferSize = SimpleLayout::computeDataSize(slSize);
0021   std::unique_ptr<std::byte, decltype(std::free) *> slBuffer{
0022       reinterpret_cast<std::byte *>(aligned_alloc(SimpleLayout::alignment, slBufferSize)), std::free};
0023   SimpleLayout sl{slBuffer.get(), slSize};
0024   SECTION("Row wide copies, row") {
0025     SimpleLayout::View slv{sl};
0026     SimpleLayout::ConstView slcv{sl};
0027     auto slv0 = slv[0];
0028     slv0.x() = 1;
0029     slv0.y() = 2;
0030     slv0.z() = 3;
0031     slv0.t() = 5;
0032     // Fill up
0033     for (SimpleLayout::View::size_type i = 1; i < slv.metadata().size(); ++i) {
0034       auto slvi = slv[i];
0035       slvi = slv[i - 1];
0036       auto slvix = slvi.x();
0037       slvi.x() += slvi.y();
0038       slvi.y() += slvi.z();
0039       slvi.z() += slvi.t();
0040       slvi.t() += slvix;
0041     }
0042     // Verification and const view access
0043     float x = 1, y = 2, z = 3, t = 5;
0044     for (SimpleLayout::View::size_type i = 0; i < slv.metadata().size(); ++i) {
0045       auto slvi = slv[i];
0046       auto slcvi = slcv[i];
0047       REQUIRE(slvi.x() == x);
0048       REQUIRE(slvi.y() == y);
0049       REQUIRE(slvi.z() == z);
0050       REQUIRE(slvi.t() == t);
0051       REQUIRE(slcvi.x() == x);
0052       REQUIRE(slcvi.y() == y);
0053       REQUIRE(slcvi.z() == z);
0054       REQUIRE(slcvi.t() == t);
0055       auto tx = x;
0056       x += y;
0057       y += z;
0058       z += t;
0059       t += tx;
0060     }
0061   }
0062 
0063   SECTION("Row initializer, const view access, restrict disabling") {
0064     // With two views, we are creating (artificially) aliasing and should warn the compiler by turning restrict qualifiers off.
0065     using View = SimpleLayout::ViewTemplate<cms::soa::RestrictQualify::disabled, cms::soa::RangeChecking::Default>;
0066     using ConstView =
0067         SimpleLayout::ConstViewTemplate<cms::soa::RestrictQualify::disabled, cms::soa::RangeChecking::Default>;
0068     View slv{sl};
0069     ConstView slcv{sl};
0070     auto slv0 = slv[0];
0071     slv0 = {7, 11, 13, 17};
0072     // Fill up
0073     for (SimpleLayout::View::size_type i = 1; i < slv.metadata().size(); ++i) {
0074       auto slvi = slv[i];
0075       // Copy the const view
0076       slvi = slcv[i - 1];
0077       auto slvix = slvi.x();
0078       slvi.x() += slvi.y();
0079       slvi.y() += slvi.z();
0080       slvi.z() += slvi.t();
0081       slvi.t() += slvix;
0082     }
0083     // Verification and const view access
0084     auto [x, y, z, t] = std::make_tuple(7.0, 11.0, 13.0, 17.0);
0085     for (SimpleLayout::View::size_type i = 0; i < slv.metadata().size(); ++i) {
0086       auto slvi = slv[i];
0087       auto slcvi = slcv[i];
0088       REQUIRE(slvi.x() == x);
0089       REQUIRE(slvi.y() == y);
0090       REQUIRE(slvi.z() == z);
0091       REQUIRE(slvi.t() == t);
0092       REQUIRE(slcvi.x() == x);
0093       REQUIRE(slcvi.y() == y);
0094       REQUIRE(slcvi.z() == z);
0095       REQUIRE(slcvi.t() == t);
0096       auto tx = x;
0097       x += y;
0098       y += z;
0099       z += t;
0100       t += tx;
0101     }
0102   }
0103 }