Skip to content

Commit 68ed834

Browse files
authored
Merge pull request #45852 from fwyzard/alpaka_implement_TrajectoryStateSoA_test
Implement a test for the `copyFromDense` and `copyToDense` utilities
2 parents fdfb559 + dba576d commit 68ed834

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<use name="eigen"/>
2+
23
<bin file="alpaka/TrackSoAHeterogeneous_test.cc alpaka/TrackSoAHeterogeneous_test.dev.cc" name="TrackSoAHeterogeneousAlpaka_test">
34
<use name="alpaka"/>
45
<use name="HeterogeneousCore/AlpakaInterface"/>
56
<flags ALPAKA_BACKENDS="1"/>
67
</bin>
8+
9+
<bin file="alpaka/TrajectoryStateSoA_t.cc alpaka/TrajectoryStateSoA_t.dev.cc" name="TrajectoryStateSoA_t">
10+
<use name="alpaka"/>
11+
<use name="HeterogeneousCore/AlpakaInterface"/>
12+
<flags ALPAKA_BACKENDS="1"/>
13+
</bin>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Simple test for the copyFromDense and copyToDense utilities from DataFormats/TrackSoA/interface/alpaka/TrackUtilities.h .
2+
*
3+
* Creates an instance of TracksSoACollection<pixelTopology::Phase1> (automatically allocates memory on device),
4+
* passes the view of the SoA data to the kernel that:
5+
* - fill the SoA with covariance data;
6+
* - copy the covariance data to the dense representation, and back to the matrix representation;
7+
* - verify that the data is copied back and forth correctly.
8+
*/
9+
10+
#include <cstdlib>
11+
#include <iostream>
12+
13+
#include <alpaka/alpaka.hpp>
14+
15+
#include "DataFormats/TrackSoA/interface/TracksSoA.h"
16+
#include "DataFormats/TrackSoA/interface/alpaka/TracksSoACollection.h"
17+
#include "FWCore/Utilities/interface/stringize.h"
18+
#include "Geometry/CommonTopologies/interface/SimplePixelTopology.h"
19+
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
20+
#include "HeterogeneousCore/AlpakaInterface/interface/devices.h"
21+
22+
#include "TrajectoryStateSoA_t.h"
23+
24+
// Each test binary is built for a single Alpaka backend.
25+
using namespace ALPAKA_ACCELERATOR_NAMESPACE;
26+
27+
int main() {
28+
// Get the list of devices on the current platform.
29+
auto const& devices = cms::alpakatools::devices<Platform>();
30+
if (devices.empty()) {
31+
std::cerr << "No devices available for the " EDM_STRINGIZE(ALPAKA_ACCELERATOR_NAMESPACE) " backend, "
32+
"the test will be skipped.\n";
33+
exit(EXIT_FAILURE);
34+
}
35+
36+
// Run the test on each device.
37+
for (const auto& device : devices) {
38+
Queue queue(device);
39+
40+
// Inner scope to deallocate memory before destroying the stream.
41+
{
42+
TracksSoACollection<pixelTopology::Phase1> tracks_d(queue);
43+
44+
test::testTrackSoA<pixelTopology::Phase1>(queue, tracks_d.view());
45+
46+
// Wait for the tests to complete.
47+
alpaka::wait(queue);
48+
}
49+
}
50+
51+
return EXIT_SUCCESS;
52+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <Eigen/Dense>
2+
3+
#include "DataFormats/TrackSoA/interface/TracksSoA.h"
4+
#include "DataFormats/TrackSoA/interface/alpaka/TrackUtilities.h"
5+
#include "Geometry/CommonTopologies/interface/SimplePixelTopology.h"
6+
#include "HeterogeneousCore/AlpakaInterface/interface/workdivision.h"
7+
8+
#include "TrajectoryStateSoA_t.h"
9+
10+
using Vector5d = Eigen::Matrix<double, 5, 1>;
11+
using Matrix5d = Eigen::Matrix<double, 5, 5>;
12+
13+
using namespace cms::alpakatools;
14+
15+
namespace ALPAKA_ACCELERATOR_NAMESPACE::test {
16+
17+
namespace {
18+
19+
ALPAKA_FN_ACC Matrix5d buildCovariance(Vector5d const& e) {
20+
Matrix5d cov;
21+
for (int i = 0; i < 5; ++i)
22+
cov(i, i) = e(i) * e(i);
23+
for (int i = 0; i < 5; ++i) {
24+
for (int j = 0; j < i; ++j) {
25+
// this makes the matrix positive defined
26+
double v = 0.3 * std::sqrt(cov(i, i) * cov(j, j));
27+
cov(i, j) = (i + j) % 2 ? -0.4 * v : 0.1 * v;
28+
cov(j, i) = cov(i, j);
29+
}
30+
}
31+
return cov;
32+
}
33+
34+
template <typename TrackerTraits>
35+
struct TestTrackSoA {
36+
using Utils = TracksUtilities<TrackerTraits>;
37+
38+
ALPAKA_FN_ACC void operator()(Acc1D const& acc, reco::TrackSoAView<TrackerTraits> tracks) const {
39+
Vector5d par0;
40+
par0 << 0.2, 0.1, 3.5, 0.8, 0.1;
41+
Vector5d e0;
42+
e0 << 0.01, 0.01, 0.035, -0.03, -0.01;
43+
Matrix5d cov0 = buildCovariance(e0);
44+
45+
for (auto i : uniform_elements(acc, tracks.metadata().size())) {
46+
Utils::copyFromDense(tracks, par0, cov0, i);
47+
Vector5d par1;
48+
Matrix5d cov1;
49+
Utils::copyToDense(tracks, par1, cov1, i);
50+
Vector5d deltaV = par1 - par0;
51+
Matrix5d deltaM = cov1 - cov0;
52+
for (int j = 0; j < 5; ++j) {
53+
ALPAKA_ASSERT(std::abs(deltaV(j)) < 1.e-5);
54+
for (int k = j; k < 5; ++k) {
55+
ALPAKA_ASSERT(cov0(k, j) == cov0(j, k));
56+
ALPAKA_ASSERT(cov1(k, j) == cov1(j, k));
57+
ALPAKA_ASSERT(std::abs(deltaM(k, j)) < 1.e-5);
58+
}
59+
}
60+
}
61+
}
62+
};
63+
64+
} // namespace
65+
66+
template <typename TrackerTraits>
67+
void testTrackSoA(Queue& queue, reco::TrackSoAView<TrackerTraits>& tracks) {
68+
auto grid = make_workdiv<Acc1D>(1, 64);
69+
alpaka::exec<Acc1D>(queue, grid, TestTrackSoA<TrackerTraits>{}, tracks);
70+
}
71+
72+
template void testTrackSoA<pixelTopology::Phase1>(Queue& queue, reco::TrackSoAView<pixelTopology::Phase1>& tracks);
73+
template void testTrackSoA<pixelTopology::Phase2>(Queue& queue, reco::TrackSoAView<pixelTopology::Phase2>& tracks);
74+
75+
} // namespace ALPAKA_ACCELERATOR_NAMESPACE::test
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef DataFormats_TrackSoA_test_alpaka_TrajectoryStateSoA_t_h
2+
#define DataFormats_TrackSoA_test_alpaka_TrajectoryStateSoA_t_h
3+
4+
#include "DataFormats/TrackSoA/interface/TracksSoA.h"
5+
#include "Geometry/CommonTopologies/interface/SimplePixelTopology.h"
6+
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
7+
8+
namespace ALPAKA_ACCELERATOR_NAMESPACE::test {
9+
10+
template <typename TrackerTraits>
11+
void testTrackSoA(Queue& queue, reco::TrackSoAView<TrackerTraits>& tracks);
12+
13+
} // namespace ALPAKA_ACCELERATOR_NAMESPACE::test
14+
15+
#endif // DataFormats_TrackSoA_test_alpaka_TrajectoryStateSoA_t_h

0 commit comments

Comments
 (0)