Skip to content

Commit

Permalink
ITSGPU: Hybrid track fitting (#11787)
Browse files Browse the repository at this point in the history
* Add hybrid tracking approach for GPU processing

Add Hybrid to bring in CPU procesing on GPU traits

* Add up to track seeds to GPU

and debugging external allocator

* Add .cxx to static compilation

* Add External allocator, remove GPUTracking dep

Showcase issues with addition of header

* Add debug flag to be removed

* Temporarily remove Propagator

* Add fast fitting on GPU

* Explicit seletion for GPUtrackerspec

* Do external allocator only if GPU

* Remove unused cluster info in seed builder

* Finalise trackFinding pipeline

Make gpuchain method protected again

Add custom getPredictedChi2Unchecked

* Fix HIP execution
  • Loading branch information
mconcas authored Oct 28, 2023
1 parent 6487bc8 commit 5e8bfe8
Show file tree
Hide file tree
Showing 34 changed files with 1,062 additions and 221 deletions.
49 changes: 25 additions & 24 deletions DataFormats/Detectors/ITSMFT/ITS/include/DataFormatsITS/TrackITS.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,23 @@ class TrackITS : public o2::track::TrackParCov
using o2::track::TrackParCov::TrackParCov; // inherit base constructors
static constexpr int MaxClusters = 16;

GPUdDefault() TrackITS() = default;
GPUdDefault() TrackITS(const TrackITS& t) = default;
GPUd() TrackITS(const o2::track::TrackParCov& parcov) : o2::track::TrackParCov{parcov} {}
GPUd() TrackITS(const o2::track::TrackParCov& parCov, float chi2, const o2::track::TrackParCov& outer)
GPUhdDefault() TrackITS() = default;
GPUhdDefault() TrackITS(const TrackITS& t) = default;
GPUhd() TrackITS(const o2::track::TrackParCov& parcov) : o2::track::TrackParCov{parcov} {}
GPUhd() TrackITS(const o2::track::TrackParCov& parCov, float chi2, const o2::track::TrackParCov& outer)
: o2::track::TrackParCov{parCov}, mParamOut{outer}, mChi2{chi2} {}
GPUdDefault() TrackITS& operator=(const TrackITS& tr) = default;
GPUdDefault() ~TrackITS() = default;
GPUhdDefault() TrackITS& operator=(const TrackITS& tr) = default;
GPUhdDefault() TrackITS& operator=(TrackITS&& tr) = default;
GPUhdDefault() ~TrackITS() = default;

// These functions must be provided
bool propagate(float alpha, float x, float bz);
bool update(const Cluster& c, float chi2);

// Other functions
float getChi2() const { return mChi2; }
int getNClusters() const { return mClusRef.getEntries(); }
int getNumberOfClusters() const { return getNClusters(); }
GPUhdi() float getChi2() const { return mChi2; }
GPUhdi() int getNClusters() const { return mClusRef.getEntries(); }
GPUhdi() int getNumberOfClusters() const { return getNClusters(); }
int getFirstClusterEntry() const { return mClusRef.getFirstEntry(); }
int getClusterEntry(int i) const { return getFirstClusterEntry() + i; }
void shiftFirstClusterEntry(int bias)
Expand All @@ -85,20 +86,20 @@ class TrackITS : public o2::track::TrackParCov
}

const ClusRefs& getClusterRefs() const { return mClusRef; }
ClusRefs& getClusterRefs() { return mClusRef; }
GPUhdi() ClusRefs& getClusterRefs() { return mClusRef; }

void setChi2(float chi2) { mChi2 = chi2; }
GPUhdi() void setChi2(float chi2) { mChi2 = chi2; }

bool isBetter(const TrackITS& best, float maxChi2) const;

o2::track::TrackParCov& getParamIn() { return *this; }
const o2::track::TrackParCov& getParamIn() const { return *this; }

o2::track::TrackParCov& getParamOut() { return mParamOut; }
const o2::track::TrackParCov& getParamOut() const { return mParamOut; }
GPUhdi() o2::track::TrackParCov& getParamOut() { return mParamOut; }
GPUhdi() const o2::track::TrackParCov& getParamOut() const { return mParamOut; }

