Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-04-06 22:42:47

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