Skip to content

Commit ddda404

Browse files
AdrianoDeeborzarinothingface0
authored andcommitted
Port the pixel track reconstruction to Alpaka
Co-authored-by: Breno Orzari <[email protected]> Co-authored-by: Dimitris Papagiannis <[email protected]>
1 parent 18a5acb commit ddda404

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+7874
-15
lines changed

DataFormats/TrackSoA/BuildFile.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<use name="alpaka"/>
2+
<use name="rootcore"/>
3+
<use name="eigen"/>
4+
<use name="DataFormats/Common"/>
5+
<use name="DataFormats/Portable"/>
6+
<use name="DataFormats/SoATemplate" source_only="1"/>
7+
<use name="DataFormats/TrackerCommon" source_only="1"/>
8+
<use name="HeterogeneousCore/AlpakaInterface"/>
9+
<flags ALPAKA_BACKENDS="!serial"/>
10+
<export>
11+
<lib name="1"/>
12+
</export>

DataFormats/TrackSoA/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# TrackSoA Data Formats
2+
3+
`DataFormat`s meant to be used on Host (CPU) or Device (GPU) for
4+
storing information about `TrackSoA`s created during the Pixel-local Reconstruction
5+
chain. It stores data in an SoA manner.
6+
7+
The host format is inheriting from `DataFormats/Portable/interface/PortableHostCollection.h`,
8+
while the device format is inheriting from `DataFormats/Portable/interface/PortableDeviceCollection.h`
9+
10+
Both formats use the same SoA Layout (`TrackSoA::Layout`) which is generated
11+
via the `GENERATE_SOA_LAYOUT` macro in the `TrackDefinitions.h` file.
12+
13+
## Notes
14+
15+
-`hitIndices` and `detIndices`, instances of `HitContainer`, have been added into the
16+
layout as `SOA_SCALAR`s, meaning that they manage their own data independently from the SoA
17+
`Layout`. This could be improved in the future, if `HitContainer` (aka a `OneToManyAssoc` of fixed size)
18+
is replaced, but there don't seem to be any conflicts in including it in the `Layout` like this.
19+
- Host and Device classes should **not** be created via inheritance, as they're done here,
20+
but via composition. See [this discussion](https://github.com/cms-sw/cmssw/pull/40465#discussion_r1066039309).
21+
22+
## TracksHost
23+
24+
The version of the data format to be used for storing `TrackSoA` data on the CPU.
25+
Instances of this class are to be used for:
26+
27+
- Having a place to copy data to host from device, via `Memcpy`, or
28+
- Running host-side algorithms using data stored in an SoA manner.
29+
30+
## TracksDevice
31+
32+
The version of the data format to be used for storing `TrackSoA` data on the GPU.
33+
34+
Instances of `TracksDevice` are to be created on host and be
35+
used on device only. To do so, the instance's `view()` method is to be called
36+
to pass a `View` to any kernel launched. Accessing data from the `view()` is not
37+
possible on the host side.
38+
39+
## TracksSoACollection
40+
41+
Depending on the Alpaka accelerator back-end enabled, `TrackSoACollection` is an alias to either the Host or Device SoA:
42+
43+
```cpp
44+
template <typename TrackerTraits>
45+
using TrackSoACollection = std::conditional_t<std::is_same_v<Device, alpaka::DevCpu>,
46+
TrackSoAHost<TrackerTraits>,
47+
TrackSoADevice<TrackerTraits, Device>>;
48+
```
49+
50+
## Utilities
51+
52+
`alpaka/TrackUtilities.h` contains a collection of methods which were originally
53+
defined as class methods inside either `TrackSoAHeterogeneousT` and `TrajectoryStateSoAT`
54+
which have been adapted to operate on `View` instances, so that they are callable
55+
from within `__global__` kernels, on both CPU and CPU.
56+
57+
## Use case
58+
59+
See `test/TrackSoAHeterogeneous_test.cpp` for a simple example of instantiation,
60+
processing and copying from device to host.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef DataFormats_Track_interface_TrackDefinitions_h
2+
#define DataFormats_Track_interface_TrackDefinitions_h
3+
#include <string>
4+
#include <algorithm>
5+
#include <stdexcept>
6+
7+
namespace pixelTrack {
8+
9+
enum class Quality : uint8_t { bad = 0, edup, dup, loose, strict, tight, highPurity, notQuality };
10+
constexpr uint32_t qualitySize{uint8_t(Quality::notQuality)};
11+
constexpr std::string_view qualityName[qualitySize]{"bad", "edup", "dup", "loose", "strict", "tight", "highPurity"};
12+
inline Quality qualityByName(std::string_view name) {
13+
auto qp = std::find(qualityName, qualityName + qualitySize, name) - qualityName;
14+
auto ret = static_cast<Quality>(qp);
15+
16+
if (ret == pixelTrack::Quality::notQuality)
17+
throw std::invalid_argument(std::string(name) + " is not a pixelTrack::Quality!");
18+
19+
return ret;
20+
}
21+
22+
#ifdef GPU_SMALL_EVENTS
23+
// kept for testing and debugging
24+
constexpr uint32_t maxNumber() { return 2 * 1024; }
25+
#else
26+
// tested on MC events with 55-75 pileup events
27+
constexpr uint32_t maxNumber() { return 32 * 1024; }
28+
#endif
29+
30+
} // namespace pixelTrack
31+
32+
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef DataFormats_Track_interface_TracksDevice_h
2+
#define DataFormats_Track_interface_TracksDevice_h
3+
4+
#include <cstdint>
5+
#include <alpaka/alpaka.hpp>
6+
#include "DataFormats/TrackSoA/interface/TracksSoA.h"
7+
#include "DataFormats/TrackSoA/interface/TrackDefinitions.h"
8+
#include "DataFormats/Portable/interface/PortableDeviceCollection.h"
9+
10+
// TODO: The class is created via inheritance of the PortableCollection.
11+
// This is generally discouraged, and should be done via composition.
12+
// See: https://github.com/cms-sw/cmssw/pull/40465#discussion_r1067364306
13+
template <typename TrackerTraits, typename TDev>
14+
class TracksDevice : public PortableDeviceCollection<reco::TrackLayout<TrackerTraits>, TDev> {
15+
public:
16+
static constexpr int32_t S = TrackerTraits::maxNumberOfTuples; //TODO: this could be made configurable at runtime
17+
TracksDevice() = default; // necessary for ROOT dictionaries
18+
19+
using PortableDeviceCollection<reco::TrackLayout<TrackerTraits>, TDev>::view;
20+
using PortableDeviceCollection<reco::TrackLayout<TrackerTraits>, TDev>::const_view;
21+
using PortableDeviceCollection<reco::TrackLayout<TrackerTraits>, TDev>::buffer;
22+
23+
// Constructor which specifies the SoA size
24+
template <typename TQueue>
25+
explicit TracksDevice<TrackerTraits, TDev>(TQueue& queue)
26+
: PortableDeviceCollection<reco::TrackLayout<TrackerTraits>, TDev>(S, queue) {}
27+
};
28+
29+
namespace pixelTrack {
30+
31+
template <typename TDev>
32+
using TracksDevicePhase1 = TracksDevice<pixelTopology::Phase1, TDev>;
33+
template <typename TDev>
34+
using TracksDevicePhase2 = TracksDevice<pixelTopology::Phase2, TDev>;
35+
36+
} // namespace pixelTrack
37+
38+
#endif // DataFormats_Track_TracksDevice_H
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef DataFormats_Track_TracksHost_H
2+
#define DataFormats_Track_TracksHost_H
3+
4+
#include <cstdint>
5+
#include <alpaka/alpaka.hpp>
6+
#include "Geometry/CommonTopologies/interface/SimplePixelTopology.h"
7+
#include "DataFormats/TrackSoA/interface/TracksSoA.h"
8+
#include "DataFormats/TrackSoA/interface/TrackDefinitions.h"
9+
#include "DataFormats/Portable/interface/PortableHostCollection.h"
10+
11+
// TODO: The class is created via inheritance of the PortableHostCollection.
12+
// This is generally discouraged, and should be done via composition.
13+
// See: https://github.com/cms-sw/cmssw/pull/40465#discussion_r1067364306
14+
template <typename TrackerTraits>
15+
class TracksHost : public PortableHostCollection<reco::TrackLayout<TrackerTraits>> {
16+
public:
17+
static constexpr int32_t S = TrackerTraits::maxNumberOfTuples; //TODO: this could be made configurable at runtime
18+
TracksHost() = default; // Needed for the dictionary; not sure if line above is needed anymore
19+
20+
using PortableHostCollection<reco::TrackLayout<TrackerTraits>>::view;
21+
using PortableHostCollection<reco::TrackLayout<TrackerTraits>>::const_view;
22+
using PortableHostCollection<reco::TrackLayout<TrackerTraits>>::buffer;
23+
24+
// Constructor which specifies the SoA size
25+
template <typename TQueue>
26+
explicit TracksHost<TrackerTraits>(TQueue& queue)
27+
: PortableHostCollection<reco::TrackLayout<TrackerTraits>>(S, queue) {}
28+
29+
// Constructor which specifies the DevHost
30+
explicit TracksHost(alpaka_common::DevHost const& host)
31+
: PortableHostCollection<reco::TrackLayout<TrackerTraits>>(S, host) {}
32+
};
33+
34+
namespace pixelTrack {
35+
36+
using TracksHostPhase1 = TracksHost<pixelTopology::Phase1>;
37+
using TracksHostPhase2 = TracksHost<pixelTopology::Phase2>;
38+
using TracksHostHIonPhase1 = TracksHost<pixelTopology::HIonPhase1>;
39+
40+
} // namespace pixelTrack
41+
42+
#endif // DataFormats_Track_TracksHost_H
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef DataFormats_Track_interface_TrackLayout_h
2+
#define DataFormats_Track_interface_TrackLayout_h
3+
4+
#include <Eigen/Core>
5+
#include "HeterogeneousCore/AlpakaInterface/interface/OneToManyAssoc.h"
6+
#include "Geometry/CommonTopologies/interface/SimplePixelTopology.h"
7+
#include "DataFormats/SoATemplate/interface/SoALayout.h"
8+
#include "DataFormats/TrackSoA/interface/TrackDefinitions.h"
9+
10+
namespace reco {
11+
12+
template <typename TrackerTraits>
13+
struct TrackSoA {
14+
static constexpr int32_t S = TrackerTraits::maxNumberOfTuples;
15+
static constexpr int32_t H = TrackerTraits::avgHitsPerTrack;
16+
// Aliases in order to not confuse the GENERATE_SOA_LAYOUT
17+
// macro with weird colons and angled brackets.
18+
using Vector5f = Eigen::Matrix<float, 5, 1>;
19+
using Vector15f = Eigen::Matrix<float, 15, 1>;
20+
using Quality = pixelTrack::Quality;
21+
22+
using hindex_type = uint32_t;
23+
24+
using HitContainer = cms::alpakatools::OneToManyAssocSequential<hindex_type, S + 1, H * S>;
25+
26+
GENERATE_SOA_LAYOUT(Layout,
27+
SOA_COLUMN(Quality, quality),
28+
SOA_COLUMN(float, chi2),
29+
SOA_COLUMN(int8_t, nLayers),
30+
SOA_COLUMN(float, eta),
31+
SOA_COLUMN(float, pt),
32+
SOA_EIGEN_COLUMN(Vector5f, state),
33+
SOA_EIGEN_COLUMN(Vector15f, covariance),
34+
SOA_SCALAR(int, nTracks),
35+
SOA_SCALAR(HitContainer, hitIndices),
36+
SOA_SCALAR(HitContainer, detIndices))
37+
};
38+
39+
template <typename TrackerTraits>
40+
using TrackLayout = typename reco::TrackSoA<TrackerTraits>::template Layout<>;
41+
template <typename TrackerTraits>
42+
using TrackSoAView = typename reco::TrackSoA<TrackerTraits>::template Layout<>::View;
43+
template <typename TrackerTraits>
44+
using TrackSoAConstView = typename reco::TrackSoA<TrackerTraits>::template Layout<>::ConstView;
45+
46+
template <typename TrackerTraits>
47+
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static constexpr float charge(const TrackSoAConstView<TrackerTraits> &tracks,
48+
int32_t i) {
49+
//was: std::copysign(1.f, tracks[i].state()(2)). Will be constexpr with C++23
50+
float v = tracks[i].state()(2);
51+
return float((0.0f < v) - (v < 0.0f));
52+
}
53+
54+
} // namespace reco
55+
56+
#endif

0 commit comments

Comments
 (0)