void setPattern(uint32_t p) { mPattern = p; }
uint32_t getPattern() const { return mPattern; }
GPUhdi() void setPattern(uint32_t p) { mPattern = p; }
GPUhdi() uint32_t getPattern() const { return mPattern; }
bool hasHitOnLayer(int i) const { return mPattern & (0x1 << i); }
bool isFakeOnLayer(int i) const { return !(mPattern & (0x1 << (16 + i))); }
uint32_t getLastClusterLayer() const
Expand Down Expand Up @@ -164,21 +165,21 @@ class TrackITSExt : public TrackITS
static constexpr int MaxClusters = 16; /// Prepare for overlaps and new detector configurations
using TrackITS::TrackITS; // inherit base constructors

GPUd() TrackITSExt(o2::track::TrackParCov&& parCov, short ncl, float chi2,
o2::track::TrackParCov&& outer, std::array<int, MaxClusters> cls)
GPUh() TrackITSExt(o2::track::TrackParCov&& parCov, short ncl, float chi2,
o2::track::TrackParCov&& outer, o2::gpu::gpustd::array<int, MaxClusters> cls)
: TrackITS(parCov, chi2, outer), mIndex{cls}
{
setNumberOfClusters(ncl);
}

GPUd() TrackITSExt(o2::track::TrackParCov& parCov, short ncl, float chi2, std::uint32_t rof,
o2::track::TrackParCov& outer, std::array<int, MaxClusters> cls)
GPUh() TrackITSExt(o2::track::TrackParCov& parCov, short ncl, float chi2, std::uint32_t rof,
o2::track::TrackParCov& outer, o2::gpu::gpustd::array<int, MaxClusters> cls)
: TrackITS(parCov, chi2, outer), mIndex{cls}
{
setNumberOfClusters(ncl);
}

GPUdDefault() TrackITSExt(const TrackITSExt& t) = default;
GPUhdDefault() TrackITSExt(const TrackITSExt& t) = default;

void setClusterIndex(int l, int i)
{
Expand All @@ -187,9 +188,9 @@ class TrackITSExt : public TrackITS
getClusterRefs().setEntries(ncl);
}

int getClusterIndex(int lr) const { return mIndex[lr]; }
GPUhdi() const int& getClusterIndex(int lr) const { return mIndex[lr]; }

void setExternalClusterIndex(int layer, int idx, bool newCluster = false)
GPUhdi() void setExternalClusterIndex(int layer, int idx, bool newCluster = false)
{
if (newCluster) {
getClusterRefs().setEntries(getNumberOfClusters() + 1);
Expand All @@ -200,13 +201,13 @@ class TrackITSExt : public TrackITS
mIndex[layer] = idx;
}

std::array<int, MaxClusters>& getClusterIndexes()
GPUh() o2::gpu::gpustd::array<int, MaxClusters>& getClusterIndexes()
{
return mIndex;
}

private:
std::array<int, MaxClusters> mIndex = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; ///< Indices of associated clusters
o2::gpu::gpustd::array<int, MaxClusters> mIndex = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; ///< Indices of associated clusters
ClassDefNV(TrackITSExt, 2);
};
} // namespace its
Expand Down
48 changes: 24 additions & 24 deletions DataFormats/Detectors/ITSMFT/ITS/src/TrackITS.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ bool TrackITS::operator<(const TrackITS& o) const
//-----------------------------------------------------------------
// This function compares tracks according to the their curvature
//-----------------------------------------------------------------
Float_t co = TMath::Abs(o.getPt());
Float_t c = TMath::Abs(getPt());
// Float_t co=o.GetSigmaY2()*o.GetSigmaZ2();
// Float_t c =GetSigmaY2()*GetSigmaZ2();
float co = TMath::Abs(o.getPt());
float c = TMath::Abs(getPt());
// float co=o.GetSigmaY2()*o.GetSigmaZ2();
// float c =GetSigmaY2()*GetSigmaZ2();

return (c > co);
}

