Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-26 23:26:38

0001 #include <Eigen/Core>
0002 #include <Eigen/Dense>
0003 
0004 #include <catch.hpp>
0005 
0006 #include "DataFormats/Portable/interface/PortableHostCollection.h"
0007 #include "DataFormats/SoATemplate/interface/SoACommon.h"
0008 #include "DataFormats/SoATemplate/interface/SoALayout.h"
0009 
0010 GENERATE_SOA_LAYOUT(SoAPositionTemplate,
0011                     SOA_COLUMN(float, x),
0012                     SOA_COLUMN(float, y),
0013                     SOA_COLUMN(float, z),
0014                     SOA_SCALAR(int, detectorType))
0015 
0016 using SoAPosition = SoAPositionTemplate<>;
0017 using SoAPositionView = SoAPosition::View;
0018 using SoAPositionConstView = SoAPosition::ConstView;
0019 
0020 GENERATE_SOA_LAYOUT(SoAPCATemplate,
0021                     SOA_COLUMN(float, eigenvalues),
0022                     SOA_COLUMN(float, eigenvector_1),
0023                     SOA_COLUMN(float, eigenvector_2),
0024                     SOA_COLUMN(float, eigenvector_3),
0025                     SOA_EIGEN_COLUMN(Eigen::Vector3d, candidateDirection))
0026 
0027 using SoAPCA = SoAPCATemplate<>;
0028 using SoAPCAView = SoAPCA::View;
0029 using SoAPCAConstView = SoAPCA::ConstView;
0030 
0031 GENERATE_SOA_LAYOUT(GenericSoATemplate,
0032                     SOA_COLUMN(float, xPos),
0033                     SOA_COLUMN(float, yPos),
0034                     SOA_COLUMN(float, zPos),
0035                     SOA_EIGEN_COLUMN(Eigen::Vector3d, candidateDirection))
0036 
0037 using GenericSoA = GenericSoATemplate<>;
0038 using GenericSoAView = GenericSoA::View;
0039 using GenericSoAConstView = GenericSoA::ConstView;
0040 
0041 TEST_CASE("Deep copy from SoA Generic View") {
0042   // common number of elements for the SoAs
0043   const std::size_t elems = 10;
0044 
0045   // Portable Collections
0046   PortableHostCollection<SoAPosition> positionCollection(elems, cms::alpakatools::host());
0047   PortableHostCollection<SoAPCA> pcaCollection(elems, cms::alpakatools::host());
0048 
0049   // Portable Collection Views
0050   SoAPositionView& positionCollectionView = positionCollection.view();
0051   SoAPCAView& pcaCollectionView = pcaCollection.view();
0052   // Portable Collection ConstViews
0053   const SoAPositionConstView& positionCollectionConstView = positionCollection.const_view();
0054   const SoAPCAConstView& pcaCollectionConstView = pcaCollection.const_view();
0055 
0056   // fill up
0057   for (size_t i = 0; i < elems; i++) {
0058     positionCollectionView[i] = {i * 1.0f, i * 2.0f, i * 3.0f};
0059   }
0060   positionCollectionView.detectorType() = 1;
0061 
0062   float time = 0.01;
0063   for (size_t i = 0; i < elems; i++) {
0064     pcaCollectionView[i].eigenvector_1() = positionCollectionView[i].x() / time;
0065     pcaCollectionView[i].eigenvector_2() = positionCollectionView[i].y() / time;
0066     pcaCollectionView[i].eigenvector_3() = positionCollectionView[i].z() / time;
0067     pcaCollectionView[i].candidateDirection()(0) = positionCollectionView[i].x() / time;
0068     pcaCollectionView[i].candidateDirection()(1) = positionCollectionView[i].y() / time;
0069     pcaCollectionView[i].candidateDirection()(2) = positionCollectionView[i].z() / time;
0070   }
0071 
0072   SECTION("Deep copy the View") {
0073     // addresses and size of the SoA columns
0074     const auto posRecs = positionCollectionView.records();
0075     const auto pcaRecs = pcaCollectionView.records();
0076 
0077     // building the View with runtime check for the size
0078     GenericSoAView genericView(posRecs.x(), posRecs.y(), posRecs.z(), pcaRecs.candidateDirection());
0079 
0080     // Check for equality of memory addresses
0081     REQUIRE(genericView.metadata().addressOf_xPos() == positionCollectionView.metadata().addressOf_x());
0082     REQUIRE(genericView.metadata().addressOf_yPos() == positionCollectionView.metadata().addressOf_y());
0083     REQUIRE(genericView.metadata().addressOf_zPos() == positionCollectionView.metadata().addressOf_z());
0084     REQUIRE(genericView.metadata().addressOf_candidateDirection() ==
0085             pcaCollectionView.metadata().addressOf_candidateDirection());
0086 
0087     // PortableHostCollection that will host the aggregated columns
0088     PortableHostCollection<GenericSoA> genericCollection(elems, cms::alpakatools::host());
0089     genericCollection.deepCopy(genericView);
0090 
0091     // Check for inequality of memory addresses
0092     REQUIRE(genericCollection.view().metadata().addressOf_xPos() != positionCollectionView.metadata().addressOf_x());
0093     REQUIRE(genericCollection.view().metadata().addressOf_yPos() != positionCollectionView.metadata().addressOf_y());
0094     REQUIRE(genericCollection.view().metadata().addressOf_zPos() != positionCollectionView.metadata().addressOf_z());
0095     REQUIRE(genericCollection.view().metadata().addressOf_candidateDirection() !=
0096             pcaCollectionView.metadata().addressOf_candidateDirection());
0097   }
0098 
0099   SECTION("Deep copy the ConstView") {
0100     // addresses and size of the SoA columns
0101     const auto posRecs = positionCollectionConstView.records();
0102     const auto pcaRecs = pcaCollectionConstView.records();
0103 
0104     // building the View with runtime check for the size
0105     GenericSoAConstView genericConstView(posRecs.x(), posRecs.y(), posRecs.z(), pcaRecs.candidateDirection());
0106 
0107     // Check for equality of memory addresses
0108     REQUIRE(genericConstView.metadata().addressOf_xPos() == positionCollectionView.metadata().addressOf_x());
0109     REQUIRE(genericConstView.metadata().addressOf_yPos() == positionCollectionView.metadata().addressOf_y());
0110     REQUIRE(genericConstView.metadata().addressOf_zPos() == positionCollectionView.metadata().addressOf_z());
0111     REQUIRE(genericConstView.metadata().addressOf_candidateDirection() ==
0112             pcaCollectionView.metadata().addressOf_candidateDirection());
0113 
0114     // PortableHostCollection that will host the aggregated columns
0115     PortableHostCollection<GenericSoA> genericCollection(elems, cms::alpakatools::host());
0116     genericCollection.deepCopy(genericConstView);
0117 
0118     // Check for inequality of memory addresses
0119     REQUIRE(genericCollection.view().metadata().addressOf_xPos() != positionCollectionView.metadata().addressOf_x());
0120     REQUIRE(genericCollection.view().metadata().addressOf_yPos() != positionCollectionView.metadata().addressOf_y());
0121     REQUIRE(genericCollection.view().metadata().addressOf_zPos() != positionCollectionView.metadata().addressOf_z());
0122     REQUIRE(genericCollection.view().metadata().addressOf_candidateDirection() !=
0123             pcaCollectionView.metadata().addressOf_candidateDirection());
0124   }
0125 }