void TrackITS::getImpactParams(Float_t x, Float_t y, Float_t z, Float_t bz, Float_t ip[2]) const
void TrackITS::getImpactParams(float x, float y, float z, float bz, float ip[2]) const
{
//------------------------------------------------------------------
// This function calculates the transverse and longitudinal impact parameters
// with respect to a point with global coordinates (x,y,0)
// in the magnetic field "bz" (kG)
//------------------------------------------------------------------
Float_t f1 = getSnp(), r1 = TMath::Sqrt((1. - f1) * (1. + f1));
Float_t xt = getX(), yt = getY();
Float_t sn = TMath::Sin(getAlpha()), cs = TMath::Cos(getAlpha());
Float_t a = x * cs + y * sn;
float f1 = getSnp(), r1 = TMath::Sqrt((1. - f1) * (1. + f1));
float xt = getX(), yt = getY();
float sn = TMath::Sin(getAlpha()), cs = TMath::Cos(getAlpha());
float a = x * cs + y * sn;
y = -x * sn + y * cs;
x = a;
xt -= x;
yt -= y;

Float_t rp4 = getCurvature(bz);
float rp4 = getCurvature(bz);
if ((TMath::Abs(bz) < Almost0) || (TMath::Abs(rp4) < Almost0)) {
ip[0] = -(xt * f1 - yt * r1);
ip[1] = getZ() + (ip[0] * f1 - xt) / r1 * getTgl() - z;
Expand All @@ -63,48 +63,48 @@ void TrackITS::getImpactParams(Float_t x, Float_t y, Float_t z, Float_t bz, Floa
sn = rp4 * xt - f1;
cs = rp4 * yt + r1;
a = 2 * (xt * f1 - yt * r1) - rp4 * (xt * xt + yt * yt);
Float_t rr = TMath::Sqrt(sn * sn + cs * cs);
float rr = TMath::Sqrt(sn * sn + cs * cs);
ip[0] = -a / (1 + rr);
Float_t f2 = -sn / rr, r2 = TMath::Sqrt((1. - f2) * (1. + f2));
float f2 = -sn / rr, r2 = TMath::Sqrt((1. - f2) * (1. + f2));
ip[1] = getZ() + getTgl() / rp4 * TMath::ASin(f2 * r1 - f1 * r2) - z;
}

Bool_t TrackITS::propagate(Float_t alpha, Float_t x, Float_t bz)
bool TrackITS::propagate(float alpha, float x, float bz)
{
if (rotate(alpha)) {
if (propagateTo(x, bz)) {
return kTRUE;
return true;
}
}
return kFALSE;
return false;
}

Bool_t TrackITS::update(const Cluster& c, Float_t chi2)
bool TrackITS::update(const Cluster& c, float chi2)
{
//--------------------------------------------------------------------
// Update track params
//--------------------------------------------------------------------
if (!o2::track::TrackParCov::update(static_cast<const o2::BaseCluster<float>&>(c))) {
return kFALSE;
return false;
}
mChi2 += chi2;
return kTRUE;
return true;
}

Bool_t TrackITS::isBetter(const TrackITS& best, Float_t maxChi2) const
bool TrackITS::isBetter(const TrackITS& best, float maxChi2) const
{
Int_t ncl = getNumberOfClusters();
Int_t nclb = best.getNumberOfClusters();
int ncl = getNumberOfClusters();
int nclb = best.getNumberOfClusters();

if (ncl >= nclb) {
Float_t chi2 = getChi2();
float chi2 = getChi2();
if (chi2 < maxChi2) {
if (ncl > nclb || chi2 < best.getChi2()) {
return kTRUE;
return true;
}
}
}
return kFALSE;
return false;
}

int TrackITS::getNFakeClusters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class PID
GPUd() PID(ID id) : mID(id) {}
GPUd() PID(const char* name);
GPUdDefault() PID(const PID& src) = default;
GPUdDefault() PID& operator=(const PID& src) = default;
GPUhdDefault() PID& operator=(const PID& src) = default;

GPUd() ID getID() const { return mID; }
GPUd() operator ID() const { return getID(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ class TrackParametrization
GPUd() TrackParametrization(const dim3_t& xyz, const dim3_t& pxpypz, int charge, bool sectorAlpha = true, const PID pid = PID::Pion);
GPUdDefault() TrackParametrization(const TrackParametrization&) = default;
GPUdDefault() TrackParametrization(TrackParametrization&&) = default;
GPUdDefault() TrackParametrization& operator=(const TrackParametrization& src) = default;
GPUdDefault() TrackParametrization& operator=(TrackParametrization&& src) = default;
GPUhdDefault() TrackParametrization& operator=(const TrackParametrization& src) = default;
GPUhdDefault() TrackParametrization& operator=(TrackParametrization&& src) = default;
GPUdDefault() ~TrackParametrization() = default;

GPUd() void set(value_t x, value_t alpha, const params_t& par, int charge = 1, const PID pid = PID::Pion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class TrackParametrizationWithError : public TrackParametrization<value_T>

GPUdDefault() TrackParametrizationWithError(const TrackParametrizationWithError& src) = default;
GPUdDefault() TrackParametrizationWithError(TrackParametrizationWithError&& src) = default;
GPUdDefault() TrackParametrizationWithError& operator=(const TrackParametrizationWithError& src) = default;
GPUdDefault() TrackParametrizationWithError& operator=(TrackParametrizationWithError&& src) = default;
GPUhdDefault() TrackParametrizationWithError& operator=(const TrackParametrizationWithError& src) = default;
GPUhdDefault() TrackParametrizationWithError& operator=(TrackParametrizationWithError&& src) = default;
GPUdDefault() ~TrackParametrizationWithError() = default;
using TrackParametrization<value_T>::TrackParametrization;

Expand Down Expand Up @@ -84,14 +84,16 @@ class TrackParametrizationWithError : public TrackParametrization<value_T>
#endif

// parameters + covmat manipulation
GPUd() bool testRotate(value_t alpha) const;
GPUd() bool rotate(value_t alpha);
GPUd() bool propagateTo(value_t xk, value_t b);
GPUd() bool propagateTo(value_t xk, const dim3_t& b);
GPUd() bool propagateToDCA(const o2::dataformats::VertexBase& vtx, value_t b, o2::dataformats::DCA* dca = nullptr, value_t maxD = 999.f);
GPUd() void invert();

GPUd() value_t getPredictedChi2(const dim2_t& p, const dim3_t& cov) const;
GPUd() value_t getPredictedChi2Unchecked(const dim2_t& p, const dim3_t& cov) const;
GPUd() value_t getPredictedChi2(const value_t* p, const value_t* cov) const;
GPUd() value_t getPredictedChi2Unchecked(const value_t* p, const value_t* cov) const;

template <typename T>
GPUd() value_t getPredictedChi2(const BaseCluster<T>& p) const;
Expand Down Expand Up @@ -147,7 +149,8 @@ GPUdi() TrackParametrizationWithError<value_T>::TrackParametrizationWithError()
template <typename value_T>
GPUdi() TrackParametrizationWithError<value_T>::TrackParametrizationWithError(value_t x, value_t alpha, const params_t& par,
const covMat_t& cov, int charge, const PID pid)
: TrackParametrization<value_T>{x, alpha, par, charge, pid}
: TrackParametrization<value_T>{
x, alpha, par, charge, pid}
{
// explicit constructor
for (int i = 0; i < kCovMatSize; i++) {
Expand Down Expand Up @@ -315,6 +318,13 @@ GPUdi() auto TrackParametrizationWithError<value_T>::getPredictedChi2(const dim2
return getPredictedChi2(p.data(), cov.data());
}

//______________________________________________
template <typename value_T>
GPUdi() auto TrackParametrizationWithError<value_T>::getPredictedChi2Unchecked(const dim2_t& p, const dim3_t& cov) const -> value_t
{
return getPredictedChi2Unchecked(p.data(), cov.data());
}

//______________________________________________
template <typename value_T>
GPUdi() bool TrackParametrizationWithError<value_T>::update(const dim2_t& p, const dim3_t& cov)
Expand Down
35 changes: 31 additions & 4 deletions DataFormats/Reconstruction/src/TrackParametrizationWithError.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ GPUd() bool TrackParametrizationWithError<value_T>::propagateTo(value_t xk, valu
return true;
}

//______________________________________________________________
template <typename value_T>
GPUd() bool TrackParametrizationWithError<value_T>::testRotate(value_t) const
{
// no ops
return true;
}
//______________________________________________________________
template <typename value_T>
GPUd() bool TrackParametrizationWithError<value_T>::rotate(value_t alpha)
Expand Down Expand Up @@ -729,6 +736,26 @@ GPUd() auto TrackParametrizationWithError<value_T>::getPredictedChi2(const value
return chi2;
}

//______________________________________________
template <typename value_T>
GPUd() auto TrackParametrizationWithError<value_T>::getPredictedChi2Unchecked(const value_t* p, const value_t* cov) const -> value_t
{
// Estimate the chi2 of the space point "p" with the cov. matrix "cov"
auto sdd = static_cast<double>(getSigmaY2()) + static_cast<double>(cov[0]);
auto sdz = static_cast<double>(getSigmaZY()) + static_cast<double>(cov[1]);
auto szz = static_cast<double>(getSigmaZ2()) + static_cast<double>(cov[2]);
auto det = sdd * szz - sdz * sdz;

if (gpu::CAMath::Abs(det) < constants::math::Almost0) {
return constants::math::VeryBig;
}

value_t d = this->getY() - p[0];
value_t z = this->getZ() - p[1];

return (d * (szz * d - sdz * z) + z * (sdd * z - d * sdz)) / det;
}

#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE) // Disable function relying on ROOT SMatrix on GPU

//______________________________________________
Expand All @@ -755,15 +782,15 @@ void TrackParametrizationWithError<value_T>::buildCombinedCovMatrix(const TrackP

//______________________________________________
template <typename value_T>
auto TrackParametrizationWithError<value_T>::getPredictedChi2(const TrackParametrizationWithError<value_T>& rhs) const -> value_t
GPUd() auto TrackParametrizationWithError<value_T>::getPredictedChi2(const TrackParametrizationWithError<value_T>& rhs) const -> value_t
{
MatrixDSym5 cov; // perform matrix operations in double!
return getPredictedChi2(rhs, cov);
}

//______________________________________________
template <typename value_T>
auto TrackParametrizationWithError<value_T>::getPredictedChi2(const TrackParametrizationWithError<value_T>& rhs, MatrixDSym5& covToSet) const -> value_t
GPUd() auto TrackParametrizationWithError<value_T>::getPredictedChi2(const TrackParametrizationWithError<value_T>& rhs, MatrixDSym5& covToSet) const -> value_t
{
// get chi2 wrt other track, which must be defined at the same parameters X,alpha
// Supplied non-initialized covToSet matrix is filled by inverse combined matrix for further use
Expand Down Expand Up @@ -796,7 +823,7 @@ auto TrackParametrizationWithError<value_T>::getPredictedChi2(const TrackParamet

//______________________________________________
template <typename value_T>
bool TrackParametrizationWithError<value_T>::update(const TrackParametrizationWithError<value_T>& rhs, const MatrixDSym5& covInv)
GPUd() bool TrackParametrizationWithError<value_T>::update(const TrackParametrizationWithError<value_T>& rhs, const MatrixDSym5& covInv)
{
// update track with other track, the inverted combined cov matrix should be supplied

Expand Down Expand Up @@ -864,7 +891,7 @@ bool TrackParametrizationWithError<value_T>::update(const TrackParametrizationWi

//______________________________________________
template <typename value_T>
bool TrackParametrizationWithError<value_T>::update(const TrackParametrizationWithError<value_T>& rhs)
GPUd() bool TrackParametrizationWithError<value_T>::update(const TrackParametrizationWithError<value_T>& rhs)
{
// update track with other track
MatrixDSym5 covI; // perform matrix operations in double!
Expand Down
17 changes: 11 additions & 6 deletions Detectors/ITSMFT/ITS/reconstruction/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.

o2_add_library(
ITSReconstruction
SOURCES src/ClustererTask.cxx src/CookedTracker.cxx src/CookedConfigParam.cxx
src/RecoGeomHelper.cxx src/FastMultEstConfig.cxx src/FastMultEst.cxx
PUBLIC_LINK_LIBRARIES O2::ITSBase O2::ITSMFTReconstruction O2::DataFormatsITS
O2::CommonUtils)
o2_add_library(ITSReconstruction
SOURCES src/ClustererTask.cxx
src/CookedTracker.cxx
src/CookedConfigParam.cxx
src/RecoGeomHelper.cxx
src/FastMultEstConfig.cxx
src/FastMultEst.cxx
PUBLIC_LINK_LIBRARIES O2::ITSBase
O2::ITSMFTReconstruction
O2::DataFormatsITS
O2::CommonUtils)

o2_target_root_dictionary(
ITSReconstruction
Expand Down
Loading

0 comments on commit 5e8bfe8

Please sign in to comment.