From 1ca9d903ec0400c5548924d2f44dc514df1cbb60 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Tue, 10 May 2022 10:49:14 +0200 Subject: [PATCH 01/30] Move TimeFrame initialisation to traits --- .../ITS/tracking/include/ITStracking/Tracker.h | 5 +++++ .../include/ITStracking/TrackerTraits.h | 17 +++++++++++++++-- Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx | 6 ++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracker.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracker.h index 067de88839170..7fb0816d180e7 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracker.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracker.h @@ -110,6 +110,11 @@ inline void Tracker::initialiseTimeFrame(int& iteration) mTimeFrame->initialise(iteration, mTrkParams[iteration]); } +inline float Tracker::getBz() const +{ + return mBz; +} + template float Tracker::evaluateTask(void (Tracker::*task)(T...), const char* taskName, std::function logger, T&&... args) diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h index 9605a4d193de2..df71512efbb91 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h @@ -52,6 +52,7 @@ class TrackerTraits { public: virtual ~TrackerTraits() = default; + virtual void initialiseTimeFrame(const int iteration, const MemoryParameters& memParams, const TrackingParameters& trackingParams); virtual void computeLayerTracklets(const int iteration); virtual void computeLayerCells(const int iteration); virtual void findCellsNeighbours(const int iteration); @@ -95,10 +96,16 @@ class TrackerTraits o2::base::PropagatorImpl::MatCorrType mCorrType = o2::base::PropagatorImpl::MatCorrType::USEMatCorrNONE; float mBz = 5.f; + void UpdateTrackingParameters(const TrackingParameters& trkPar); + TimeFrame* getTimeFrame() { return mTimeFrame; } + void adoptTimeFrame(TimeFrame* tf) { mTimeFrame = tf; } + void setIsGPU(const unsigned char isgpu) { mIsGPU = isgpu; }; + protected: TimeFrame* mTimeFrame; std::vector mTrkParams; + bool mIsGPU = false; o2::gpu::GPUChainITS* mChain = nullptr; FuncRunITSTrackFit_t mChainRunITSTrackFit; }; @@ -123,8 +130,14 @@ inline const int4 TrackerTraits::getBinsRect(const Cluster& currentCluster, int return getBinsRect(layerIndex, currentCluster.phi, maxdeltaphi, z1, z2, maxdeltaz); } -inline const int4 TrackerTraits::getBinsRect(const int layerIndex, float phi, float maxdeltaphi, - float z1, float z2, float maxdeltaz) +inline void TrackerTraits::initialiseTimeFrame(const int iteration, const MemoryParameters& memParams, const TrackingParameters& trackingParams) +{ + mTimeFrame->initialise(iteration, memParams, trackingParams, 7); + setIsGPU(false); +} + +inline const int4 TrackerTraits::getBinsRect(const Cluster& currentCluster, const int layerIndex, + const float z1, const float z2, float maxdeltaz, float maxdeltaphi) { const float zRangeMin = o2::gpu::GPUCommonMath::Min(z1, z2) - maxdeltaz; const float phiRangeMin = phi - maxdeltaphi; diff --git a/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx b/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx index b603469d8cb2a..0c191c013104b 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx @@ -99,6 +99,12 @@ void Tracker::clustersToTracks(std::function logger, std::f mNumberOfRuns++; } +template +void Tracker::initialiseTimeFrame(T&&... args) +{ + mTraits->initialiseTimeFrame(std::forward(args)...); +} + void Tracker::computeTracklets(int& iteration) { mTraits->computeLayerTracklets(iteration); From 17595aa374e5164103610744b3cb95992e94881f Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Tue, 10 May 2022 10:59:54 +0200 Subject: [PATCH 02/30] Improve debug prints with HIP --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 2 +- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 47 ++++++++++--------- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 6 --- .../include/ITStracking/VertexerTraits.h | 8 ---- 4 files changed, 25 insertions(+), 38 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 5abf07d4cbd9d..606622fe9e457 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -45,7 +45,7 @@ class TimeFrameGPU : public TimeFrame public: TimeFrameGPU(); ~TimeFrameGPU(); - void loadToDevice(const int maxLayers); + void initialiseDevice(const int maxLayers); void checkBufferSizes(); void initialise(const int iteration, diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 74668d006c4fe..1b395d1f44005 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -40,7 +40,7 @@ GPUh() void gpuThrowOnError() template TimeFrameGPU::TimeFrameGPU() { - getDeviceMemory(); // We don't check if we can store the data in the GPU for the moment, only log it. + getDeviceMemory(); for (int iLayer{0}; iLayer < NLayers; ++iLayer) { // Tracker and vertexer mClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; @@ -76,6 +76,7 @@ TimeFrameGPU::TimeFrameGPU() template float TimeFrameGPU::getDeviceMemory() { + // We don't check if we can store the data in the GPU for the moment, only log it. float totalMemory{0}; totalMemory += NLayers * mConfig.clustersPerLayerCapacity * sizeof(Cluster); totalMemory += NLayers * mConfig.clustersPerLayerCapacity * sizeof(TrackingFrameInfo); @@ -98,32 +99,32 @@ float TimeFrameGPU::getDeviceMemory() totalMemory += 2 * mConfig.nMaxROFs * sizeof(float); totalMemory += mConfig.nMaxROFs * mConfig.maxVerticesCapacity * sizeof(Vertex); - LOGP(debug, "Total requested memory for GPU: {:.2f} MB", totalMemory / MB); - LOGP(debug, "\t- Clusters: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(Cluster) / MB); - LOGP(debug, "\t- Tracking frame info: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(TrackingFrameInfo) / MB); - LOGP(debug, "\t- Cluster external indices: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(int) / MB); - LOGP(debug, "\t- Clusters per ROf: {:.2f} MB", NLayers * mConfig.clustersPerROfCapacity * sizeof(int) / MB); - LOGP(debug, "\t- Tracklets: {:.2f} MB", (NLayers - 1) * mConfig.trackletsCapacity * sizeof(Tracklet) / MB); - LOGP(debug, "\t- N tracklets per cluster: {:.2f} MB", 2 * mConfig.clustersPerLayerCapacity * sizeof(int) / MB); - LOGP(debug, "\t- Index tables: {:.2f} MB", 2 * mConfig.nMaxROFs * (ZBins * PhiBins + 1) * sizeof(int) / MB); - LOGP(debug, "\t- Lines: {:.2f} MB", mConfig.trackletsCapacity * sizeof(Line) / MB); - LOGP(debug, "\t- N found lines: {:.2f} MB", mConfig.clustersPerLayerCapacity * sizeof(int) / MB); - LOGP(debug, "\t- N exclusive-scan found lines: {:.2f} MB", mConfig.clustersPerLayerCapacity * sizeof(int) / MB); - LOGP(debug, "\t- Used tracklets: {:.2f} MB", mConfig.trackletsCapacity * sizeof(unsigned char) / MB); - LOGP(debug, "\t- CUB tmp buffers: {:.2f} MB", mConfig.nMaxROFs * mConfig.tmpCUBBufferSize / MB); - LOGP(debug, "\t- XY centroids: {:.2f} MB", 2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity * sizeof(float) / MB); - LOGP(debug, "\t- Z centroids: {:.2f} MB", mConfig.nMaxROFs * mConfig.maxLinesCapacity * sizeof(float) / MB); - LOGP(debug, "\t- XY histograms: {:.2f} MB", 2 * mConfig.nMaxROFs * mConfig.histConf.nBinsXYZ[0] * sizeof(int) / MB); - LOGP(debug, "\t- Z histograms: {:.2f} MB", mConfig.nMaxROFs * mConfig.histConf.nBinsXYZ[2] * sizeof(int) / MB); - LOGP(debug, "\t- TMP Vertex position bins: {:.2f} MB", 3 * mConfig.nMaxROFs * sizeof(cub::KeyValuePair) / MB); - LOGP(debug, "\t- Beam positions: {:.2f} MB", 2 * mConfig.nMaxROFs * sizeof(float) / MB); - LOGP(debug, "\t- Vertices: {:.2f} MB", mConfig.nMaxROFs * mConfig.maxVerticesCapacity * sizeof(Vertex) / MB); + LOG(info) << fmt::format("Total requested memory for GPU: {:.2f} MB", totalMemory / MB); + LOG(info) << fmt::format("\t- Clusters: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(Cluster) / MB); + LOG(info) << fmt::format("\t- Tracking frame info: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(TrackingFrameInfo) / MB); + LOG(info) << fmt::format("\t- Cluster external indices: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(int) / MB); + LOG(info) << fmt::format("\t- Clusters per ROf: {:.2f} MB", NLayers * mConfig.clustersPerROfCapacity * sizeof(int) / MB); + LOG(info) << fmt::format("\t- Tracklets: {:.2f} MB", (NLayers - 1) * mConfig.trackletsCapacity * sizeof(Tracklet) / MB); + LOG(info) << fmt::format("\t- N tracklets per cluster: {:.2f} MB", 2 * mConfig.clustersPerLayerCapacity * sizeof(int) / MB); + LOG(info) << fmt::format("\t- Index tables: {:.2f} MB", 2 * mConfig.nMaxROFs * (ZBins * PhiBins + 1) * sizeof(int) / MB); + LOG(info) << fmt::format("\t- Lines: {:.2f} MB", mConfig.trackletsCapacity * sizeof(Line) / MB); + LOG(info) << fmt::format("\t- N found lines: {:.2f} MB", mConfig.clustersPerLayerCapacity * sizeof(int) / MB); + LOG(info) << fmt::format("\t- N exclusive-scan found lines: {:.2f} MB", mConfig.clustersPerLayerCapacity * sizeof(int) / MB); + LOG(info) << fmt::format("\t- Used tracklets: {:.2f} MB", mConfig.trackletsCapacity * sizeof(unsigned char) / MB); + LOG(info) << fmt::format("\t- CUB tmp buffers: {:.2f} MB", mConfig.nMaxROFs * mConfig.tmpCUBBufferSize / MB); + LOG(info) << fmt::format("\t- XY centroids: {:.2f} MB", 2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity * sizeof(float) / MB); + LOG(info) << fmt::format("\t- Z centroids: {:.2f} MB", mConfig.nMaxROFs * mConfig.maxLinesCapacity * sizeof(float) / MB); + LOG(info) << fmt::format("\t- XY histograms: {:.2f} MB", 2 * mConfig.nMaxROFs * mConfig.histConf.nBinsXYZ[0] * sizeof(int) / MB); + LOG(info) << fmt::format("\t- Z histograms: {:.2f} MB", mConfig.nMaxROFs * mConfig.histConf.nBinsXYZ[2] * sizeof(int) / MB); + LOG(info) << fmt::format("\t- TMP Vertex position bins: {:.2f} MB", 3 * mConfig.nMaxROFs * sizeof(cub::KeyValuePair) / MB); + LOG(info) << fmt::format("\t- Beam positions: {:.2f} MB", 2 * mConfig.nMaxROFs * sizeof(float) / MB); + LOG(info) << fmt::format("\t- Vertices: {:.2f} MB", mConfig.nMaxROFs * mConfig.maxVerticesCapacity * sizeof(Vertex) / MB); return totalMemory; } template -void TimeFrameGPU::loadToDevice(const int maxLayers) +void TimeFrameGPU::initialiseDevice(const int maxLayers) { for (int iLayer{0}; iLayer < maxLayers; ++iLayer) { mClustersD[iLayer].reset(mClusters[iLayer].data(), static_cast(mClusters[iLayer].size())); @@ -149,7 +150,7 @@ void TimeFrameGPU::initialise(const int iteration, { o2::its::TimeFrame::initialise(iteration, trkParam, maxLayers); checkBufferSizes(); - loadToDevice(maxLayers); + initialiseDevice(maxLayers); } template diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index a04e51dfcd69f..97197e0439c61 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -57,12 +57,6 @@ GPUd() const int4 getBinsRect(const Cluster& currentCluster, const int layerInde getPhiBinIndex(phiRangeMax)}; } -// template -// void TrackerTraitsGPU::loadToDevice() -// { -// mTimeFrameGPU.loadToDevice(); -// } - namespace gpu { diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h index 56a56115e78be..690e7fb5e0208 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h @@ -96,14 +96,6 @@ class VertexerTraits virtual void initialise(const TrackingParameters& trackingParams); virtual void computeTracklets(); virtual void computeTrackletMatching(); - // virtual void computeMCFiltering(); - // virtual void filterTrackletsWithMC(std::vector&, - // std::vector&, - // std::vector&, - // std::vector&, - // const int); - - // virtual void computeTrackletsPureMontecarlo(); virtual void computeVertices(); // virtual void computeHistVertices(); From ad25ab2866245761f856c99de62c3e0e59c5b76c Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Thu, 12 May 2022 20:48:04 +0200 Subject: [PATCH 03/30] Add timeframeGPU init in tracker Cleanup code --- .../include/MathUtils/CartesianGPU.h | 4 +- .../ReconstructionDataFormats/Vertex.h | 4 +- .../GPU/ITStrackingGPU/TrackerTraitsGPU.h | 32 +- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 516 ++++++------------ .../include/ITStracking/TrackerTraits.h | 6 +- .../include/ITStracking/VertexerTraits.h | 3 +- 6 files changed, 221 insertions(+), 344 deletions(-) diff --git a/Common/MathUtils/include/MathUtils/CartesianGPU.h b/Common/MathUtils/include/MathUtils/CartesianGPU.h index 99ae8bb4b85aa..905f2f660f9c0 100644 --- a/Common/MathUtils/include/MathUtils/CartesianGPU.h +++ b/Common/MathUtils/include/MathUtils/CartesianGPU.h @@ -25,7 +25,7 @@ namespace detail template struct GPUPoint2D { GPUdDefault() GPUPoint2D() = default; - GPUd() GPUPoint2D(T a, T b) : xx(a), yy(b) {} + GPUhd() GPUPoint2D(T a, T b) : xx(a), yy(b) {} GPUhd() float X() const { return xx; } GPUhd() float Y() const { return yy; } GPUd() float R() const { return o2::gpu::CAMath::Sqrt(xx * xx + yy * yy); } @@ -38,7 +38,7 @@ struct GPUPoint2D { template struct GPUPoint3D : public GPUPoint2D { GPUdDefault() GPUPoint3D() = default; - GPUd() GPUPoint3D(T a, T b, T c) : GPUPoint2D(a, b), zz(c) {} + GPUhd() GPUPoint3D(T a, T b, T c) : GPUPoint2D(a, b), zz(c) {} GPUhd() float Z() const { return zz; } GPUd() float R() const { return o2::gpu::CAMath::Sqrt(GPUPoint2D::xx * GPUPoint2D::xx + GPUPoint2D::yy * GPUPoint2D::yy + zz * zz); } GPUd() void SetZ(float v) { zz = v; } diff --git a/DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h b/DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h index a1663a256612c..da68048420f4d 100644 --- a/DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h +++ b/DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h @@ -42,7 +42,7 @@ class VertexBase static constexpr int kNCov = 6; GPUhdDefault() VertexBase() = default; GPUhdDefault() ~VertexBase() = default; - GPUd() VertexBase(const math_utils::Point3D& pos, const gpu::gpustd::array& cov) : mPos(pos), mCov(cov) + GPUhd() VertexBase(const math_utils::Point3D& pos, const gpu::gpustd::array& cov) : mPos(pos), mCov(cov) { } @@ -121,7 +121,7 @@ class Vertex : public VertexBase GPUhdDefault() Vertex() = default; GPUhdDefault() ~Vertex() = default; - GPUd() Vertex(const math_utils::Point3D& pos, const gpu::gpustd::array& cov, ushort nCont, float chi2) + GPUhd() Vertex(const math_utils::Point3D& pos, const gpu::gpustd::array& cov, ushort nCont, float chi2) : VertexBase(pos, cov), mChi2(chi2), mNContributors(nCont) { } diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h index 01c5675115621..eaecd650c46b7 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h @@ -25,8 +25,25 @@ namespace its namespace gpu { template -struct StaticTrackingParameters; -} +struct StaticTrackingParameters { + // StaticTrackingParameters& operator=(const StaticTrackingParameters& t); + // int CellMinimumLevel(); + /// General parameters + int ClusterSharing = 0; + int MinTrackLength = NLayers; + /// Trackleting cuts + float TrackletMaxDeltaPhi = 0.3f; + float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; + /// Cell finding cuts + // float CellMaxDeltaTanLambda = 0.025f; + // float CellMaxDCA[NLayers - 2] = {0.05f, 0.04f, 0.05f, 0.2f, 0.4f}; + // float CellMaxDeltaPhi = 0.14f; + // float CellMaxDeltaZ[NLayers - 2] = {0.2f, 0.4f, 0.5f, 0.6f, 3.0f}; + // /// Neighbour finding cuts + // float NeighbourMaxDeltaCurvature[NLayers - 3] = {0.008f, 0.0025f, 0.003f, 0.0035f}; + // float NeighbourMaxDeltaN[NLayers - 3] = {0.002f, 0.0090f, 0.002f, 0.005f}; +}; +} // namespace gpu template class TrackerTraitsGPU : public TrackerTraits @@ -36,13 +53,22 @@ class TrackerTraitsGPU : public TrackerTraits ~TrackerTraitsGPU() override = default; // void computeLayerCells() final; + void adoptTimeFrame(TimeFrame* tf) override; + void initialiseTimeFrame(const int iteration, const TrackingParameters& trackingParams) override; void computeLayerTracklets(const int iteration) final; + void computeLayerCells(const int iteration) override; // void refitTracks(const std::vector>& tf, std::vector& tracks) override; private: - gpu::TimeFrameGPU mTimeFrameGPU; + gpu::TimeFrameGPU<7>* mTimeFrameGPU; gpu::StaticTrackingParameters* mStaticTrkPars; }; + +template +inline void TrackerTraitsGPU::adoptTimeFrame(TimeFrame* tf) +{ + mTimeFrameGPU = static_cast*>(tf); +} } // namespace its } // namespace o2 diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 97197e0439c61..1b7cc95d6052a 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -14,20 +14,12 @@ #include #include -// #ifndef GPUCA_GPUCODE_GENRTC -// #include -// #include "cub/cub.cuh" -// #endif - #include "ITStracking/Constants.h" #include "ITStracking/Configuration.h" #include "ITStracking/IndexTableUtils.h" #include "ITStracking/MathUtils.h" -#include "ITStracking/TimeFrame.h" -#include "ITStrackingGPU/Context.h" #include "ITStrackingGPU/Stream.h" -#include "ITStrackingGPU/Vector.h" #include "ITStrackingGPU/TrackerTraitsGPU.h" namespace o2 @@ -37,126 +29,62 @@ namespace its using gpu::utils::host::checkGPUError; using namespace constants::its2; -GPUd() const int4 getBinsRect(const Cluster& currentCluster, const int layerIndex, - const float z1, const float z2, float maxdeltaz, float maxdeltaphi) -{ - const float zRangeMin = o2::gpu::GPUCommonMath::Min(z1, z2) - maxdeltaz; - const float phiRangeMin = currentCluster.phi - maxdeltaphi; - const float zRangeMax = o2::gpu::GPUCommonMath::Max(z1, z2) + maxdeltaz; - const float phiRangeMax = currentCluster.phi + maxdeltaphi; - - if (zRangeMax < -LayersZCoordinate()[layerIndex + 1] || - zRangeMin > LayersZCoordinate()[layerIndex + 1] || zRangeMin > zRangeMax) { - - return getEmptyBinsRect(); - } - - return int4{o2::gpu::GPUCommonMath::Max(0, getZBinIndex(layerIndex + 1, zRangeMin)), - getPhiBinIndex(phiRangeMin), - o2::gpu::GPUCommonMath::Min(ZBins - 1, getZBinIndex(layerIndex + 1, zRangeMax)), - getPhiBinIndex(phiRangeMax)}; -} - namespace gpu { - -template -struct StaticTrackingParameters { - // StaticTrackingParameters& operator=(const StaticTrackingParameters& t); - // int CellMinimumLevel(); - /// General parameters - int ClusterSharing = 0; - int MinTrackLength = NLayers; - /// Trackleting cuts - float TrackletMaxDeltaPhi = 0.3f; - float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; - /// Cell finding cuts - // float CellMaxDeltaTanLambda = 0.025f; - // float CellMaxDCA[NLayers - 2] = {0.05f, 0.04f, 0.05f, 0.2f, 0.4f}; - // float CellMaxDeltaPhi = 0.14f; - // float CellMaxDeltaZ[NLayers - 2] = {0.2f, 0.4f, 0.5f, 0.6f, 3.0f}; - // /// Neighbour finding cuts - // float NeighbourMaxDeltaCurvature[NLayers - 3] = {0.008f, 0.0025f, 0.003f, 0.0035f}; - // float NeighbourMaxDeltaN[NLayers - 3] = {0.002f, 0.0090f, 0.002f, 0.005f}; -}; - -template struct gpu::StaticTrackingParameters<7>; __constant__ StaticTrackingParameters<7> kTrkPar; -// GPUd() void computeLayerTracklets(DeviceStoreNV& devStore, const int layerIndex, -// Vector& trackletsVector) -// { -// const int currentClusterIndex = static_cast(blockDim.x * blockIdx.x + threadIdx.x); -// int clusterTrackletsNum = 0; - -// if (currentClusterIndex < devStore.getClusters()[layerIndex].size()) { - -// Vector nextLayerClusters{devStore.getClusters()[layerIndex + 1].getWeakCopy()}; -// const Cluster currentCluster{devStore.getClusters()[layerIndex][currentClusterIndex]}; - -// /*if (mUsedClustersTable[currentCluster.clusterId] != constants::its::UnusedIndex) { - -// continue; -// }*/ - -// const float tanLambda{(currentCluster.zCoordinate - devStore.getPrimaryVertex().z) / currentCluster.radius}; -// const float zAtRmin{tanLambda * (devStore.getRmin(layerIndex + 1) - currentCluster.radius) + currentCluster.zCoordinate}; -// const float zAtRmax{tanLambda * (devStore.getRmax(layerIndex + 1) - currentCluster.radius) + currentCluster.zCoordinate}; - -// const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, zAtRmin, zAtRmax, -// kTrkPar.TrackletMaxDeltaZ[layerIndex], kTrkPar.TrackletMaxDeltaPhi)}; - -// if (selectedBinsRect.x != 0 || selectedBinsRect.y != 0 || selectedBinsRect.z != 0 || selectedBinsRect.w != 0) { - -// const int nextLayerClustersNum{static_cast(nextLayerClusters.size())}; -// int phiBinsNum{selectedBinsRect.w - selectedBinsRect.y + 1}; - -// if (phiBinsNum < 0) { - -// phiBinsNum += constants::its2::PhiBins; -// } - -// for (int iPhiBin{selectedBinsRect.y}, iPhiCount{0}; iPhiCount < phiBinsNum; -// iPhiBin = ++iPhiBin == constants::its2::PhiBins ? 0 : iPhiBin, iPhiCount++) { - -// const int firstBinIndex{constants::its2::getBinIndex(selectedBinsRect.x, iPhiBin)}; -// const int firstRowClusterIndex = devStore.getIndexTables()[layerIndex][firstBinIndex]; -// const int maxRowClusterIndex = devStore.getIndexTables()[layerIndex][{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}]; - -// for (int iNextLayerCluster{firstRowClusterIndex}; -// iNextLayerCluster <= maxRowClusterIndex && iNextLayerCluster < nextLayerClustersNum; ++iNextLayerCluster) { - -// const Cluster& nextCluster{nextLayerClusters[iNextLayerCluster]}; - -// const float deltaZ{o2::gpu::GPUCommonMath::Abs( -// tanLambda * (nextCluster.radius - currentCluster.radius) + currentCluster.zCoordinate - nextCluster.zCoordinate)}; -// const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi)}; - -// if (deltaZ < kTrkPar.TrackletMaxDeltaZ[layerIndex] && (deltaPhi < kTrkPar.TrackletMaxDeltaPhi || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < kTrkPar.TrackletMaxDeltaPhi)) { - -// cooperative_groups::coalesced_group threadGroup = cooperative_groups::coalesced_threads(); -// int currentIndex{}; - -// if (threadGroup.thread_rank() == 0) { - -// currentIndex = trackletsVector.extend(threadGroup.size()); -// } - -// currentIndex = threadGroup.shfl(currentIndex, 0) + threadGroup.thread_rank(); - -// trackletsVector.emplace(currentIndex, currentClusterIndex, iNextLayerCluster, currentCluster, nextCluster); -// ++clusterTrackletsNum; -// } -// } -// } - -// if (layerIndex > 0) { - -// devStore.getTrackletsPerClusterTable()[layerIndex - 1][currentClusterIndex] = clusterTrackletsNum; -// } -// } -// } -// } +GPUg() void computeLayerTracklets(const int layerIndex, + const Cluster* clustersNextLayer, + const Cluster* clustersCurrentLayer, + const int sizeCurrentLClusters, + Tracklet* trackletsVector, + const float meanDeltaR) +{ + int clusterTrackletsNum = 0; + for (int currentClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; currentClusterIndex < sizeCurrentLClusters; currentClusterIndex += blockDim.x * gridDim.x) { + const Cluster currentCluster{clustersCurrentLayer[currentClusterIndex]}; + + // const float tanLambda{(currentCluster.zCoordinate - devStore.getPrimaryVertex().z) / currentCluster.radius}; + // const float zAtRmin{tanLambda * (devStore.getRmin(layerIndex + 1) - currentCluster.radius) + currentCluster.zCoordinate}; + // const float zAtRmax{tanLambda * (devStore.getRmax(layerIndex + 1) - currentCluster.radius) + currentCluster.zCoordinate}; + + // const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, zAtRmin, zAtRmax, + // kTrkPar.TrackletMaxDeltaZ[layerIndex], kTrkPar.TrackletMaxDeltaPhi)}; + // if (selectedBinsRect.x != 0 || selectedBinsRect.y != 0 || selectedBinsRect.z != 0 || selectedBinsRect.w != 0) { + // const int nextLayerClustersNum{static_cast(nextLayerClusters.size())}; + // int phiBinsNum{selectedBinsRect.w - selectedBinsRect.y + 1}; + // if (phiBinsNum < 0) { + // phiBinsNum += constants::its2::PhiBins; + // } + // for (int iPhiBin{selectedBinsRect.y}, iPhiCount{0}; iPhiCount < phiBinsNum; + // iPhiBin = ++iPhiBin == constants::its2::PhiBins ? 0 : iPhiBin, iPhiCount++) { + // const int firstBinIndex{constants::its2::getBinIndex(selectedBinsRect.x, iPhiBin)}; + // const int firstRowClusterIndex = devStore.getIndexTables()[layerIndex][firstBinIndex]; + // const int maxRowClusterIndex = devStore.getIndexTables()[layerIndex][{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}]; + // for (int iNextLayerCluster{firstRowClusterIndex}; + // iNextLayerCluster <= maxRowClusterIndex && iNextLayerCluster < nextLayerClustersNum; ++iNextLayerCluster) { + // const Cluster& nextCluster{nextLayerClusters[iNextLayerCluster]}; + // const float deltaZ{o2::gpu::GPUCommonMath::Abs( + // tanLambda * (nextCluster.radius - currentCluster.radius) + currentCluster.zCoordinate - nextCluster.zCoordinate)}; + // const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi)}; + // if (deltaZ < kTrkPar.TrackletMaxDeltaZ[layerIndex] && (deltaPhi < kTrkPar.TrackletMaxDeltaPhi || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < kTrkPar.TrackletMaxDeltaPhi)) { + // cooperative_groups::coalesced_group threadGroup = cooperative_groups::coalesced_threads(); + // int currentIndex{}; + // if (threadGroup.thread_rank() == 0) { + // currentIndex = trackletsVector.extend(threadGroup.size()); + // } + // currentIndex = threadGroup.shfl(currentIndex, 0) + threadGroup.thread_rank(); + // trackletsVector.emplace(currentIndex, currentClusterIndex, iNextLayerCluster, currentCluster, nextCluster); + // ++clusterTrackletsNum; + // } + // } + // } + // if (layerIndex > 0) { + // devStore.getTrackletsPerClusterTable()[layerIndex - 1][currentClusterIndex] = clusterTrackletsNum; + // } + // } + } +} // GPUd() void computeLayerCells(DeviceStoreNV& devStore, const int layerIndex, // Vector& cellsVector) @@ -164,17 +92,13 @@ __constant__ StaticTrackingParameters<7> kTrkPar; // const int currentTrackletIndex = static_cast(blockDim.x * blockIdx.x + threadIdx.x); // const float3& primaryVertex = devStore.getPrimaryVertex(); // int trackletCellsNum = 0; - // if (currentTrackletIndex < devStore.getTracklets()[layerIndex].size()) { - // const Tracklet& currentTracklet{devStore.getTracklets()[layerIndex][currentTrackletIndex]}; // const int nextLayerClusterIndex{currentTracklet.secondClusterIndex}; // const int nextLayerFirstTrackletIndex{ // devStore.getTrackletsLookupTable()[layerIndex][nextLayerClusterIndex]}; // const int nextLayerTrackletsNum{static_cast(devStore.getTracklets()[layerIndex + 1].size())}; - // if (devStore.getTracklets()[layerIndex + 1][nextLayerFirstTrackletIndex].firstClusterIndex == nextLayerClusterIndex) { - // const Cluster& firstCellCluster{ // devStore.getClusters()[layerIndex][currentTracklet.firstClusterIndex]}; // const Cluster& secondCellCluster{ @@ -183,37 +107,25 @@ __constant__ StaticTrackingParameters<7> kTrkPar; // const float secondCellClusterQuadraticRCoordinate{secondCellCluster.radius * secondCellCluster.radius}; // const float3 firstDeltaVector{secondCellCluster.xCoordinate - firstCellCluster.xCoordinate, // secondCellCluster.yCoordinate - firstCellCluster.yCoordinate, secondCellClusterQuadraticRCoordinate - firstCellClusterQuadraticRCoordinate}; - // for (int iNextLayerTracklet{nextLayerFirstTrackletIndex}; // iNextLayerTracklet < nextLayerTrackletsNum && devStore.getTracklets()[layerIndex + 1][iNextLayerTracklet].firstClusterIndex == nextLayerClusterIndex; ++iNextLayerTracklet) { - // const Tracklet& nextTracklet{devStore.getTracklets()[layerIndex + 1][iNextLayerTracklet]}; // const float deltaTanLambda{o2::gpu::GPUCommonMath::Abs(currentTracklet.tanLambda - nextTracklet.tanLambda)}; // const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentTracklet.phi - nextTracklet.phi)}; - // if (deltaTanLambda < kTrkPar.CellMaxDeltaTanLambda && (deltaPhi < kTrkPar.CellMaxDeltaPhi || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < kTrkPar.CellMaxDeltaPhi)) { - // const float averageTanLambda{0.5f * (currentTracklet.tanLambda + nextTracklet.tanLambda)}; // const float directionZIntersection{-averageTanLambda * firstCellCluster.radius + firstCellCluster.zCoordinate}; // const float deltaZ{o2::gpu::GPUCommonMath::Abs(directionZIntersection - primaryVertex.z)}; - // if (deltaZ < kTrkPar.CellMaxDeltaZ[layerIndex]) { - // const Cluster& thirdCellCluster{ // devStore.getClusters()[layerIndex + 2][nextTracklet.secondClusterIndex]}; - // const float thirdCellClusterQuadraticRCoordinate{thirdCellCluster.radius * thirdCellCluster.radius}; - // const float3 secondDeltaVector{thirdCellCluster.xCoordinate - firstCellCluster.xCoordinate, // thirdCellCluster.yCoordinate - firstCellCluster.yCoordinate, thirdCellClusterQuadraticRCoordinate - firstCellClusterQuadraticRCoordinate}; - // float3 cellPlaneNormalVector{math_utils::crossProduct(firstDeltaVector, secondDeltaVector)}; - // const float vectorNorm{o2::gpu::GPUCommonMath::Sqrt( // cellPlaneNormalVector.x * cellPlaneNormalVector.x + cellPlaneNormalVector.y * cellPlaneNormalVector.y + cellPlaneNormalVector.z * cellPlaneNormalVector.z)}; - // if (!(vectorNorm < constants::math::FloatMinThreshold || o2::gpu::GPUCommonMath::Abs(cellPlaneNormalVector.z) < constants::math::FloatMinThreshold)) { - // const float inverseVectorNorm{1.0f / vectorNorm}; // const float3 normalizedPlaneVector{cellPlaneNormalVector.x * inverseVectorNorm, cellPlaneNormalVector.y * inverseVectorNorm, cellPlaneNormalVector.z * inverseVectorNorm}; // const float planeDistance{-normalizedPlaneVector.x * (secondCellCluster.xCoordinate - primaryVertex.x) - (normalizedPlaneVector.y * secondCellCluster.yCoordinate - primaryVertex.y) - normalizedPlaneVector.z * secondCellClusterQuadraticRCoordinate}; @@ -223,19 +135,13 @@ __constant__ StaticTrackingParameters<7> kTrkPar; // const float2 circleCenter{-0.5f * normalizedPlaneVector.x / normalizedPlaneVector.z, -0.5f * normalizedPlaneVector.y / normalizedPlaneVector.z}; // const float distanceOfClosestApproach{o2::gpu::GPUCommonMath::Abs( // cellTrajectoryRadius - o2::gpu::GPUCommonMath::Sqrt(circleCenter.x * circleCenter.x + circleCenter.y * circleCenter.y))}; - // if (distanceOfClosestApproach <= kTrkPar.CellMaxDCA[layerIndex]) { - // cooperative_groups::coalesced_group threadGroup = cooperative_groups::coalesced_threads(); // int currentIndex{}; - // if (threadGroup.thread_rank() == 0) { - // currentIndex = cellsVector.extend(threadGroup.size()); // } - // currentIndex = threadGroup.shfl(currentIndex, 0) + threadGroup.thread_rank(); - // cellsVector.emplace(currentIndex, currentTracklet.firstClusterIndex, // nextTracklet.firstClusterIndex, nextTracklet.secondClusterIndex, currentTrackletIndex, // iNextLayerTracklet, averageTanLambda); @@ -245,34 +151,23 @@ __constant__ StaticTrackingParameters<7> kTrkPar; // } // } // } - // if (layerIndex > 0) { - // devStore.getCellsPerTrackletTable()[layerIndex - 1][currentTrackletIndex] = trackletCellsNum; // } // } // } // } -// GPUg() void layerTrackletsKernel(DeviceStoreNV& devStore, const int layerIndex, -// Vector trackletsVector) -// { -// computeLayerTracklets(devStore, layerIndex, trackletsVector); -// } - // GPUg() void sortTrackletsKernel(DeviceStoreNV& devStore, const int layerIndex, // Vector tempTrackletArray) // { // const int currentTrackletIndex{static_cast(blockDim.x * blockIdx.x + threadIdx.x)}; - // if (currentTrackletIndex < tempTrackletArray.size()) { - // const int firstClusterIndex = tempTrackletArray[currentTrackletIndex].firstClusterIndex; // const int offset = atomicAdd(&devStore.getTrackletsPerClusterTable()[layerIndex - 1][firstClusterIndex], // -1) - // 1; // const int startIndex = devStore.getTrackletsLookupTable()[layerIndex - 1][firstClusterIndex]; - // memcpy(&devStore.getTracklets()[layerIndex][startIndex + offset], // &tempTrackletArray[currentTrackletIndex], sizeof(Tracklet)); // } @@ -288,15 +183,12 @@ __constant__ StaticTrackingParameters<7> kTrkPar; // Vector tempCellsArray) // { // const int currentCellIndex = static_cast(blockDim.x * blockIdx.x + threadIdx.x); - // if (currentCellIndex < tempCellsArray.size()) { - // const int firstTrackletIndex = tempCellsArray[currentCellIndex].getFirstTrackletIndex(); // const int offset = atomicAdd(&devStore.getCellsPerTrackletTable()[layerIndex - 1][firstTrackletIndex], // -1) - // 1; // const int startIndex = devStore.getCellsLookupTable()[layerIndex - 1][firstTrackletIndex]; - // memcpy(&devStore.getCells()[layerIndex][startIndex + offset], &tempCellsArray[currentCellIndex], // sizeof(Cell)); // } @@ -304,225 +196,181 @@ __constant__ StaticTrackingParameters<7> kTrkPar; } // namespace gpu -// void TrackeTraitsGPU::adoptTimeFrame(TimeFrame* tf) -// { -// mTimeFrameGPU = tf; -// } -// TrackerTraits* createTrackerTraitsGPU() -// { -// return new TrackerTraitsGPU; -// } +template +void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const MemoryParameters& memParams, const TrackingParameters& trackingParams) +{ + mTimeFrameGPU->initialise(iteration, memParams, trackingParams, NLayers); + setIsGPU(true); +} template void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { - // PrimaryVertexContextNV* primaryVertexContext = static_cast(nullptr); //TODO: FIX THIS with Time Frames + checkGPUError(cudaMemcpyToSymbol(gpu::kTrkPar, &mTrkParams, sizeof(gpu::StaticTrackingParameters)), __FILE__, __LINE__); + std::array tempSize; + // std::array trackletsNum; + std::array streamArray; + + // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { + // // primaryVertexContext->getTempTrackletArray()[iLayer].reset( + // // static_cast(primaryVertexContext->getDeviceTracklets()[iLayer + 1].capacity())); + // cub::DeviceScan::ExclusiveSum(static_cast(NULL), tempSize[iLayer], + // primaryVertexContext->getDeviceTrackletsPerClustersTable()[iLayer].get(), + // primaryVertexContext->getDeviceTrackletsLookupTable()[iLayer].get(), + // primaryVertexContext->getClusters()[iLayer + 1].size()); // N clusters layer+1 + // primaryVertexContext->getTempTableArray()[iLayer].reset(static_cast(tempSize[iLayer])); + // } + + // cudaDeviceSynchronize(); + // const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); + // gsl::span diamondSpan(&diamondVert, 1); + for (int rof0{0}; rof0 < mTimeFrameGPU->getNrof(); ++rof0) { + LOGP(info, "Processing rof {}", rof0); + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + // gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0);// replace with GPU one + int minRof = (rof0 >= mTrkParams.DeltaROF) ? rof0 - mTrkParams.DeltaROF : 0; + int maxRof = (rof0 == mTimeFrameGPU->getNrof() - mTrkParams.DeltaROF) ? rof0 : rof0 + mTrkParams.DeltaROF; + const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; + const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; + const float meanDeltaR{mTrkParams.LayerRadii[iLayer + 1] - mTrkParams.LayerRadii[iLayer]}; + if (iLayer == 0) { + if (!mTimeFrameGPU->getClustersOnLayer(rof0, iLayer).size()) { + LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); + continue; + } + // /*, 0, streamArray[iLayer]*/ + // gpu::computeLayerTracklets<<>>(iLayer, + // mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), + // mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), + // mTimeFrameGPU->getNClustersLayer(rof0, iLayer), + // mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), + // meanDeltaR); + } /*else { + gpu::layerTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), + iLayer, primaryVertexContext->getTempTrackletArray()[iLayer - 1].getWeakCopy()); + }*/ + } + } - checkGPUError(cudaMemcpyToSymbol(gpu::kTrkPar, &mTrkParams, sizeof(gpu::StaticTrackingParameters<7>)), __FILE__, __LINE__); - // std::array tempSize; - // std::array trackletsNum; - // std::array streamArray; + // cudaDeviceSynchronize(); // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - // tempSize[iLayer] = 0; - // primaryVertexContext->getTempTrackletArray()[iLayer].reset( - // static_cast(primaryVertexContext->getDeviceTracklets()[iLayer + 1].capacity())); - - // cub::DeviceScan::ExclusiveSum(static_cast(NULL), tempSize[iLayer], + // trackletsNum[iLayer] = primaryVertexContext->getTempTrackletArray()[iLayer].getSizeFromDevice(); + // if (trackletsNum[iLayer] == 0) { + // continue; + // } + // primaryVertexContext->getDeviceTracklets()[iLayer + 1].resize(trackletsNum[iLayer]); + // cub::DeviceScan::ExclusiveSum(static_cast(primaryVertexContext->getTempTableArray()[iLayer].get()), tempSize[iLayer], // primaryVertexContext->getDeviceTrackletsPerClustersTable()[iLayer].get(), // primaryVertexContext->getDeviceTrackletsLookupTable()[iLayer].get(), - // primaryVertexContext->getClusters()[iLayer + 1].size()); + // primaryVertexContext->getClusters()[iLayer + 1].size(), streamArray[iLayer + 1].get()); + // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsNum[iLayer])}; + // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsNum[iLayer])}; + // gpu::sortTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), + // iLayer + 1, primaryVertexContext->getTempTrackletArray()[iLayer].getWeakCopy()); + // cudaError_t error = cudaGetLastError(); + // if (error != cudaSuccess) { + // std::ostringstream errorString{}; + // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" + // << std::endl; + // throw std::runtime_error{errorString.str()}; + // } + // } +} + +template +void TrackerTraitsGPU::computeLayerCells() +{ + // PrimaryVertexContextNV* primaryVertexContext = static_cast(nullptr); //TODO: FIX THIS with Time Frames + // std::array tempSize; + // std::array trackletsNum; + // std::array cellsNum; + // std::array streamArray; + // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad - 1; ++iLayer) { + // tempSize[iLayer] = 0; + // trackletsNum[iLayer] = primaryVertexContext->getDeviceTracklets()[iLayer + 1].getSizeFromDevice(); + // primaryVertexContext->getTempCellArray()[iLayer].reset( + // static_cast(primaryVertexContext->getDeviceCells()[iLayer + 1].capacity())); + // if (trackletsNum[iLayer] == 0) { + // continue; + // } + // cub::DeviceScan::ExclusiveSum(static_cast(NULL), tempSize[iLayer], + // primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), + // primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer]); // primaryVertexContext->getTempTableArray()[iLayer].reset(static_cast(tempSize[iLayer])); // } - // cudaDeviceSynchronize(); - - // for (int iLayer{0}; iLayer < constants::its2::TrackletsPerRoad; ++iLayer) { - + // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { // const gpu::DeviceProperties& deviceProperties = gpu::Context::getInstance().getDeviceProperties(); - // const int clustersNum{static_cast(primaryVertexContext->getClusters()[iLayer].size())}; - // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(clustersNum, 1, 192)}; - // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, clustersNum)}; - + // const int trackletsSize = primaryVertexContext->getDeviceTracklets()[iLayer].getSizeFromDevice(); + // if (trackletsSize == 0) { + // continue; + // } + // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsSize)}; + // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsSize)}; // if (iLayer == 0) { - - // gpu::layerTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), - // iLayer, primaryVertexContext->getDeviceTracklets()[iLayer].getWeakCopy()); - + // gpu::layerCellsKernel<<>>(primaryVertexContext->getDeviceContext(), + // iLayer, primaryVertexContext->getDeviceCells()[iLayer].getWeakCopy()); // } else { - - // gpu::layerTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), - // iLayer, primaryVertexContext->getTempTrackletArray()[iLayer - 1].getWeakCopy()); + // gpu::layerCellsKernel<<>>(primaryVertexContext->getDeviceContext(), + // iLayer, primaryVertexContext->getTempCellArray()[iLayer - 1].getWeakCopy()); // } - // cudaError_t error = cudaGetLastError(); - // if (error != cudaSuccess) { - // std::ostringstream errorString{}; // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" // << std::endl; - // throw std::runtime_error{errorString.str()}; // } // } - // cudaDeviceSynchronize(); - - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - - // trackletsNum[iLayer] = primaryVertexContext->getTempTrackletArray()[iLayer].getSizeFromDevice(); - // if (trackletsNum[iLayer] == 0) { + // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad - 1; ++iLayer) { + // cellsNum[iLayer] = primaryVertexContext->getTempCellArray()[iLayer].getSizeFromDevice(); + // if (cellsNum[iLayer] == 0) { // continue; // } - // primaryVertexContext->getDeviceTracklets()[iLayer + 1].resize(trackletsNum[iLayer]); - + // primaryVertexContext->getDeviceCells()[iLayer + 1].resize(cellsNum[iLayer]); // cub::DeviceScan::ExclusiveSum(static_cast(primaryVertexContext->getTempTableArray()[iLayer].get()), tempSize[iLayer], - // primaryVertexContext->getDeviceTrackletsPerClustersTable()[iLayer].get(), - // primaryVertexContext->getDeviceTrackletsLookupTable()[iLayer].get(), - // primaryVertexContext->getClusters()[iLayer + 1].size(), streamArray[iLayer + 1].get()); - + // primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), + // primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer], + // streamArray[iLayer + 1].get()); // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsNum[iLayer])}; // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsNum[iLayer])}; - - // gpu::sortTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), - // iLayer + 1, primaryVertexContext->getTempTrackletArray()[iLayer].getWeakCopy()); - + // gpu::sortCellsKernel<<>>(primaryVertexContext->getDeviceContext(), + // iLayer + 1, primaryVertexContext->getTempCellArray()[iLayer].getWeakCopy()); // cudaError_t error = cudaGetLastError(); - // if (error != cudaSuccess) { - // std::ostringstream errorString{}; // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" // << std::endl; - // throw std::runtime_error{errorString.str()}; // } // } + // cudaDeviceSynchronize(); + // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { + // int cellsSize = 0; + // if (iLayer == 0) { + // cellsSize = primaryVertexContext->getDeviceCells()[iLayer].getSizeFromDevice(); + // if (cellsSize == 0) { + // continue; + // } + // } else { + // cellsSize = cellsNum[iLayer - 1]; + // if (cellsSize == 0) { + // continue; + // } + // primaryVertexContext->getDeviceCellsLookupTable()[iLayer - 1].copyIntoVector( + // primaryVertexContext->getCellsLookupTable()[iLayer - 1], trackletsNum[iLayer - 1]); + // } + // primaryVertexContext->getDeviceCells()[iLayer].copyIntoVector(primaryVertexContext->getCells()[iLayer], cellsSize); + // } } -// void TrackerTraitsGPU::computeLayerCells() -// { - -// PrimaryVertexContextNV* primaryVertexContext = static_cast(nullptr); //TODO: FIX THIS with Time Frames -// std::array tempSize; -// std::array trackletsNum; -// std::array cellsNum; -// std::array streamArray; - -// for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad - 1; ++iLayer) { - -// tempSize[iLayer] = 0; -// trackletsNum[iLayer] = primaryVertexContext->getDeviceTracklets()[iLayer + 1].getSizeFromDevice(); -// primaryVertexContext->getTempCellArray()[iLayer].reset( -// static_cast(primaryVertexContext->getDeviceCells()[iLayer + 1].capacity())); -// if (trackletsNum[iLayer] == 0) { -// continue; -// } -// cub::DeviceScan::ExclusiveSum(static_cast(NULL), tempSize[iLayer], -// primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), -// primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer]); - -// primaryVertexContext->getTempTableArray()[iLayer].reset(static_cast(tempSize[iLayer])); -// } - -// cudaDeviceSynchronize(); - -// for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { -// const gpu::DeviceProperties& deviceProperties = gpu::Context::getInstance().getDeviceProperties(); -// const int trackletsSize = primaryVertexContext->getDeviceTracklets()[iLayer].getSizeFromDevice(); -// if (trackletsSize == 0) { -// continue; -// } -// dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsSize)}; -// dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsSize)}; - -// if (iLayer == 0) { - -// gpu::layerCellsKernel<<>>(primaryVertexContext->getDeviceContext(), -// iLayer, primaryVertexContext->getDeviceCells()[iLayer].getWeakCopy()); - -// } else { - -// gpu::layerCellsKernel<<>>(primaryVertexContext->getDeviceContext(), -// iLayer, primaryVertexContext->getTempCellArray()[iLayer - 1].getWeakCopy()); -// } - -// cudaError_t error = cudaGetLastError(); - -// if (error != cudaSuccess) { - -// std::ostringstream errorString{}; -// errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" -// << std::endl; - -// throw std::runtime_error{errorString.str()}; -// } -// } - -// cudaDeviceSynchronize(); - -// for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad - 1; ++iLayer) { -// cellsNum[iLayer] = primaryVertexContext->getTempCellArray()[iLayer].getSizeFromDevice(); -// if (cellsNum[iLayer] == 0) { -// continue; -// } -// primaryVertexContext->getDeviceCells()[iLayer + 1].resize(cellsNum[iLayer]); - -// cub::DeviceScan::ExclusiveSum(static_cast(primaryVertexContext->getTempTableArray()[iLayer].get()), tempSize[iLayer], -// primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), -// primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer], -// streamArray[iLayer + 1].get()); - -// dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsNum[iLayer])}; -// dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsNum[iLayer])}; - -// gpu::sortCellsKernel<<>>(primaryVertexContext->getDeviceContext(), -// iLayer + 1, primaryVertexContext->getTempCellArray()[iLayer].getWeakCopy()); - -// cudaError_t error = cudaGetLastError(); - -// if (error != cudaSuccess) { - -// std::ostringstream errorString{}; -// errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" -// << std::endl; - -// throw std::runtime_error{errorString.str()}; -// } -// } - -// cudaDeviceSynchronize(); - -// for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - -// int cellsSize = 0; -// if (iLayer == 0) { - -// cellsSize = primaryVertexContext->getDeviceCells()[iLayer].getSizeFromDevice(); -// if (cellsSize == 0) { -// continue; -// } -// } else { - -// cellsSize = cellsNum[iLayer - 1]; -// if (cellsSize == 0) { -// continue; -// } -// primaryVertexContext->getDeviceCellsLookupTable()[iLayer - 1].copyIntoVector( -// primaryVertexContext->getCellsLookupTable()[iLayer - 1], trackletsNum[iLayer - 1]); -// } - -// primaryVertexContext->getDeviceCells()[iLayer].copyIntoVector(primaryVertexContext->getCells()[iLayer], cellsSize); -// } -// } - // void TrackerTraitsGPU::refitTracks(const std::vector>& tf, std::vector& tracks) // { // PrimaryVertexContextNV* pvctx = static_cast(nullptr); //TODO: FIX THIS with Time Frames - // std::array cells; // for (int iLayer = 0; iLayer < 5; iLayer++) { // cells[iLayer] = pvctx->getDeviceCells()[iLayer].get(); diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h index df71512efbb91..bd78f1136894c 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h @@ -52,7 +52,7 @@ class TrackerTraits { public: virtual ~TrackerTraits() = default; - virtual void initialiseTimeFrame(const int iteration, const MemoryParameters& memParams, const TrackingParameters& trackingParams); + virtual void initialiseTimeFrame(const int iteration, const TrackingParameters& trackingParams); virtual void computeLayerTracklets(const int iteration); virtual void computeLayerCells(const int iteration); virtual void findCellsNeighbours(const int iteration); @@ -98,7 +98,7 @@ class TrackerTraits void UpdateTrackingParameters(const TrackingParameters& trkPar); TimeFrame* getTimeFrame() { return mTimeFrame; } - void adoptTimeFrame(TimeFrame* tf) { mTimeFrame = tf; } + void setIsGPU(const unsigned char isgpu) { mIsGPU = isgpu; }; protected: @@ -136,6 +136,8 @@ inline void TrackerTraits::initialiseTimeFrame(const int iteration, const Memory setIsGPU(false); } +inline void TrackerTraits::adoptTimeFrame(TimeFrame* tf) { mTimeFrame = tf; } + inline const int4 TrackerTraits::getBinsRect(const Cluster& currentCluster, const int layerIndex, const float z1, const float z2, float maxdeltaz, float maxdeltaphi) { diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h index 690e7fb5e0208..358c5746575f4 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h @@ -97,6 +97,7 @@ class VertexerTraits virtual void computeTracklets(); virtual void computeTrackletMatching(); virtual void computeVertices(); + virtual void adoptTimeFrame(TimeFrame* tf); // virtual void computeHistVertices(); void updateVertexingParameters(const VertexingParameters& vrtPar); @@ -107,7 +108,7 @@ class VertexerTraits std::vector getVertices() const { return mVertices; } // utils - virtual void adoptTimeFrame(TimeFrame* tf); + void setIsGPU(const unsigned char isgpu) { mIsGPU = isgpu; }; unsigned char getIsGPU() const { return mIsGPU; }; void dumpVertexerTraits(); From c9ece0b9c009bc70d716c9b13a8f48aa4b8161de Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Fri, 13 May 2022 11:23:23 +0200 Subject: [PATCH 04/30] Remove unused buffer --- Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h | 1 - Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 2 -- 2 files changed, 3 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 606622fe9e457..703ed93d05745 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -88,7 +88,6 @@ class TimeFrameGPU : public TimeFrame std::array, NLayers> mTrackingFrameInfoD; std::array, NLayers - 1> mIndexTablesD; std::array, NLayers> mClusterExternalIndicesD; - std::array, NLayers> mROframesClustersD; std::array, NLayers - 1> mTrackletsD; int* mCUBTmpBuffers; // don't know whether will be used by the tracker diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 1b395d1f44005..40b4b47e29a13 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -46,7 +46,6 @@ TimeFrameGPU::TimeFrameGPU() mClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mTrackingFrameInfoD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mClusterExternalIndicesD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - mROframesClustersD[iLayer] = Vector{mConfig.clustersPerROfCapacity, mConfig.clustersPerROfCapacity}; if (iLayer < NLayers - 1) { mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; @@ -128,7 +127,6 @@ void TimeFrameGPU::initialiseDevice(const int maxLayers) { for (int iLayer{0}; iLayer < maxLayers; ++iLayer) { mClustersD[iLayer].reset(mClusters[iLayer].data(), static_cast(mClusters[iLayer].size())); - mROframesClustersD[iLayer].reset(mROframesClusters[iLayer].data(), static_cast(mROframesClusters[iLayer].size())); } if (maxLayers == NLayers) { // Tracker-only: we don't need to copy data in vertexer From d25fa10730fa39f056b68a37335db76cf6530f8d Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Fri, 13 May 2022 18:56:24 +0200 Subject: [PATCH 05/30] Add used clusters check --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 12 +++++ .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 3 ++ .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 54 ++++++++++++------- .../tracking/include/ITStracking/TimeFrame.h | 3 ++ 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 703ed93d05745..26b21e8a609ef 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -54,6 +54,7 @@ class TimeFrameGPU : public TimeFrame /// Getters float getDeviceMemory(); Cluster* getDeviceClustersOnLayer(const int rofId, const int layerId) const; + unsigned char* getDeviceUsedClustersOnLayer(const int rofId, const int layerId); int getNClustersLayer(const int rofId, const int layerId) const; TimeFrameGPUConfig& getConfig() { return mConfig; } @@ -85,6 +86,7 @@ class TimeFrameGPU : public TimeFrame // Per-layer information, do not expand at runtime std::array, NLayers> mClustersD; + std::array, NLayers> mUsedClustersD; std::array, NLayers> mTrackingFrameInfoD; std::array, NLayers - 1> mIndexTablesD; std::array, NLayers> mClusterExternalIndicesD; @@ -121,6 +123,16 @@ inline Cluster* TimeFrameGPU::getDeviceClustersOnLayer(const int rofId, return mClustersD[layerId].get() + mROframesClusters[layerId][rofId]; } +template +inline unsigned char* TimeFrameGPU::getDeviceUsedClustersOnLayer(const int rofId, const int layerId) +{ + if (rofId < 0 || rofId >= mNrof) { + LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; + return nullptr; + } + return mUsedClustersD[layerId].get() + mROframesClusters[layerId][rofId]; +} + template inline int TimeFrameGPU::getNClustersLayer(const int rofId, const int layerId) const { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 40b4b47e29a13..9bc6ff86e93db 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -44,6 +44,7 @@ TimeFrameGPU::TimeFrameGPU() for (int iLayer{0}; iLayer < NLayers; ++iLayer) { // Tracker and vertexer mClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + mUsedClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mTrackingFrameInfoD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mClusterExternalIndicesD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; if (iLayer < NLayers - 1) { @@ -78,6 +79,7 @@ float TimeFrameGPU::getDeviceMemory() // We don't check if we can store the data in the GPU for the moment, only log it. float totalMemory{0}; totalMemory += NLayers * mConfig.clustersPerLayerCapacity * sizeof(Cluster); + totalMemory += NLayers * mConfig.clustersPerLayerCapacity * sizeof(unsigned char); totalMemory += NLayers * mConfig.clustersPerLayerCapacity * sizeof(TrackingFrameInfo); totalMemory += NLayers * mConfig.clustersPerLayerCapacity * sizeof(int); totalMemory += NLayers * mConfig.clustersPerROfCapacity * sizeof(int); @@ -100,6 +102,7 @@ float TimeFrameGPU::getDeviceMemory() LOG(info) << fmt::format("Total requested memory for GPU: {:.2f} MB", totalMemory / MB); LOG(info) << fmt::format("\t- Clusters: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(Cluster) / MB); + LOG(info) << fmt::format("\t- Used clusters: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(unsigned char) / MB); LOG(info) << fmt::format("\t- Tracking frame info: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(TrackingFrameInfo) / MB); LOG(info) << fmt::format("\t- Cluster external indices: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(int) / MB); LOG(info) << fmt::format("\t- Clusters per ROf: {:.2f} MB", NLayers * mConfig.clustersPerROfCapacity * sizeof(int) / MB); diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 1b7cc95d6052a..7644c70812474 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -33,17 +33,28 @@ namespace gpu { __constant__ StaticTrackingParameters<7> kTrkPar; -GPUg() void computeLayerTracklets(const int layerIndex, - const Cluster* clustersNextLayer, - const Cluster* clustersCurrentLayer, - const int sizeCurrentLClusters, - Tracklet* trackletsVector, - const float meanDeltaR) +GPUg() void computeLayerTrackletsKernel(const int layerIndex, + const Cluster* clustersNextLayer, + const Cluster* clustersCurrentLayer, + unsigned char* usedClusters, + Tracklet* trackletsVector, + const int nVertices, + const int startSortedIndex, + const int sizeCurrentLClusters, + const float minR, + const float maxR, + const float meanDeltaR) { int clusterTrackletsNum = 0; for (int currentClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; currentClusterIndex < sizeCurrentLClusters; currentClusterIndex += blockDim.x * gridDim.x) { - const Cluster currentCluster{clustersCurrentLayer[currentClusterIndex]}; - + const Cluster& currentCluster{clustersCurrentLayer[currentClusterIndex]}; + const int currentSortedIndex{startSortedIndex + currentClusterIndex}; + if (usedClusters[currentSortedIndex]) { + continue; + } + const float inverseR0{1.f / currentCluster.radius}; + for (int iPrimaryVertex{0}; iPrimaryVertex < nVertices; iPrimaryVertex++) { + } // const float tanLambda{(currentCluster.zCoordinate - devStore.getPrimaryVertex().z) / currentCluster.radius}; // const float zAtRmin{tanLambda * (devStore.getRmin(layerIndex + 1) - currentCluster.radius) + currentCluster.zCoordinate}; // const float zAtRmax{tanLambda * (devStore.getRmax(layerIndex + 1) - currentCluster.radius) + currentCluster.zCoordinate}; @@ -164,9 +175,7 @@ GPUg() void computeLayerTracklets(const int layerIndex, // const int currentTrackletIndex{static_cast(blockDim.x * blockIdx.x + threadIdx.x)}; // if (currentTrackletIndex < tempTrackletArray.size()) { // const int firstClusterIndex = tempTrackletArray[currentTrackletIndex].firstClusterIndex; -// const int offset = atomicAdd(&devStore.getTrackletsPerClusterTable()[layerIndex - 1][firstClusterIndex], -// -1) - -// 1; +// const int offset = atomicAdd(&devStore.getTrackletsPerClusterTable()[layerIndex - 1][firstClusterIndex], -1) - 1; // const int startIndex = devStore.getTrackletsLookupTable()[layerIndex - 1][firstClusterIndex]; // memcpy(&devStore.getTracklets()[layerIndex][startIndex + offset], // &tempTrackletArray[currentTrackletIndex], sizeof(Tracklet)); @@ -225,7 +234,6 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) // const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); // gsl::span diamondSpan(&diamondVert, 1); for (int rof0{0}; rof0 < mTimeFrameGPU->getNrof(); ++rof0) { - LOGP(info, "Processing rof {}", rof0); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0);// replace with GPU one int minRof = (rof0 >= mTrkParams.DeltaROF) ? rof0 - mTrkParams.DeltaROF : 0; @@ -233,18 +241,24 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const float meanDeltaR{mTrkParams.LayerRadii[iLayer + 1] - mTrkParams.LayerRadii[iLayer]}; - if (iLayer == 0) { + if (!iLayer) { if (!mTimeFrameGPU->getClustersOnLayer(rof0, iLayer).size()) { LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); continue; } - // /*, 0, streamArray[iLayer]*/ - // gpu::computeLayerTracklets<<>>(iLayer, - // mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), - // mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), - // mTimeFrameGPU->getNClustersLayer(rof0, iLayer), - // mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), - // meanDeltaR); + // Reset used tracklets + checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), false, sizeof(unsigned char) * mTimeFrameGPU->getNClustersLayer(rof0, iLayer)), __FILE__, __LINE__); + gpu::computeLayerTrackletsKernel<<>>(iLayer, + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), + mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), + mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), + mTimeFrameGPU->getConfig().maxVerticesCapacity, + mTimeFrameGPU->getSortedStartIndex(rof0, iLayer), + mTimeFrameGPU->getNClustersLayer(rof0, iLayer), + mTimeFrameGPU->getMinR(iLayer + 1), + mTimeFrameGPU->getMaxR(iLayer + 1), + meanDeltaR); } /*else { gpu::layerTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), iLayer, primaryVertexContext->getTempTrackletArray()[iLayer - 1].getWeakCopy()); diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h index 729af6d8b5924..6572a3d3b9001 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h @@ -84,6 +84,7 @@ class TimeFrame bool empty() const; int getSortedIndex(int rof, int layer, int i) const; + int getSortedStartIndex(const int, const int) const; int getNrof() const; void resetBeamXY(const float x, const float y, const float w = 0); @@ -259,6 +260,8 @@ inline bool TimeFrame::empty() const { return getTotalClusters() == 0; } inline int TimeFrame::getSortedIndex(int rof, int layer, int index) const { return mROframesClusters[layer][rof] + index; } +inline int TimeFrame::getSortedStartIndex(const int rof, const int layer) const { return mROframesClusters[layer][rof]; } + inline int TimeFrame::getNrof() const { return mNrof; } inline void TimeFrame::resetBeamXY(const float x, const float y, const float w) From b135510410930a7bfb0f6efda56fad5056c18965 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Mon, 16 May 2022 14:52:39 +0200 Subject: [PATCH 06/30] Cleanup TimeFrameGPU apis --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 96 +++---------- .../GPU/ITStrackingGPU/TrackerTraitsGPU.h | 7 +- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 2 + .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 130 ++++++++++++++---- .../tracking/GPU/cuda/VertexerTraitsGPU.cu | 77 ++++++----- .../include/ITStracking/IndexTableUtils.h | 11 +- 6 files changed, 172 insertions(+), 151 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 26b21e8a609ef..31b9cafdbe4d7 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -27,6 +27,7 @@ #include "GPUCommonDef.h" #include "GPUCommonMath.h" +#include "GPUCommonLogger.h" namespace o2 { @@ -38,6 +39,13 @@ using namespace constants::its2; class TimeFrameGPUConfig; namespace gpu { + +template +GPUhd() T* getPtrFromRuler(int index, T* src, const int* ruler, const int stride = 1) +{ + return src + ruler[index] * stride; +} + template class TimeFrameGPU : public TimeFrame { @@ -55,6 +63,7 @@ class TimeFrameGPU : public TimeFrame float getDeviceMemory(); Cluster* getDeviceClustersOnLayer(const int rofId, const int layerId) const; unsigned char* getDeviceUsedClustersOnLayer(const int rofId, const int layerId); + int* getDeviceROframesClustersOnLayer(const int layerId) const { return mROframesClustersD[layerId].get(); } int getNClustersLayer(const int rofId, const int layerId) const; TimeFrameGPUConfig& getConfig() { return mConfig; } @@ -91,7 +100,8 @@ class TimeFrameGPU : public TimeFrame std::array, NLayers - 1> mIndexTablesD; std::array, NLayers> mClusterExternalIndicesD; std::array, NLayers - 1> mTrackletsD; - int* mCUBTmpBuffers; // don't know whether will be used by the tracker + std::array, NLayers> mROframesClustersD; // layers x roframes + int* mCUBTmpBuffers; // don't know whether will be used by the tracker // Vertexer only Vector mLines; @@ -116,91 +126,55 @@ class TimeFrameGPU : public TimeFrame template inline Cluster* TimeFrameGPU::getDeviceClustersOnLayer(const int rofId, const int layerId) const { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } - return mClustersD[layerId].get() + mROframesClusters[layerId][rofId]; + return getPtrFromRuler(rofId, mClustersD[layerId].get(), mROframesClusters[layerId].data()); } template inline unsigned char* TimeFrameGPU::getDeviceUsedClustersOnLayer(const int rofId, const int layerId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } - return mUsedClustersD[layerId].get() + mROframesClusters[layerId][rofId]; + return getPtrFromRuler(rofId, mUsedClustersD[layerId].get(), mROframesClusters[layerId].data()); } template inline int TimeFrameGPU::getNClustersLayer(const int rofId, const int layerId) const { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning 0 as value"; - return 0; - } return static_cast(mROframesClusters[layerId][rofId + 1] - mROframesClusters[layerId][rofId]); } template inline int* TimeFrameGPU::getDeviceNTrackletsCluster(int rofId, int combId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } - return mNTrackletsPerClusterD[combId].get() + mROframesClusters[1][rofId]; + return getPtrFromRuler(rofId, mNTrackletsPerClusterD[combId].get(), mROframesClusters[1].data()); } template inline unsigned char* TimeFrameGPU::getDeviceUsedTracklets(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } - return mUsedTracklets.get() + mROframesClusters[1][rofId] * mConfig.maxTrackletsPerCluster; + return getPtrFromRuler(rofId, mUsedTracklets.get(), mROframesClusters[1].data(), mConfig.maxTrackletsPerCluster); } template inline Line* TimeFrameGPU::getDeviceLines(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } - return mLines.get() + mROframesClusters[1][rofId]; + return getPtrFromRuler(rofId, mLines.get(), mROframesClusters[1].data()); } template inline Tracklet* TimeFrameGPU::getDeviceTracklets(const int rofId, const int layerId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } - return mTrackletsD[layerId].get() + mROframesClusters[1][rofId] * mConfig.maxTrackletsPerCluster; + return getPtrFromRuler(rofId, mTrackletsD[layerId].get(), mROframesClusters[1].data(), mConfig.maxTrackletsPerCluster); } template inline int* TimeFrameGPU::getDeviceNFoundLines(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } - return mNFoundLines.get() + mROframesClusters[1][rofId]; + return getPtrFromRuler(rofId, mNFoundLines.get(), mROframesClusters[1].data()); } template inline int* TimeFrameGPU::getDeviceExclusiveNFoundLines(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } - return mNExclusiveFoundLines.get() + mROframesClusters[1][rofId]; + return getPtrFromRuler(rofId, mNExclusiveFoundLines.get(), mROframesClusters[1].data()); } template @@ -215,50 +189,30 @@ inline int* TimeFrameGPU::getDeviceCUBBuffer(const size_t rofId) template inline float* TimeFrameGPU::getDeviceXYCentroids(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } return mXYCentroids.get() + 2 * rofId * mConfig.maxCentroidsXYCapacity; } template inline float* TimeFrameGPU::getDeviceZCentroids(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } return mZCentroids.get() + rofId * mConfig.maxLinesCapacity; } template inline int* TimeFrameGPU::getDeviceXHistograms(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } return mXYZHistograms[0].get() + rofId * mConfig.histConf.nBinsXYZ[0]; } template inline int* TimeFrameGPU::getDeviceYHistograms(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } return mXYZHistograms[1].get() + rofId * mConfig.histConf.nBinsXYZ[1]; } template inline int* TimeFrameGPU::getDeviceZHistograms(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } return mXYZHistograms[2].get() + rofId * mConfig.histConf.nBinsXYZ[2]; } @@ -269,30 +223,18 @@ inline hipcub::KeyValuePair* TimeFrameGPU::getTmpVertexPositi inline cub::KeyValuePair* TimeFrameGPU::getTmpVertexPositionBins(const int rofId) #endif { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } return mTmpVertexPositionBins.get() + 3 * rofId; } template inline float* TimeFrameGPU::getDeviceBeamPosition(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } return mBeamPosition.get() + 2 * rofId; } template inline Vertex* TimeFrameGPU::getDeviceVertices(const int rofId) { - if (rofId < 0 || rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - return nullptr; - } return mGPUVertices.get() + rofId * mConfig.maxVerticesCapacity; } diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h index eaecd650c46b7..83de244fbf7f9 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h @@ -26,11 +26,16 @@ namespace gpu { template struct StaticTrackingParameters { - // StaticTrackingParameters& operator=(const StaticTrackingParameters& t); + StaticTrackingParameters& operator=(const StaticTrackingParameters& t) = default; // int CellMinimumLevel(); /// General parameters int ClusterSharing = 0; int MinTrackLength = NLayers; + float NSigmaCut = 5; + float PVres = 1.e-2f; + int DeltaROF = 0; + int ZBins{256}; + int PhiBins{128}; /// Trackleting cuts float TrackletMaxDeltaPhi = 0.3f; float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 9bc6ff86e93db..ec8d44906024a 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -47,6 +47,7 @@ TimeFrameGPU::TimeFrameGPU() mUsedClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mTrackingFrameInfoD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mClusterExternalIndicesD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + mROframesClustersD[iLayer] = Vector{mConfig.nMaxROFs, mConfig.nMaxROFs}; if (iLayer < NLayers - 1) { mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; @@ -136,6 +137,7 @@ void TimeFrameGPU::initialiseDevice(const int maxLayers) for (int iLayer{0}; iLayer < maxLayers; ++iLayer) { mTrackingFrameInfoD[iLayer].reset(mTrackingFrameInfo[iLayer].data(), static_cast(mTrackingFrameInfo[iLayer].size())); mClusterExternalIndicesD[iLayer].reset(mClusterExternalIndices[iLayer].data(), static_cast(mClusterExternalIndices[iLayer].size())); + mROframesClustersD[iLayer].reset(mROframesClusters[iLayer].data(), static_cast(mROframesClusters[iLayer].size())); } } else { mIndexTablesLayer0D.reset(getIndexTableWhole(0).data(), static_cast(getIndexTableWhole(0).size())); diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 7644c70812474..2f36132b60f14 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -22,28 +22,66 @@ #include "ITStrackingGPU/Stream.h" #include "ITStrackingGPU/TrackerTraitsGPU.h" +#include "GPUCommonLogger.h" +namespace +{ +GPUd() float Sq(float q) +{ + return q * q; +} +} // namespace + namespace o2 { namespace its { using gpu::utils::host::checkGPUError; using namespace constants::its2; +GPUd() const int4 getBinsRect(const Cluster& currentCluster, const int layerIndex, + const o2::its::IndexTableUtils& utils, + const float z1, const float z2, float maxdeltaz, float maxdeltaphi) +{ + const float zRangeMin = o2::gpu::GPUCommonMath::Min(z1, z2) - maxdeltaz; + const float phiRangeMin = currentCluster.phi - maxdeltaphi; + const float zRangeMax = o2::gpu::GPUCommonMath::Max(z1, z2) + maxdeltaz; + const float phiRangeMax = currentCluster.phi + maxdeltaphi; + + if (zRangeMax < -LayersZCoordinate()[layerIndex + 1] || + zRangeMin > LayersZCoordinate()[layerIndex + 1] || zRangeMin > zRangeMax) { + + return getEmptyBinsRect(); + } + + return int4{o2::gpu::GPUCommonMath::Max(0, getZBinIndex(layerIndex + 1, zRangeMin)), + utils.getPhiBinIndex(math_utils::getNormalizedPhi(phiRangeMin)), + o2::gpu::GPUCommonMath::Min(ZBins - 1, getZBinIndex(layerIndex + 1, zRangeMax)), + utils.getPhiBinIndex(math_utils::getNormalizedPhi(phiRangeMax))}; +} namespace gpu { __constant__ StaticTrackingParameters<7> kTrkPar; +__constant__ IndexTableUtils kIndexTableUtils; -GPUg() void computeLayerTrackletsKernel(const int layerIndex, - const Cluster* clustersNextLayer, - const Cluster* clustersCurrentLayer, - unsigned char* usedClusters, - Tracklet* trackletsVector, - const int nVertices, - const int startSortedIndex, - const int sizeCurrentLClusters, - const float minR, - const float maxR, - const float meanDeltaR) +GPUg() void computeLayerTrackletsKernel( + const int rof, + const int maxRofs, + const int layerIndex, + const Cluster* clustersNextLayer, + const Cluster* clustersCurrentLayer, + const int* roFrameClusters, + const unsigned char* usedClusters, + const Vertex* vertices, + Tracklet* trackletsVector, + const int nVertices, + const int startSortedIndex, + const int sizeCurrentLClusters, + const float phiCut, + const float minR, + const float maxR, + const float meanDeltaR, + const float positionResolution, + const float mSAngle) { int clusterTrackletsNum = 0; for (int currentClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; currentClusterIndex < sizeCurrentLClusters; currentClusterIndex += blockDim.x * gridDim.x) { @@ -52,15 +90,44 @@ GPUg() void computeLayerTrackletsKernel(const int layerIndex, if (usedClusters[currentSortedIndex]) { continue; } + int minRof = (rof >= kTrkPar.DeltaROF) ? rof - kTrkPar.DeltaROF : 0; + int maxRof = (rof == maxRofs - kTrkPar.DeltaROF) ? rof : rof + kTrkPar.DeltaROF; const float inverseR0{1.f / currentCluster.radius}; for (int iPrimaryVertex{0}; iPrimaryVertex < nVertices; iPrimaryVertex++) { + const auto primaryVertex{vertices[iPrimaryVertex]}; + if (primaryVertex.getX() || primaryVertex.getY() || primaryVertex.getZ()) { + continue; + } + const float resolution{o2::gpu::GPUCommonMath::Sqrt(Sq(kTrkPar.PVres) / primaryVertex.getNContributors() + Sq(positionResolution))}; + const float tanLambda{(currentCluster.zCoordinate - primaryVertex.getZ()) * inverseR0}; + const float zAtRmin{tanLambda * (minR - currentCluster.radius) + currentCluster.zCoordinate}; + const float zAtRmax{tanLambda * (maxR - currentCluster.radius) + currentCluster.zCoordinate}; + const float sqInverseDeltaZ0{1.f / (Sq(currentCluster.zCoordinate - primaryVertex.getZ()) + 2.e-8f)}; /// protecting from overflows adding the detector resolution + const float sigmaZ{std::sqrt(Sq(resolution) * Sq(tanLambda) * ((Sq(inverseR0) + sqInverseDeltaZ0) * Sq(meanDeltaR) + 1.f) + Sq(meanDeltaR * mSAngle))}; + + const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, kIndexTableUtils, zAtRmin, zAtRmax, sigmaZ * kTrkPar.NSigmaCut, phiCut)}; + if (selectedBinsRect.x == 0 && selectedBinsRect.y == 0 && selectedBinsRect.z == 0 && selectedBinsRect.w == 0) { + continue; + } + + int phiBinsNum{selectedBinsRect.w - selectedBinsRect.y + 1}; + + if (phiBinsNum < 0) { + phiBinsNum += kTrkPar.PhiBins; + } + for (int rof1{minRof}; rof1 <= maxRof; ++rof1) { + // gsl::span layer1 = tf->getClustersOnLayer(rof1, iLayer + 1); + if (!(roFrameClusters[rof1 + 1] - roFrameClusters[rof1])) { + continue; + } + for (int iPhiCount{0}; iPhiCount < phiBinsNum; iPhiCount++) { + int iPhiBin = (selectedBinsRect.y + iPhiCount) % kTrkPar.PhiBins; + const int firstBinIndex{kIndexTableUtils.getBinIndex(selectedBinsRect.x, iPhiBin)}; + const int maxBinIndex{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}; + } + } } - // const float tanLambda{(currentCluster.zCoordinate - devStore.getPrimaryVertex().z) / currentCluster.radius}; - // const float zAtRmin{tanLambda * (devStore.getRmin(layerIndex + 1) - currentCluster.radius) + currentCluster.zCoordinate}; - // const float zAtRmax{tanLambda * (devStore.getRmax(layerIndex + 1) - currentCluster.radius) + currentCluster.zCoordinate}; - // const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, zAtRmin, zAtRmax, - // kTrkPar.TrackletMaxDeltaZ[layerIndex], kTrkPar.TrackletMaxDeltaPhi)}; // if (selectedBinsRect.x != 0 || selectedBinsRect.y != 0 || selectedBinsRect.z != 0 || selectedBinsRect.w != 0) { // const int nextLayerClustersNum{static_cast(nextLayerClusters.size())}; // int phiBinsNum{selectedBinsRect.w - selectedBinsRect.y + 1}; @@ -216,6 +283,7 @@ template void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { checkGPUError(cudaMemcpyToSymbol(gpu::kTrkPar, &mTrkParams, sizeof(gpu::StaticTrackingParameters)), __FILE__, __LINE__); + checkGPUError(cudaMemcpyToSymbol(gpu::kIndexTableUtils, &mTimeFrameGPU->mIndexTableUtils, sizeof(IndexTableUtils)), __FILE__, __LINE__); std::array tempSize; // std::array trackletsNum; std::array streamArray; @@ -248,17 +316,25 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) } // Reset used tracklets checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), false, sizeof(unsigned char) * mTimeFrameGPU->getNClustersLayer(rof0, iLayer)), __FILE__, __LINE__); - gpu::computeLayerTrackletsKernel<<>>(iLayer, - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), - mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), - mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), - mTimeFrameGPU->getConfig().maxVerticesCapacity, - mTimeFrameGPU->getSortedStartIndex(rof0, iLayer), - mTimeFrameGPU->getNClustersLayer(rof0, iLayer), - mTimeFrameGPU->getMinR(iLayer + 1), - mTimeFrameGPU->getMaxR(iLayer + 1), - meanDeltaR); + gpu::computeLayerTrackletsKernel<<>>( + rof0, + mTimeFrameGPU->getNrof(), + iLayer, + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), + mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), + mTimeFrameGPU->getDeviceVertices(rof0), + mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), + mTimeFrameGPU->getConfig().maxVerticesCapacity, + mTimeFrameGPU->getSortedStartIndex(rof0, iLayer), + mTimeFrameGPU->getNClustersLayer(rof0, iLayer), + mTimeFrameGPU->getPhiCut(iLayer), + mTimeFrameGPU->getMinR(iLayer + 1), + mTimeFrameGPU->getMaxR(iLayer + 1), + meanDeltaR, + mTimeFrameGPU->getPositionResolution(iLayer), + mTimeFrameGPU->getMSangle(iLayer)); } /*else { gpu::layerTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), iLayer, primaryVertexContext->getTempTrackletArray()[iLayer - 1].getWeakCopy()); diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu index 68cac94936658..0f29e0a52f0c2 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu @@ -237,16 +237,17 @@ GPUg() void trackletSelectionKernel( } } -GPUg() void computeCentroidsKernel(Line* lines, - int* nFoundLines, - int* nExclusiveFoundLines, - const size_t nClustersMiddleLayer, - float* centroids, - const float lowHistX, - const float highHistX, - const float lowHistY, - const float highHistY, - const float pairCut) +GPUg() void computeCentroidsKernel( + Line* lines, + int* nFoundLines, + int* nExclusiveFoundLines, + const size_t nClustersMiddleLayer, + float* centroids, + const float lowHistX, + const float highHistX, + const float lowHistY, + const float highHistY, + const float pairCut) { const int nLines = nExclusiveFoundLines[nClustersMiddleLayer - 1] + nFoundLines[nClustersMiddleLayer - 1]; const int maxIterations{nLines * (nLines - 1) / 2}; @@ -281,23 +282,24 @@ GPUg() void computeCentroidsKernel(Line* lines, } } -GPUg() void computeZCentroidsKernel(const int nLines, - const cub::KeyValuePair* tmpVtX, - float* beamPosition, - Line* lines, - float* centroids, - const int* histX, // X - const float lowHistX, - const float binSizeHistX, - const int nBinsHistX, - const int* histY, // Y - const float lowHistY, - const float binSizeHistY, - const int nBinsHistY, - const float lowHistZ, // Z - const float pairCut, - const int binOpeningX, - const int binOpeningY) +GPUg() void computeZCentroidsKernel( + const int nLines, + const cub::KeyValuePair* tmpVtX, + float* beamPosition, + Line* lines, + float* centroids, + const int* histX, // X + const float lowHistX, + const float binSizeHistX, + const int nBinsHistX, + const int* histY, // Y + const float lowHistY, + const float binSizeHistY, + const int nBinsHistY, + const float lowHistZ, // Z + const float pairCut, + const int binOpeningX, + const int binOpeningY) { for (size_t currentThreadIndex = blockIdx.x * blockDim.x + threadIdx.x; currentThreadIndex < nLines; currentThreadIndex += blockDim.x * gridDim.x) { if (tmpVtX[0].value || tmpVtX[1].value) { @@ -334,16 +336,17 @@ GPUg() void computeZCentroidsKernel(const int nLines, } } -GPUg() void computeVertexKernel(cub::KeyValuePair* tmpVertexBins, - int* histZ, // Z - const float lowHistZ, - const float binSizeHistZ, - const int nBinsHistZ, - Vertex* vertices, - float* beamPosition, - const int vertIndex, - const int minContributors, - const int binOpeningZ) +GPUg() void computeVertexKernel( + cub::KeyValuePair* tmpVertexBins, + int* histZ, // Z + const float lowHistZ, + const float binSizeHistZ, + const int nBinsHistZ, + Vertex* vertices, + float* beamPosition, + const int vertIndex, + const int minContributors, + const int binOpeningZ) { for (size_t currentThreadIndex = blockIdx.x * blockDim.x + threadIdx.x; currentThreadIndex < binOpeningZ; currentThreadIndex += blockDim.x * gridDim.x) { if (currentThreadIndex == 0) { diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/IndexTableUtils.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/IndexTableUtils.h index e91deb6d72127..66de759f943e3 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/IndexTableUtils.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/IndexTableUtils.h @@ -16,18 +16,11 @@ #ifndef TRACKINGITSU_INCLUDE_INDEXTABLEUTILS_H_ #define TRACKINGITSU_INCLUDE_INDEXTABLEUTILS_H_ -#ifndef GPUCA_GPUCODE_DEVICE -#include -#include -#include -#endif - #include "ITStracking/Constants.h" #include "ITStracking/Configuration.h" #include "ITStracking/Definitions.h" #include "GPUCommonMath.h" #include "GPUCommonDef.h" -#include "GPUCommonLogger.h" namespace o2 { @@ -52,8 +45,8 @@ class IndexTableUtils int mNzBins = 0; int mNphiBins = 0; float mInversePhiBinSize = 0.f; - float mLayerZ[7]; - float mInverseZBinSize[7]; + float mLayerZ[7] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; + float mInverseZBinSize[7] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; }; template From 4c47a1d729a1f28d20e2fb31abf2be4df2587652 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Mon, 23 May 2022 19:15:47 +0200 Subject: [PATCH 07/30] Improve TimeFrameGPU allocations --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 50 ++++- .../GPU/ITStrackingGPU/TrackerTraitsGPU.h | 27 --- .../ITS/tracking/GPU/ITStrackingGPU/Vector.h | 84 ++++--- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 92 +++++--- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 210 +++++++++--------- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 1 + .../include/ITSWorkflow/TrackerSpec.h | 1 + .../ITSMFT/ITS/workflow/src/TrackerSpec.cxx | 3 +- 8 files changed, 255 insertions(+), 213 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 31b9cafdbe4d7..947dc8c24cdb0 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -40,12 +40,43 @@ class TimeFrameGPUConfig; namespace gpu { +template +struct StaticTrackingParameters { + StaticTrackingParameters& operator=(const StaticTrackingParameters& t) = default; + // int CellMinimumLevel(); + /// General parameters + int ClusterSharing = 0; + int MinTrackLength = NLayers; + float NSigmaCut = 5; + float PVres = 1.e-2f; + int DeltaROF = 0; + int ZBins{256}; + int PhiBins{128}; + /// Trackleting cuts + float TrackletMaxDeltaPhi = 0.3f; + float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; + /// Cell finding cuts + // float CellMaxDeltaTanLambda = 0.025f; + // float CellMaxDCA[NLayers - 2] = {0.05f, 0.04f, 0.05f, 0.2f, 0.4f}; + // float CellMaxDeltaPhi = 0.14f; + // float CellMaxDeltaZ[NLayers - 2] = {0.2f, 0.4f, 0.5f, 0.6f, 3.0f}; + // /// Neighbour finding cuts + // float NeighbourMaxDeltaCurvature[NLayers - 3] = {0.008f, 0.0025f, 0.003f, 0.0035f}; + // float NeighbourMaxDeltaN[NLayers - 3] = {0.002f, 0.0090f, 0.002f, 0.005f}; +}; + template GPUhd() T* getPtrFromRuler(int index, T* src, const int* ruler, const int stride = 1) { return src + ruler[index] * stride; } +template +GPUhd() const T* getPtrFromRuler(int index, const T* src, const int* ruler, const int stride = 1) +{ + return src + ruler[index] * stride; +} + template class TimeFrameGPU : public TimeFrame { @@ -53,27 +84,30 @@ class TimeFrameGPU : public TimeFrame public: TimeFrameGPU(); ~TimeFrameGPU(); - void initialiseDevice(const int maxLayers); void checkBufferSizes(); void initialise(const int iteration, const TrackingParameters& trkParam, const int maxLayers); + template + void initialiseDevice(const TrackingParameters&); /// Getters float getDeviceMemory(); Cluster* getDeviceClustersOnLayer(const int rofId, const int layerId) const; unsigned char* getDeviceUsedClustersOnLayer(const int rofId, const int layerId); - int* getDeviceROframesClustersOnLayer(const int layerId) const { return mROframesClustersD[layerId].get(); } + int* getDeviceROframesClustersOnLayer(const int layerId) const { return mROframesClustersD[layerId].get(); } int getNClustersLayer(const int rofId, const int layerId) const; TimeFrameGPUConfig& getConfig() { return mConfig; } // Vertexer only int* getDeviceNTrackletsCluster(int rofId, int combId); + int* getDeviceIndexTables(const int layerId) { return mIndexTablesD[layerId].get(); } int* getDeviceIndexTableL0(const int rofId) { return mIndexTablesLayer0D.get() + rofId * (ZBins * PhiBins + 1); } int* getDeviceIndexTableL2(const int rofId) { return mIndexTablesLayer2D.get() + rofId * (ZBins * PhiBins + 1); } unsigned char* getDeviceUsedTracklets(const int rofId); Line* getDeviceLines(const int rofId); Tracklet* getDeviceTracklets(const int rofId, const int layerId); + int* getDeviceTrackletsLookupTable(const int rofId, const int layerId); int* getDeviceNFoundLines(const int rofId); int* getDeviceExclusiveNFoundLines(const int rofId); int* getDeviceCUBBuffer(const size_t rofId); @@ -82,6 +116,9 @@ class TimeFrameGPU : public TimeFrame int* getDeviceXHistograms(const int rofId); int* getDeviceYHistograms(const int rofId); int* getDeviceZHistograms(const int rofId); + gpu::StaticTrackingParameters* getDeviceTrackingParameters() const { return mDeviceTrackingParams; } + IndexTableUtils* getDeviceIndexTableUtils() const { return mDeviceIndexTableUtils; } + #ifdef __HIPCC__ hipcub::KeyValuePair* getTmpVertexPositionBins(const int rofId); #else @@ -100,8 +137,11 @@ class TimeFrameGPU : public TimeFrame std::array, NLayers - 1> mIndexTablesD; std::array, NLayers> mClusterExternalIndicesD; std::array, NLayers - 1> mTrackletsD; + std::array, NLayers - 1> mTrackletsLookupTablesD; std::array, NLayers> mROframesClustersD; // layers x roframes int* mCUBTmpBuffers; // don't know whether will be used by the tracker + gpu::StaticTrackingParameters* mDeviceTrackingParams; + IndexTableUtils* mDeviceIndexTableUtils; // Vertexer only Vector mLines; @@ -165,6 +205,12 @@ inline Tracklet* TimeFrameGPU::getDeviceTracklets(const int rofId, cons return getPtrFromRuler(rofId, mTrackletsD[layerId].get(), mROframesClusters[1].data(), mConfig.maxTrackletsPerCluster); } +template +inline int* TimeFrameGPU::getDeviceTrackletsLookupTable(const int rofId, const int layerId) +{ + return getPtrFromRuler(rofId, mTrackletsLookupTablesD[layerId].get(), mROframesClusters[layerId].data()); +} + template inline int* TimeFrameGPU::getDeviceNFoundLines(const int rofId) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h index 83de244fbf7f9..e19fb37b7bcc9 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h @@ -22,33 +22,6 @@ namespace o2 { namespace its { -namespace gpu -{ -template -struct StaticTrackingParameters { - StaticTrackingParameters& operator=(const StaticTrackingParameters& t) = default; - // int CellMinimumLevel(); - /// General parameters - int ClusterSharing = 0; - int MinTrackLength = NLayers; - float NSigmaCut = 5; - float PVres = 1.e-2f; - int DeltaROF = 0; - int ZBins{256}; - int PhiBins{128}; - /// Trackleting cuts - float TrackletMaxDeltaPhi = 0.3f; - float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; - /// Cell finding cuts - // float CellMaxDeltaTanLambda = 0.025f; - // float CellMaxDCA[NLayers - 2] = {0.05f, 0.04f, 0.05f, 0.2f, 0.4f}; - // float CellMaxDeltaPhi = 0.14f; - // float CellMaxDeltaZ[NLayers - 2] = {0.2f, 0.4f, 0.5f, 0.6f, 3.0f}; - // /// Neighbour finding cuts - // float NeighbourMaxDeltaCurvature[NLayers - 3] = {0.008f, 0.0025f, 0.003f, 0.0035f}; - // float NeighbourMaxDeltaN[NLayers - 3] = {0.002f, 0.0090f, 0.002f, 0.005f}; -}; -} // namespace gpu template class TrackerTraitsGPU : public TrackerTraits diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h index 64b7c2dababc2..ffd29e30aceca 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h @@ -74,8 +74,8 @@ class Vector final private: GPUhd() Vector(const Vector&, const bool); - T* mArrayPointer = nullptr; - size_t* mDeviceSize = nullptr; + T* mArrayPtr = nullptr; + size_t* mDeviceSizePtr = nullptr; size_t mCapacity; bool mIsWeak; }; @@ -98,17 +98,17 @@ Vector::Vector(const T* const source, const size_t size, const size_t initial if (size > 0) { try { - utils::host::gpuMalloc(reinterpret_cast(&mArrayPointer), size * sizeof(T)); - utils::host::gpuMalloc(reinterpret_cast(&mDeviceSize), sizeof(size_t)); + utils::host::gpuMalloc(reinterpret_cast(&mArrayPtr), size * sizeof(T)); + utils::host::gpuMalloc(reinterpret_cast(&mDeviceSizePtr), sizeof(size_t)); if (source != nullptr) { - utils::host::gpuMemcpyHostToDevice(mArrayPointer, source, size * sizeof(T)); - utils::host::gpuMemcpyHostToDevice(mDeviceSize, &size, sizeof(size_t)); + utils::host::gpuMemcpyHostToDevice(mArrayPtr, source, size * sizeof(T)); + utils::host::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(size_t)); } else { - utils::host::gpuMemcpyHostToDevice(mDeviceSize, &initialSize, sizeof(size_t)); + utils::host::gpuMemcpyHostToDevice(mDeviceSizePtr, &initialSize, sizeof(size_t)); } } catch (...) { @@ -122,8 +122,8 @@ Vector::Vector(const T* const source, const size_t size, const size_t initial template GPUhd() Vector::Vector(const Vector& other, const bool isWeak) - : mArrayPointer{other.mArrayPointer}, - mDeviceSize{other.mDeviceSize}, + : mArrayPtr{other.mArrayPtr}, + mDeviceSizePtr{other.mDeviceSizePtr}, mCapacity{other.mCapacity}, mIsWeak{isWeak} { @@ -146,13 +146,13 @@ GPUhd() Vector::~Vector() template GPUhd() Vector::Vector(Vector&& other) - : mArrayPointer{other.mArrayPointer}, - mDeviceSize{other.mDeviceSize}, + : mArrayPtr{other.mArrayPtr}, + mDeviceSizePtr{other.mDeviceSizePtr}, mCapacity{other.mCapacity}, mIsWeak{other.mIsWeak} { - other.mArrayPointer = nullptr; - other.mDeviceSize = nullptr; + other.mArrayPtr = nullptr; + other.mDeviceSizePtr = nullptr; } template @@ -160,13 +160,13 @@ Vector& Vector::operator=(Vector&& other) { destroy(); - mArrayPointer = other.mArrayPointer; - mDeviceSize = other.mDeviceSize; + mArrayPtr = other.mArrayPtr; + mDeviceSizePtr = other.mDeviceSizePtr; mCapacity = other.mCapacity; mIsWeak = other.mIsWeak; - other.mArrayPointer = nullptr; - other.mDeviceSize = nullptr; + other.mArrayPtr = nullptr; + other.mDeviceSizePtr = nullptr; return *this; } @@ -175,7 +175,7 @@ template size_t Vector::getSizeFromDevice() const { size_t size; - utils::host::gpuMemcpyDeviceToHost(&size, mDeviceSize, sizeof(size_t)); + utils::host::gpuMemcpyDeviceToHost(&size, mDeviceSizePtr, sizeof(size_t)); return size; } @@ -183,7 +183,7 @@ size_t Vector::getSizeFromDevice() const template void Vector::resize(const size_t size) { - utils::host::gpuMemcpyHostToDevice(mDeviceSize, &size, sizeof(size_t)); + utils::host::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(size_t)); } template @@ -196,49 +196,45 @@ template void Vector::reset(const T* const source, const size_t size, const size_t initialSize) { if (size > mCapacity) { - if (mArrayPointer != nullptr) { - utils::host::gpuFree(mArrayPointer); + if (mArrayPtr != nullptr) { + utils::host::gpuFree(mArrayPtr); } - utils::host::gpuMalloc(reinterpret_cast(&mArrayPointer), size * sizeof(T)); + utils::host::gpuMalloc(reinterpret_cast(&mArrayPtr), size * sizeof(T)); mCapacity = size; } + if (mDeviceSizePtr == nullptr) { + utils::host::gpuMalloc(reinterpret_cast(&mDeviceSizePtr), sizeof(size_t)); + } if (source != nullptr) { - utils::host::gpuMemcpyHostToDevice(mArrayPointer, source, size * sizeof(T)); - utils::host::gpuMemcpyHostToDevice(mDeviceSize, &size, sizeof(size_t)); - + utils::host::gpuMemcpyHostToDevice(mArrayPtr, source, size * sizeof(T)); + utils::host::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(size_t)); } else { - if (mDeviceSize == nullptr) { - utils::host::gpuMalloc(reinterpret_cast(&mDeviceSize), sizeof(size_t)); - } - utils::host::gpuMemcpyHostToDevice(mDeviceSize, &initialSize, sizeof(size_t)); + utils::host::gpuMemcpyHostToDevice(mDeviceSizePtr, &initialSize, sizeof(size_t)); } } template void Vector::copyIntoSizedVector(std::vector& destinationVector) { - utils::host::gpuMemcpyDeviceToHost(destinationVector.data(), mArrayPointer, destinationVector.size() * sizeof(T)); + utils::host::gpuMemcpyDeviceToHost(destinationVector.data(), mArrayPtr, destinationVector.size() * sizeof(T)); } template inline void Vector::destroy() { - if (mArrayPointer != nullptr) { - - utils::host::gpuFree(mArrayPointer); + if (mArrayPtr != nullptr) { + utils::host::gpuFree(mArrayPtr); } - - if (mDeviceSize != nullptr) { - - utils::host::gpuFree(mDeviceSize); + if (mDeviceSizePtr != nullptr) { + utils::host::gpuFree(mDeviceSizePtr); } } template GPUhd() T* Vector::get() const { - return mArrayPointer; + return mArrayPtr; } template @@ -256,14 +252,14 @@ GPUhd() Vector Vector::getWeakCopy() const template GPUd() T& Vector::operator[](const size_t index) const { - return mArrayPointer[index]; + return mArrayPtr[index]; } template GPUh() T Vector::getElementFromDevice(const size_t index) const { T element; - utils::host::gpuMemcpyDeviceToHost(&element, mArrayPointer + index, sizeof(T)); + utils::host::gpuMemcpyDeviceToHost(&element, mArrayPtr + index, sizeof(T)); return element; } @@ -271,21 +267,21 @@ GPUh() T Vector::getElementFromDevice(const size_t index) const template GPUd() size_t Vector::size() const { - return *mDeviceSize; + return *mDeviceSizePtr; } template template GPUd() void Vector::emplace(const size_t index, Args&&... arguments) { - new (mArrayPointer + index) T(std::forward(arguments)...); + new (mArrayPtr + index) T(std::forward(arguments)...); } template GPUhd() void Vector::dump() { - printf("mArrayPointer = %p\nmDeviceSize = %p\nmCapacity = %d\nmIsWeak = %s\n", - mArrayPointer, mDeviceSize, mCapacity, mIsWeak ? "true" : "false"); + printf("mArrayPtr = %p\nmDeviceSize = %p\nmCapacity = %d\nmIsWeak = %s\n", + mArrayPtr, mDeviceSizePtr, mCapacity, mIsWeak ? "true" : "false"); } } // namespace gpu } // namespace its diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index ec8d44906024a..ece072b505c34 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -41,37 +41,6 @@ template TimeFrameGPU::TimeFrameGPU() { getDeviceMemory(); - - for (int iLayer{0}; iLayer < NLayers; ++iLayer) { // Tracker and vertexer - mClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - mUsedClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - mTrackingFrameInfoD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - mClusterExternalIndicesD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - mROframesClustersD[iLayer] = Vector{mConfig.nMaxROFs, mConfig.nMaxROFs}; - if (iLayer < NLayers - 1) { - mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, - mConfig.trackletsCapacity}; - } - } - - for (auto iComb{0}; iComb < 2; ++iComb) { // Vertexer only - mNTrackletsPerClusterD[iComb] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - } - mIndexTablesLayer0D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; - mIndexTablesLayer2D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; - mLines = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; - mNFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - mNExclusiveFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - mUsedTracklets = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; - discardResult(cudaMalloc(&mCUBTmpBuffers, mConfig.nMaxROFs * mConfig.tmpCUBBufferSize)); - mXYCentroids = Vector{2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity, 2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity}; - mZCentroids = Vector{mConfig.nMaxROFs * mConfig.maxLinesCapacity, mConfig.nMaxROFs * mConfig.maxLinesCapacity}; - for (size_t i{0}; i < 3; ++i) { - mXYZHistograms[i] = Vector{mConfig.nMaxROFs * mConfig.histConf.nBinsXYZ[i], mConfig.nMaxROFs * mConfig.histConf.nBinsXYZ[i]}; - } - mTmpVertexPositionBins = Vector>{3 * mConfig.nMaxROFs, 3 * mConfig.nMaxROFs}; - mBeamPosition = Vector{2 * mConfig.nMaxROFs, 2 * mConfig.nMaxROFs}; - mGPUVertices = Vector{mConfig.nMaxROFs * mConfig.maxVerticesCapacity, mConfig.nMaxROFs * mConfig.maxVerticesCapacity}; } template @@ -85,6 +54,7 @@ float TimeFrameGPU::getDeviceMemory() totalMemory += NLayers * mConfig.clustersPerLayerCapacity * sizeof(int); totalMemory += NLayers * mConfig.clustersPerROfCapacity * sizeof(int); totalMemory += (NLayers - 1) * mConfig.trackletsCapacity * sizeof(Tracklet); + totalMemory += (NLayers - 1) * mConfig.nMaxROFs * (256 * 128 + 1) * sizeof(int); totalMemory += 2 * mConfig.clustersPerLayerCapacity * sizeof(int); totalMemory += 2 * mConfig.nMaxROFs * (ZBins * PhiBins + 1) * sizeof(int); totalMemory += mConfig.trackletsCapacity * sizeof(Line); @@ -108,6 +78,7 @@ float TimeFrameGPU::getDeviceMemory() LOG(info) << fmt::format("\t- Cluster external indices: {:.2f} MB", NLayers * mConfig.clustersPerLayerCapacity * sizeof(int) / MB); LOG(info) << fmt::format("\t- Clusters per ROf: {:.2f} MB", NLayers * mConfig.clustersPerROfCapacity * sizeof(int) / MB); LOG(info) << fmt::format("\t- Tracklets: {:.2f} MB", (NLayers - 1) * mConfig.trackletsCapacity * sizeof(Tracklet) / MB); + LOG(info) << fmt::format("\t- Tracklet index tables: {:.2f} MB", (NLayers - 1) * mConfig.nMaxROFs * (256 * 128 + 1) * sizeof(int) / MB); LOG(info) << fmt::format("\t- N tracklets per cluster: {:.2f} MB", 2 * mConfig.clustersPerLayerCapacity * sizeof(int) / MB); LOG(info) << fmt::format("\t- Index tables: {:.2f} MB", 2 * mConfig.nMaxROFs * (ZBins * PhiBins + 1) * sizeof(int) / MB); LOG(info) << fmt::format("\t- Lines: {:.2f} MB", mConfig.trackletsCapacity * sizeof(Line) / MB); @@ -127,14 +98,52 @@ float TimeFrameGPU::getDeviceMemory() } template -void TimeFrameGPU::initialiseDevice(const int maxLayers) +template +void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) { - for (int iLayer{0}; iLayer < maxLayers; ++iLayer) { + + for (int iLayer{0}; iLayer < NLayers; ++iLayer) { // Tracker and vertexer + // mClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + mUsedClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + // mTrackingFrameInfoD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + // mClusterExternalIndicesD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + // mROframesClustersD[iLayer] = Vector{mConfig.nMaxROFs, mConfig.nMaxROFs}; + if (iLayer < NLayers - 1) { + mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; + mIndexTablesD[iLayer] = Vector{mConfig.nMaxROFs * (256 * 128 + 1), mConfig.nMaxROFs * (256 * 128 + 1)}; + } + } + + for (auto iComb{0}; iComb < 2; ++iComb) { // Vertexer only + mNTrackletsPerClusterD[iComb] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + } + // mIndexTablesLayer0D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; + // mIndexTablesLayer2D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; + mLines = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; + mNFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + mNExclusiveFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + mUsedTracklets = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; + discardResult(cudaMalloc(&mCUBTmpBuffers, mConfig.nMaxROFs * mConfig.tmpCUBBufferSize)); + mXYCentroids = Vector{2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity, 2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity}; + mZCentroids = Vector{mConfig.nMaxROFs * mConfig.maxLinesCapacity, mConfig.nMaxROFs * mConfig.maxLinesCapacity}; + for (size_t i{0}; i < 3; ++i) { + mXYZHistograms[i] = Vector{mConfig.nMaxROFs * mConfig.histConf.nBinsXYZ[i], mConfig.nMaxROFs * mConfig.histConf.nBinsXYZ[i]}; + } + mTmpVertexPositionBins = Vector>{3 * mConfig.nMaxROFs, 3 * mConfig.nMaxROFs}; + mBeamPosition = Vector{2 * mConfig.nMaxROFs, 2 * mConfig.nMaxROFs}; + mGPUVertices = Vector{mConfig.nMaxROFs * mConfig.maxVerticesCapacity, mConfig.nMaxROFs * mConfig.maxVerticesCapacity}; + ////////////////////////////////////////////////////////////////////////////// + constexpr int layers = isTracker ? NLayers : 3; + for (int iLayer{0}; iLayer < layers; ++iLayer) { mClustersD[iLayer].reset(mClusters[iLayer].data(), static_cast(mClusters[iLayer].size())); } - if (maxLayers == NLayers) { + if constexpr (isTracker) { + discardResult(cudaMalloc(reinterpret_cast(&mDeviceTrackingParams), sizeof(gpu::StaticTrackingParameters))); + discardResult(cudaMalloc(reinterpret_cast(&mDeviceIndexTableUtils), sizeof(IndexTableUtils))); + discardResult(cudaMemcpy(mDeviceTrackingParams, &trkParam, sizeof(gpu::StaticTrackingParameters), cudaMemcpyHostToDevice)); + discardResult(cudaMemcpy(mDeviceIndexTableUtils, &mIndexTableUtils, sizeof(IndexTableUtils), cudaMemcpyHostToDevice)); // Tracker-only: we don't need to copy data in vertexer - for (int iLayer{0}; iLayer < maxLayers; ++iLayer) { + for (int iLayer{0}; iLayer < NLayers; ++iLayer) { mTrackingFrameInfoD[iLayer].reset(mTrackingFrameInfo[iLayer].data(), static_cast(mTrackingFrameInfo[iLayer].size())); mClusterExternalIndicesD[iLayer].reset(mClusterExternalIndices[iLayer].data(), static_cast(mClusterExternalIndices[iLayer].size())); mROframesClustersD[iLayer].reset(mROframesClusters[iLayer].data(), static_cast(mROframesClusters[iLayer].size())); @@ -153,11 +162,20 @@ void TimeFrameGPU::initialise(const int iteration, { o2::its::TimeFrame::initialise(iteration, trkParam, maxLayers); checkBufferSizes(); - initialiseDevice(maxLayers); + if (maxLayers < NLayers) { + initialiseDevice(trkParam); // vertexer + } else { + initialiseDevice(trkParam); // tracker + } } template -TimeFrameGPU::~TimeFrameGPU() = default; +TimeFrameGPU::~TimeFrameGPU() +{ + discardResult(cudaFree(mCUBTmpBuffers)); + discardResult(cudaFree(mDeviceTrackingParams)); + discardResult(cudaFree(mDeviceIndexTableUtils)); +} template void TimeFrameGPU::checkBufferSizes() diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 2f36132b60f14..61c10083457e6 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -23,20 +23,16 @@ #include "ITStrackingGPU/TrackerTraitsGPU.h" #include "GPUCommonLogger.h" -namespace -{ -GPUd() float Sq(float q) -{ - return q * q; -} -} // namespace - namespace o2 { namespace its { using gpu::utils::host::checkGPUError; using namespace constants::its2; + +namespace gpu +{ + GPUd() const int4 getBinsRect(const Cluster& currentCluster, const int layerIndex, const o2::its::IndexTableUtils& utils, const float z1, const float z2, float maxdeltaz, float maxdeltaphi) @@ -58,109 +54,115 @@ GPUd() const int4 getBinsRect(const Cluster& currentCluster, const int layerInde utils.getPhiBinIndex(math_utils::getNormalizedPhi(phiRangeMax))}; } -namespace gpu +GPUd() float Sq(float q) { -__constant__ StaticTrackingParameters<7> kTrkPar; -__constant__ IndexTableUtils kIndexTableUtils; + return q * q; +} +template GPUg() void computeLayerTrackletsKernel( - const int rof, + const int rof0, const int maxRofs, const int layerIndex, - const Cluster* clustersNextLayer, - const Cluster* clustersCurrentLayer, - const int* roFrameClusters, - const unsigned char* usedClusters, - const Vertex* vertices, - Tracklet* trackletsVector, + const Cluster* clustersNextLayer, // input data rof0-delta * trkPars, + const IndexTableUtils* utils) { int clusterTrackletsNum = 0; for (int currentClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; currentClusterIndex < sizeCurrentLClusters; currentClusterIndex += blockDim.x * gridDim.x) { const Cluster& currentCluster{clustersCurrentLayer[currentClusterIndex]}; - const int currentSortedIndex{startSortedIndex + currentClusterIndex}; - if (usedClusters[currentSortedIndex]) { + const int currentSortedIndex{startSortedIndexLayer + currentClusterIndex}; + if (usedClustersLayer[currentSortedIndex]) { continue; } - int minRof = (rof >= kTrkPar.DeltaROF) ? rof - kTrkPar.DeltaROF : 0; - int maxRof = (rof == maxRofs - kTrkPar.DeltaROF) ? rof : rof + kTrkPar.DeltaROF; + int minRof = (rof0 >= trkPars->DeltaROF) ? rof0 - trkPars->DeltaROF : 0; + int maxRof = (rof0 == maxRofs - trkPars->DeltaROF) ? rof0 : rof0 + trkPars->DeltaROF; + if (!currentClusterIndex) { + printf(" >>>> %d\n", trkPars->DeltaROF); + } const float inverseR0{1.f / currentCluster.radius}; for (int iPrimaryVertex{0}; iPrimaryVertex < nVertices; iPrimaryVertex++) { - const auto primaryVertex{vertices[iPrimaryVertex]}; - if (primaryVertex.getX() || primaryVertex.getY() || primaryVertex.getZ()) { + const auto& primaryVertex{vertices[iPrimaryVertex]}; + if (primaryVertex.getX() == 0.f && primaryVertex.getY() == 0.f && primaryVertex.getZ() == 0.f) { continue; } - const float resolution{o2::gpu::GPUCommonMath::Sqrt(Sq(kTrkPar.PVres) / primaryVertex.getNContributors() + Sq(positionResolution))}; + if (!currentClusterIndex) { + printf("rof0: %d x: %lf, y: %lf, z: %lf\n", rof0, primaryVertex.getX(), primaryVertex.getY(), primaryVertex.getZ()); + } + + const float resolution{o2::gpu::GPUCommonMath::Sqrt(Sq(trkPars->PVres) / primaryVertex.getNContributors() + Sq(positionResolution))}; const float tanLambda{(currentCluster.zCoordinate - primaryVertex.getZ()) * inverseR0}; const float zAtRmin{tanLambda * (minR - currentCluster.radius) + currentCluster.zCoordinate}; const float zAtRmax{tanLambda * (maxR - currentCluster.radius) + currentCluster.zCoordinate}; const float sqInverseDeltaZ0{1.f / (Sq(currentCluster.zCoordinate - primaryVertex.getZ()) + 2.e-8f)}; /// protecting from overflows adding the detector resolution const float sigmaZ{std::sqrt(Sq(resolution) * Sq(tanLambda) * ((Sq(inverseR0) + sqInverseDeltaZ0) * Sq(meanDeltaR) + 1.f) + Sq(meanDeltaR * mSAngle))}; - const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, kIndexTableUtils, zAtRmin, zAtRmax, sigmaZ * kTrkPar.NSigmaCut, phiCut)}; + const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, *utils, zAtRmin, zAtRmax, sigmaZ * trkPars->NSigmaCut, phiCut)}; if (selectedBinsRect.x == 0 && selectedBinsRect.y == 0 && selectedBinsRect.z == 0 && selectedBinsRect.w == 0) { continue; } - int phiBinsNum{selectedBinsRect.w - selectedBinsRect.y + 1}; - if (phiBinsNum < 0) { - phiBinsNum += kTrkPar.PhiBins; + phiBinsNum += trkPars->PhiBins; } - for (int rof1{minRof}; rof1 <= maxRof; ++rof1) { - // gsl::span layer1 = tf->getClustersOnLayer(rof1, iLayer + 1); + int indexTableOffset{0}; // used to access on rof-based index + constexpr int tableSize{256 * 128 + 1}; // hardcoded for the time being + for (int rof1{minRof}; rof1 <= maxRof; ++rof1, indexTableOffset += tableSize) { + // // gsl::span layer1 = tf->getClustersOnLayer(rof1, iLayer + 1); if (!(roFrameClusters[rof1 + 1] - roFrameClusters[rof1])) { continue; } for (int iPhiCount{0}; iPhiCount < phiBinsNum; iPhiCount++) { - int iPhiBin = (selectedBinsRect.y + iPhiCount) % kTrkPar.PhiBins; - const int firstBinIndex{kIndexTableUtils.getBinIndex(selectedBinsRect.x, iPhiBin)}; + int iPhiBin = (selectedBinsRect.y + iPhiCount) % trkPars->PhiBins; + const int firstBinIndex{utils->getBinIndex(selectedBinsRect.x, iPhiBin)}; const int maxBinIndex{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}; + printf("%d %d %d %d %d %d %d\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0); + // const int firstRowClusterIndex = indexTable[indexTableOffset + firstBinIndex]; + // const int maxRowClusterIndex = indexTable[indexTableOffset + maxBinIndex]; + + // for (int iNextCluster{firstRowClusterIndex}; iNextCluster < maxRowClusterIndex; ++iNextCluster) { + // if (iNextCluster >= roFrameClusters[rof1]) { + // break; + // } + // const Cluster& nextCluster{getPtrFromRuler(rof0, clustersNextLayer, roFrameClusters)[iNextCluster]}; + // if (usedClustersNextLayer[nextCluster.clusterId]) { + // continue; + // } + // const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi)}; + // const float deltaZ{o2::gpu::GPUCommonMath::Abs(tanLambda * (nextCluster.radius - currentCluster.radius) + + // currentCluster.zCoordinate - nextCluster.zCoordinate)}; + // if (deltaZ / sigmaZ < trkPars->NSigmaCut && (deltaPhi < phiCut || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < phiCut)) { + // if constexpr (!Layer0) { + // trackletsLookUpTable[currentSortedIndex]++; // Should be race-condition safe + // } + // const float phi{o2::gpu::GPUCommonMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, + // currentCluster.xCoordinate - nextCluster.xCoordinate)}; + // const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / + // (currentCluster.radius - nextCluster.radius)}; + // // Missing actual saving of the tracklet + // } + // } } } } - - // if (selectedBinsRect.x != 0 || selectedBinsRect.y != 0 || selectedBinsRect.z != 0 || selectedBinsRect.w != 0) { - // const int nextLayerClustersNum{static_cast(nextLayerClusters.size())}; - // int phiBinsNum{selectedBinsRect.w - selectedBinsRect.y + 1}; - // if (phiBinsNum < 0) { - // phiBinsNum += constants::its2::PhiBins; - // } - // for (int iPhiBin{selectedBinsRect.y}, iPhiCount{0}; iPhiCount < phiBinsNum; - // iPhiBin = ++iPhiBin == constants::its2::PhiBins ? 0 : iPhiBin, iPhiCount++) { - // const int firstBinIndex{constants::its2::getBinIndex(selectedBinsRect.x, iPhiBin)}; - // const int firstRowClusterIndex = devStore.getIndexTables()[layerIndex][firstBinIndex]; - // const int maxRowClusterIndex = devStore.getIndexTables()[layerIndex][{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}]; - // for (int iNextLayerCluster{firstRowClusterIndex}; - // iNextLayerCluster <= maxRowClusterIndex && iNextLayerCluster < nextLayerClustersNum; ++iNextLayerCluster) { - // const Cluster& nextCluster{nextLayerClusters[iNextLayerCluster]}; - // const float deltaZ{o2::gpu::GPUCommonMath::Abs( - // tanLambda * (nextCluster.radius - currentCluster.radius) + currentCluster.zCoordinate - nextCluster.zCoordinate)}; - // const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi)}; - // if (deltaZ < kTrkPar.TrackletMaxDeltaZ[layerIndex] && (deltaPhi < kTrkPar.TrackletMaxDeltaPhi || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < kTrkPar.TrackletMaxDeltaPhi)) { - // cooperative_groups::coalesced_group threadGroup = cooperative_groups::coalesced_threads(); - // int currentIndex{}; - // if (threadGroup.thread_rank() == 0) { - // currentIndex = trackletsVector.extend(threadGroup.size()); - // } - // currentIndex = threadGroup.shfl(currentIndex, 0) + threadGroup.thread_rank(); - // trackletsVector.emplace(currentIndex, currentClusterIndex, iNextLayerCluster, currentCluster, nextCluster); - // ++clusterTrackletsNum; - // } - // } - // } - // if (layerIndex > 0) { - // devStore.getTrackletsPerClusterTable()[layerIndex - 1][currentClusterIndex] = clusterTrackletsNum; - // } - // } } } @@ -190,11 +192,11 @@ GPUg() void computeLayerTrackletsKernel( // const Tracklet& nextTracklet{devStore.getTracklets()[layerIndex + 1][iNextLayerTracklet]}; // const float deltaTanLambda{o2::gpu::GPUCommonMath::Abs(currentTracklet.tanLambda - nextTracklet.tanLambda)}; // const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentTracklet.phi - nextTracklet.phi)}; -// if (deltaTanLambda < kTrkPar.CellMaxDeltaTanLambda && (deltaPhi < kTrkPar.CellMaxDeltaPhi || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < kTrkPar.CellMaxDeltaPhi)) { +// if (deltaTanLambda < trkPars->CellMaxDeltaTanLambda && (deltaPhi < trkPars->CellMaxDeltaPhi || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < trkPars->CellMaxDeltaPhi)) { // const float averageTanLambda{0.5f * (currentTracklet.tanLambda + nextTracklet.tanLambda)}; // const float directionZIntersection{-averageTanLambda * firstCellCluster.radius + firstCellCluster.zCoordinate}; // const float deltaZ{o2::gpu::GPUCommonMath::Abs(directionZIntersection - primaryVertex.z)}; -// if (deltaZ < kTrkPar.CellMaxDeltaZ[layerIndex]) { +// if (deltaZ < trkPars->CellMaxDeltaZ[layerIndex]) { // const Cluster& thirdCellCluster{ // devStore.getClusters()[layerIndex + 2][nextTracklet.secondClusterIndex]}; // const float thirdCellClusterQuadraticRCoordinate{thirdCellCluster.radius * thirdCellCluster.radius}; @@ -213,7 +215,7 @@ GPUg() void computeLayerTrackletsKernel( // const float2 circleCenter{-0.5f * normalizedPlaneVector.x / normalizedPlaneVector.z, -0.5f * normalizedPlaneVector.y / normalizedPlaneVector.z}; // const float distanceOfClosestApproach{o2::gpu::GPUCommonMath::Abs( // cellTrajectoryRadius - o2::gpu::GPUCommonMath::Sqrt(circleCenter.x * circleCenter.x + circleCenter.y * circleCenter.y))}; -// if (distanceOfClosestApproach <= kTrkPar.CellMaxDCA[layerIndex]) { +// if (distanceOfClosestApproach <= trkPars->CellMaxDCA[layerIndex]) { // cooperative_groups::coalesced_group threadGroup = cooperative_groups::coalesced_threads(); // int currentIndex{}; // if (threadGroup.thread_rank() == 0) { @@ -282,29 +284,25 @@ void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const M template void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { - checkGPUError(cudaMemcpyToSymbol(gpu::kTrkPar, &mTrkParams, sizeof(gpu::StaticTrackingParameters)), __FILE__, __LINE__); - checkGPUError(cudaMemcpyToSymbol(gpu::kIndexTableUtils, &mTimeFrameGPU->mIndexTableUtils, sizeof(IndexTableUtils)), __FILE__, __LINE__); + LOG(info) << " >>> " << mTrkParams.DeltaROF; std::array tempSize; - // std::array trackletsNum; std::array streamArray; - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - // // primaryVertexContext->getTempTrackletArray()[iLayer].reset( - // // static_cast(primaryVertexContext->getDeviceTracklets()[iLayer + 1].capacity())); - // cub::DeviceScan::ExclusiveSum(static_cast(NULL), tempSize[iLayer], - // primaryVertexContext->getDeviceTrackletsPerClustersTable()[iLayer].get(), - // primaryVertexContext->getDeviceTrackletsLookupTable()[iLayer].get(), - // primaryVertexContext->getClusters()[iLayer + 1].size()); // N clusters layer+1 - // primaryVertexContext->getTempTableArray()[iLayer].reset(static_cast(tempSize[iLayer])); - // } - - // cudaDeviceSynchronize(); - // const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); - // gsl::span diamondSpan(&diamondVert, 1); - for (int rof0{0}; rof0 < mTimeFrameGPU->getNrof(); ++rof0) { + const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); + gsl::span diamondSpan(&diamondVert, 1); + for (int rof0{1}; rof0 < /*mTimeFrameGPU->getNrof()*/ 2; ++rof0) { for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - // gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0);// replace with GPU one - int minRof = (rof0 >= mTrkParams.DeltaROF) ? rof0 - mTrkParams.DeltaROF : 0; + gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one + std::vector paddedVertices; + for (int iVertex{0}; iVertex < mTimeFrameGPU->getConfig().maxVerticesCapacity; ++iVertex) { + if (iVertex < primaryVertices.size()) { + paddedVertices.emplace_back(primaryVertices[iVertex]); + } else { + paddedVertices.emplace_back(Vertex()); + } + } + checkGPUError(cudaMemcpy(mTimeFrameGPU->getDeviceVertices(rof0), paddedVertices.data(), mTimeFrameGPU->getConfig().maxVerticesCapacity * sizeof(Vertex), cudaMemcpyHostToDevice), __FILE__, __LINE__); + // int minRof = (rof0 >= mTrkParams.DeltaROF) ? rof0 - mTrkParams.DeltaROF : 0; int maxRof = (rof0 == mTimeFrameGPU->getNrof() - mTrkParams.DeltaROF) ? rof0 : rof0 + mTrkParams.DeltaROF; const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; @@ -316,33 +314,41 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) } // Reset used tracklets checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), false, sizeof(unsigned char) * mTimeFrameGPU->getNClustersLayer(rof0, iLayer)), __FILE__, __LINE__); - gpu::computeLayerTrackletsKernel<<>>( + gpu::computeLayerTrackletsKernel<<<1, 1, 0, streamArray[iLayer].get()>>>( rof0, mTimeFrameGPU->getNrof(), iLayer, - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), - mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), // :check: + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: + mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), // :check: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer + 1), // WRONG, to be fixed! mTimeFrameGPU->getDeviceVertices(rof0), + mTimeFrameGPU->getDeviceTrackletsLookupTable(rof0, iLayer), mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), mTimeFrameGPU->getConfig().maxVerticesCapacity, mTimeFrameGPU->getSortedStartIndex(rof0, iLayer), + mTimeFrameGPU->getSortedStartIndex(rof0, iLayer + 1), // WRONG, to be fixed! mTimeFrameGPU->getNClustersLayer(rof0, iLayer), mTimeFrameGPU->getPhiCut(iLayer), mTimeFrameGPU->getMinR(iLayer + 1), mTimeFrameGPU->getMaxR(iLayer + 1), meanDeltaR, mTimeFrameGPU->getPositionResolution(iLayer), - mTimeFrameGPU->getMSangle(iLayer)); - } /*else { - gpu::layerTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), - iLayer, primaryVertexContext->getTempTrackletArray()[iLayer - 1].getWeakCopy()); - }*/ + mTimeFrameGPU->getMSangle(iLayer), + mTimeFrameGPU->getDeviceTrackingParameters(), + mTimeFrameGPU->getDeviceIndexTableUtils()); + // } + /*else { + gpu::layerTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), + iLayer, primaryVertexContext->getTempTrackletArray()[iLayer - 1].getWeakCopy()); + }*/ + } } } - // cudaDeviceSynchronize(); + checkGPUError(cudaDeviceSynchronize(), __FILE__, __LINE__); // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 8b89f37e28896..1e903cc778370 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -116,6 +116,7 @@ void TrackerTraits::computeLayerTracklets(const int iteration) int iPhiBin = (selectedBinsRect.y + iPhiCount) % mTrkParams[iteration].PhiBins; const int firstBinIndex{tf->mIndexTableUtils.getBinIndex(selectedBinsRect.x, iPhiBin)}; const int maxBinIndex{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}; + printf("%d %d %d %d %d %d %d\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0); if constexpr (debugLevel) { if (firstBinIndex < 0 || firstBinIndex > tf->getIndexTable(rof1, iLayer + 1).size() || maxBinIndex < 0 || maxBinIndex > tf->getIndexTable(rof1, iLayer + 1).size()) { diff --git a/Detectors/ITSMFT/ITS/workflow/include/ITSWorkflow/TrackerSpec.h b/Detectors/ITSMFT/ITS/workflow/include/ITSWorkflow/TrackerSpec.h index 7c23f93067a1a..8e531a197044b 100644 --- a/Detectors/ITSMFT/ITS/workflow/include/ITSWorkflow/TrackerSpec.h +++ b/Detectors/ITSMFT/ITS/workflow/include/ITSWorkflow/TrackerSpec.h @@ -60,6 +60,7 @@ class TrackerDPL : public framework::Task std::shared_ptr mGGCCDBRequest; const o2::itsmft::TopologyDictionary* mDict = nullptr; std::unique_ptr mRecChain = nullptr; + std::unique_ptr mRecChainCPU = nullptr; std::unique_ptr mChainITS = nullptr; std::unique_ptr mTracker = nullptr; std::unique_ptr mVertexer = nullptr; diff --git a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx index 7e7fb817142a2..b0070119d8755 100644 --- a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx +++ b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx @@ -61,7 +61,8 @@ void TrackerDPL::init(InitContext& ic) mTimer.Reset(); o2::base::GRPGeomHelper::instance().setRequest(mGGCCDBRequest); mChainITS.reset(mRecChain->AddChain()); - mVertexer = std::make_unique(mChainITS->GetITSVertexerTraits()); + mChainITSCPU.reset(mRecChainCPU->AddChain()); + mVertexer = std::make_unique(mChainITSCPU->GetITSVertexerTraits()); mTracker = std::make_unique(mChainITS->GetITSTrackerTraits()); mRunVertexer = true; mCosmicsProcessing = false; From df5aa3e23670e2bc9c1f0d651ebdf61fc0d448da Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Wed, 25 May 2022 11:59:00 +0200 Subject: [PATCH 08/30] Trackleting for L0 produces same indices --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 22 ++++++++- .../ITS/tracking/GPU/ITStrackingGPU/Utils.h | 2 - .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 15 ++++--- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 45 +++++++++++++------ .../tracking/include/ITStracking/TimeFrame.h | 2 +- 5 files changed, 61 insertions(+), 25 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 947dc8c24cdb0..90b2387b5e7a7 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -43,6 +43,7 @@ namespace gpu template struct StaticTrackingParameters { StaticTrackingParameters& operator=(const StaticTrackingParameters& t) = default; + void set(const TrackingParameters& pars); // int CellMinimumLevel(); /// General parameters int ClusterSharing = 0; @@ -53,8 +54,8 @@ struct StaticTrackingParameters { int ZBins{256}; int PhiBins{128}; /// Trackleting cuts - float TrackletMaxDeltaPhi = 0.3f; - float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; + // float TrackletMaxDeltaPhi = 0.3f; + // float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; /// Cell finding cuts // float CellMaxDeltaTanLambda = 0.025f; // float CellMaxDCA[NLayers - 2] = {0.05f, 0.04f, 0.05f, 0.2f, 0.4f}; @@ -65,6 +66,23 @@ struct StaticTrackingParameters { // float NeighbourMaxDeltaN[NLayers - 3] = {0.002f, 0.0090f, 0.002f, 0.005f}; }; +template +void StaticTrackingParameters::set(const TrackingParameters& pars) +{ + ClusterSharing = pars.ClusterSharing; + MinTrackLength = pars.MinTrackLength; + NSigmaCut = pars.NSigmaCut; + PVres = pars.PVres; + DeltaROF = pars.DeltaROF; + ZBins = pars.ZBins; + PhiBins = pars.PhiBins; + // TrackletMaxDeltaPhi = pars.TrackletMaxDeltaPhi; + // for (int i = 0; i < NLayers - 1; i++) { + // TrackletMaxDeltaZ[i] = pars.TrackletMaxDeltaZ[i]; + // } +} + + template GPUhd() T* getPtrFromRuler(int index, T* src, const int* ruler, const int stride = 1) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Utils.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Utils.h index 71ef7dad82691..acd28de061690 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Utils.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Utils.h @@ -25,10 +25,8 @@ namespace its { namespace gpu { - namespace utils { - namespace host { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index ece072b505c34..4a7651f0a3bdb 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -25,7 +25,7 @@ namespace its using constants::MB; namespace gpu { - +using utils::host::checkGPUError; GPUh() void gpuThrowOnError() { cudaError_t error = cudaGetLastError(); @@ -104,7 +104,7 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) for (int iLayer{0}; iLayer < NLayers; ++iLayer) { // Tracker and vertexer // mClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - mUsedClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + // mUsedClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; // mTrackingFrameInfoD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; // mClusterExternalIndicesD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; // mROframesClustersD[iLayer] = Vector{mConfig.nMaxROFs, mConfig.nMaxROFs}; @@ -138,12 +138,15 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) mClustersD[iLayer].reset(mClusters[iLayer].data(), static_cast(mClusters[iLayer].size())); } if constexpr (isTracker) { - discardResult(cudaMalloc(reinterpret_cast(&mDeviceTrackingParams), sizeof(gpu::StaticTrackingParameters))); - discardResult(cudaMalloc(reinterpret_cast(&mDeviceIndexTableUtils), sizeof(IndexTableUtils))); - discardResult(cudaMemcpy(mDeviceTrackingParams, &trkParam, sizeof(gpu::StaticTrackingParameters), cudaMemcpyHostToDevice)); - discardResult(cudaMemcpy(mDeviceIndexTableUtils, &mIndexTableUtils, sizeof(IndexTableUtils), cudaMemcpyHostToDevice)); + StaticTrackingParameters pars; + pars.set(trkParam); + checkGPUError(cudaMalloc(reinterpret_cast(&mDeviceTrackingParams), sizeof(gpu::StaticTrackingParameters)), __FILE__, __LINE__); + checkGPUError(cudaMalloc(reinterpret_cast(&mDeviceIndexTableUtils), sizeof(IndexTableUtils)), __FILE__, __LINE__); + checkGPUError(cudaMemcpy(mDeviceTrackingParams, &pars, sizeof(gpu::StaticTrackingParameters), cudaMemcpyHostToDevice), __FILE__, __LINE__); + checkGPUError(cudaMemcpy(mDeviceIndexTableUtils, &mIndexTableUtils, sizeof(IndexTableUtils), cudaMemcpyHostToDevice), __FILE__, __LINE__); // Tracker-only: we don't need to copy data in vertexer for (int iLayer{0}; iLayer < NLayers; ++iLayer) { + mUsedClustersD[iLayer].reset(mUsedClusters[iLayer].data(), static_cast(mUsedClusters[iLayer].size())); mTrackingFrameInfoD[iLayer].reset(mTrackingFrameInfo[iLayer].data(), static_cast(mTrackingFrameInfo[iLayer].size())); mClusterExternalIndicesD[iLayer].reset(mClusterExternalIndices[iLayer].data(), static_cast(mClusterExternalIndices[iLayer].size())); mROframesClustersD[iLayer].reset(mROframesClusters[iLayer].data(), static_cast(mROframesClusters[iLayer].size())); diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 61c10083457e6..61b78012b5e3b 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -59,7 +59,7 @@ GPUd() float Sq(float q) return q * q; } -template +template GPUg() void computeLayerTrackletsKernel( const int rof0, const int maxRofs, @@ -86,7 +86,7 @@ GPUg() void computeLayerTrackletsKernel( const StaticTrackingParameters* trkPars, const IndexTableUtils* utils) { - int clusterTrackletsNum = 0; + // int clusterTrackletsNum = 0; for (int currentClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; currentClusterIndex < sizeCurrentLClusters; currentClusterIndex += blockDim.x * gridDim.x) { const Cluster& currentCluster{clustersCurrentLayer[currentClusterIndex]}; const int currentSortedIndex{startSortedIndexLayer + currentClusterIndex}; @@ -95,9 +95,6 @@ GPUg() void computeLayerTrackletsKernel( } int minRof = (rof0 >= trkPars->DeltaROF) ? rof0 - trkPars->DeltaROF : 0; int maxRof = (rof0 == maxRofs - trkPars->DeltaROF) ? rof0 : rof0 + trkPars->DeltaROF; - if (!currentClusterIndex) { - printf(" >>>> %d\n", trkPars->DeltaROF); - } const float inverseR0{1.f / currentCluster.radius}; for (int iPrimaryVertex{0}; iPrimaryVertex < nVertices; iPrimaryVertex++) { const auto& primaryVertex{vertices[iPrimaryVertex]}; @@ -284,7 +281,6 @@ void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const M template void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { - LOG(info) << " >>> " << mTrkParams.DeltaROF; std::array tempSize; std::array streamArray; @@ -312,9 +308,9 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); continue; } - // Reset used tracklets - checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), false, sizeof(unsigned char) * mTimeFrameGPU->getNClustersLayer(rof0, iLayer)), __FILE__, __LINE__); - gpu::computeLayerTrackletsKernel<<<1, 1, 0, streamArray[iLayer].get()>>>( + // // Reset used tracklets + // checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), false, sizeof(unsigned char) * mTimeFrameGPU->getNClustersLayer(rof0, iLayer)), __FILE__, __LINE__); + gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( rof0, mTimeFrameGPU->getNrof(), iLayer, @@ -339,11 +335,32 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getMSangle(iLayer), mTimeFrameGPU->getDeviceTrackingParameters(), mTimeFrameGPU->getDeviceIndexTableUtils()); - // } - /*else { - gpu::layerTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), - iLayer, primaryVertexContext->getTempTrackletArray()[iLayer - 1].getWeakCopy()); - }*/ + } else { + // gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( + // rof0, + // mTimeFrameGPU->getNrof(), + // iLayer, + // mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), // :check: + // mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: + // mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: + // mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: + // mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), // :check: + // mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer + 1), // WRONG, to be fixed! + // mTimeFrameGPU->getDeviceVertices(rof0), + // mTimeFrameGPU->getDeviceTrackletsLookupTable(rof0, iLayer), + // mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), + // mTimeFrameGPU->getConfig().maxVerticesCapacity, + // mTimeFrameGPU->getSortedStartIndex(rof0, iLayer), + // mTimeFrameGPU->getSortedStartIndex(rof0, iLayer + 1), // WRONG, to be fixed! + // mTimeFrameGPU->getNClustersLayer(rof0, iLayer), + // mTimeFrameGPU->getPhiCut(iLayer), + // mTimeFrameGPU->getMinR(iLayer + 1), + // mTimeFrameGPU->getMaxR(iLayer + 1), + // meanDeltaR, + // mTimeFrameGPU->getPositionResolution(iLayer), + // mTimeFrameGPU->getMSangle(iLayer), + // mTimeFrameGPU->getDeviceTrackingParameters(), + // mTimeFrameGPU->getDeviceIndexTableUtils()); } } } diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h index 6572a3d3b9001..5f09062cb880c 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h @@ -178,6 +178,7 @@ class TimeFrame std::vector> mClusterExternalIndices; std::vector> mROframesClusters; std::vector> mIndexTables; + std::vector> mUsedClusters; int mNrof = 0; private: @@ -200,7 +201,6 @@ class TimeFrame std::vector mPrimaryVertices; std::vector> mPValphaX; /// PV x and alpha for track propagation std::vector> mUnsortedClusters; - std::vector> mUsedClusters; const dataformats::MCTruthContainer* mClusterLabels = nullptr; std::vector> mTrackletLabels; std::vector> mCellLabels; From c0251bc3b0d0d438c45c36bf43628bac603f5b5e Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Wed, 25 May 2022 18:25:40 +0200 Subject: [PATCH 09/30] Delete stale files --- .../GPU/ITStrackingGPU/DeviceStoreGPU.h | 132 ------------- .../ITS/tracking/GPU/cuda/DeviceStoreGPU.cu | 184 ------------------ .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 15 +- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 125 ++++++------ .../tracking/include/ITStracking/TimeFrame.h | 3 +- .../ITSMFT/ITS/tracking/src/TimeFrame.cxx | 26 ++- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 4 +- 7 files changed, 97 insertions(+), 392 deletions(-) delete mode 100644 Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/DeviceStoreGPU.h delete mode 100644 Detectors/ITSMFT/ITS/tracking/GPU/cuda/DeviceStoreGPU.cu diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/DeviceStoreGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/DeviceStoreGPU.h deleted file mode 100644 index 107d94e8e3e34..0000000000000 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/DeviceStoreGPU.h +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright 2019-2020 CERN and copyright holders of ALICE O2. -// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. -// All rights not expressly granted are reserved. -// -// This software is distributed under the terms of the GNU General Public -// License v3 (GPL Version 3), copied verbatim in the file "COPYING". -// -// In applying this license CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. -/// -/// \file DeviceStoreNV.h -/// \brief -/// - -#ifndef ITSTRACKINGGPU_DEVICESTOREGPU_H_ -#define ITSTRACKINGGPU_DEVICESTOREGPU_H_ - -#ifndef GPUCA_GPUCODE_GENRTC -#include -#include -#endif - -#include "ITStracking/Cell.h" -#include "ITStracking/Configuration.h" -#include "ITStracking/Cluster.h" -#include "ITStracking/Constants.h" -#include "ITStracking/Tracklet.h" - -#include "Array.h" -#include "UniquePointer.h" -#include "Vector.h" - -namespace o2 -{ -namespace its -{ - -namespace gpu -{ - -class DeviceStoreNV final -{ - public: - DeviceStoreNV(); - - UniquePointer initialise(const float3&, - const std::array, constants::its2::LayersNumber>&, - const std::array, constants::its2::TrackletsPerRoad>&, - const std::array, constants::its2::CellsPerRoad>&, - const std::array, constants::its2::CellsPerRoad - 1>&, - const std::array&, - const std::array&); - GPUd() const float3& getPrimaryVertex() { return *mPrimaryVertex; }; - GPUhd() Array, constants::its2::LayersNumber>& getClusters(); - GPUd() Array, - constants::its2::TrackletsPerRoad>& getIndexTables() { return mIndexTables; }; - GPUhd() Array, constants::its2::TrackletsPerRoad>& getTracklets(); - GPUhd() Array, constants::its2::CellsPerRoad>& getTrackletsLookupTable(); - GPUhd() Array, constants::its2::CellsPerRoad>& getTrackletsPerClusterTable(); - GPUhd() Array, constants::its2::CellsPerRoad>& getCells(); - GPUhd() Array, constants::its2::CellsPerRoad - 1>& getCellsLookupTable(); - GPUhd() Array, constants::its2::CellsPerRoad - 1>& getCellsPerTrackletTable(); - Array, constants::its2::CellsPerRoad>& getTempTableArray(); - - GPUhd() float getRmin(int layer); - GPUhd() float getRmax(int layer); - - private: - UniquePointer mPrimaryVertex; - Array, constants::its2::LayersNumber> mClusters; - Array mRmin; - Array mRmax; - Array, constants::its2::TrackletsPerRoad> - mIndexTables; - Array, constants::its2::TrackletsPerRoad> mTracklets; - Array, constants::its2::CellsPerRoad> mTrackletsLookupTable; - Array, constants::its2::CellsPerRoad> mTrackletsPerClusterTable; - Array, constants::its2::CellsPerRoad> mCells; - Array, constants::its2::CellsPerRoad - 1> mCellsLookupTable; - Array, constants::its2::CellsPerRoad - 1> mCellsPerTrackletTable; -}; - -GPUhd() Array, constants::its2::LayersNumber>& DeviceStoreNV::getClusters() -{ - return mClusters; -} - -GPUd() Array, constants::its2::TrackletsPerRoad>& DeviceStoreNV::getTracklets() -{ - return mTracklets; -} - -GPUd() Array, constants::its2::CellsPerRoad>& DeviceStoreNV::getTrackletsLookupTable() -{ - return mTrackletsLookupTable; -} - -GPUd() Array, constants::its2::CellsPerRoad>& DeviceStoreNV::getTrackletsPerClusterTable() -{ - return mTrackletsPerClusterTable; -} - -GPUhd() Array, constants::its2::CellsPerRoad>& DeviceStoreNV::getCells() -{ - return mCells; -} - -GPUhd() Array, constants::its2::CellsPerRoad - 1>& DeviceStoreNV::getCellsLookupTable() -{ - return mCellsLookupTable; -} - -GPUhd() Array, constants::its2::CellsPerRoad - 1>& DeviceStoreNV::getCellsPerTrackletTable() -{ - return mCellsPerTrackletTable; -} - -GPUhd() float DeviceStoreNV::getRmin(int layer) -{ - return mRmin[layer]; -} - -GPUhd() float DeviceStoreNV::getRmax(int layer) -{ - return mRmax[layer]; -} - -} // namespace gpu -} // namespace its -} // namespace o2 -#endif diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/DeviceStoreGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/DeviceStoreGPU.cu deleted file mode 100644 index 3a3fd910226ea..0000000000000 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/DeviceStoreGPU.cu +++ /dev/null @@ -1,184 +0,0 @@ -// Copyright 2019-2020 CERN and copyright holders of ALICE O2. -// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. -// All rights not expressly granted are reserved. -// -// This software is distributed under the terms of the GNU General Public -// License v3 (GPL Version 3), copied verbatim in the file "COPYING". -// -// In applying this license CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. -/// -/// \file DeviceStoreNV.cxx -/// \brief -/// - -#include "ITStrackingGPU/DeviceStoreGPU.h" -#include "ITStrackingGPU/Stream.h" -#include - -namespace -{ - -using namespace o2::its; - -__device__ void fillIndexTables(o2::its::gpu::DeviceStoreNV& primaryVertexContext, const int layerIndex) -{ - - const int currentClusterIndex{static_cast(blockDim.x * blockIdx.x + threadIdx.x)}; - const int nextLayerClustersNum{static_cast(primaryVertexContext.getClusters()[layerIndex + 1].size())}; - - if (currentClusterIndex < nextLayerClustersNum) { - - const int currentBinIndex{ - primaryVertexContext.getClusters()[layerIndex + 1][currentClusterIndex].indexTableBinIndex}; - int previousBinIndex; - - if (currentClusterIndex == 0) { - - primaryVertexContext.getIndexTables()[layerIndex][0] = 0; - previousBinIndex = 0; - - } else { - - previousBinIndex = primaryVertexContext.getClusters()[layerIndex + 1][currentClusterIndex - 1].indexTableBinIndex; - } - - if (currentBinIndex > previousBinIndex) { - - for (int iBin{previousBinIndex + 1}; iBin <= currentBinIndex; ++iBin) { - - primaryVertexContext.getIndexTables()[layerIndex][iBin] = currentClusterIndex; - } - - previousBinIndex = currentBinIndex; - } - - if (currentClusterIndex == nextLayerClustersNum - 1) { - - for (int iBin{currentBinIndex + 1}; iBin <= o2::its::constants::its2::ZBins * o2::its::constants::its2::PhiBins; - iBin++) { - - primaryVertexContext.getIndexTables()[layerIndex][iBin] = nextLayerClustersNum; - } - } - } -} - -__device__ void fillTrackletsPerClusterTables(o2::its::gpu::DeviceStoreNV& primaryVertexContext, const int layerIndex) -{ - const int currentClusterIndex{static_cast(blockDim.x * blockIdx.x + threadIdx.x)}; - const int clustersSize{static_cast(primaryVertexContext.getClusters()[layerIndex + 1].size())}; - - if (currentClusterIndex < clustersSize) { - - primaryVertexContext.getTrackletsPerClusterTable()[layerIndex][currentClusterIndex] = 0; - } -} - -__device__ void fillCellsPerClusterTables(o2::its::gpu::DeviceStoreNV& primaryVertexContext, const int layerIndex) -{ - const int totalThreadNum{static_cast(primaryVertexContext.getClusters()[layerIndex + 1].size())}; - const int trackletsSize{static_cast(primaryVertexContext.getTracklets()[layerIndex + 1].capacity())}; - const int trackletsPerThread{1 + (trackletsSize - 1) / totalThreadNum}; - const int firstTrackletIndex{static_cast(blockDim.x * blockIdx.x + threadIdx.x) * trackletsPerThread}; - - if (firstTrackletIndex < trackletsSize) { - - const int trackletsToSet{min(trackletsSize, firstTrackletIndex + trackletsPerThread) - firstTrackletIndex}; - memset(&primaryVertexContext.getCellsPerTrackletTable()[layerIndex][firstTrackletIndex], 0, - trackletsToSet * sizeof(int)); - } -} - -__global__ void fillDeviceStructures(o2::its::gpu::DeviceStoreNV& primaryVertexContext, const int layerIndex) -{ - fillIndexTables(primaryVertexContext, layerIndex); - - if (layerIndex < o2::its::constants::its2::CellsPerRoad) { - - fillTrackletsPerClusterTables(primaryVertexContext, layerIndex); - } - - if (layerIndex < o2::its::constants::its2::CellsPerRoad - 1) { - - fillCellsPerClusterTables(primaryVertexContext, layerIndex); - } -} -} // namespace - -namespace o2 -{ -namespace its -{ -namespace gpu -{ - -DeviceStoreNV::DeviceStoreNV() = default; - -UniquePointer DeviceStoreNV::initialise(const float3& primaryVertex, - const std::array, constants::its2::LayersNumber>& clusters, - const std::array, constants::its2::TrackletsPerRoad>& tracklets, - const std::array, constants::its2::CellsPerRoad>& cells, - const std::array, constants::its2::CellsPerRoad - 1>& cellsLookupTable, - const std::array& rmin, - const std::array& rmax) -{ - mPrimaryVertex = UniquePointer{primaryVertex}; - - for (int iLayer{0}; iLayer < constants::its2::LayersNumber; ++iLayer) { - this->mRmin[iLayer] = rmin[iLayer]; - this->mRmax[iLayer] = rmax[iLayer]; - - this->mClusters[iLayer] = - Vector{&clusters[iLayer][0], static_cast(clusters[iLayer].size())}; - - if (iLayer < constants::its2::TrackletsPerRoad) { - this->mTracklets[iLayer].reset(tracklets[iLayer].capacity()); - } - - if (iLayer < constants::its2::CellsPerRoad) { - - this->mTrackletsLookupTable[iLayer].reset(static_cast(clusters[iLayer + 1].size())); - this->mTrackletsPerClusterTable[iLayer].reset(static_cast(clusters[iLayer + 1].size())); - this->mCells[iLayer].reset(static_cast(cells[iLayer].capacity())); - } - - if (iLayer < constants::its2::CellsPerRoad - 1) { - - this->mCellsLookupTable[iLayer].reset(static_cast(cellsLookupTable[iLayer].size())); - this->mCellsPerTrackletTable[iLayer].reset(static_cast(cellsLookupTable[iLayer].size())); - } - } - - UniquePointer gpuContextDevicePointer{*this}; - - std::array streamArray; - - for (int iLayer{0}; iLayer < constants::its2::TrackletsPerRoad; ++iLayer) { - - const int nextLayerClustersNum = static_cast(clusters[iLayer + 1].size()); - - dim3 threadsPerBlock{utils::host::getBlockSize(nextLayerClustersNum)}; - dim3 blocksGrid{utils::host::getBlocksGrid(threadsPerBlock, nextLayerClustersNum)}; - - fillDeviceStructures<<>>(*gpuContextDevicePointer, iLayer); - - cudaError_t error = cudaGetLastError(); - - if (error != cudaSuccess) { - - std::ostringstream errorString{}; - errorString << __FILE__ << ":" << __LINE__ << " CUDA API returned error [" << cudaGetErrorString(error) - << "] (code " << error << ")" << std::endl; - - throw std::runtime_error{errorString.str()}; - } - } - - return gpuContextDevicePointer; -} - -} // namespace gpu -} // namespace its -} // namespace o2 diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 4a7651f0a3bdb..cb6268a990932 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -40,6 +40,7 @@ GPUh() void gpuThrowOnError() template TimeFrameGPU::TimeFrameGPU() { + mIsGPU = true; getDeviceMemory(); } @@ -103,22 +104,17 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) { for (int iLayer{0}; iLayer < NLayers; ++iLayer) { // Tracker and vertexer - // mClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - // mUsedClustersD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - // mTrackingFrameInfoD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - // mClusterExternalIndicesD[iLayer] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; - // mROframesClustersD[iLayer] = Vector{mConfig.nMaxROFs, mConfig.nMaxROFs}; if (iLayer < NLayers - 1) { mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; - mIndexTablesD[iLayer] = Vector{mConfig.nMaxROFs * (256 * 128 + 1), mConfig.nMaxROFs * (256 * 128 + 1)}; + } + if (iLayer < NLayers - 2) { + mTrackletsLookupTablesD[iLayer].reset(mTrackletsLookupTable[iLayer].data(), static_cast(mTrackletsLookupTable[iLayer].size())); } } for (auto iComb{0}; iComb < 2; ++iComb) { // Vertexer only mNTrackletsPerClusterD[iComb] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; } - // mIndexTablesLayer0D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; - // mIndexTablesLayer2D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; mLines = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; mNFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mNExclusiveFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; @@ -150,6 +146,9 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) mTrackingFrameInfoD[iLayer].reset(mTrackingFrameInfo[iLayer].data(), static_cast(mTrackingFrameInfo[iLayer].size())); mClusterExternalIndicesD[iLayer].reset(mClusterExternalIndices[iLayer].data(), static_cast(mClusterExternalIndices[iLayer].size())); mROframesClustersD[iLayer].reset(mROframesClusters[iLayer].data(), static_cast(mROframesClusters[iLayer].size())); + if (iLayer < NLayers - 1) { + mIndexTablesD[iLayer].reset(mFlatIndexTables[iLayer].data(), static_cast(mFlatIndexTables[iLayer].size())); + } } } else { mIndexTablesLayer0D.reset(getIndexTableWhole(0).data(), static_cast(getIndexTableWhole(0).size())); diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 61b78012b5e3b..4ee5ca276b31f 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -68,14 +68,13 @@ GPUg() void computeLayerTrackletsKernel( const Cluster* clustersCurrentLayer, // input data rof0 const int* indexTable, // input data rof0-delta PhiBins; } - int indexTableOffset{0}; // used to access on rof-based index constexpr int tableSize{256 * 128 + 1}; // hardcoded for the time being - for (int rof1{minRof}; rof1 <= maxRof; ++rof1, indexTableOffset += tableSize) { - // // gsl::span layer1 = tf->getClustersOnLayer(rof1, iLayer + 1); - if (!(roFrameClusters[rof1 + 1] - roFrameClusters[rof1])) { + for (int rof1{minRof}; rof1 <= maxRof; ++rof1) { + if (!(roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1])) { // number of clusters on next layer > 0 continue; } + for (int iPhiCount{0}; iPhiCount < phiBinsNum; iPhiCount++) { int iPhiBin = (selectedBinsRect.y + iPhiCount) % trkPars->PhiBins; const int firstBinIndex{utils->getBinIndex(selectedBinsRect.x, iPhiBin)}; const int maxBinIndex{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}; - printf("%d %d %d %d %d %d %d\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0); - // const int firstRowClusterIndex = indexTable[indexTableOffset + firstBinIndex]; - // const int maxRowClusterIndex = indexTable[indexTableOffset + maxBinIndex]; + const int firstRowClusterIndex = indexTable[rof1 * tableSize + firstBinIndex]; + const int maxRowClusterIndex = indexTable[rof1 * tableSize + maxBinIndex]; + for (int iNextCluster{firstRowClusterIndex}; iNextCluster < maxRowClusterIndex; ++iNextCluster) { + if (iNextCluster >= roFrameClustersNext[rof1]) { + break; + } + + const Cluster& nextCluster{getPtrFromRuler(rof1, clustersNextLayer, roFrameClustersNext)[iNextCluster]}; + // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate); - // for (int iNextCluster{firstRowClusterIndex}; iNextCluster < maxRowClusterIndex; ++iNextCluster) { - // if (iNextCluster >= roFrameClusters[rof1]) { - // break; - // } - // const Cluster& nextCluster{getPtrFromRuler(rof0, clustersNextLayer, roFrameClusters)[iNextCluster]}; - // if (usedClustersNextLayer[nextCluster.clusterId]) { - // continue; - // } - // const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi)}; - // const float deltaZ{o2::gpu::GPUCommonMath::Abs(tanLambda * (nextCluster.radius - currentCluster.radius) + - // currentCluster.zCoordinate - nextCluster.zCoordinate)}; - // if (deltaZ / sigmaZ < trkPars->NSigmaCut && (deltaPhi < phiCut || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < phiCut)) { - // if constexpr (!Layer0) { - // trackletsLookUpTable[currentSortedIndex]++; // Should be race-condition safe - // } - // const float phi{o2::gpu::GPUCommonMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, - // currentCluster.xCoordinate - nextCluster.xCoordinate)}; - // const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / - // (currentCluster.radius - nextCluster.radius)}; - // // Missing actual saving of the tracklet - // } - // } + if (usedClustersNextLayer[nextCluster.clusterId]) { + continue; + } + const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi)}; + const float deltaZ{o2::gpu::GPUCommonMath::Abs(tanLambda * (nextCluster.radius - currentCluster.radius) + + currentCluster.zCoordinate - nextCluster.zCoordinate)}; + if (deltaZ / sigmaZ < trkPars->NSigmaCut && (deltaPhi < phiCut || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < phiCut)) { + if constexpr (!Layer0) { + trackletsLookUpTable[currentSortedIndex]++; // Should be race-condition safe + } + const float phi{o2::gpu::GPUCommonMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, + currentCluster.xCoordinate - nextCluster.xCoordinate)}; + const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / + (currentCluster.radius - nextCluster.radius)}; + Tracklet t{currentSortedIndex, roFrameClustersNext[rof1] + iNextCluster, tanL, phi, rof0, rof1}; + printf("%d %d %lf %lf %hu %hu\n", t.firstClusterIndex, t.secondClusterIndex, t.tanLambda, t.phi, t.rof[0], t.rof[1]); + } + } } } } @@ -286,7 +286,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); gsl::span diamondSpan(&diamondVert, 1); - for (int rof0{1}; rof0 < /*mTimeFrameGPU->getNrof()*/ 2; ++rof0) { + for (int rof0{476}; rof0 < /*mTimeFrameGPU->getNrof()*/ 477; ++rof0) { for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one std::vector paddedVertices; @@ -308,24 +308,22 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); continue; } - // // Reset used tracklets - // checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), false, sizeof(unsigned char) * mTimeFrameGPU->getNClustersLayer(rof0, iLayer)), __FILE__, __LINE__); + gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( rof0, mTimeFrameGPU->getNrof(), iLayer, - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), // :check: + mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), // :check: mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer + 1), // WRONG, to be fixed! mTimeFrameGPU->getDeviceVertices(rof0), - mTimeFrameGPU->getDeviceTrackletsLookupTable(rof0, iLayer), + nullptr, // :check: tracklet lookup table not needed mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), mTimeFrameGPU->getConfig().maxVerticesCapacity, - mTimeFrameGPU->getSortedStartIndex(rof0, iLayer), - mTimeFrameGPU->getSortedStartIndex(rof0, iLayer + 1), // WRONG, to be fixed! mTimeFrameGPU->getNClustersLayer(rof0, iLayer), mTimeFrameGPU->getPhiCut(iLayer), mTimeFrameGPU->getMinR(iLayer + 1), @@ -336,31 +334,30 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getDeviceTrackingParameters(), mTimeFrameGPU->getDeviceIndexTableUtils()); } else { - // gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( - // rof0, - // mTimeFrameGPU->getNrof(), - // iLayer, - // mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer + 1), // :check: - // mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: - // mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: - // mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: - // mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), // :check: - // mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer + 1), // WRONG, to be fixed! - // mTimeFrameGPU->getDeviceVertices(rof0), - // mTimeFrameGPU->getDeviceTrackletsLookupTable(rof0, iLayer), - // mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), - // mTimeFrameGPU->getConfig().maxVerticesCapacity, - // mTimeFrameGPU->getSortedStartIndex(rof0, iLayer), - // mTimeFrameGPU->getSortedStartIndex(rof0, iLayer + 1), // WRONG, to be fixed! - // mTimeFrameGPU->getNClustersLayer(rof0, iLayer), - // mTimeFrameGPU->getPhiCut(iLayer), - // mTimeFrameGPU->getMinR(iLayer + 1), - // mTimeFrameGPU->getMaxR(iLayer + 1), - // meanDeltaR, - // mTimeFrameGPU->getPositionResolution(iLayer), - // mTimeFrameGPU->getMSangle(iLayer), - // mTimeFrameGPU->getDeviceTrackingParameters(), - // mTimeFrameGPU->getDeviceIndexTableUtils()); + gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( + rof0, + mTimeFrameGPU->getNrof(), + iLayer, + mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: + mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), // :check: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer + 1), // WRONG, to be fixed! + mTimeFrameGPU->getDeviceVertices(rof0), + mTimeFrameGPU->getDeviceTrackletsLookupTable(rof0, iLayer - 1), + mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), + mTimeFrameGPU->getConfig().maxVerticesCapacity, + mTimeFrameGPU->getNClustersLayer(rof0, iLayer), + mTimeFrameGPU->getPhiCut(iLayer), + mTimeFrameGPU->getMinR(iLayer + 1), + mTimeFrameGPU->getMaxR(iLayer + 1), + meanDeltaR, + mTimeFrameGPU->getPositionResolution(iLayer), + mTimeFrameGPU->getMSangle(iLayer), + mTimeFrameGPU->getDeviceTrackingParameters(), + mTimeFrameGPU->getDeviceIndexTableUtils()); } } } diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h index 5f09062cb880c..c743ea9dd21e1 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h @@ -173,11 +173,13 @@ class TimeFrame IndexTableUtils mIndexTableUtils; + bool mIsGPU = false; std::vector> mClusters; std::vector> mTrackingFrameInfo; std::vector> mClusterExternalIndices; std::vector> mROframesClusters; std::vector> mIndexTables; + std::vector> mTrackletsLookupTable; std::vector> mUsedClusters; int mNrof = 0; @@ -213,7 +215,6 @@ class TimeFrame std::vector mBogusClusters; /// keep track of clusters with wild coordinates std::vector> mTracklets; - std::vector> mTrackletsLookupTable; std::vector> mRoadLabels; int mCutClusterMult; diff --git a/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx b/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx index b61f73afa062a..e90dbf8a3200d 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx @@ -216,7 +216,7 @@ void TimeFrame::initialise(const int iteration, const TrackingParameters& trkPar mTracklets.resize(std::min(trkParam.TrackletsPerRoad(), maxLayers - 1)); mTrackletLabels.resize(trkParam.TrackletsPerRoad()); mTrackletsLookupTable.resize(trkParam.CellsPerRoad()); - mIndexTables.clear(); + mFlatIndexTables.clear(); mIndexTableUtils.setTrackingParameters(trkParam); mPositionResolution.resize(trkParam.NLayers); mBogusClusters.resize(trkParam.NLayers, 0); @@ -288,11 +288,35 @@ void TimeFrame::initialise(const int iteration, const TrackingParameters& trkPar c.indexTableBinIndex = h.bin; } +<<<<<<< HEAD for (unsigned int iB{0}; iB < clsPerBin.size(); ++iB) { mIndexTables[iLayer][rof * (trkParam.ZBins * trkParam.PhiBins + 1) + iB] = lutPerBin[iB]; } +<<<<<<< HEAD for (auto iB{clsPerBin.size()}; iB < (trkParam.ZBins * trkParam.PhiBins + 1); iB++) { mIndexTables[iLayer][rof * (trkParam.ZBins * trkParam.PhiBins + 1) + iB] = clustersNum; +======= + for (auto iB{clsPerBin.size()}; iB < mIndexTables[rof][iLayer].size(); iB++) { + mIndexTables[rof][iLayer][iB] = clustersNum; +======= + if (iLayer > 0) { + for (unsigned int iB{0}; iB < clsPerBin.size(); ++iB) { + // mIndexTables[rof][iLayer - 1][iB] = lutPerBin[iB]; + mFlatIndexTables[iLayer - 1][rof * (trkParam.ZBins * trkParam.PhiBins + 1) + iB] = lutPerBin[iB]; + } + for (auto iB{clsPerBin.size()}; iB < (trkParam.ZBins * trkParam.PhiBins + 1); iB++) { + // mIndexTables[rof][iLayer - 1][iB] = clustersNum; + mFlatIndexTables[iLayer - 1][rof * (trkParam.ZBins * trkParam.PhiBins + 1) + iB] = clustersNum; + } + } else { // LUTs on layer 0 are only for vertexer + for (unsigned int iB{0}; iB < clsPerBin.size(); ++iB) { + mIndexTablesL0[rof][iB] = lutPerBin[iB]; + } + for (auto iB{clsPerBin.size()}; iB < mIndexTablesL0[rof].size(); iB++) { + mIndexTablesL0[rof][iB] = clustersNum; + } +>>>>>>> cp +>>>>>>> cp } } } diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 1e903cc778370..c47d5cba5189c 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -116,7 +116,6 @@ void TrackerTraits::computeLayerTracklets(const int iteration) int iPhiBin = (selectedBinsRect.y + iPhiCount) % mTrkParams[iteration].PhiBins; const int firstBinIndex{tf->mIndexTableUtils.getBinIndex(selectedBinsRect.x, iPhiBin)}; const int maxBinIndex{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}; - printf("%d %d %d %d %d %d %d\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0); if constexpr (debugLevel) { if (firstBinIndex < 0 || firstBinIndex > tf->getIndexTable(rof1, iLayer + 1).size() || maxBinIndex < 0 || maxBinIndex > tf->getIndexTable(rof1, iLayer + 1).size()) { @@ -135,7 +134,6 @@ void TrackerTraits::computeLayerTracklets(const int iteration) break; } const Cluster& nextCluster{layer1[iNextCluster]}; - if (tf->isClusterUsed(iLayer + 1, nextCluster.clusterId)) { continue; } @@ -172,7 +170,9 @@ void TrackerTraits::computeLayerTracklets(const int iteration) currentCluster.xCoordinate - nextCluster.xCoordinate)}; const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / (currentCluster.radius - nextCluster.radius)}; + // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate); tf->getTracklets()[iLayer].emplace_back(currentSortedIndex, tf->getSortedIndex(rof1, iLayer + 1, iNextCluster), tanL, phi, rof0, rof1); + printf("%d %d %lf %lf %hu %hu\n", tf->getTracklets()[iLayer].back().firstClusterIndex, tf->getTracklets()[iLayer].back().secondClusterIndex, tf->getTracklets()[iLayer].back().tanLambda, tf->getTracklets()[iLayer].back().phi, tf->getTracklets()[iLayer].back().rof[0], tf->getTracklets()[iLayer].back().rof[0]); } } } From adaa2b34e8cd97ef4e6dce2354f885ce545b480d Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Thu, 2 Jun 2022 18:29:50 +0200 Subject: [PATCH 10/30] COmbinatorial part seems working --- .../ITS/tracking/GPU/ITStrackingGPU/Vector.h | 22 +++ .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 4 +- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 172 +++++++++--------- .../include/ITStracking/TrackerTraits.h | 4 +- .../ITSMFT/ITS/tracking/src/TimeFrame.cxx | 24 --- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 15 +- 6 files changed, 127 insertions(+), 114 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h index ffd29e30aceca..ff931a37d3631 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h @@ -55,6 +55,9 @@ class Vector final void resize(const size_t); void reset(const size_t, const size_t = 0); void reset(const T* const, const size_t, const size_t = 0); + + template ::value, int>> + void reset(const size_t, const int value = 0); void copyIntoSizedVector(std::vector&); GPUhd() T* get() const; @@ -214,6 +217,25 @@ void Vector::reset(const T* const source, const size_t size, const size_t ini } } +template +template ::value, int>> +void Vector::reset(const size_t size, const int value) +{ + if (size > mCapacity) { + if (mArrayPtr != nullptr) { + utils::host::gpuFree(mArrayPtr); + } + utils::host::gpuMalloc(reinterpret_cast(&mArrayPtr), size * sizeof(int)); + mCapacity = size; + } + if (mDeviceSizePtr == nullptr) { + utils::host::gpuMalloc(reinterpret_cast(&mDeviceSizePtr), sizeof(int)); + } + + utils::host::gpuMemset(mArrayPtr, value, size); + utils::host::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(int)); +} + template void Vector::copyIntoSizedVector(std::vector& destinationVector) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index cb6268a990932..61bd87e6af659 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -107,8 +107,8 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) if (iLayer < NLayers - 1) { mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; } - if (iLayer < NLayers - 2) { - mTrackletsLookupTablesD[iLayer].reset(mTrackletsLookupTable[iLayer].data(), static_cast(mTrackletsLookupTable[iLayer].size())); + if (iLayer < NLayers - 1) { + mTrackletsLookupTablesD[iLayer].reset(static_cast(mTrackletsLookupTable[iLayer].size())); } } diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 4ee5ca276b31f..a91e8314a7961 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -14,6 +14,10 @@ #include #include +#include +#include +#include + #include "ITStracking/Constants.h" #include "ITStracking/Configuration.h" #include "ITStracking/IndexTableUtils.h" @@ -59,7 +63,7 @@ GPUd() float Sq(float q) return q * q; } -template +template GPUg() void computeLayerTrackletsKernel( const int rof0, const int maxRofs, @@ -73,9 +77,9 @@ GPUg() void computeLayerTrackletsKernel( const unsigned char* usedClustersNextLayer, // input data rof0-delta * trkPars, - const IndexTableUtils* utils) + const IndexTableUtils* utils, + const unsigned int maxTrackletsPerCluster = 10) { // int clusterTrackletsNum = 0; - for (int currentClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; currentClusterIndex < sizeCurrentLClusters; currentClusterIndex += blockDim.x * gridDim.x) { + for (int currentClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; currentClusterIndex < currentLayerClustersSize; currentClusterIndex += blockDim.x * gridDim.x) { + unsigned int storedTracklets{0}; const Cluster& currentCluster{clustersCurrentLayer[currentClusterIndex]}; const int currentSortedIndex{roFrameClusters[rof0] + currentClusterIndex}; if (usedClustersLayer[currentSortedIndex]) { @@ -100,9 +106,9 @@ GPUg() void computeLayerTrackletsKernel( if (primaryVertex.getX() == 0.f && primaryVertex.getY() == 0.f && primaryVertex.getZ() == 0.f) { continue; } - if (!currentClusterIndex) { - printf("rof0: %d x: %lf, y: %lf, z: %lf\n", rof0, primaryVertex.getX(), primaryVertex.getY(), primaryVertex.getZ()); - } + // if (!currentClusterIndex) { + // printf("rof0 %d: Nv: %d -> x: %lf, y: %lf, z: %lf\n", rof0, iPrimaryVertex, primaryVertex.getX(), primaryVertex.getY(), primaryVertex.getZ()); + // } const float resolution{o2::gpu::GPUCommonMath::Sqrt(Sq(trkPars->PVres) / primaryVertex.getNContributors() + Sq(positionResolution))}; const float tanLambda{(currentCluster.zCoordinate - primaryVertex.getZ()) * inverseR0}; @@ -112,6 +118,9 @@ GPUg() void computeLayerTrackletsKernel( const float sigmaZ{std::sqrt(Sq(resolution) * Sq(tanLambda) * ((Sq(inverseR0) + sqInverseDeltaZ0) * Sq(meanDeltaR) + 1.f) + Sq(meanDeltaR * mSAngle))}; const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, *utils, zAtRmin, zAtRmax, sigmaZ * trkPars->NSigmaCut, phiCut)}; + // if (!currentClusterIndex) { + // printf("%d %d %d %d\n", selectedBinsRect.x, selectedBinsRect.y, selectedBinsRect.z, selectedBinsRect.w); + // } if (selectedBinsRect.x == 0 && selectedBinsRect.y == 0 && selectedBinsRect.z == 0 && selectedBinsRect.w == 0) { continue; } @@ -120,11 +129,12 @@ GPUg() void computeLayerTrackletsKernel( phiBinsNum += trkPars->PhiBins; } constexpr int tableSize{256 * 128 + 1}; // hardcoded for the time being + for (int rof1{minRof}; rof1 <= maxRof; ++rof1) { + // printf("%d %d %d \n", minRof, maxRof, roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1]); if (!(roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1])) { // number of clusters on next layer > 0 continue; } - for (int iPhiCount{0}; iPhiCount < phiBinsNum; iPhiCount++) { int iPhiBin = (selectedBinsRect.y + iPhiCount) % trkPars->PhiBins; const int firstBinIndex{utils->getBinIndex(selectedBinsRect.x, iPhiBin)}; @@ -132,29 +142,30 @@ GPUg() void computeLayerTrackletsKernel( const int firstRowClusterIndex = indexTable[rof1 * tableSize + firstBinIndex]; const int maxRowClusterIndex = indexTable[rof1 * tableSize + maxBinIndex]; for (int iNextCluster{firstRowClusterIndex}; iNextCluster < maxRowClusterIndex; ++iNextCluster) { - if (iNextCluster >= roFrameClustersNext[rof1]) { + // printf("%d %d %d %d \n", firstBinIndex, maxBinIndex, firstRowClusterIndex, maxRowClusterIndex); + // printf("%d %d\n", iNextCluster, roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1]); + if (iNextCluster >= (roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1])) { break; } const Cluster& nextCluster{getPtrFromRuler(rof1, clustersNextLayer, roFrameClustersNext)[iNextCluster]}; - // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate); - if (usedClustersNextLayer[nextCluster.clusterId]) { continue; } const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi)}; - const float deltaZ{o2::gpu::GPUCommonMath::Abs(tanLambda * (nextCluster.radius - currentCluster.radius) + - currentCluster.zCoordinate - nextCluster.zCoordinate)}; + const float deltaZ{o2::gpu::GPUCommonMath::Abs(tanLambda * (nextCluster.radius - currentCluster.radius) + currentCluster.zCoordinate - nextCluster.zCoordinate)}; + if (deltaZ / sigmaZ < trkPars->NSigmaCut && (deltaPhi < phiCut || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < phiCut)) { - if constexpr (!Layer0) { - trackletsLookUpTable[currentSortedIndex]++; // Should be race-condition safe - } + trackletsLookUpTable[currentSortedIndex]++; // Should be race-condition safe + // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f %f %f \n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate, deltaPhi, deltaZ); const float phi{o2::gpu::GPUCommonMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, currentCluster.xCoordinate - nextCluster.xCoordinate)}; const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / (currentCluster.radius - nextCluster.radius)}; - Tracklet t{currentSortedIndex, roFrameClustersNext[rof1] + iNextCluster, tanL, phi, rof0, rof1}; - printf("%d %d %lf %lf %hu %hu\n", t.firstClusterIndex, t.secondClusterIndex, t.tanLambda, t.phi, t.rof[0], t.rof[1]); + const size_t stride{currentClusterIndex * maxTrackletsPerCluster}; + new (tracklets + stride + storedTracklets) Tracklet{currentSortedIndex, roFrameClustersNext[rof1] + iNextCluster, tanL, phi, rof0, rof1}; + ++storedTracklets; + // printf("%d %d %lf %lf %hu %hu\n", t.firstClusterIndex, t.secondClusterIndex, t.tanLambda, t.phi, t.rof[0], t.rof[1]); } } } @@ -281,12 +292,12 @@ void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const M template void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { - std::array tempSize; + // std::array tempSize; std::array streamArray; const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); gsl::span diamondSpan(&diamondVert, 1); - for (int rof0{476}; rof0 < /*mTimeFrameGPU->getNrof()*/ 477; ++rof0) { + for (int rof0{0}; rof0 < mTimeFrameGPU->getNrof(); ++rof0) { for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one std::vector paddedVertices; @@ -298,8 +309,6 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) } } checkGPUError(cudaMemcpy(mTimeFrameGPU->getDeviceVertices(rof0), paddedVertices.data(), mTimeFrameGPU->getConfig().maxVerticesCapacity * sizeof(Vertex), cudaMemcpyHostToDevice), __FILE__, __LINE__); - // int minRof = (rof0 >= mTrkParams.DeltaROF) ? rof0 - mTrkParams.DeltaROF : 0; - int maxRof = (rof0 == mTimeFrameGPU->getNrof() - mTrkParams.DeltaROF) ? rof0 : rof0 + mTrkParams.DeltaROF; const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const float meanDeltaR{mTrkParams.LayerRadii[iLayer + 1] - mTrkParams.LayerRadii[iLayer]}; @@ -308,45 +317,19 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); continue; } - - gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( - rof0, - mTimeFrameGPU->getNrof(), - iLayer, - mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: - mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: - mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), // :check: - mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer + 1), // WRONG, to be fixed! - mTimeFrameGPU->getDeviceVertices(rof0), - nullptr, // :check: tracklet lookup table not needed - mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), - mTimeFrameGPU->getConfig().maxVerticesCapacity, - mTimeFrameGPU->getNClustersLayer(rof0, iLayer), - mTimeFrameGPU->getPhiCut(iLayer), - mTimeFrameGPU->getMinR(iLayer + 1), - mTimeFrameGPU->getMaxR(iLayer + 1), - meanDeltaR, - mTimeFrameGPU->getPositionResolution(iLayer), - mTimeFrameGPU->getMSangle(iLayer), - mTimeFrameGPU->getDeviceTrackingParameters(), - mTimeFrameGPU->getDeviceIndexTableUtils()); - } else { - gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( + gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( rof0, mTimeFrameGPU->getNrof(), iLayer, - mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: - mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: - mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer), // :check: - mTimeFrameGPU->getDeviceUsedClustersOnLayer(rof0, iLayer + 1), // WRONG, to be fixed! + mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: + mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer), // :check: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer + 1), // :check: mTimeFrameGPU->getDeviceVertices(rof0), - mTimeFrameGPU->getDeviceTrackletsLookupTable(rof0, iLayer - 1), + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), mTimeFrameGPU->getConfig().maxVerticesCapacity, mTimeFrameGPU->getNClustersLayer(rof0, iLayer), @@ -357,36 +340,59 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getPositionResolution(iLayer), mTimeFrameGPU->getMSangle(iLayer), mTimeFrameGPU->getDeviceTrackingParameters(), - mTimeFrameGPU->getDeviceIndexTableUtils()); + mTimeFrameGPU->getDeviceIndexTableUtils(), + mTimeFrameGPU->getConfig().maxTrackletsPerCluster); } } } - checkGPUError(cudaDeviceSynchronize(), __FILE__, __LINE__); - - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - - // trackletsNum[iLayer] = primaryVertexContext->getTempTrackletArray()[iLayer].getSizeFromDevice(); - // if (trackletsNum[iLayer] == 0) { - // continue; - // } - // primaryVertexContext->getDeviceTracklets()[iLayer + 1].resize(trackletsNum[iLayer]); - // cub::DeviceScan::ExclusiveSum(static_cast(primaryVertexContext->getTempTableArray()[iLayer].get()), tempSize[iLayer], - // primaryVertexContext->getDeviceTrackletsPerClustersTable()[iLayer].get(), - // primaryVertexContext->getDeviceTrackletsLookupTable()[iLayer].get(), - // primaryVertexContext->getClusters()[iLayer + 1].size(), streamArray[iLayer + 1].get()); - // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsNum[iLayer])}; - // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsNum[iLayer])}; - // gpu::sortTrackletsKernel<<>>(primaryVertexContext->getDeviceContext(), - // iLayer + 1, primaryVertexContext->getTempTrackletArray()[iLayer].getWeakCopy()); - // cudaError_t error = cudaGetLastError(); - // if (error != cudaSuccess) { - // std::ostringstream errorString{}; - // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" - // << std::endl; - // throw std::runtime_error{errorString.str()}; - // } + // std::vector> tables(5); + // for (int i{0}; i < 1; ++i) { + // tables[i].resize(mTimeFrameGPU->mClusters[i + 1].size()); + // checkGPUError(cudaMemcpy(tables[i].data(), mTimeFrameGPU->getDeviceTrackletsLookupTable(0, i), mTimeFrameGPU->mClusters[i + 1].size() * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + // std::cout << " === " << std::endl; + // for (auto j : tables[i]) { + // std::cout << j << "\n"; // } + // std::cout << std::endl; + // } + std::vector> trackletsHost(6, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); + for (int iLayer{0}; iLayer < 6; ++iLayer) { + checkGPUError(cudaMemcpy(trackletsHost[iLayer].data(), mTimeFrameGPU->getDeviceTracklets(0, iLayer), mTimeFrameGPU->getConfig().trackletsCapacity * sizeof(Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].end(), [](const Tracklet& a, const Tracklet& b) { return !a.isEmpty() && b.isEmpty(); }); + // std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].begin() +) + // int count{0}; + // for (auto& t : trackletsHost[iLayer]) { + // std::cout << (int)t.isEmpty() << "\t"; + // if (++count > 100) { + // break; + // } + // } + // std::cout << std::endl; + } + + int* trackletSizesD; + int trackletSizeH[6]; + checkGPUError(cudaMalloc(reinterpret_cast(&trackletSizesD), 6 * sizeof(int)), __FILE__, __LINE__); + for (int iLayer{0}; iLayer < 6; ++iLayer) { + size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; + discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + bufferSize, // temp_storage_bytes + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + trackletSizesD + iLayer, // d_out + mTimeFrameGPU->mClusters[iLayer + 1].size())); // num_items + // printf("buffer size: %zu\n", bufferSize); + checkGPUError(cudaMemcpy(trackletSizeH + iLayer, trackletSizesD + iLayer, sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + // printf("size: %d\n", trackletSizeH[iLayer]); + if (trackletSizeH[iLayer] == 0) { + continue; + } + discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + bufferSize, // temp_storage_bytes + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out + mTimeFrameGPU->mClusters[iLayer + 1].size() /*, streamArray[iLayer + 1].get()*/)); // num_items + } } template diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h index bd78f1136894c..990ee224e78e9 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h @@ -138,8 +138,8 @@ inline void TrackerTraits::initialiseTimeFrame(const int iteration, const Memory inline void TrackerTraits::adoptTimeFrame(TimeFrame* tf) { mTimeFrame = tf; } -inline const int4 TrackerTraits::getBinsRect(const Cluster& currentCluster, const int layerIndex, - const float z1, const float z2, float maxdeltaz, float maxdeltaphi) +inline const int4 TrackerTraits::getBinsRect(const int layerIndex, float phi, float maxdeltaphi, + float z1, float z2, float maxdeltaz) { const float zRangeMin = o2::gpu::GPUCommonMath::Min(z1, z2) - maxdeltaz; const float phiRangeMin = phi - maxdeltaphi; diff --git a/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx b/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx index e90dbf8a3200d..2e002974b62f9 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx @@ -288,35 +288,11 @@ void TimeFrame::initialise(const int iteration, const TrackingParameters& trkPar c.indexTableBinIndex = h.bin; } -<<<<<<< HEAD for (unsigned int iB{0}; iB < clsPerBin.size(); ++iB) { mIndexTables[iLayer][rof * (trkParam.ZBins * trkParam.PhiBins + 1) + iB] = lutPerBin[iB]; } -<<<<<<< HEAD for (auto iB{clsPerBin.size()}; iB < (trkParam.ZBins * trkParam.PhiBins + 1); iB++) { mIndexTables[iLayer][rof * (trkParam.ZBins * trkParam.PhiBins + 1) + iB] = clustersNum; -======= - for (auto iB{clsPerBin.size()}; iB < mIndexTables[rof][iLayer].size(); iB++) { - mIndexTables[rof][iLayer][iB] = clustersNum; -======= - if (iLayer > 0) { - for (unsigned int iB{0}; iB < clsPerBin.size(); ++iB) { - // mIndexTables[rof][iLayer - 1][iB] = lutPerBin[iB]; - mFlatIndexTables[iLayer - 1][rof * (trkParam.ZBins * trkParam.PhiBins + 1) + iB] = lutPerBin[iB]; - } - for (auto iB{clsPerBin.size()}; iB < (trkParam.ZBins * trkParam.PhiBins + 1); iB++) { - // mIndexTables[rof][iLayer - 1][iB] = clustersNum; - mFlatIndexTables[iLayer - 1][rof * (trkParam.ZBins * trkParam.PhiBins + 1) + iB] = clustersNum; - } - } else { // LUTs on layer 0 are only for vertexer - for (unsigned int iB{0}; iB < clsPerBin.size(); ++iB) { - mIndexTablesL0[rof][iB] = lutPerBin[iB]; - } - for (auto iB{clsPerBin.size()}; iB < mIndexTablesL0[rof].size(); iB++) { - mIndexTablesL0[rof][iB] = clustersNum; - } ->>>>>>> cp ->>>>>>> cp } } } diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index c47d5cba5189c..d82dd41289eba 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -81,7 +81,7 @@ void TrackerTraits::computeLayerTracklets(const int iteration) continue; } const float inverseR0{1.f / currentCluster.radius}; - + int iPrimaryVertex{0}; for (auto& primaryVertex : primaryVertices) { const float resolution = std::sqrt(Sq(mTrkParams[iteration].PVres) / primaryVertex.getNContributors() + Sq(tf->getPositionResolution(iLayer))); @@ -105,9 +105,9 @@ void TrackerTraits::computeLayerTracklets(const int iteration) if (phiBinsNum < 0) { phiBinsNum += mTrkParams[iteration].PhiBins; } - for (int rof1{minRof}; rof1 <= maxRof; ++rof1) { gsl::span layer1 = tf->getClustersOnLayer(rof1, iLayer + 1); + // printf("%d %d %d \n", minRof, maxRof, layer1.size()); if (layer1.empty()) { continue; } @@ -130,6 +130,7 @@ void TrackerTraits::computeLayerTracklets(const int iteration) const int maxRowClusterIndex = tf->getIndexTable(rof1, iLayer + 1)[maxBinIndex]; for (int iNextCluster{firstRowClusterIndex}; iNextCluster < maxRowClusterIndex; ++iNextCluster) { + // printf("%d %d\n", iNextCluster, (int)layer1.size()); if (iNextCluster >= (int)layer1.size()) { break; } @@ -172,7 +173,7 @@ void TrackerTraits::computeLayerTracklets(const int iteration) (currentCluster.radius - nextCluster.radius)}; // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate); tf->getTracklets()[iLayer].emplace_back(currentSortedIndex, tf->getSortedIndex(rof1, iLayer + 1, iNextCluster), tanL, phi, rof0, rof1); - printf("%d %d %lf %lf %hu %hu\n", tf->getTracklets()[iLayer].back().firstClusterIndex, tf->getTracklets()[iLayer].back().secondClusterIndex, tf->getTracklets()[iLayer].back().tanLambda, tf->getTracklets()[iLayer].back().phi, tf->getTracklets()[iLayer].back().rof[0], tf->getTracklets()[iLayer].back().rof[0]); + // printf("%d %d %lf %lf %hu %hu\n", tf->getTracklets()[iLayer].back().firstClusterIndex, tf->getTracklets()[iLayer].back().secondClusterIndex, tf->getTracklets()[iLayer].back().tanLambda, tf->getTracklets()[iLayer].back().phi, tf->getTracklets()[iLayer].back().rof[0], tf->getTracklets()[iLayer].back().rof[0]); } } } @@ -184,11 +185,19 @@ void TrackerTraits::computeLayerTracklets(const int iteration) } } } + // for (int i{0}; i < 1; ++i) { + // std::cout << " === " << std::endl; + // for (auto j : tf->getTrackletsLookupTable()[i]) { + // std::cout << j << "\n"; + // } + // std::cout << std::endl; + // } /// Cold code, fixups for (int iLayer{0}; iLayer < mTrkParams[iteration].CellsPerRoad(); ++iLayer) { /// Sort tracklets auto& trkl{tf->getTracklets()[iLayer + 1]}; + // printf("size: %zu\n", trkl.size()); std::sort(trkl.begin(), trkl.end(), [](const Tracklet& a, const Tracklet& b) { return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); }); From c71951acba052eb559c9a524b350cfcb9063ff7d Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Mon, 20 Jun 2022 12:34:44 +0200 Subject: [PATCH 11/30] Fix trackleting, works with HIP --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 10 +- .../ITS/tracking/GPU/ITStrackingGPU/Vector.h | 6 +- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 4 +- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 153 +++++++++--------- .../tracking/GPU/cuda/VertexerTraitsGPU.cu | 4 +- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 12 ++ 6 files changed, 99 insertions(+), 90 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 90b2387b5e7a7..0282ba7f7d7b3 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -82,7 +82,6 @@ void StaticTrackingParameters::set(const TrackingParameters& pars) // } } - template GPUhd() T* getPtrFromRuler(int index, T* src, const int* ruler, const int stride = 1) { @@ -120,8 +119,9 @@ class TimeFrameGPU : public TimeFrame // Vertexer only int* getDeviceNTrackletsCluster(int rofId, int combId); int* getDeviceIndexTables(const int layerId) { return mIndexTablesD[layerId].get(); } - int* getDeviceIndexTableL0(const int rofId) { return mIndexTablesLayer0D.get() + rofId * (ZBins * PhiBins + 1); } - int* getDeviceIndexTableL2(const int rofId) { return mIndexTablesLayer2D.get() + rofId * (ZBins * PhiBins + 1); } + int* getDeviceIndexTableAtRof(const int layerId, const int rofId) { return mIndexTablesD[layerId].get() + rofId * (ZBins * PhiBins + 1); } + // int* getDeviceIndexTableL0(const int rofId) { return mIndexTablesLayer0D.get() + rofId * (ZBins * PhiBins + 1); } + // int* getDeviceIndexTableL2(const int rofId) { return mIndexTablesLayer2D.get() + rofId * (ZBins * PhiBins + 1); } unsigned char* getDeviceUsedTracklets(const int rofId); Line* getDeviceLines(const int rofId); Tracklet* getDeviceTracklets(const int rofId, const int layerId); @@ -163,8 +163,8 @@ class TimeFrameGPU : public TimeFrame // Vertexer only Vector mLines; - Vector mIndexTablesLayer0D; - Vector mIndexTablesLayer2D; + // Vector mIndexTablesLayer0D; + // Vector mIndexTablesLayer2D; Vector mNFoundLines; Vector mNExclusiveFoundLines; Vector mUsedTracklets; diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h index ff931a37d3631..24613d70bcd98 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h @@ -56,8 +56,7 @@ class Vector final void reset(const size_t, const size_t = 0); void reset(const T* const, const size_t, const size_t = 0); - template ::value, int>> - void reset(const size_t, const int value = 0); + void resetInt(const size_t, const int value = 0); void copyIntoSizedVector(std::vector&); GPUhd() T* get() const; @@ -218,8 +217,7 @@ void Vector::reset(const T* const source, const size_t size, const size_t ini } template -template ::value, int>> -void Vector::reset(const size_t size, const int value) +void Vector::resetInt(const size_t size, const int value) { if (size > mCapacity) { if (mArrayPtr != nullptr) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 61bd87e6af659..20c3633efc4ef 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -102,13 +102,12 @@ template template void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) { - for (int iLayer{0}; iLayer < NLayers; ++iLayer) { // Tracker and vertexer if (iLayer < NLayers - 1) { mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; } if (iLayer < NLayers - 1) { - mTrackletsLookupTablesD[iLayer].reset(static_cast(mTrackletsLookupTable[iLayer].size())); + mTrackletsLookupTablesD[iLayer].resetInt(mClusters[iLayer].size()); } } @@ -154,6 +153,7 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) mIndexTablesLayer0D.reset(getIndexTableWhole(0).data(), static_cast(getIndexTableWhole(0).size())); mIndexTablesLayer2D.reset(getIndexTableWhole(2).data(), static_cast(getIndexTableWhole(2).size())); } + gpuThrowOnError(); } diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index a91e8314a7961..5385f6fb6740f 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -74,7 +74,7 @@ GPUg() void computeLayerTrackletsKernel( const int* roFrameClusters, // input data O(1) const int* roFrameClustersNext, // input data O(1) const unsigned char* usedClustersLayer, // input data rof0 - const unsigned char* usedClustersNextLayer, // input data rof0-delta = (roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1])) { break; @@ -292,7 +292,6 @@ void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const M template void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { - // std::array tempSize; std::array streamArray; const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); @@ -312,87 +311,87 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const float meanDeltaR{mTrkParams.LayerRadii[iLayer + 1] - mTrkParams.LayerRadii[iLayer]}; - if (!iLayer) { - if (!mTimeFrameGPU->getClustersOnLayer(rof0, iLayer).size()) { - LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); - continue; - } - gpu::computeLayerTrackletsKernel<<<1, 1 /*, 0, streamArray[iLayer].get()*/>>>( - rof0, - mTimeFrameGPU->getNrof(), - iLayer, - mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: - mTimeFrameGPU->getDeviceIndexTables(iLayer), // :check: - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: - mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer), // :check: - mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer + 1), // :check: - mTimeFrameGPU->getDeviceVertices(rof0), - mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), - mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), - mTimeFrameGPU->getConfig().maxVerticesCapacity, - mTimeFrameGPU->getNClustersLayer(rof0, iLayer), - mTimeFrameGPU->getPhiCut(iLayer), - mTimeFrameGPU->getMinR(iLayer + 1), - mTimeFrameGPU->getMaxR(iLayer + 1), - meanDeltaR, - mTimeFrameGPU->getPositionResolution(iLayer), - mTimeFrameGPU->getMSangle(iLayer), - mTimeFrameGPU->getDeviceTrackingParameters(), - mTimeFrameGPU->getDeviceIndexTableUtils(), - mTimeFrameGPU->getConfig().maxTrackletsPerCluster); + + if (!mTimeFrameGPU->getClustersOnLayer(rof0, iLayer).size()) { + LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); + continue; } + gpu::computeLayerTrackletsKernel<<>>( + rof0, + mTimeFrameGPU->getNrof(), + iLayer, + mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: + mTimeFrameGPU->getDeviceIndexTables(iLayer + 1), // :check: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer), // :check: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer + 1), // :check: + mTimeFrameGPU->getDeviceVertices(rof0), + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), + mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), + mTimeFrameGPU->getConfig().maxVerticesCapacity, + mTimeFrameGPU->getNClustersLayer(rof0, iLayer), + mTimeFrameGPU->getPhiCut(iLayer), + mTimeFrameGPU->getMinR(iLayer + 1), + mTimeFrameGPU->getMaxR(iLayer + 1), + meanDeltaR, + mTimeFrameGPU->getPositionResolution(iLayer), + mTimeFrameGPU->getMSangle(iLayer), + mTimeFrameGPU->getDeviceTrackingParameters(), + mTimeFrameGPU->getDeviceIndexTableUtils(), + mTimeFrameGPU->getConfig().maxTrackletsPerCluster); } } checkGPUError(cudaDeviceSynchronize(), __FILE__, __LINE__); - // std::vector> tables(5); - // for (int i{0}; i < 1; ++i) { - // tables[i].resize(mTimeFrameGPU->mClusters[i + 1].size()); - // checkGPUError(cudaMemcpy(tables[i].data(), mTimeFrameGPU->getDeviceTrackletsLookupTable(0, i), mTimeFrameGPU->mClusters[i + 1].size() * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - // std::cout << " === " << std::endl; - // for (auto j : tables[i]) { - // std::cout << j << "\n"; + std::vector> tables(NLayers - 1); + for (int i{1}; i < /*NLayers - 1*/ 2; ++i) { + tables[i].resize(mTimeFrameGPU->mClusters[i].size()); + checkGPUError(cudaMemcpy(tables[i].data(), mTimeFrameGPU->getDeviceTrackletsLookupTable(0, i), mTimeFrameGPU->mClusters[i].size() * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + std::exclusive_scan(tables[i].begin(), tables[i].end(), tables[i].begin(), 0); + std::cout << " === table " << i << " ===" << std::endl; + for (auto j : tables[i]) { + std::cout << j << "\n"; + } + std::cout << std::endl; + } + // std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); + // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + // checkGPUError(cudaMemcpy(trackletsHost[iLayer].data(), mTimeFrameGPU->getDeviceTracklets(0, iLayer), mTimeFrameGPU->getConfig().trackletsCapacity * sizeof(Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + // std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].end(), [](const Tracklet& a, const Tracklet& b) { return !a.isEmpty() && b.isEmpty(); }); + // // std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].begin() +) + // int count{0}; + // for (auto& t : trackletsHost[iLayer]) { + // t.dump(); + // if (++count > 10) { + // break; + // } // } - // std::cout << std::endl; + // std::cout << iLayer << " ===" << std::endl; // } - std::vector> trackletsHost(6, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); - for (int iLayer{0}; iLayer < 6; ++iLayer) { - checkGPUError(cudaMemcpy(trackletsHost[iLayer].data(), mTimeFrameGPU->getDeviceTracklets(0, iLayer), mTimeFrameGPU->getConfig().trackletsCapacity * sizeof(Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].end(), [](const Tracklet& a, const Tracklet& b) { return !a.isEmpty() && b.isEmpty(); }); - // std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].begin() +) - // int count{0}; - // for (auto& t : trackletsHost[iLayer]) { - // std::cout << (int)t.isEmpty() << "\t"; - // if (++count > 100) { - // break; - // } - // } - // std::cout << std::endl; - } - int* trackletSizesD; - int trackletSizeH[6]; - checkGPUError(cudaMalloc(reinterpret_cast(&trackletSizesD), 6 * sizeof(int)), __FILE__, __LINE__); - for (int iLayer{0}; iLayer < 6; ++iLayer) { - size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; - discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage - bufferSize, // temp_storage_bytes - mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in - trackletSizesD + iLayer, // d_out - mTimeFrameGPU->mClusters[iLayer + 1].size())); // num_items - // printf("buffer size: %zu\n", bufferSize); - checkGPUError(cudaMemcpy(trackletSizeH + iLayer, trackletSizesD + iLayer, sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - // printf("size: %d\n", trackletSizeH[iLayer]); - if (trackletSizeH[iLayer] == 0) { - continue; - } - discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage - bufferSize, // temp_storage_bytes - mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in - mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out - mTimeFrameGPU->mClusters[iLayer + 1].size() /*, streamArray[iLayer + 1].get()*/)); // num_items - } + // int* trackletSizesD; + // int trackletSizeH[6]; + // checkGPUError(cudaMalloc(reinterpret_cast(&trackletSizesD), (NLayers - 1) * sizeof(int)), __FILE__, __LINE__); + // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + // size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; + // discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + // bufferSize, // temp_storage_bytes + // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + // trackletSizesD + iLayer, // d_out + // mTimeFrameGPU->mClusters[iLayer + 1].size())); // num_items + // // printf("buffer size: %zu\n", bufferSize); + // checkGPUError(cudaMemcpy(trackletSizeH + iLayer, trackletSizesD + iLayer, sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + // // printf("size: %d\n", trackletSizeH[iLayer]); + // if (trackletSizeH[iLayer] == 0) { + // continue; + // } + // discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + // bufferSize, // temp_storage_bytes + // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out + // mTimeFrameGPU->mClusters[iLayer + 1].size() /*, streamArray[iLayer + 1].get()*/)); // num_items + // } } template diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu index 0f29e0a52f0c2..6acd45cdb2264 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu @@ -390,7 +390,7 @@ void VertexerTraitsGPU::computeTracklets() mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 1), mTimeFrameGPU->getNClustersLayer(rofId, 0), mTimeFrameGPU->getNClustersLayer(rofId, 1), - mTimeFrameGPU->getDeviceIndexTableL0(rofId), + mTimeFrameGPU->getDeviceIndexTableAtRof(0, rofId), mVrtParams.phiCut, mTimeFrameGPU->getDeviceTracklets(rofId, 0), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 0), @@ -402,7 +402,7 @@ void VertexerTraitsGPU::computeTracklets() mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 1), mTimeFrameGPU->getNClustersLayer(rofId, 2), mTimeFrameGPU->getNClustersLayer(rofId, 1), - mTimeFrameGPU->getDeviceIndexTableL2(rofId), + mTimeFrameGPU->getDeviceIndexTableAtRof(2, rofId), mVrtParams.phiCut, mTimeFrameGPU->getDeviceTracklets(rofId, 1), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 1), diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index d82dd41289eba..04c4133374f5c 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -167,6 +167,8 @@ void TrackerTraits::computeLayerTracklets(const int iteration) if (iLayer > 0) { tf->getTrackletsLookupTable()[iLayer - 1][currentSortedIndex]++; } + // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f %f %f \n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate, deltaPhi, deltaZ); + const float phi{o2::gpu::GPUCommonMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, currentCluster.xCoordinate - nextCluster.xCoordinate)}; const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / @@ -192,6 +194,16 @@ void TrackerTraits::computeLayerTracklets(const int iteration) // } // std::cout << std::endl; // } + // std::vector> tables(5); + // for (int i{0}; i < 1; ++i) { + // tables[i] = tf->getTrackletsLookupTable()[i]; + // std::exclusive_scan(tables[i].begin(), tables[i].end(), tables[i].begin(), 0); + // std::cout << " === table " << i << " ===" << std::endl; + // for (auto j : tables[i]) { + // std::cout << j << "\n"; + // } + // std::cout << std::endl; + // } /// Cold code, fixups for (int iLayer{0}; iLayer < mTrkParams[iteration].CellsPerRoad(); ++iLayer) { From cc8c7a5fea4b05a14926921c9189440045f8573b Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Wed, 22 Jun 2022 12:29:57 +0200 Subject: [PATCH 12/30] Trackleting kernel works after rebase --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 6 +- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 6 +- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 95 ++++++++++--------- .../ITSMFT/ITS/tracking/GPU/cuda/Utils.cu | 3 +- .../tracking/include/ITStracking/Tracklet.h | 40 ++++---- .../ITSMFT/ITS/tracking/src/TimeFrame.cxx | 1 - .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 1 - GPU/Common/GPUCommonMath.h | 4 +- 8 files changed, 81 insertions(+), 75 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 0282ba7f7d7b3..0a28c24b02936 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -120,8 +120,6 @@ class TimeFrameGPU : public TimeFrame int* getDeviceNTrackletsCluster(int rofId, int combId); int* getDeviceIndexTables(const int layerId) { return mIndexTablesD[layerId].get(); } int* getDeviceIndexTableAtRof(const int layerId, const int rofId) { return mIndexTablesD[layerId].get() + rofId * (ZBins * PhiBins + 1); } - // int* getDeviceIndexTableL0(const int rofId) { return mIndexTablesLayer0D.get() + rofId * (ZBins * PhiBins + 1); } - // int* getDeviceIndexTableL2(const int rofId) { return mIndexTablesLayer2D.get() + rofId * (ZBins * PhiBins + 1); } unsigned char* getDeviceUsedTracklets(const int rofId); Line* getDeviceLines(const int rofId); Tracklet* getDeviceTracklets(const int rofId, const int layerId); @@ -163,8 +161,8 @@ class TimeFrameGPU : public TimeFrame // Vertexer only Vector mLines; - // Vector mIndexTablesLayer0D; - // Vector mIndexTablesLayer2D; + Vector mIndexTablesLayer0D; + Vector mIndexTablesLayer2D; Vector mNFoundLines; Vector mNExclusiveFoundLines; Vector mUsedTracklets; diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 20c3633efc4ef..d25b5b6be31ec 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -114,6 +114,8 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) for (auto iComb{0}; iComb < 2; ++iComb) { // Vertexer only mNTrackletsPerClusterD[iComb] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; } + mIndexTablesLayer0D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; + mIndexTablesLayer2D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; mLines = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; mNFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mNExclusiveFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; @@ -145,8 +147,8 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) mTrackingFrameInfoD[iLayer].reset(mTrackingFrameInfo[iLayer].data(), static_cast(mTrackingFrameInfo[iLayer].size())); mClusterExternalIndicesD[iLayer].reset(mClusterExternalIndices[iLayer].data(), static_cast(mClusterExternalIndices[iLayer].size())); mROframesClustersD[iLayer].reset(mROframesClusters[iLayer].data(), static_cast(mROframesClusters[iLayer].size())); - if (iLayer < NLayers - 1) { - mIndexTablesD[iLayer].reset(mFlatIndexTables[iLayer].data(), static_cast(mFlatIndexTables[iLayer].size())); + if (iLayer < NLayers) { + mIndexTablesD[iLayer].reset(mIndexTables[iLayer].data(), static_cast(mIndexTables[iLayer].size())); } } } else { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 5385f6fb6740f..f5d6b4c29c8ca 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -14,9 +14,11 @@ #include #include +#include +#include #include #include -#include +#include #include "ITStracking/Constants.h" #include "ITStracking/Configuration.h" @@ -119,7 +121,7 @@ GPUg() void computeLayerTrackletsKernel( const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, *utils, zAtRmin, zAtRmax, sigmaZ * trkPars->NSigmaCut, phiCut)}; // if (!currentClusterIndex) { - // printf("%d %d %d %d\n", selectedBinsRect.x, selectedBinsRect.y, selectedBinsRect.z, selectedBinsRect.w); + // printf("%d %d %d %d\n", selectedBinsRect.x, selectedBinsRect.y, selectedBinsRect.z, selectedBinsRect.w); // } if (selectedBinsRect.x == 0 && selectedBinsRect.y == 0 && selectedBinsRect.z == 0 && selectedBinsRect.w == 0) { continue; @@ -141,7 +143,9 @@ GPUg() void computeLayerTrackletsKernel( const int maxBinIndex{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}; const int firstRowClusterIndex = indexTable[rof1 * tableSize + firstBinIndex]; const int maxRowClusterIndex = indexTable[rof1 * tableSize + maxBinIndex]; - // printf("%d %d %d %d \n", firstBinIndex, maxBinIndex, firstRowClusterIndex, maxRowClusterIndex); + // if (!currentClusterIndex) { + // printf("%d %d %d %d\n", firstBinIndex, maxBinIndex, firstRowClusterIndex, maxRowClusterIndex); + // } for (int iNextCluster{firstRowClusterIndex}; iNextCluster < maxRowClusterIndex; ++iNextCluster) { // printf("%d %d\n", iNextCluster, roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1]); if (iNextCluster >= (roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1])) { @@ -297,17 +301,17 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); gsl::span diamondSpan(&diamondVert, 1); for (int rof0{0}; rof0 < mTimeFrameGPU->getNrof(); ++rof0) { - for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one - std::vector paddedVertices; - for (int iVertex{0}; iVertex < mTimeFrameGPU->getConfig().maxVerticesCapacity; ++iVertex) { - if (iVertex < primaryVertices.size()) { - paddedVertices.emplace_back(primaryVertices[iVertex]); - } else { - paddedVertices.emplace_back(Vertex()); - } + gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one + std::vector paddedVertices; + for (int iVertex{0}; iVertex < mTimeFrameGPU->getConfig().maxVerticesCapacity; ++iVertex) { + if (iVertex < primaryVertices.size()) { + paddedVertices.emplace_back(primaryVertices[iVertex]); + } else { + paddedVertices.emplace_back(Vertex()); } - checkGPUError(cudaMemcpy(mTimeFrameGPU->getDeviceVertices(rof0), paddedVertices.data(), mTimeFrameGPU->getConfig().maxVerticesCapacity * sizeof(Vertex), cudaMemcpyHostToDevice), __FILE__, __LINE__); + } + checkGPUError(cudaMemcpy(mTimeFrameGPU->getDeviceVertices(rof0), paddedVertices.data(), mTimeFrameGPU->getConfig().maxVerticesCapacity * sizeof(Vertex), cudaMemcpyHostToDevice), __FILE__, __LINE__); + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const float meanDeltaR{mTrkParams.LayerRadii[iLayer + 1] - mTrkParams.LayerRadii[iLayer]}; @@ -316,7 +320,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); continue; } - gpu::computeLayerTrackletsKernel<<>>( + gpu::computeLayerTrackletsKernel<<>>( rof0, mTimeFrameGPU->getNrof(), iLayer, @@ -344,17 +348,17 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) } } checkGPUError(cudaDeviceSynchronize(), __FILE__, __LINE__); - std::vector> tables(NLayers - 1); - for (int i{1}; i < /*NLayers - 1*/ 2; ++i) { - tables[i].resize(mTimeFrameGPU->mClusters[i].size()); - checkGPUError(cudaMemcpy(tables[i].data(), mTimeFrameGPU->getDeviceTrackletsLookupTable(0, i), mTimeFrameGPU->mClusters[i].size() * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - std::exclusive_scan(tables[i].begin(), tables[i].end(), tables[i].begin(), 0); - std::cout << " === table " << i << " ===" << std::endl; - for (auto j : tables[i]) { - std::cout << j << "\n"; - } - std::cout << std::endl; - } + // std::vector> tables(NLayers - 1); + // for (int i{1}; i < /*NLayers - 1*/ 2; ++i) { + // tables[i].resize(mTimeFrameGPU->mClusters[i].size()); + // checkGPUError(cudaMemcpy(tables[i].data(), mTimeFrameGPU->getDeviceTrackletsLookupTable(0, i), mTimeFrameGPU->mClusters[i].size() * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + // std::exclusive_scan(tables[i].begin(), tables[i].end(), tables[i].begin(), 0); + // std::cout << " === table " << i << " ===" << std::endl; + // for (auto j : tables[i]) { + // std::cout << j << "\n"; + // } + // std::cout << std::endl; + // } // std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // checkGPUError(cudaMemcpy(trackletsHost[iLayer].data(), mTimeFrameGPU->getDeviceTracklets(0, iLayer), mTimeFrameGPU->getConfig().trackletsCapacity * sizeof(Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); @@ -373,25 +377,28 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) // int* trackletSizesD; // int trackletSizeH[6]; // checkGPUError(cudaMalloc(reinterpret_cast(&trackletSizesD), (NLayers - 1) * sizeof(int)), __FILE__, __LINE__); - // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - // size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; - // discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage - // bufferSize, // temp_storage_bytes - // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in - // trackletSizesD + iLayer, // d_out - // mTimeFrameGPU->mClusters[iLayer + 1].size())); // num_items - // // printf("buffer size: %zu\n", bufferSize); - // checkGPUError(cudaMemcpy(trackletSizeH + iLayer, trackletSizesD + iLayer, sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - // // printf("size: %d\n", trackletSizeH[iLayer]); - // if (trackletSizeH[iLayer] == 0) { - // continue; - // } - // discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage - // bufferSize, // temp_storage_bytes - // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in - // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out - // mTimeFrameGPU->mClusters[iLayer + 1].size() /*, streamArray[iLayer + 1].get()*/)); // num_items - // } + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; + auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTracklets(0, iLayer)); + auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTracklets(0, iLayer) + mTimeFrameGPU->mClusters[iLayer].size()); + thrust::sort(begin, end); + // discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + // bufferSize, // temp_storage_bytes + // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + // trackletSizesD + iLayer, // d_out + // mTimeFrameGPU->mClusters[iLayer + 1].size())); // num_items + // // printf("buffer size: %zu\n", bufferSize); + // checkGPUError(cudaMemcpy(trackletSizeH + iLayer, trackletSizesD + iLayer, sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + // // printf("size: %d\n", trackletSizeH[iLayer]); + // if (trackletSizeH[iLayer] == 0) { + // continue; + // } + // discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + // bufferSize, // temp_storage_bytes + // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out + // mTimeFrameGPU->mClusters[iLayer + 1].size() /*, streamArray[iLayer + 1].get()*/)); // num_items + } } template diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/Utils.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/Utils.cu index de858d876f71c..e6b07965c3172 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/Utils.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/Utils.cu @@ -60,7 +60,8 @@ void utils::host::checkGPUError(const cudaError_t error, const char* file, const { if (error != cudaSuccess) { std::ostringstream errorString{}; - errorString << file << ":" << line << GPU_ARCH << " API returned error [" << cudaGetErrorString(error) << "] (code " + errorString << file << ":" << line << std::endl + << GPU_ARCH << " API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" << std::endl; throw std::runtime_error{errorString.str()}; } diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h index 877e348d80d81..1fb01dc770e3d 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h @@ -27,17 +27,17 @@ namespace its { struct Tracklet final { - Tracklet(); - GPUdi() Tracklet(const int, const int, const Cluster&, const Cluster&, int rof0, int rof1); - GPUdi() Tracklet(const int, const int, float tanL, float phi, int rof0, int rof1); - bool operator==(const Tracklet&) const; - bool operator!=(const Tracklet&) const; - GPUhd() unsigned char isEmpty() const + GPUhdi() Tracklet(); + GPUhdi() Tracklet(const int, const int, const Cluster&, const Cluster&, int rof0, int rof1); + GPUhdi() Tracklet(const int, const int, float tanL, float phi, int rof0, int rof1); + GPUhdi() bool operator==(const Tracklet&) const; + GPUhdi() bool operator!=(const Tracklet&) const; + GPUhdi() unsigned char isEmpty() const { return !firstClusterIndex && !secondClusterIndex && !tanLambda && !phi; } - void dump(); - unsigned char operator<(const Tracklet&) const; + GPUhdi() void dump(); + GPUhdi() unsigned char operator<(const Tracklet&) const; int firstClusterIndex; int secondClusterIndex; @@ -46,13 +46,13 @@ struct Tracklet final { unsigned short rof[2]; }; -inline Tracklet::Tracklet() : firstClusterIndex{0}, secondClusterIndex{0}, tanLambda{0.0f}, phi{0.0f} +GPUhdi() Tracklet::Tracklet() : firstClusterIndex{0}, secondClusterIndex{0}, tanLambda{0.0f}, phi{0.0f} { // Nothing to do } -GPUdi() Tracklet::Tracklet(const int firstClusterOrderingIndex, const int secondClusterOrderingIndex, - const Cluster& firstCluster, const Cluster& secondCluster, int rof0 = -1, int rof1 = -1) +GPUhdi() Tracklet::Tracklet(const int firstClusterOrderingIndex, const int secondClusterOrderingIndex, + const Cluster& firstCluster, const Cluster& secondCluster, int rof0 = -1, int rof1 = -1) : firstClusterIndex{firstClusterOrderingIndex}, secondClusterIndex{secondClusterOrderingIndex}, tanLambda{(firstCluster.zCoordinate - secondCluster.zCoordinate) / @@ -64,7 +64,7 @@ GPUdi() Tracklet::Tracklet(const int firstClusterOrderingIndex, const int second // Nothing to do } -GPUdi() Tracklet::Tracklet(const int idx0, const int idx1, float tanL, float phi, int rof0, int rof1) +GPUhdi() Tracklet::Tracklet(const int idx0, const int idx1, float tanL, float phi, int rof0, int rof1) : firstClusterIndex{idx0}, secondClusterIndex{idx1}, tanLambda{tanL}, @@ -74,7 +74,7 @@ GPUdi() Tracklet::Tracklet(const int idx0, const int idx1, float tanL, float phi // Nothing to do } -inline bool Tracklet::operator==(const Tracklet& rhs) const +GPUhdi() bool Tracklet::operator==(const Tracklet& rhs) const { return this->firstClusterIndex == rhs.firstClusterIndex && this->secondClusterIndex == rhs.secondClusterIndex && @@ -82,7 +82,7 @@ inline bool Tracklet::operator==(const Tracklet& rhs) const this->phi == rhs.phi; } -inline bool Tracklet::operator!=(const Tracklet& rhs) const +GPUhdi() bool Tracklet::operator!=(const Tracklet& rhs) const { return this->firstClusterIndex != rhs.firstClusterIndex || this->secondClusterIndex != rhs.secondClusterIndex || @@ -90,7 +90,7 @@ inline bool Tracklet::operator!=(const Tracklet& rhs) const this->phi != rhs.phi; } -inline unsigned char Tracklet::operator<(const Tracklet& t) const +GPUhdi() unsigned char Tracklet::operator<(const Tracklet& t) const { if (isEmpty() && t.isEmpty()) { return false; @@ -102,12 +102,12 @@ inline unsigned char Tracklet::operator<(const Tracklet& t) const return true; } -inline void Tracklet::dump() +GPUhdi() void Tracklet::dump() { - std::cout << "firstClusterIndex: " << firstClusterIndex << std::endl; - std::cout << "secondClusterIndex: " << secondClusterIndex << std::endl; - std::cout << "tanLambda: " << tanLambda << std::endl; - std::cout << "phi: " << phi << std::endl; + printf("firstClusterIndex: %d", firstClusterIndex); + printf("secondClusterIndex: %d", secondClusterIndex); + printf("tanLambda: %f", tanLambda); + printf("phi: %f", phi); } } // namespace its diff --git a/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx b/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx index 2e002974b62f9..6464786bc4fb8 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx @@ -216,7 +216,6 @@ void TimeFrame::initialise(const int iteration, const TrackingParameters& trkPar mTracklets.resize(std::min(trkParam.TrackletsPerRoad(), maxLayers - 1)); mTrackletLabels.resize(trkParam.TrackletsPerRoad()); mTrackletsLookupTable.resize(trkParam.CellsPerRoad()); - mFlatIndexTables.clear(); mIndexTableUtils.setTrackingParameters(trkParam); mPositionResolution.resize(trkParam.NLayers); mBogusClusters.resize(trkParam.NLayers, 0); diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 04c4133374f5c..4e49d6237764e 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -209,7 +209,6 @@ void TrackerTraits::computeLayerTracklets(const int iteration) for (int iLayer{0}; iLayer < mTrkParams[iteration].CellsPerRoad(); ++iLayer) { /// Sort tracklets auto& trkl{tf->getTracklets()[iLayer + 1]}; - // printf("size: %zu\n", trkl.size()); std::sort(trkl.begin(), trkl.end(), [](const Tracklet& a, const Tracklet& b) { return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); }); diff --git a/GPU/Common/GPUCommonMath.h b/GPU/Common/GPUCommonMath.h index 18c9d695ce242..995d4eb503e8b 100644 --- a/GPU/Common/GPUCommonMath.h +++ b/GPU/Common/GPUCommonMath.h @@ -60,7 +60,7 @@ class GPUCommonMath GPUd() static float ASin(float x); GPUd() static float ACos(float x); GPUd() static float ATan(float x); - GPUd() static float ATan2(float y, float x); + GPUhd() static float ATan2(float y, float x); GPUd() static float Sin(float x); GPUd() static float Cos(float x); GPUhdni() static void SinCos(float x, float& s, float& c); @@ -243,7 +243,7 @@ GPUdi() bool GPUCommonMath::Finite(float x) { return CHOICE(std::isfinite(x), tr GPUdi() float GPUCommonMath::ATan(float x) { return CHOICE(atanf(x), atanf(x), atan(x)); } -GPUdi() float GPUCommonMath::ATan2(float y, float x) { return CHOICE(atan2f(y, x), atan2f(y, x), atan2(y, x)); } +GPUhdi() float GPUCommonMath::ATan2(float y, float x) { return CHOICE(atan2f(y, x), atan2f(y, x), atan2(y, x)); } GPUdi() float GPUCommonMath::Sin(float x) { return CHOICE(sinf(x), sinf(x), sin(x)); } From b18ebf1ba57c17bd1688e5fc931905546427a7c6 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Thu, 23 Jun 2022 18:00:30 +0200 Subject: [PATCH 13/30] Checkpoint, investigating missing tracklets --- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 43 ++++++++++++++++++- .../tracking/include/ITStracking/Tracklet.h | 5 +-- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 15 +++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index f5d6b4c29c8ca..2b04aeb46879d 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -168,6 +168,10 @@ GPUg() void computeLayerTrackletsKernel( (currentCluster.radius - nextCluster.radius)}; const size_t stride{currentClusterIndex * maxTrackletsPerCluster}; new (tracklets + stride + storedTracklets) Tracklet{currentSortedIndex, roFrameClustersNext[rof1] + iNextCluster, tanL, phi, rof0, rof1}; + if (currentSortedIndex == 127 && (roFrameClustersNext[rof1] + iNextCluster) == 145) { + tracklets[stride + storedTracklets].dump(); + printf("id: %lu \n", stride + storedTracklets); + } ++storedTracklets; // printf("%d %d %lf %lf %hu %hu\n", t.firstClusterIndex, t.secondClusterIndex, t.tanLambda, t.phi, t.rof[0], t.rof[1]); } @@ -380,8 +384,43 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTracklets(0, iLayer)); - auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTracklets(0, iLayer) + mTimeFrameGPU->mClusters[iLayer].size()); - thrust::sort(begin, end); + auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTracklets(0, iLayer) + mTimeFrameGPU->mClusters[iLayer].size() * mTimeFrameGPU->getConfig().maxTrackletsPerCluster); + // thrust::sort(begin, end); + } + std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); + + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + checkGPUError(cudaMemcpy(trackletsHost[iLayer].data(), mTimeFrameGPU->getDeviceTracklets(0, iLayer), mTimeFrameGPU->getConfig().trackletsCapacity * sizeof(Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + // auto it = trackletsHost[iLayer].begin(); + // do { + // ++it; + // } while (!((*it).isEmpty())); + // std::sort(trackletsHost[iLayer].begin(), it, [](const Tracklet& a, const Tracklet& b) { + // return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); + // }); + int count{0}; + int count2{0}; + trackletsHost[0][2650].dump(); + for (auto& t : trackletsHost[iLayer]) { + if (!t.isEmpty()) { + ++count; + } + if (t.firstClusterIndex == 127 && t.secondClusterIndex == 145) { + std::cout << "got it at: " << count2 << std::endl; + t.dump(); + } + ++count2; + } + std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].begin() + count, [](const Tracklet& a, const Tracklet& b) { + return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); + }); + // for (auto& t : trackletsHost[iLayer]) { + // if (t.isEmpty()) { + // continue; + // } + // std::cout << "layer: " << iLayer << ":\t"; + // t.dump(); + // } // discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage // bufferSize, // temp_storage_bytes // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h index 1fb01dc770e3d..5ec0112ac619c 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h @@ -104,10 +104,7 @@ GPUhdi() unsigned char Tracklet::operator<(const Tracklet& t) const GPUhdi() void Tracklet::dump() { - printf("firstClusterIndex: %d", firstClusterIndex); - printf("secondClusterIndex: %d", secondClusterIndex); - printf("tanLambda: %f", tanLambda); - printf("phi: %f", phi); + printf("fClIdx: %d sClIdx: %d tanL: %f phi: %f rof1: %hu rof2: %hu\n", firstClusterIndex, secondClusterIndex, tanLambda, phi, rof[0], rof[1]); } } // namespace its diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 4e49d6237764e..c70ab09bfbb20 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -212,6 +212,14 @@ void TrackerTraits::computeLayerTracklets(const int iteration) std::sort(trkl.begin(), trkl.end(), [](const Tracklet& a, const Tracklet& b) { return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); }); + for (auto& t : tf->getTracklets()[iLayer + 1]) { + if (t.isEmpty()) { + break; + } + std::cout << "layer: " << iLayer + 1 << ":\t"; + t.dump(); + } + /// Remove duplicates auto& lut{tf->getTrackletsLookupTable()[iLayer]}; int id0{-1}, id1{-1}; @@ -233,6 +241,13 @@ void TrackerTraits::computeLayerTracklets(const int iteration) lut.push_back(trkl.size()); } /// Layer 0 is done outside the loop + for (auto& t : tf->getTracklets()[0]) { + if (t.isEmpty()) { + break; + } + std::cout << "layer: " << 0 << ":\t"; + t.dump(); + } std::sort(tf->getTracklets()[0].begin(), tf->getTracklets()[0].end(), [](const Tracklet& a, const Tracklet& b) { return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); }); From 243792f2c5e418301be2d82ab895b086edc05af3 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Mon, 27 Jun 2022 16:17:50 +0200 Subject: [PATCH 14/30] Assess CPU-GPU found tracklets equality --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 13 +- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 2 + .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 321 ++++++------------ .../include/ITStracking/Configuration.h | 2 +- .../tracking/include/ITStracking/Tracklet.h | 10 +- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 39 +-- 6 files changed, 132 insertions(+), 255 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 0a28c24b02936..f8df83f39f7dd 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -123,10 +123,12 @@ class TimeFrameGPU : public TimeFrame unsigned char* getDeviceUsedTracklets(const int rofId); Line* getDeviceLines(const int rofId); Tracklet* getDeviceTracklets(const int rofId, const int layerId); + Tracklet* getDeviceTrackletsAll(const int layerId); int* getDeviceTrackletsLookupTable(const int rofId, const int layerId); int* getDeviceNFoundLines(const int rofId); int* getDeviceExclusiveNFoundLines(const int rofId); int* getDeviceCUBBuffer(const size_t rofId); + int* getDeviceNFoundTracklets() const { return mFoundTracklets; }; float* getDeviceXYCentroids(const int rofId); float* getDeviceZCentroids(const int rofId); int* getDeviceXHistograms(const int rofId); @@ -155,7 +157,8 @@ class TimeFrameGPU : public TimeFrame std::array, NLayers - 1> mTrackletsD; std::array, NLayers - 1> mTrackletsLookupTablesD; std::array, NLayers> mROframesClustersD; // layers x roframes - int* mCUBTmpBuffers; // don't know whether will be used by the tracker + int* mCUBTmpBuffers; + int* mFoundTracklets; gpu::StaticTrackingParameters* mDeviceTrackingParams; IndexTableUtils* mDeviceIndexTableUtils; @@ -218,7 +221,13 @@ inline Line* TimeFrameGPU::getDeviceLines(const int rofId) template inline Tracklet* TimeFrameGPU::getDeviceTracklets(const int rofId, const int layerId) { - return getPtrFromRuler(rofId, mTrackletsD[layerId].get(), mROframesClusters[1].data(), mConfig.maxTrackletsPerCluster); + return getPtrFromRuler(rofId, mTrackletsD[layerId].get(), mROframesClusters[layerId].data(), mConfig.maxTrackletsPerCluster); +} + +template +inline Tracklet* TimeFrameGPU::getDeviceTrackletsAll(const int layerId) +{ + return mTrackletsD[layerId].get(); } template diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index d25b5b6be31ec..fe579e74c20bb 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -121,6 +121,7 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) mNExclusiveFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mUsedTracklets = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; discardResult(cudaMalloc(&mCUBTmpBuffers, mConfig.nMaxROFs * mConfig.tmpCUBBufferSize)); + discardResult(cudaMalloc(&mFoundTracklets, (NLayers - 1) * sizeof(int))); mXYCentroids = Vector{2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity, 2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity}; mZCentroids = Vector{mConfig.nMaxROFs * mConfig.maxLinesCapacity, mConfig.nMaxROFs * mConfig.maxLinesCapacity}; for (size_t i{0}; i < 3; ++i) { @@ -177,6 +178,7 @@ template TimeFrameGPU::~TimeFrameGPU() { discardResult(cudaFree(mCUBTmpBuffers)); + discardResult(cudaFree(mFoundTracklets)); discardResult(cudaFree(mDeviceTrackingParams)); discardResult(cudaFree(mDeviceIndexTableUtils)); } diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 2b04aeb46879d..010cd320b3fca 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -65,13 +65,22 @@ GPUd() float Sq(float q) return q * q; } +template +struct trackletSortLambda : public thrust::binary_function { + GPUhd() bool operator()(const T& lhs, const T& rhs) const + { + return lhs.firstClusterIndex < rhs.firstClusterIndex || (lhs.firstClusterIndex == rhs.firstClusterIndex && lhs.secondClusterIndex < rhs.secondClusterIndex); + } +}; + +// Compute the tracklets for a given layer template GPUg() void computeLayerTrackletsKernel( const int rof0, const int maxRofs, const int layerIndex, - const Cluster* clustersNextLayer, // input data rof0-delta * trkPars, const IndexTableUtils* utils, - const unsigned int maxTrackletsPerCluster = 10) + const unsigned int maxTrackletsPerCluster = 50) { - // int clusterTrackletsNum = 0; for (int currentClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; currentClusterIndex < currentLayerClustersSize; currentClusterIndex += blockDim.x * gridDim.x) { unsigned int storedTracklets{0}; const Cluster& currentCluster{clustersCurrentLayer[currentClusterIndex]}; @@ -108,10 +116,6 @@ GPUg() void computeLayerTrackletsKernel( if (primaryVertex.getX() == 0.f && primaryVertex.getY() == 0.f && primaryVertex.getZ() == 0.f) { continue; } - // if (!currentClusterIndex) { - // printf("rof0 %d: Nv: %d -> x: %lf, y: %lf, z: %lf\n", rof0, iPrimaryVertex, primaryVertex.getX(), primaryVertex.getY(), primaryVertex.getZ()); - // } - const float resolution{o2::gpu::GPUCommonMath::Sqrt(Sq(trkPars->PVres) / primaryVertex.getNContributors() + Sq(positionResolution))}; const float tanLambda{(currentCluster.zCoordinate - primaryVertex.getZ()) * inverseR0}; const float zAtRmin{tanLambda * (minR - currentCluster.radius) + currentCluster.zCoordinate}; @@ -120,9 +124,6 @@ GPUg() void computeLayerTrackletsKernel( const float sigmaZ{std::sqrt(Sq(resolution) * Sq(tanLambda) * ((Sq(inverseR0) + sqInverseDeltaZ0) * Sq(meanDeltaR) + 1.f) + Sq(meanDeltaR * mSAngle))}; const int4 selectedBinsRect{getBinsRect(currentCluster, layerIndex, *utils, zAtRmin, zAtRmax, sigmaZ * trkPars->NSigmaCut, phiCut)}; - // if (!currentClusterIndex) { - // printf("%d %d %d %d\n", selectedBinsRect.x, selectedBinsRect.y, selectedBinsRect.z, selectedBinsRect.w); - // } if (selectedBinsRect.x == 0 && selectedBinsRect.y == 0 && selectedBinsRect.z == 0 && selectedBinsRect.w == 0) { continue; } @@ -133,7 +134,6 @@ GPUg() void computeLayerTrackletsKernel( constexpr int tableSize{256 * 128 + 1}; // hardcoded for the time being for (int rof1{minRof}; rof1 <= maxRof; ++rof1) { - // printf("%d %d %d \n", minRof, maxRof, roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1]); if (!(roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1])) { // number of clusters on next layer > 0 continue; } @@ -143,15 +143,10 @@ GPUg() void computeLayerTrackletsKernel( const int maxBinIndex{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1}; const int firstRowClusterIndex = indexTable[rof1 * tableSize + firstBinIndex]; const int maxRowClusterIndex = indexTable[rof1 * tableSize + maxBinIndex]; - // if (!currentClusterIndex) { - // printf("%d %d %d %d\n", firstBinIndex, maxBinIndex, firstRowClusterIndex, maxRowClusterIndex); - // } for (int iNextCluster{firstRowClusterIndex}; iNextCluster < maxRowClusterIndex; ++iNextCluster) { - // printf("%d %d\n", iNextCluster, roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1]); if (iNextCluster >= (roFrameClustersNext[rof1 + 1] - roFrameClustersNext[rof1])) { break; } - const Cluster& nextCluster{getPtrFromRuler(rof1, clustersNextLayer, roFrameClustersNext)[iNextCluster]}; if (usedClustersNextLayer[nextCluster.clusterId]) { continue; @@ -160,134 +155,42 @@ GPUg() void computeLayerTrackletsKernel( const float deltaZ{o2::gpu::GPUCommonMath::Abs(tanLambda * (nextCluster.radius - currentCluster.radius) + currentCluster.zCoordinate - nextCluster.zCoordinate)}; if (deltaZ / sigmaZ < trkPars->NSigmaCut && (deltaPhi < phiCut || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < phiCut)) { - trackletsLookUpTable[currentSortedIndex]++; // Should be race-condition safe - // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f %f %f \n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate, deltaPhi, deltaZ); - const float phi{o2::gpu::GPUCommonMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, - currentCluster.xCoordinate - nextCluster.xCoordinate)}; - const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / - (currentCluster.radius - nextCluster.radius)}; + trackletsLookUpTable[currentSortedIndex]++; // Race-condition safe + const float phi{o2::gpu::GPUCommonMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, currentCluster.xCoordinate - nextCluster.xCoordinate)}; + const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / (currentCluster.radius - nextCluster.radius)}; const size_t stride{currentClusterIndex * maxTrackletsPerCluster}; new (tracklets + stride + storedTracklets) Tracklet{currentSortedIndex, roFrameClustersNext[rof1] + iNextCluster, tanL, phi, rof0, rof1}; - if (currentSortedIndex == 127 && (roFrameClustersNext[rof1] + iNextCluster) == 145) { - tracklets[stride + storedTracklets].dump(); - printf("id: %lu \n", stride + storedTracklets); - } ++storedTracklets; - // printf("%d %d %lf %lf %hu %hu\n", t.firstClusterIndex, t.secondClusterIndex, t.tanLambda, t.phi, t.rof[0], t.rof[1]); } } } } } + if (storedTracklets > maxTrackletsPerCluster) { + printf("its-gpu-tracklet finder: found more tracklets per clusters (%d) than maximum set (%d), check the configuration!\n", maxTrackletsPerCluster, storedTracklets); + } } } -// GPUd() void computeLayerCells(DeviceStoreNV& devStore, const int layerIndex, -// Vector& cellsVector) -// { -// const int currentTrackletIndex = static_cast(blockDim.x * blockIdx.x + threadIdx.x); -// const float3& primaryVertex = devStore.getPrimaryVertex(); -// int trackletCellsNum = 0; -// if (currentTrackletIndex < devStore.getTracklets()[layerIndex].size()) { -// const Tracklet& currentTracklet{devStore.getTracklets()[layerIndex][currentTrackletIndex]}; -// const int nextLayerClusterIndex{currentTracklet.secondClusterIndex}; -// const int nextLayerFirstTrackletIndex{ -// devStore.getTrackletsLookupTable()[layerIndex][nextLayerClusterIndex]}; -// const int nextLayerTrackletsNum{static_cast(devStore.getTracklets()[layerIndex + 1].size())}; -// if (devStore.getTracklets()[layerIndex + 1][nextLayerFirstTrackletIndex].firstClusterIndex == nextLayerClusterIndex) { -// const Cluster& firstCellCluster{ -// devStore.getClusters()[layerIndex][currentTracklet.firstClusterIndex]}; -// const Cluster& secondCellCluster{ -// devStore.getClusters()[layerIndex + 1][currentTracklet.secondClusterIndex]}; -// const float firstCellClusterQuadraticRCoordinate{firstCellCluster.radius * firstCellCluster.radius}; -// const float secondCellClusterQuadraticRCoordinate{secondCellCluster.radius * secondCellCluster.radius}; -// const float3 firstDeltaVector{secondCellCluster.xCoordinate - firstCellCluster.xCoordinate, -// secondCellCluster.yCoordinate - firstCellCluster.yCoordinate, secondCellClusterQuadraticRCoordinate - firstCellClusterQuadraticRCoordinate}; -// for (int iNextLayerTracklet{nextLayerFirstTrackletIndex}; -// iNextLayerTracklet < nextLayerTrackletsNum && devStore.getTracklets()[layerIndex + 1][iNextLayerTracklet].firstClusterIndex == nextLayerClusterIndex; ++iNextLayerTracklet) { -// const Tracklet& nextTracklet{devStore.getTracklets()[layerIndex + 1][iNextLayerTracklet]}; -// const float deltaTanLambda{o2::gpu::GPUCommonMath::Abs(currentTracklet.tanLambda - nextTracklet.tanLambda)}; -// const float deltaPhi{o2::gpu::GPUCommonMath::Abs(currentTracklet.phi - nextTracklet.phi)}; -// if (deltaTanLambda < trkPars->CellMaxDeltaTanLambda && (deltaPhi < trkPars->CellMaxDeltaPhi || o2::gpu::GPUCommonMath::Abs(deltaPhi - constants::math::TwoPi) < trkPars->CellMaxDeltaPhi)) { -// const float averageTanLambda{0.5f * (currentTracklet.tanLambda + nextTracklet.tanLambda)}; -// const float directionZIntersection{-averageTanLambda * firstCellCluster.radius + firstCellCluster.zCoordinate}; -// const float deltaZ{o2::gpu::GPUCommonMath::Abs(directionZIntersection - primaryVertex.z)}; -// if (deltaZ < trkPars->CellMaxDeltaZ[layerIndex]) { -// const Cluster& thirdCellCluster{ -// devStore.getClusters()[layerIndex + 2][nextTracklet.secondClusterIndex]}; -// const float thirdCellClusterQuadraticRCoordinate{thirdCellCluster.radius * thirdCellCluster.radius}; -// const float3 secondDeltaVector{thirdCellCluster.xCoordinate - firstCellCluster.xCoordinate, -// thirdCellCluster.yCoordinate - firstCellCluster.yCoordinate, thirdCellClusterQuadraticRCoordinate - firstCellClusterQuadraticRCoordinate}; -// float3 cellPlaneNormalVector{math_utils::crossProduct(firstDeltaVector, secondDeltaVector)}; -// const float vectorNorm{o2::gpu::GPUCommonMath::Sqrt( -// cellPlaneNormalVector.x * cellPlaneNormalVector.x + cellPlaneNormalVector.y * cellPlaneNormalVector.y + cellPlaneNormalVector.z * cellPlaneNormalVector.z)}; -// if (!(vectorNorm < constants::math::FloatMinThreshold || o2::gpu::GPUCommonMath::Abs(cellPlaneNormalVector.z) < constants::math::FloatMinThreshold)) { -// const float inverseVectorNorm{1.0f / vectorNorm}; -// const float3 normalizedPlaneVector{cellPlaneNormalVector.x * inverseVectorNorm, cellPlaneNormalVector.y * inverseVectorNorm, cellPlaneNormalVector.z * inverseVectorNorm}; -// const float planeDistance{-normalizedPlaneVector.x * (secondCellCluster.xCoordinate - primaryVertex.x) - (normalizedPlaneVector.y * secondCellCluster.yCoordinate - primaryVertex.y) - normalizedPlaneVector.z * secondCellClusterQuadraticRCoordinate}; -// const float normalizedPlaneVectorQuadraticZCoordinate{normalizedPlaneVector.z * normalizedPlaneVector.z}; -// const float cellTrajectoryRadius{o2::gpu::GPUCommonMath::Sqrt( -// (1.0f - normalizedPlaneVectorQuadraticZCoordinate - 4.0f * planeDistance * normalizedPlaneVector.z) / (4.0f * normalizedPlaneVectorQuadraticZCoordinate))}; -// const float2 circleCenter{-0.5f * normalizedPlaneVector.x / normalizedPlaneVector.z, -0.5f * normalizedPlaneVector.y / normalizedPlaneVector.z}; -// const float distanceOfClosestApproach{o2::gpu::GPUCommonMath::Abs( -// cellTrajectoryRadius - o2::gpu::GPUCommonMath::Sqrt(circleCenter.x * circleCenter.x + circleCenter.y * circleCenter.y))}; -// if (distanceOfClosestApproach <= trkPars->CellMaxDCA[layerIndex]) { -// cooperative_groups::coalesced_group threadGroup = cooperative_groups::coalesced_threads(); -// int currentIndex{}; -// if (threadGroup.thread_rank() == 0) { -// currentIndex = cellsVector.extend(threadGroup.size()); -// } -// currentIndex = threadGroup.shfl(currentIndex, 0) + threadGroup.thread_rank(); -// cellsVector.emplace(currentIndex, currentTracklet.firstClusterIndex, -// nextTracklet.firstClusterIndex, nextTracklet.secondClusterIndex, currentTrackletIndex, -// iNextLayerTracklet, averageTanLambda); -// ++trackletCellsNum; -// } -// } -// } -// } -// } -// if (layerIndex > 0) { -// devStore.getCellsPerTrackletTable()[layerIndex - 1][currentTrackletIndex] = trackletCellsNum; -// } -// } -// } -// } - -// GPUg() void sortTrackletsKernel(DeviceStoreNV& devStore, const int layerIndex, -// Vector tempTrackletArray) -// { -// const int currentTrackletIndex{static_cast(blockDim.x * blockIdx.x + threadIdx.x)}; -// if (currentTrackletIndex < tempTrackletArray.size()) { -// const int firstClusterIndex = tempTrackletArray[currentTrackletIndex].firstClusterIndex; -// const int offset = atomicAdd(&devStore.getTrackletsPerClusterTable()[layerIndex - 1][firstClusterIndex], -1) - 1; -// const int startIndex = devStore.getTrackletsLookupTable()[layerIndex - 1][firstClusterIndex]; -// memcpy(&devStore.getTracklets()[layerIndex][startIndex + offset], -// &tempTrackletArray[currentTrackletIndex], sizeof(Tracklet)); -// } -// } - -// GPUg() void layerCellsKernel(DeviceStoreNV& devStore, const int layerIndex, -// Vector cellsVector) -// { -// computeLayerCells(devStore, layerIndex, cellsVector); -// } - -// GPUg() void sortCellsKernel(DeviceStoreNV& devStore, const int layerIndex, -// Vector tempCellsArray) -// { -// const int currentCellIndex = static_cast(blockDim.x * blockIdx.x + threadIdx.x); -// if (currentCellIndex < tempCellsArray.size()) { -// const int firstTrackletIndex = tempCellsArray[currentCellIndex].getFirstTrackletIndex(); -// const int offset = atomicAdd(&devStore.getCellsPerTrackletTable()[layerIndex - 1][firstTrackletIndex], -// -1) - -// 1; -// const int startIndex = devStore.getCellsLookupTable()[layerIndex - 1][firstTrackletIndex]; -// memcpy(&devStore.getCells()[layerIndex][startIndex + offset], &tempCellsArray[currentCellIndex], -// sizeof(Cell)); -// } -// } - +// Decrease LUT entries corresponding to duplicated tracklets. NB: duplicate tracklets are removed separately (see const Tracklets*). +GPUg() void removeDuplicateTrackletsEntriesLUTKernel( + int* trackletsLookUpTable, + const Tracklet* tracklets, + const int* nTracklets, + const int layerIndex) +{ + int id0{-1}, id1{-1}; + for (int iTracklet{0}; iTracklet < nTracklets[layerIndex]; ++iTracklet) { + auto& trk = tracklets[iTracklet]; + if (trk.firstClusterIndex == id0 && trk.secondClusterIndex == id1) { + printf("layer: %d, tracklet: %d/%d, decreasing index: %d\n", layerIndex, iTracklet, nTracklets[layerIndex], id0); + trackletsLookUpTable[id0]--; + } else { + id0 = trk.firstClusterIndex; + id1 = trk.secondClusterIndex; + } + } +} } // namespace gpu template @@ -328,8 +231,8 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) rof0, mTimeFrameGPU->getNrof(), iLayer, - mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: + mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: mTimeFrameGPU->getDeviceIndexTables(iLayer + 1), // :check: mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: @@ -351,92 +254,82 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getConfig().maxTrackletsPerCluster); } } - checkGPUError(cudaDeviceSynchronize(), __FILE__, __LINE__); - // std::vector> tables(NLayers - 1); - // for (int i{1}; i < /*NLayers - 1*/ 2; ++i) { - // tables[i].resize(mTimeFrameGPU->mClusters[i].size()); - // checkGPUError(cudaMemcpy(tables[i].data(), mTimeFrameGPU->getDeviceTrackletsLookupTable(0, i), mTimeFrameGPU->mClusters[i].size() * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - // std::exclusive_scan(tables[i].begin(), tables[i].end(), tables[i].begin(), 0); - // std::cout << " === table " << i << " ===" << std::endl; - // for (auto j : tables[i]) { - // std::cout << j << "\n"; - // } - // std::cout << std::endl; - // } - // std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); - // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - // checkGPUError(cudaMemcpy(trackletsHost[iLayer].data(), mTimeFrameGPU->getDeviceTracklets(0, iLayer), mTimeFrameGPU->getConfig().trackletsCapacity * sizeof(Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - // std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].end(), [](const Tracklet& a, const Tracklet& b) { return !a.isEmpty() && b.isEmpty(); }); - // // std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].begin() +) - // int count{0}; - // for (auto& t : trackletsHost[iLayer]) { - // t.dump(); - // if (++count > 10) { - // break; - // } - // } - // std::cout << iLayer << " ===" << std::endl; - // } - - // int* trackletSizesD; - // int trackletSizeH[6]; - // checkGPUError(cudaMalloc(reinterpret_cast(&trackletSizesD), (NLayers - 1) * sizeof(int)), __FILE__, __LINE__); + std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); + std::vector trackletSizeH(NLayers - 1, 0); + size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; - auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTracklets(0, iLayer)); - auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTracklets(0, iLayer) + mTimeFrameGPU->mClusters[iLayer].size() * mTimeFrameGPU->getConfig().maxTrackletsPerCluster); - // thrust::sort(begin, end); + auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); + auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + mTimeFrameGPU->getConfig().trackletsCapacity); + + // Sort tracklets to put empty ones on the right side of the array. + thrust::sort(begin, end); + // Get number of found tracklets + discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + bufferSize, // temp_storage_bytes + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + mTimeFrameGPU->getDeviceNFoundTracklets() + iLayer, // d_out + mTimeFrameGPU->mClusters[iLayer].size(), // num_items + streamArray[iLayer].get())); } - std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); + checkGPUError(cudaMemcpy(trackletSizeH.data(), mTimeFrameGPU->getDeviceNFoundTracklets(), (NLayers - 1) * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - checkGPUError(cudaMemcpy(trackletsHost[iLayer].data(), mTimeFrameGPU->getDeviceTracklets(0, iLayer), mTimeFrameGPU->getConfig().trackletsCapacity * sizeof(Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - // auto it = trackletsHost[iLayer].begin(); - // do { - // ++it; - // } while (!((*it).isEmpty())); - // std::sort(trackletsHost[iLayer].begin(), it, [](const Tracklet& a, const Tracklet& b) { - // return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); - // }); - int count{0}; - int count2{0}; - trackletsHost[0][2650].dump(); - for (auto& t : trackletsHost[iLayer]) { - if (!t.isEmpty()) { - ++count; - } - if (t.firstClusterIndex == 127 && t.secondClusterIndex == 145) { - std::cout << "got it at: " << count2 << std::endl; - t.dump(); + // Sort tracklets according to cluster ids + auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); + auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); + thrust::sort(begin, end, gpu::trackletSortLambda()); + + // Remove duplicate entries in LUTs, done by single thread so far + gpu::removeDuplicateTrackletsEntriesLUTKernel<<<1, 1, 0, streamArray[iLayer].get()>>>( + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), + mTimeFrameGPU->getDeviceTrackletsAll(iLayer), + mTimeFrameGPU->getDeviceNFoundTracklets(), + iLayer); + } + discardResult(cudaDeviceSynchronize()); + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); + auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); + // Remove actual tracklet duplicates + auto new_end = thrust::unique(begin, end); + LOG(info) << iLayer << " new size is: " << new_end - begin; + } + discardResult(cudaDeviceSynchronize()); + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + // Compute LUT + discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + bufferSize, // temp_storage_bytes + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out + mTimeFrameGPU->mClusters[iLayer].size(), // num_items + streamArray[iLayer].get())); + } + discardResult(cudaDeviceSynchronize()); + + /// Create tracklets labels + if (tf->hasMCinformation()) { + + for (int iLayer{0}; iLayer < mTrkParams.TrackletsPerRoad(); ++iLayer) { + std::vector tracklets(std::vector(trackletSizeH[iLayer])); + checkGPUError(cudaMemcpy(), __FILE__, __LINE__); + for (auto& trk : tf->getTracklets()[iLayer]) { + MCCompLabel label; + int currentId{tf->getClusters()[iLayer][trk.firstClusterIndex].clusterId}; + int nextId{tf->getClusters()[iLayer + 1][trk.secondClusterIndex].clusterId}; + for (auto& lab1 : tf->getClusterLabels(iLayer, currentId)) { + for (auto& lab2 : tf->getClusterLabels(iLayer + 1, nextId)) { + if (lab1 == lab2 && lab1.isValid()) { + label = lab1; + break; + } + } + if (label.isValid()) { + break; + } + } + tf->getTrackletsLabel(iLayer).emplace_back(label); } - ++count2; } - std::sort(trackletsHost[iLayer].begin(), trackletsHost[iLayer].begin() + count, [](const Tracklet& a, const Tracklet& b) { - return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); - }); - // for (auto& t : trackletsHost[iLayer]) { - // if (t.isEmpty()) { - // continue; - // } - // std::cout << "layer: " << iLayer << ":\t"; - // t.dump(); - // } - // discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage - // bufferSize, // temp_storage_bytes - // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in - // trackletSizesD + iLayer, // d_out - // mTimeFrameGPU->mClusters[iLayer + 1].size())); // num_items - // // printf("buffer size: %zu\n", bufferSize); - // checkGPUError(cudaMemcpy(trackletSizeH + iLayer, trackletSizesD + iLayer, sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - // // printf("size: %d\n", trackletSizeH[iLayer]); - // if (trackletSizeH[iLayer] == 0) { - // continue; - // } - // discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage - // bufferSize, // temp_storage_bytes - // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in - // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out - // mTimeFrameGPU->mClusters[iLayer + 1].size() /*, streamArray[iLayer + 1].get()*/)); // num_items } } diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h index 96cdd862ec31f..0bcfa4b1c58b7 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h @@ -149,7 +149,7 @@ struct TimeFrameGPUConfig { size_t maxTrkCap, size_t maxVertCap); - size_t tmpCUBBufferSize = 1e5; // In average in pp events there are required 4096 bytes + size_t tmpCUBBufferSize = 1e6; // In average in pp events there are required 4096 bytes size_t maxTrackletsPerCluster = 50; size_t clustersPerLayerCapacity = 5e5; size_t clustersPerROfCapacity = 1e4; diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h index 5ec0112ac619c..8d94990d90c1b 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h @@ -92,20 +92,18 @@ GPUhdi() bool Tracklet::operator!=(const Tracklet& rhs) const GPUhdi() unsigned char Tracklet::operator<(const Tracklet& t) const { - if (isEmpty() && t.isEmpty()) { + if (isEmpty()) { return false; - } else { - if (isEmpty()) { - return false; - } } return true; } GPUhdi() void Tracklet::dump() { - printf("fClIdx: %d sClIdx: %d tanL: %f phi: %f rof1: %hu rof2: %hu\n", firstClusterIndex, secondClusterIndex, tanLambda, phi, rof[0], rof[1]); + printf("fClIdx: %d sClIdx: %d rof1: %hu rof2: %hu\n", firstClusterIndex, secondClusterIndex, rof[0], rof[1]); } +// tanL: %f phi: %f +// tanLambda, phi, } // namespace its } // namespace o2 diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index c70ab09bfbb20..611c9f2d8df2b 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -187,24 +187,6 @@ void TrackerTraits::computeLayerTracklets(const int iteration) } } } - // for (int i{0}; i < 1; ++i) { - // std::cout << " === " << std::endl; - // for (auto j : tf->getTrackletsLookupTable()[i]) { - // std::cout << j << "\n"; - // } - // std::cout << std::endl; - // } - // std::vector> tables(5); - // for (int i{0}; i < 1; ++i) { - // tables[i] = tf->getTrackletsLookupTable()[i]; - // std::exclusive_scan(tables[i].begin(), tables[i].end(), tables[i].begin(), 0); - // std::cout << " === table " << i << " ===" << std::endl; - // for (auto j : tables[i]) { - // std::cout << j << "\n"; - // } - // std::cout << std::endl; - // } - /// Cold code, fixups for (int iLayer{0}; iLayer < mTrkParams[iteration].CellsPerRoad(); ++iLayer) { /// Sort tracklets @@ -212,27 +194,23 @@ void TrackerTraits::computeLayerTracklets(const int iteration) std::sort(trkl.begin(), trkl.end(), [](const Tracklet& a, const Tracklet& b) { return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); }); - for (auto& t : tf->getTracklets()[iLayer + 1]) { - if (t.isEmpty()) { - break; - } - std::cout << "layer: " << iLayer + 1 << ":\t"; - t.dump(); - } /// Remove duplicates auto& lut{tf->getTrackletsLookupTable()[iLayer]}; int id0{-1}, id1{-1}; std::vector newTrk; newTrk.reserve(trkl.size()); + int count{0}; for (auto& trk : trkl) { if (trk.firstClusterIndex == id0 && trk.secondClusterIndex == id1) { + printf("layer %d: tracklet: %d/%d, decreasing index %d\n", iLayer + 1, count, trkl.size(), id0); lut[id0]--; } else { id0 = trk.firstClusterIndex; id1 = trk.secondClusterIndex; newTrk.push_back(trk); } + count++; } trkl.swap(newTrk); @@ -241,25 +219,22 @@ void TrackerTraits::computeLayerTracklets(const int iteration) lut.push_back(trkl.size()); } /// Layer 0 is done outside the loop - for (auto& t : tf->getTracklets()[0]) { - if (t.isEmpty()) { - break; - } - std::cout << "layer: " << 0 << ":\t"; - t.dump(); - } std::sort(tf->getTracklets()[0].begin(), tf->getTracklets()[0].end(), [](const Tracklet& a, const Tracklet& b) { return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); }); int id0{-1}, id1{-1}; std::vector newTrk; newTrk.reserve(tf->getTracklets()[0].size()); + int count{0}; for (auto& trk : tf->getTracklets()[0]) { if (trk.firstClusterIndex != id0 || trk.secondClusterIndex != id1) { id0 = trk.firstClusterIndex; id1 = trk.secondClusterIndex; newTrk.push_back(trk); + } else { + printf("layer 0: tracklet: %d/%lu, decreasing index %d\n", count, tf->getTracklets()[0].size(), id0); } + count++; } tf->getTracklets()[0].swap(newTrk); From aac9cbfa90e1aa6bde341b6febbf73ea2f1ab125 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Wed, 29 Jun 2022 10:10:16 +0200 Subject: [PATCH 15/30] Add MC labels generation --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 3 - .../ITS/tracking/GPU/ITStrackingGPU/Vector.h | 2 +- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 10 +- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 119 ++++++++++-------- .../include/ITStracking/Configuration.h | 2 +- 5 files changed, 74 insertions(+), 62 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index f8df83f39f7dd..46c80e469b844 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -251,9 +251,6 @@ inline int* TimeFrameGPU::getDeviceExclusiveNFoundLines(const int rofId template inline int* TimeFrameGPU::getDeviceCUBBuffer(const size_t rofId) { - if (rofId >= mNrof) { - LOG(error) << "Invalid rofId: " << rofId << "/" << mNrof << ", returning nullptr"; - } return reinterpret_cast(reinterpret_cast(mCUBTmpBuffers) + (static_cast(rofId * mConfig.tmpCUBBufferSize) & 0xFFFFFFFFFFFFF000)); } diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h index 24613d70bcd98..62516293827d8 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h @@ -230,7 +230,7 @@ void Vector::resetInt(const size_t size, const int value) utils::host::gpuMalloc(reinterpret_cast(&mDeviceSizePtr), sizeof(int)); } - utils::host::gpuMemset(mArrayPtr, value, size); + utils::host::gpuMemset(mArrayPtr, value, size * sizeof(int)); utils::host::gpuMemcpyHostToDevice(mDeviceSizePtr, &size, sizeof(int)); } diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index fe579e74c20bb..0a0d940a8fade 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -102,13 +102,9 @@ template template void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) { - for (int iLayer{0}; iLayer < NLayers; ++iLayer) { // Tracker and vertexer - if (iLayer < NLayers - 1) { - mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; - } - if (iLayer < NLayers - 1) { - mTrackletsLookupTablesD[iLayer].resetInt(mClusters[iLayer].size()); - } + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Tracker and vertexer + mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; + mTrackletsLookupTablesD[iLayer].resetInt(mClusters[iLayer].size()); } for (auto iComb{0}; iComb < 2; ++iComb) { // Vertexer only diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 010cd320b3fca..0296d887fd739 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -13,10 +13,12 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -29,6 +31,7 @@ #include "ITStrackingGPU/TrackerTraitsGPU.h" #include "GPUCommonLogger.h" +#include "GPUCommonAlgorithmThrust.h" namespace o2 { namespace its @@ -180,6 +183,9 @@ GPUg() void removeDuplicateTrackletsEntriesLUTKernel( const int layerIndex) { int id0{-1}, id1{-1}; + if (threadIdx.x == 0) { + printf("kernel: %d started\n", layerIndex); + } for (int iTracklet{0}; iTracklet < nTracklets[layerIndex]; ++iTracklet) { auto& trk = tracklets[iTracklet]; if (trk.firstClusterIndex == id0 && trk.secondClusterIndex == id1) { @@ -190,6 +196,9 @@ GPUg() void removeDuplicateTrackletsEntriesLUTKernel( id1 = trk.secondClusterIndex; } } + if (threadIdx.x == 0) { + printf("kernel: %d done \n", layerIndex); + } } } // namespace gpu @@ -227,6 +236,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); continue; } + gpu::computeLayerTrackletsKernel<<>>( rof0, mTimeFrameGPU->getNrof(), @@ -258,12 +268,17 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) std::vector trackletSizeH(NLayers - 1, 0); size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); - auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + mTimeFrameGPU->getConfig().trackletsCapacity); - // Sort tracklets to put empty ones on the right side of the array. - thrust::sort(begin, end); + auto thrustTrackletsBegin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); + auto thrustTrackletsEnd = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + mTimeFrameGPU->getConfig().trackletsCapacity); + thrust::sort(thrustTrackletsBegin, thrustTrackletsEnd); + // Get number of found tracklets + // With thrust: + // auto thrustTrackletsLUTbegin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer)); + // auto thrustTrackletsLUTend = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer) + mTimeFrameGPU->mClusters[iLayer].size()); + // trackletSizeH[iLayer] = thrust::reduce(thrustTrackletsLUTbegin, thrustTrackletsLUTend, 0); + discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage bufferSize, // temp_storage_bytes mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in @@ -275,10 +290,13 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Sort tracklets according to cluster ids - auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); - auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); - thrust::sort(begin, end, gpu::trackletSortLambda()); - + std::cout << ">>>> " << trackletSizeH[iLayer] << std::endl; + auto thrustTrackletsBegin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); + auto thrustTrackletsEnd = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); + thrust::sort(thrustTrackletsBegin, thrustTrackletsEnd, gpu::trackletSortLambda()); + } + discardResult(cudaDeviceSynchronize()); + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Remove duplicate entries in LUTs, done by single thread so far gpu::removeDuplicateTrackletsEntriesLUTKernel<<<1, 1, 0, streamArray[iLayer].get()>>>( mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), @@ -286,51 +304,52 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getDeviceNFoundTracklets(), iLayer); } + std::cout << "Here" << std::endl; + // // Remove actual tracklet duplicates + // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + // auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); + // auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); + + // auto new_end = thrust::unique(begin, end); + // trackletSizeH[iLayer] = new_end - begin; + // } discardResult(cudaDeviceSynchronize()); - for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); - auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); - // Remove actual tracklet duplicates - auto new_end = thrust::unique(begin, end); - LOG(info) << iLayer << " new size is: " << new_end - begin; - } - discardResult(cudaDeviceSynchronize()); - for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - // Compute LUT - discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage - bufferSize, // temp_storage_bytes - mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in - mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out - mTimeFrameGPU->mClusters[iLayer].size(), // num_items - streamArray[iLayer].get())); - } - discardResult(cudaDeviceSynchronize()); + // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - /// Create tracklets labels - if (tf->hasMCinformation()) { + // // Compute LUT + // discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + // bufferSize, // temp_storage_bytes + // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out + // mTimeFrameGPU->mClusters[iLayer].size(), // num_items + // streamArray[iLayer].get())); + // } + // discardResult(cudaDeviceSynchronize()); - for (int iLayer{0}; iLayer < mTrkParams.TrackletsPerRoad(); ++iLayer) { - std::vector tracklets(std::vector(trackletSizeH[iLayer])); - checkGPUError(cudaMemcpy(), __FILE__, __LINE__); - for (auto& trk : tf->getTracklets()[iLayer]) { - MCCompLabel label; - int currentId{tf->getClusters()[iLayer][trk.firstClusterIndex].clusterId}; - int nextId{tf->getClusters()[iLayer + 1][trk.secondClusterIndex].clusterId}; - for (auto& lab1 : tf->getClusterLabels(iLayer, currentId)) { - for (auto& lab2 : tf->getClusterLabels(iLayer + 1, nextId)) { - if (lab1 == lab2 && lab1.isValid()) { - label = lab1; - break; - } - } - if (label.isValid()) { - break; - } - } - tf->getTrackletsLabel(iLayer).emplace_back(label); - } - } - } + // // Create tracklets labels, at the moment on the host + // if (mTimeFrameGPU->hasMCinformation()) { + // for (int iLayer{0}; iLayer < mTrkParams.TrackletsPerRoad(); ++iLayer) { + // std::vector tracklets(trackletSizeH[iLayer]); + // checkGPUError(cudaMemcpy(tracklets.data(), mTimeFrameGPU->getDeviceTrackletsAll(iLayer), trackletSizeH[iLayer] * sizeof(o2::its::Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + // for (auto& trk : tracklets) { + // MCCompLabel label; + // int currentId{mTimeFrameGPU->mClusters[iLayer][trk.firstClusterIndex].clusterId}; + // int nextId{mTimeFrameGPU->mClusters[iLayer + 1][trk.secondClusterIndex].clusterId}; + // for (auto& lab1 : mTimeFrameGPU->getClusterLabels(iLayer, currentId)) { + // for (auto& lab2 : mTimeFrameGPU->getClusterLabels(iLayer + 1, nextId)) { + // if (lab1 == lab2 && lab1.isValid()) { + // label = lab1; + // break; + // } + // } + // if (label.isValid()) { + // break; + // } + // } + // mTimeFrameGPU->getTrackletsLabel(iLayer).emplace_back(label); + // } + // } + // } } template diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h index 0bcfa4b1c58b7..96cdd862ec31f 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h @@ -149,7 +149,7 @@ struct TimeFrameGPUConfig { size_t maxTrkCap, size_t maxVertCap); - size_t tmpCUBBufferSize = 1e6; // In average in pp events there are required 4096 bytes + size_t tmpCUBBufferSize = 1e5; // In average in pp events there are required 4096 bytes size_t maxTrackletsPerCluster = 50; size_t clustersPerLayerCapacity = 5e5; size_t clustersPerROfCapacity = 1e4; From 1ca18e4325637697c33f6859833e930ad1076e59 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Thu, 30 Jun 2022 17:09:57 +0200 Subject: [PATCH 16/30] Fix sorting for HIP --- .../ITS/tracking/GPU/ITStrackingGPU/Vector.h | 4 +- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 8 +++- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 47 +++++++++++-------- .../ITS/tracking/GPU/hip/CMakeLists.txt | 2 +- .../tracking/include/ITStracking/Tracklet.h | 9 ++-- 5 files changed, 43 insertions(+), 27 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h index 62516293827d8..38694156e0299 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/Vector.h @@ -56,7 +56,7 @@ class Vector final void reset(const size_t, const size_t = 0); void reset(const T* const, const size_t, const size_t = 0); - void resetInt(const size_t, const int value = 0); + void resetWithInt(const size_t, const int value = 0); void copyIntoSizedVector(std::vector&); GPUhd() T* get() const; @@ -217,7 +217,7 @@ void Vector::reset(const T* const source, const size_t size, const size_t ini } template -void Vector::resetInt(const size_t size, const int value) +void Vector::resetWithInt(const size_t size, const int value) { if (size > mCapacity) { if (mArrayPtr != nullptr) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 0a0d940a8fade..29af5ab9e69e7 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -13,6 +13,8 @@ #include #include +#include + #include "ITStracking/Constants.h" #include "ITStrackingGPU/Utils.h" @@ -104,7 +106,10 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) { for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Tracker and vertexer mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; - mTrackletsLookupTablesD[iLayer].resetInt(mClusters[iLayer].size()); + auto thrustTrackletsBegin = thrust::device_ptr(mTrackletsD[iLayer].get()); + auto thrustTrackletsEnd = thrustTrackletsBegin + mConfig.trackletsCapacity; + thrust::fill(thrustTrackletsBegin, thrustTrackletsEnd, Tracklet{}); + mTrackletsLookupTablesD[iLayer].resetWithInt(mClusters[iLayer].size()); } for (auto iComb{0}; iComb < 2; ++iComb) { // Vertexer only @@ -118,6 +123,7 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) mUsedTracklets = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; discardResult(cudaMalloc(&mCUBTmpBuffers, mConfig.nMaxROFs * mConfig.tmpCUBBufferSize)); discardResult(cudaMalloc(&mFoundTracklets, (NLayers - 1) * sizeof(int))); + discardResult(cudaMemset(mFoundTracklets, 0, (NLayers - 1) * sizeof(int))); mXYCentroids = Vector{2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity, 2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity}; mZCentroids = Vector{mConfig.nMaxROFs * mConfig.maxLinesCapacity, mConfig.nMaxROFs * mConfig.maxLinesCapacity}; for (size_t i{0}; i < 3; ++i) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 0296d887fd739..98877cd6c55a8 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -69,7 +69,15 @@ GPUd() float Sq(float q) } template -struct trackletSortLambda : public thrust::binary_function { +struct trackletSortEmptyFunctor : public thrust::binary_function { + GPUhd() bool operator()(const T& lhs, const T& rhs) const + { + return lhs.firstClusterIndex > rhs.firstClusterIndex; + } +}; + +template +struct trackletSortIndexFunctor : public thrust::binary_function { GPUhd() bool operator()(const T& lhs, const T& rhs) const { return lhs.firstClusterIndex < rhs.firstClusterIndex || (lhs.firstClusterIndex == rhs.firstClusterIndex && lhs.secondClusterIndex < rhs.secondClusterIndex); @@ -183,11 +191,12 @@ GPUg() void removeDuplicateTrackletsEntriesLUTKernel( const int layerIndex) { int id0{-1}, id1{-1}; - if (threadIdx.x == 0) { - printf("kernel: %d started\n", layerIndex); - } for (int iTracklet{0}; iTracklet < nTracklets[layerIndex]; ++iTracklet) { + // printf("accessing tracklet %d/%d\n", iTracklet, nTracklets[layerIndex]); auto& trk = tracklets[iTracklet]; + // if (!threadIdx.x) { + // trk.dump(); + // } if (trk.firstClusterIndex == id0 && trk.secondClusterIndex == id1) { printf("layer: %d, tracklet: %d/%d, decreasing index: %d\n", layerIndex, iTracklet, nTracklets[layerIndex], id0); trackletsLookUpTable[id0]--; @@ -196,9 +205,6 @@ GPUg() void removeDuplicateTrackletsEntriesLUTKernel( id1 = trk.secondClusterIndex; } } - if (threadIdx.x == 0) { - printf("kernel: %d done \n", layerIndex); - } } } // namespace gpu @@ -265,13 +271,14 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) } } std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); + std::vector trackletSizeH(NLayers - 1, 0); size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Sort tracklets to put empty ones on the right side of the array. auto thrustTrackletsBegin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); auto thrustTrackletsEnd = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + mTimeFrameGPU->getConfig().trackletsCapacity); - thrust::sort(thrustTrackletsBegin, thrustTrackletsEnd); + thrust::sort(thrustTrackletsBegin, thrustTrackletsEnd, gpu::trackletSortEmptyFunctor()); // Get number of found tracklets // With thrust: @@ -286,14 +293,14 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->mClusters[iLayer].size(), // num_items streamArray[iLayer].get())); } + discardResult(cudaDeviceSynchronize()); checkGPUError(cudaMemcpy(trackletSizeH.data(), mTimeFrameGPU->getDeviceNFoundTracklets(), (NLayers - 1) * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + // Sort tracklets according to cluster ids - std::cout << ">>>> " << trackletSizeH[iLayer] << std::endl; auto thrustTrackletsBegin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); auto thrustTrackletsEnd = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); - thrust::sort(thrustTrackletsBegin, thrustTrackletsEnd, gpu::trackletSortLambda()); + thrust::sort(thrustTrackletsBegin, thrustTrackletsEnd, gpu::trackletSortIndexFunctor()); } discardResult(cudaDeviceSynchronize()); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { @@ -304,15 +311,17 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getDeviceNFoundTracklets(), iLayer); } - std::cout << "Here" << std::endl; - // // Remove actual tracklet duplicates - // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - // auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); - // auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); + // Remove actual tracklet duplicates + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + std::cout << iLayer << " prima: " << trackletSizeH[iLayer] << std::endl; + auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); + auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); - // auto new_end = thrust::unique(begin, end); - // trackletSizeH[iLayer] = new_end - begin; - // } + auto new_end = thrust::unique(begin, end); + trackletSizeH[iLayer] = new_end - begin; + std::cout << iLayer << " dopo: " << trackletSizeH[iLayer] << std::endl; + discardResult(cudaDeviceSynchronize()); + } discardResult(cudaDeviceSynchronize()); // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/hip/CMakeLists.txt b/Detectors/ITSMFT/ITS/tracking/GPU/hip/CMakeLists.txt index 3c1c83f71a9b2..7f9643b89ccb4 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/hip/CMakeLists.txt +++ b/Detectors/ITSMFT/ITS/tracking/GPU/hip/CMakeLists.txt @@ -31,7 +31,7 @@ if(HIP_ENABLED) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${O2_HIP_CMAKE_CXX_FLAGS} -fgpu-rdc") - message(STATUS "Building ITS HIP vertexer") + message(STATUS "Building ITS HIP tracker") o2_add_library(ITStrackingHIP SOURCES ClusterLinesGPU.hip.cxx Context.hip.cxx diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h index 8d94990d90c1b..530b772abb97a 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h @@ -34,7 +34,7 @@ struct Tracklet final { GPUhdi() bool operator!=(const Tracklet&) const; GPUhdi() unsigned char isEmpty() const { - return !firstClusterIndex && !secondClusterIndex && !tanLambda && !phi; + return firstClusterIndex < 0 || secondClusterIndex < 0 && !tanLambda && !phi; } GPUhdi() void dump(); GPUhdi() unsigned char operator<(const Tracklet&) const; @@ -46,9 +46,8 @@ struct Tracklet final { unsigned short rof[2]; }; -GPUhdi() Tracklet::Tracklet() : firstClusterIndex{0}, secondClusterIndex{0}, tanLambda{0.0f}, phi{0.0f} +GPUhdi() Tracklet::Tracklet() : firstClusterIndex{-1}, secondClusterIndex{-1}, tanLambda{0.0f}, phi{0.0f} { - // Nothing to do } GPUhdi() Tracklet::Tracklet(const int firstClusterOrderingIndex, const int secondClusterOrderingIndex, @@ -79,7 +78,9 @@ GPUhdi() bool Tracklet::operator==(const Tracklet& rhs) const return this->firstClusterIndex == rhs.firstClusterIndex && this->secondClusterIndex == rhs.secondClusterIndex && this->tanLambda == rhs.tanLambda && - this->phi == rhs.phi; + this->phi == rhs.phi && + this->rof[0] == rhs.rof[0] && + this->rof[1] == rhs.rof[1]; } GPUhdi() bool Tracklet::operator!=(const Tracklet& rhs) const From e537c897994192dbef5baee220fb6aabd19bcf4e Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Thu, 30 Jun 2022 17:52:27 +0200 Subject: [PATCH 17/30] Add MC label creation again --- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 80 +++++++++---------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 98877cd6c55a8..31da9b2bcc8ff 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -192,13 +192,8 @@ GPUg() void removeDuplicateTrackletsEntriesLUTKernel( { int id0{-1}, id1{-1}; for (int iTracklet{0}; iTracklet < nTracklets[layerIndex]; ++iTracklet) { - // printf("accessing tracklet %d/%d\n", iTracklet, nTracklets[layerIndex]); auto& trk = tracklets[iTracklet]; - // if (!threadIdx.x) { - // trk.dump(); - // } if (trk.firstClusterIndex == id0 && trk.secondClusterIndex == id1) { - printf("layer: %d, tracklet: %d/%d, decreasing index: %d\n", layerIndex, iTracklet, nTracklets[layerIndex], id0); trackletsLookUpTable[id0]--; } else { id0 = trk.firstClusterIndex; @@ -313,52 +308,49 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) } // Remove actual tracklet duplicates for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - std::cout << iLayer << " prima: " << trackletSizeH[iLayer] << std::endl; auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); auto new_end = thrust::unique(begin, end); trackletSizeH[iLayer] = new_end - begin; - std::cout << iLayer << " dopo: " << trackletSizeH[iLayer] << std::endl; - discardResult(cudaDeviceSynchronize()); } discardResult(cudaDeviceSynchronize()); - // for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - - // // Compute LUT - // discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage - // bufferSize, // temp_storage_bytes - // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in - // mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out - // mTimeFrameGPU->mClusters[iLayer].size(), // num_items - // streamArray[iLayer].get())); - // } - // discardResult(cudaDeviceSynchronize()); - - // // Create tracklets labels, at the moment on the host - // if (mTimeFrameGPU->hasMCinformation()) { - // for (int iLayer{0}; iLayer < mTrkParams.TrackletsPerRoad(); ++iLayer) { - // std::vector tracklets(trackletSizeH[iLayer]); - // checkGPUError(cudaMemcpy(tracklets.data(), mTimeFrameGPU->getDeviceTrackletsAll(iLayer), trackletSizeH[iLayer] * sizeof(o2::its::Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - // for (auto& trk : tracklets) { - // MCCompLabel label; - // int currentId{mTimeFrameGPU->mClusters[iLayer][trk.firstClusterIndex].clusterId}; - // int nextId{mTimeFrameGPU->mClusters[iLayer + 1][trk.secondClusterIndex].clusterId}; - // for (auto& lab1 : mTimeFrameGPU->getClusterLabels(iLayer, currentId)) { - // for (auto& lab2 : mTimeFrameGPU->getClusterLabels(iLayer + 1, nextId)) { - // if (lab1 == lab2 && lab1.isValid()) { - // label = lab1; - // break; - // } - // } - // if (label.isValid()) { - // break; - // } - // } - // mTimeFrameGPU->getTrackletsLabel(iLayer).emplace_back(label); - // } - // } - // } + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { + + // Compute LUT + discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + bufferSize, // temp_storage_bytes + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out + mTimeFrameGPU->mClusters[iLayer].size(), // num_items + streamArray[iLayer].get())); + } + discardResult(cudaDeviceSynchronize()); + + // Create tracklets labels, at the moment on the host + if (mTimeFrameGPU->hasMCinformation()) { + for (int iLayer{0}; iLayer < mTrkParams.TrackletsPerRoad(); ++iLayer) { + std::vector tracklets(trackletSizeH[iLayer]); + checkGPUError(cudaMemcpy(tracklets.data(), mTimeFrameGPU->getDeviceTrackletsAll(iLayer), trackletSizeH[iLayer] * sizeof(o2::its::Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + for (auto& trk : tracklets) { + MCCompLabel label; + int currentId{mTimeFrameGPU->mClusters[iLayer][trk.firstClusterIndex].clusterId}; + int nextId{mTimeFrameGPU->mClusters[iLayer + 1][trk.secondClusterIndex].clusterId}; + for (auto& lab1 : mTimeFrameGPU->getClusterLabels(iLayer, currentId)) { + for (auto& lab2 : mTimeFrameGPU->getClusterLabels(iLayer + 1, nextId)) { + if (lab1 == lab2 && lab1.isValid()) { + label = lab1; + break; + } + } + if (label.isValid()) { + break; + } + } + mTimeFrameGPU->getTrackletsLabel(iLayer).emplace_back(label); + } + } + } } template From 91a5b7a1f2ba14a17dd65260becb8980d0653385 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Fri, 1 Jul 2022 14:01:12 +0200 Subject: [PATCH 18/30] Improve API and add Cell finder kernel skeleton --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 5 + .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 1 + .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 234 ++++++++++-------- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 2 +- 4 files changed, 139 insertions(+), 103 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 46c80e469b844..976c74930c8a5 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -21,6 +21,7 @@ #include "ITStracking/Configuration.h" #include "ITStrackingGPU/ClusterLinesGPU.h" +#include "ITStrackingGPU/Stream.h" #include "Array.h" #include "Vector.h" @@ -115,6 +116,8 @@ class TimeFrameGPU : public TimeFrame int* getDeviceROframesClustersOnLayer(const int layerId) const { return mROframesClustersD[layerId].get(); } int getNClustersLayer(const int rofId, const int layerId) const; TimeFrameGPUConfig& getConfig() { return mConfig; } + gpu::Stream& getStream(const int iLayer) { return mStreamArray[iLayer]; } + std::vector& getTrackletSizeHost() { return mTrackletSizeHost; } // Vertexer only int* getDeviceNTrackletsCluster(int rofId, int combId); @@ -147,6 +150,8 @@ class TimeFrameGPU : public TimeFrame private: TimeFrameGPUConfig mConfig; + std::array mStreamArray; + std::vector mTrackletSizeHost; // Per-layer information, do not expand at runtime std::array, NLayers> mClustersD; diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 29af5ab9e69e7..4d59c2963ada7 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -104,6 +104,7 @@ template template void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) { + mTrackletSizeHost.resize(NLayers - 1, 0); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Tracker and vertexer mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; auto thrustTrackletsBegin = thrust::device_ptr(mTrackletsD[iLayer].get()); diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 31da9b2bcc8ff..1142a431f7018 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -27,7 +27,6 @@ #include "ITStracking/IndexTableUtils.h" #include "ITStracking/MathUtils.h" -#include "ITStrackingGPU/Stream.h" #include "ITStrackingGPU/TrackerTraitsGPU.h" #include "GPUCommonLogger.h" @@ -63,7 +62,7 @@ GPUd() const int4 getBinsRect(const Cluster& currentCluster, const int layerInde utils.getPhiBinIndex(math_utils::getNormalizedPhi(phiRangeMax))}; } -GPUd() float Sq(float q) +GPUhd() float Sq(float q) { return q * q; } @@ -201,6 +200,34 @@ GPUg() void removeDuplicateTrackletsEntriesLUTKernel( } } } + +// Compute cells kernel +template +GPUg() void computeLayerCellsKernel( + const Tracklet* trackletsCurrentLayer, + const Tracklet* trackletsNextLayer, + const int* trackletsCurrentLayerLUT, + const int nTracklets) +{ + for (int currentTrackletIndex = blockIdx.x * blockDim.x + threadIdx.x; currentTrackletIndex < nTracklets; currentTrackletIndex += blockDim.x * gridDim.x) { + const Tracklet& currentTracklet = trackletsCurrentLayer[currentTrackletIndex]; + const int nextLayerClusterIndex{currentTracklet.secondClusterIndex}; + const int nextLayerFirstTrackletIndex{trackletsCurrentLayerLUT[nextLayerClusterIndex]}; + const int nextLayerLastTrackletIndex{trackletsCurrentLayerLUT[nextLayerClusterIndex + 1]}; + + if (nextLayerFirstTrackletIndex == nextLayerLastTrackletIndex) { + continue; + } + for (int iNextTracklet{nextLayerFirstTrackletIndex}; iNextTracklet < nextLayerLastTrackletIndex; ++iNextTracklet) { + if (trackletsNextLayer[iNextTracklet].firstClusterIndex != nextLayerClusterIndex) { + break; + } + const Tracklet& nextTracklet = trackletsNextLayer[iNextTracklet]; + const float deltaTanLambda{o2::gpu::GPUCommonMath::Abs(currentTracklet.tanLambda - nextTracklet.tanLambda)}; + const float tanLambda{(currentTracklet.tanLambda + nextTracklet.tanLambda) * 0.5f}; + } + } +} } // namespace gpu template @@ -213,10 +240,9 @@ void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const M template void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { - std::array streamArray; - const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); gsl::span diamondSpan(&diamondVert, 1); + for (int rof0{0}; rof0 < mTimeFrameGPU->getNrof(); ++rof0) { gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one std::vector paddedVertices; @@ -238,7 +264,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) continue; } - gpu::computeLayerTrackletsKernel<<>>( + gpu::computeLayerTrackletsKernel<<getStream(iLayer).get()>>>( rof0, mTimeFrameGPU->getNrof(), iLayer, @@ -265,9 +291,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getConfig().maxTrackletsPerCluster); } } - std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); - - std::vector trackletSizeH(NLayers - 1, 0); + // std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Sort tracklets to put empty ones on the right side of the array. @@ -279,28 +303,28 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) // With thrust: // auto thrustTrackletsLUTbegin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer)); // auto thrustTrackletsLUTend = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer) + mTimeFrameGPU->mClusters[iLayer].size()); - // trackletSizeH[iLayer] = thrust::reduce(thrustTrackletsLUTbegin, thrustTrackletsLUTend, 0); + // mTimeFrameGPU->getTrackletSizeHost()[iLayer] = thrust::reduce(thrustTrackletsLUTbegin, thrustTrackletsLUTend, 0); discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage bufferSize, // temp_storage_bytes mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in mTimeFrameGPU->getDeviceNFoundTracklets() + iLayer, // d_out mTimeFrameGPU->mClusters[iLayer].size(), // num_items - streamArray[iLayer].get())); + mTimeFrameGPU->getStream(iLayer).get())); } discardResult(cudaDeviceSynchronize()); - checkGPUError(cudaMemcpy(trackletSizeH.data(), mTimeFrameGPU->getDeviceNFoundTracklets(), (NLayers - 1) * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + checkGPUError(cudaMemcpy(mTimeFrameGPU->getTrackletSizeHost().data(), mTimeFrameGPU->getDeviceNFoundTracklets(), (NLayers - 1) * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Sort tracklets according to cluster ids auto thrustTrackletsBegin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); - auto thrustTrackletsEnd = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); + auto thrustTrackletsEnd = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + mTimeFrameGPU->getTrackletSizeHost()[iLayer]); thrust::sort(thrustTrackletsBegin, thrustTrackletsEnd, gpu::trackletSortIndexFunctor()); } discardResult(cudaDeviceSynchronize()); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Remove duplicate entries in LUTs, done by single thread so far - gpu::removeDuplicateTrackletsEntriesLUTKernel<<<1, 1, 0, streamArray[iLayer].get()>>>( + gpu::removeDuplicateTrackletsEntriesLUTKernel<<<1, 1, 0, mTimeFrameGPU->getStream(iLayer).get()>>>( mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), mTimeFrameGPU->getDeviceTrackletsAll(iLayer), mTimeFrameGPU->getDeviceNFoundTracklets(), @@ -309,10 +333,10 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) // Remove actual tracklet duplicates for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { auto begin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); - auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + trackletSizeH[iLayer]); + auto end = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer) + mTimeFrameGPU->getTrackletSizeHost()[iLayer]); auto new_end = thrust::unique(begin, end); - trackletSizeH[iLayer] = new_end - begin; + mTimeFrameGPU->getTrackletSizeHost()[iLayer] = new_end - begin; } discardResult(cudaDeviceSynchronize()); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { @@ -323,15 +347,15 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_in mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), // d_out mTimeFrameGPU->mClusters[iLayer].size(), // num_items - streamArray[iLayer].get())); + mTimeFrameGPU->getStream(iLayer).get())); } discardResult(cudaDeviceSynchronize()); // Create tracklets labels, at the moment on the host if (mTimeFrameGPU->hasMCinformation()) { for (int iLayer{0}; iLayer < mTrkParams.TrackletsPerRoad(); ++iLayer) { - std::vector tracklets(trackletSizeH[iLayer]); - checkGPUError(cudaMemcpy(tracklets.data(), mTimeFrameGPU->getDeviceTrackletsAll(iLayer), trackletSizeH[iLayer] * sizeof(o2::its::Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + std::vector tracklets(mTimeFrameGPU->getTrackletSizeHost()[iLayer]); + checkGPUError(cudaMemcpy(tracklets.data(), mTimeFrameGPU->getDeviceTrackletsAll(iLayer), mTimeFrameGPU->getTrackletSizeHost()[iLayer] * sizeof(o2::its::Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); for (auto& trk : tracklets) { MCCompLabel label; int currentId{mTimeFrameGPU->mClusters[iLayer][trk.firstClusterIndex].clusterId}; @@ -356,90 +380,96 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) template void TrackerTraitsGPU::computeLayerCells() { - // PrimaryVertexContextNV* primaryVertexContext = static_cast(nullptr); //TODO: FIX THIS with Time Frames - // std::array tempSize; - // std::array trackletsNum; - // std::array cellsNum; - // std::array streamArray; - - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad - 1; ++iLayer) { - // tempSize[iLayer] = 0; - // trackletsNum[iLayer] = primaryVertexContext->getDeviceTracklets()[iLayer + 1].getSizeFromDevice(); - // primaryVertexContext->getTempCellArray()[iLayer].reset( - // static_cast(primaryVertexContext->getDeviceCells()[iLayer + 1].capacity())); - // if (trackletsNum[iLayer] == 0) { - // continue; - // } - // cub::DeviceScan::ExclusiveSum(static_cast(NULL), tempSize[iLayer], - // primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), - // primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer]); - // primaryVertexContext->getTempTableArray()[iLayer].reset(static_cast(tempSize[iLayer])); - // } - // cudaDeviceSynchronize(); - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - // const gpu::DeviceProperties& deviceProperties = gpu::Context::getInstance().getDeviceProperties(); - // const int trackletsSize = primaryVertexContext->getDeviceTracklets()[iLayer].getSizeFromDevice(); - // if (trackletsSize == 0) { - // continue; - // } - // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsSize)}; - // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsSize)}; - // if (iLayer == 0) { - // gpu::layerCellsKernel<<>>(primaryVertexContext->getDeviceContext(), - // iLayer, primaryVertexContext->getDeviceCells()[iLayer].getWeakCopy()); - // } else { - // gpu::layerCellsKernel<<>>(primaryVertexContext->getDeviceContext(), - // iLayer, primaryVertexContext->getTempCellArray()[iLayer - 1].getWeakCopy()); - // } - // cudaError_t error = cudaGetLastError(); - // if (error != cudaSuccess) { - // std::ostringstream errorString{}; - // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" - // << std::endl; - // throw std::runtime_error{errorString.str()}; - // } - // } - // cudaDeviceSynchronize(); - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad - 1; ++iLayer) { - // cellsNum[iLayer] = primaryVertexContext->getTempCellArray()[iLayer].getSizeFromDevice(); - // if (cellsNum[iLayer] == 0) { - // continue; - // } - // primaryVertexContext->getDeviceCells()[iLayer + 1].resize(cellsNum[iLayer]); - // cub::DeviceScan::ExclusiveSum(static_cast(primaryVertexContext->getTempTableArray()[iLayer].get()), tempSize[iLayer], - // primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), - // primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer], - // streamArray[iLayer + 1].get()); - // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsNum[iLayer])}; - // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsNum[iLayer])}; - // gpu::sortCellsKernel<<>>(primaryVertexContext->getDeviceContext(), - // iLayer + 1, primaryVertexContext->getTempCellArray()[iLayer].getWeakCopy()); - // cudaError_t error = cudaGetLastError(); - // if (error != cudaSuccess) { - // std::ostringstream errorString{}; - // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" - // << std::endl; - // throw std::runtime_error{errorString.str()}; - // } - // } - // cudaDeviceSynchronize(); - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - // int cellsSize = 0; - // if (iLayer == 0) { - // cellsSize = primaryVertexContext->getDeviceCells()[iLayer].getSizeFromDevice(); - // if (cellsSize == 0) { - // continue; - // } - // } else { - // cellsSize = cellsNum[iLayer - 1]; - // if (cellsSize == 0) { - // continue; - // } - // primaryVertexContext->getDeviceCellsLookupTable()[iLayer - 1].copyIntoVector( - // primaryVertexContext->getCellsLookupTable()[iLayer - 1], trackletsNum[iLayer - 1]); - // } - // primaryVertexContext->getDeviceCells()[iLayer].copyIntoVector(primaryVertexContext->getCells()[iLayer], cellsSize); - // } + for (int iLayer{0}; iLayer < NLayers - 2 - 1; ++iLayer) { + if (!mTimeFrameGPU->getTrackletSizeHost()[iLayer + 1] || + !mTimeFrameGPU->getTrackletSizeHost()[iLayer]) { + continue; + } + float resolution{std::sqrt(gpu::Sq(mTrkParams.LayerMisalignment[iLayer]) + gpu::Sq(mTrkParams.LayerMisalignment[iLayer + 1]) + gpu::Sq(mTrkParams.LayerMisalignment[iLayer + 2])) / mTrkParams.LayerResolution[iLayer]}; + resolution = resolution > 1.e-12 ? resolution : 1.f; + + gpu::computeLayerCellsKernel<<<1, 1, 0, mTimeFrameGPU->getStream(iLayer).get()>>>( + mTimeFrameGPU->getDeviceTrackletsAll(iLayer), + mTimeFrameGPU->getDeviceTrackletsAll(iLayer + 1), + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), + mTimeFrameGPU->getTrackletSizeHost()[iLayer]); + // tempSize[iLayer] = 0; + // trackletsNum[iLayer] = primaryVertexContext->getDeviceTracklets()[iLayer + 1].getSizeFromDevice(); + // primaryVertexContext->getTempCellArray()[iLayer].reset( + // static_cast(primaryVertexContext->getDeviceCells()[iLayer + 1].capacity())); + // if (trackletsNum[iLayer] == 0) { + // continue; + // } + // cub::DeviceScan::ExclusiveSum(static_cast(NULL), tempSize[iLayer], + // primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), + // primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer]); + // primaryVertexContext->getTempTableArray()[iLayer].reset(static_cast(tempSize[iLayer])); + // } + // cudaDeviceSynchronize(); + // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { + // const gpu::DeviceProperties& deviceProperties = gpu::Context::getInstance().getDeviceProperties(); + // const int trackletsSize = primaryVertexContext->getDeviceTracklets()[iLayer].getSizeFromDevice(); + // if (trackletsSize == 0) { + // continue; + // } + // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsSize)}; + // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsSize)}; + // if (iLayer == 0) { + // gpu::layerCellsKernel<<getStream(iLayer).get()>>>(primaryVertexContext->getDeviceContext(), + // iLayer, primaryVertexContext->getDeviceCells()[iLayer].getWeakCopy()); + // } else { + // gpu::layerCellsKernel<<getStream(iLayer).get()>>>(primaryVertexContext->getDeviceContext(), + // iLayer, primaryVertexContext->getTempCellArray()[iLayer - 1].getWeakCopy()); + // } + // cudaError_t error = cudaGetLastError(); + // if (error != cudaSuccess) { + // std::ostringstream errorString{}; + // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" + // << std::endl; + // throw std::runtime_error{errorString.str()}; + // } + // } + // cudaDeviceSynchronize(); + // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad - 1; ++iLayer) { + // cellsNum[iLayer] = primaryVertexContext->getTempCellArray()[iLayer].getSizeFromDevice(); + // if (cellsNum[iLayer] == 0) { + // continue; + // } + // primaryVertexContext->getDeviceCells()[iLayer + 1].resize(cellsNum[iLayer]); + // cub::DeviceScan::ExclusiveSum(static_cast(primaryVertexContext->getTempTableArray()[iLayer].get()), tempSize[iLayer], + // primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), + // primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer], + // streamArray[iLayer + 1].get()); + // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsNum[iLayer])}; + // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsNum[iLayer])}; + // gpu::sortCellsKernel<<>>(primaryVertexContext->getDeviceContext(), + // iLayer + 1, primaryVertexContext->getTempCellArray()[iLayer].getWeakCopy()); + // cudaError_t error = cudaGetLastError(); + // if (error != cudaSuccess) { + // std::ostringstream errorString{}; + // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" + // << std::endl; + // throw std::runtime_error{errorString.str()}; + // } + // } + // cudaDeviceSynchronize(); + // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { + // int cellsSize = 0; + // if (iLayer == 0) { + // cellsSize = primaryVertexContext->getDeviceCells()[iLayer].getSizeFromDevice(); + // if (cellsSize == 0) { + // continue; + // } + // } else { + // cellsSize = cellsNum[iLayer - 1]; + // if (cellsSize == 0) { + // continue; + // } + // primaryVertexContext->getDeviceCellsLookupTable()[iLayer - 1].copyIntoVector( + // primaryVertexContext->getCellsLookupTable()[iLayer - 1], trackletsNum[iLayer - 1]); + // } + // primaryVertexContext->getDeviceCells()[iLayer].copyIntoVector(primaryVertexContext->getCells()[iLayer], cellsSize); + } } // void TrackerTraitsGPU::refitTracks(const std::vector>& tf, std::vector& tracks) diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 611c9f2d8df2b..52e401d1e398f 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -232,7 +232,7 @@ void TrackerTraits::computeLayerTracklets(const int iteration) id1 = trk.secondClusterIndex; newTrk.push_back(trk); } else { - printf("layer 0: tracklet: %d/%lu, decreasing index %d\n", count, tf->getTracklets()[0].size(), id0); + // printf("layer 0: tracklet: %d/%lu, decreasing index %d\n", count, tf->getTracklets()[0].size(), id0); } count++; } From 4485680d29280ef58f34b70bbb031cfdd8528569 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Sat, 2 Jul 2022 17:50:05 +0200 Subject: [PATCH 19/30] Add Cell finder on GPU, to be validated --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 21 ++- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 4 + .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 151 +++++++----------- .../include/ITStracking/Configuration.h | 19 ++- .../include/ITStracking/TrackerTraits.h | 4 +- .../tracking/include/ITStracking/Tracklet.h | 6 + .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 22 +-- 7 files changed, 106 insertions(+), 121 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 976c74930c8a5..fa696c73aa86d 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -57,8 +57,9 @@ struct StaticTrackingParameters { /// Trackleting cuts // float TrackletMaxDeltaPhi = 0.3f; // float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; + /// Cell finding cuts - // float CellMaxDeltaTanLambda = 0.025f; + float CellDeltaTanLambdaSigma = 0.007f; // float CellMaxDCA[NLayers - 2] = {0.05f, 0.04f, 0.05f, 0.2f, 0.4f}; // float CellMaxDeltaPhi = 0.14f; // float CellMaxDeltaZ[NLayers - 2] = {0.2f, 0.4f, 0.5f, 0.6f, 3.0f}; @@ -77,7 +78,7 @@ void StaticTrackingParameters::set(const TrackingParameters& pars) DeltaROF = pars.DeltaROF; ZBins = pars.ZBins; PhiBins = pars.PhiBins; - // TrackletMaxDeltaPhi = pars.TrackletMaxDeltaPhi; + CellDeltaTanLambdaSigma = pars.CellDeltaTanLambdaSigma; // for (int i = 0; i < NLayers - 1; i++) { // TrackletMaxDeltaZ[i] = pars.TrackletMaxDeltaZ[i]; // } @@ -127,7 +128,9 @@ class TimeFrameGPU : public TimeFrame Line* getDeviceLines(const int rofId); Tracklet* getDeviceTracklets(const int rofId, const int layerId); Tracklet* getDeviceTrackletsAll(const int layerId); + Cell* getDeviceCells(const int layerId); int* getDeviceTrackletsLookupTable(const int rofId, const int layerId); + int* getDeviceCellsLookupTable(const int layerId); int* getDeviceNFoundLines(const int rofId); int* getDeviceExclusiveNFoundLines(const int rofId); int* getDeviceCUBBuffer(const size_t rofId); @@ -161,6 +164,8 @@ class TimeFrameGPU : public TimeFrame std::array, NLayers> mClusterExternalIndicesD; std::array, NLayers - 1> mTrackletsD; std::array, NLayers - 1> mTrackletsLookupTablesD; + std::array, NLayers - 2> mCellsD; + std::array, NLayers - 2> mCellsLookupTablesD; std::array, NLayers> mROframesClustersD; // layers x roframes int* mCUBTmpBuffers; int* mFoundTracklets; @@ -235,12 +240,24 @@ inline Tracklet* TimeFrameGPU::getDeviceTrackletsAll(const int layerId) return mTrackletsD[layerId].get(); } +template +inline Cell* TimeFrameGPU::getDeviceCells(const int layerId) +{ + return mCellsD[layerId].get(); +} + template inline int* TimeFrameGPU::getDeviceTrackletsLookupTable(const int rofId, const int layerId) { return getPtrFromRuler(rofId, mTrackletsLookupTablesD[layerId].get(), mROframesClusters[layerId].data()); } +template +inline int* TimeFrameGPU::getDeviceCellsLookupTable(const int layerId) +{ + return mCellsLookupTablesD[layerId].get(); +} + template inline int* TimeFrameGPU::getDeviceNFoundLines(const int rofId) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 4d59c2963ada7..5088d6ed5b955 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -111,6 +111,10 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) auto thrustTrackletsEnd = thrustTrackletsBegin + mConfig.trackletsCapacity; thrust::fill(thrustTrackletsBegin, thrustTrackletsEnd, Tracklet{}); mTrackletsLookupTablesD[iLayer].resetWithInt(mClusters[iLayer].size()); + if (iLayer < NLayers - 2) { + mCellsD[iLayer] = Vector{mConfig.validatedTrackletsCapacity, mConfig.validatedTrackletsCapacity}; + mCellsLookupTablesD[iLayer] = Vector{mConfig.cellsLUTsize, mConfig.cellsLUTsize}; + } } for (auto iComb{0}; iComb < 2; ++iComb) { // Vertexer only diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 1142a431f7018..36a606f2c2334 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -202,38 +202,56 @@ GPUg() void removeDuplicateTrackletsEntriesLUTKernel( } // Compute cells kernel -template +template GPUg() void computeLayerCellsKernel( const Tracklet* trackletsCurrentLayer, const Tracklet* trackletsNextLayer, const int* trackletsCurrentLayerLUT, - const int nTracklets) + const int nTrackletsCurrent, + Cell* cells, + int* cellsLUT, + const StaticTrackingParameters* trkPars) { - for (int currentTrackletIndex = blockIdx.x * blockDim.x + threadIdx.x; currentTrackletIndex < nTracklets; currentTrackletIndex += blockDim.x * gridDim.x) { - const Tracklet& currentTracklet = trackletsCurrentLayer[currentTrackletIndex]; + for (int iCurrentTrackletIndex = blockIdx.x * blockDim.x + threadIdx.x; iCurrentTrackletIndex < nTrackletsCurrent; iCurrentTrackletIndex += blockDim.x * gridDim.x) { + const Tracklet& currentTracklet = trackletsCurrentLayer[iCurrentTrackletIndex]; const int nextLayerClusterIndex{currentTracklet.secondClusterIndex}; const int nextLayerFirstTrackletIndex{trackletsCurrentLayerLUT[nextLayerClusterIndex]}; const int nextLayerLastTrackletIndex{trackletsCurrentLayerLUT[nextLayerClusterIndex + 1]}; - if (nextLayerFirstTrackletIndex == nextLayerLastTrackletIndex) { continue; } - for (int iNextTracklet{nextLayerFirstTrackletIndex}; iNextTracklet < nextLayerLastTrackletIndex; ++iNextTracklet) { - if (trackletsNextLayer[iNextTracklet].firstClusterIndex != nextLayerClusterIndex) { + int foundCells{0}; + for (int iNextTrackletIndex{nextLayerFirstTrackletIndex}; iNextTrackletIndex < nextLayerLastTrackletIndex; ++iNextTrackletIndex) { + if (trackletsNextLayer[iNextTrackletIndex].firstClusterIndex != nextLayerClusterIndex) { break; } - const Tracklet& nextTracklet = trackletsNextLayer[iNextTracklet]; + const Tracklet& nextTracklet = trackletsNextLayer[iNextTrackletIndex]; const float deltaTanLambda{o2::gpu::GPUCommonMath::Abs(currentTracklet.tanLambda - nextTracklet.tanLambda)}; const float tanLambda{(currentTracklet.tanLambda + nextTracklet.tanLambda) * 0.5f}; + + if (deltaTanLambda / trkPars->CellDeltaTanLambdaSigma < trkPars->NSigmaCut) { + if constexpr (!initRun) { + new (cells + cellsLUT[iCurrentTrackletIndex] + foundCells) Cell{currentTracklet.firstClusterIndex, nextTracklet.firstClusterIndex, + nextTracklet.secondClusterIndex, + iCurrentTrackletIndex, + iNextTrackletIndex, + tanLambda}; + } + ++foundCells; + } + } + if constexpr (initRun) { + // Fill cell Lookup table + cellsLUT[iCurrentTrackletIndex] = foundCells; } } } } // namespace gpu template -void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const MemoryParameters& memParams, const TrackingParameters& trackingParams) +void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const TrackingParameters& trackingParams) { - mTimeFrameGPU->initialise(iteration, memParams, trackingParams, NLayers); + mTimeFrameGPU->initialise(iteration, trackingParams, NLayers); setIsGPU(true); } @@ -242,6 +260,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); gsl::span diamondSpan(&diamondVert, 1); + size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; for (int rof0{0}; rof0 < mTimeFrameGPU->getNrof(); ++rof0) { gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one @@ -291,8 +310,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) mTimeFrameGPU->getConfig().maxTrackletsPerCluster); } } - // std::vector> trackletsHost(NLayers - 1, std::vector(mTimeFrameGPU->getConfig().trackletsCapacity)); - size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; + for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Sort tracklets to put empty ones on the right side of the array. auto thrustTrackletsBegin = thrust::device_ptr(mTimeFrameGPU->getDeviceTrackletsAll(iLayer)); @@ -340,7 +358,6 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) } discardResult(cudaDeviceSynchronize()); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { - // Compute LUT discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage bufferSize, // temp_storage_bytes @@ -380,95 +397,39 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) template void TrackerTraitsGPU::computeLayerCells() { - for (int iLayer{0}; iLayer < NLayers - 2 - 1; ++iLayer) { + size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; + for (int iLayer{0}; iLayer < NLayers - 2; ++iLayer) { if (!mTimeFrameGPU->getTrackletSizeHost()[iLayer + 1] || !mTimeFrameGPU->getTrackletSizeHost()[iLayer]) { continue; } - float resolution{std::sqrt(gpu::Sq(mTrkParams.LayerMisalignment[iLayer]) + gpu::Sq(mTrkParams.LayerMisalignment[iLayer + 1]) + gpu::Sq(mTrkParams.LayerMisalignment[iLayer + 2])) / mTrkParams.LayerResolution[iLayer]}; - resolution = resolution > 1.e-12 ? resolution : 1.f; + const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getTrackletSizeHost()[iLayer])}; + const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getTrackletSizeHost()[iLayer])}; + gpu::computeLayerCellsKernel<<getStream(iLayer).get()>>>( + mTimeFrameGPU->getDeviceTrackletsAll(iLayer), + mTimeFrameGPU->getDeviceTrackletsAll(iLayer + 1), + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer + 1), + mTimeFrameGPU->getTrackletSizeHost()[iLayer], + nullptr, + mTimeFrameGPU->getDeviceCellsLookupTable(iLayer), + mTimeFrameGPU->getDeviceTrackingParameters()); - gpu::computeLayerCellsKernel<<<1, 1, 0, mTimeFrameGPU->getStream(iLayer).get()>>>( + // Compute LUT + discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + bufferSize, // temp_storage_bytes + mTimeFrameGPU->getDeviceCellsLookupTable(iLayer), // d_in + mTimeFrameGPU->getDeviceCellsLookupTable(iLayer), // d_out + mTimeFrameGPU->getTrackletSizeHost()[iLayer], // num_items + mTimeFrameGPU->getStream(iLayer).get())); + + gpu::computeLayerCellsKernel<<getStream(iLayer).get()>>>( mTimeFrameGPU->getDeviceTrackletsAll(iLayer), mTimeFrameGPU->getDeviceTrackletsAll(iLayer + 1), - mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), - mTimeFrameGPU->getTrackletSizeHost()[iLayer]); - // tempSize[iLayer] = 0; - // trackletsNum[iLayer] = primaryVertexContext->getDeviceTracklets()[iLayer + 1].getSizeFromDevice(); - // primaryVertexContext->getTempCellArray()[iLayer].reset( - // static_cast(primaryVertexContext->getDeviceCells()[iLayer + 1].capacity())); - // if (trackletsNum[iLayer] == 0) { - // continue; - // } - // cub::DeviceScan::ExclusiveSum(static_cast(NULL), tempSize[iLayer], - // primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), - // primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer]); - // primaryVertexContext->getTempTableArray()[iLayer].reset(static_cast(tempSize[iLayer])); - // } - // cudaDeviceSynchronize(); - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - // const gpu::DeviceProperties& deviceProperties = gpu::Context::getInstance().getDeviceProperties(); - // const int trackletsSize = primaryVertexContext->getDeviceTracklets()[iLayer].getSizeFromDevice(); - // if (trackletsSize == 0) { - // continue; - // } - // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsSize)}; - // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsSize)}; - // if (iLayer == 0) { - // gpu::layerCellsKernel<<getStream(iLayer).get()>>>(primaryVertexContext->getDeviceContext(), - // iLayer, primaryVertexContext->getDeviceCells()[iLayer].getWeakCopy()); - // } else { - // gpu::layerCellsKernel<<getStream(iLayer).get()>>>(primaryVertexContext->getDeviceContext(), - // iLayer, primaryVertexContext->getTempCellArray()[iLayer - 1].getWeakCopy()); - // } - // cudaError_t error = cudaGetLastError(); - // if (error != cudaSuccess) { - // std::ostringstream errorString{}; - // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" - // << std::endl; - // throw std::runtime_error{errorString.str()}; - // } - // } - // cudaDeviceSynchronize(); - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad - 1; ++iLayer) { - // cellsNum[iLayer] = primaryVertexContext->getTempCellArray()[iLayer].getSizeFromDevice(); - // if (cellsNum[iLayer] == 0) { - // continue; - // } - // primaryVertexContext->getDeviceCells()[iLayer + 1].resize(cellsNum[iLayer]); - // cub::DeviceScan::ExclusiveSum(static_cast(primaryVertexContext->getTempTableArray()[iLayer].get()), tempSize[iLayer], - // primaryVertexContext->getDeviceCellsPerTrackletTable()[iLayer].get(), - // primaryVertexContext->getDeviceCellsLookupTable()[iLayer].get(), trackletsNum[iLayer], - // streamArray[iLayer + 1].get()); - // dim3 threadsPerBlock{gpu::utils::host::getBlockSize(trackletsNum[iLayer])}; - // dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, trackletsNum[iLayer])}; - // gpu::sortCellsKernel<<>>(primaryVertexContext->getDeviceContext(), - // iLayer + 1, primaryVertexContext->getTempCellArray()[iLayer].getWeakCopy()); - // cudaError_t error = cudaGetLastError(); - // if (error != cudaSuccess) { - // std::ostringstream errorString{}; - // errorString << "CUDA API returned error [" << cudaGetErrorString(error) << "] (code " << error << ")" - // << std::endl; - // throw std::runtime_error{errorString.str()}; - // } - // } - // cudaDeviceSynchronize(); - // for (int iLayer{0}; iLayer < constants::its2::CellsPerRoad; ++iLayer) { - // int cellsSize = 0; - // if (iLayer == 0) { - // cellsSize = primaryVertexContext->getDeviceCells()[iLayer].getSizeFromDevice(); - // if (cellsSize == 0) { - // continue; - // } - // } else { - // cellsSize = cellsNum[iLayer - 1]; - // if (cellsSize == 0) { - // continue; - // } - // primaryVertexContext->getDeviceCellsLookupTable()[iLayer - 1].copyIntoVector( - // primaryVertexContext->getCellsLookupTable()[iLayer - 1], trackletsNum[iLayer - 1]); - // } - // primaryVertexContext->getDeviceCells()[iLayer].copyIntoVector(primaryVertexContext->getCells()[iLayer], cellsSize); + mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer + 1), + mTimeFrameGPU->getTrackletSizeHost()[iLayer], + mTimeFrameGPU->getDeviceCells(iLayer), + mTimeFrameGPU->getDeviceCellsLookupTable(iLayer), + mTimeFrameGPU->getDeviceTrackingParameters()); } } diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h index 96cdd862ec31f..8a6caf1b9da31 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h @@ -147,13 +147,16 @@ struct TimeFrameGPUConfig { size_t cluLayCap, size_t cluROfCap, size_t maxTrkCap, - size_t maxVertCap); + size_t maxVertCap, + size_t maxROFs); size_t tmpCUBBufferSize = 1e5; // In average in pp events there are required 4096 bytes size_t maxTrackletsPerCluster = 50; size_t clustersPerLayerCapacity = 5e5; size_t clustersPerROfCapacity = 1e4; size_t trackletsCapacity = maxTrackletsPerCluster * clustersPerLayerCapacity; + size_t validatedTrackletsCapacity = 1e5; + size_t cellsLUTsize = validatedTrackletsCapacity; size_t maxLinesCapacity = 1e2; size_t maxCentroidsXYCapacity = std::ceil(maxLinesCapacity * (maxLinesCapacity - 1) / (float)2); size_t maxVerticesCapacity = 10; @@ -167,12 +170,14 @@ inline TimeFrameGPUConfig::TimeFrameGPUConfig(size_t cubBufferSize, size_t cluLayCap, size_t cluROfCap, size_t maxTrkCap, - size_t maxVertCap) : tmpCUBBufferSize{cubBufferSize}, - maxTrackletsPerCluster{maxTrkClu}, - clustersPerLayerCapacity{cluLayCap}, - clustersPerROfCapacity{cluROfCap}, - maxLinesCapacity{maxTrkCap}, - maxVerticesCapacity{maxVertCap} + size_t maxVertCap, + size_t maxROFs) : tmpCUBBufferSize{cubBufferSize}, + maxTrackletsPerCluster{maxTrkClu}, + clustersPerLayerCapacity{cluLayCap}, + clustersPerROfCapacity{cluROfCap}, + maxLinesCapacity{maxTrkCap}, + maxVerticesCapacity{maxVertCap}, + nMaxROFs{maxROFs} { maxCentroidsXYCapacity = std::ceil(maxLinesCapacity * (maxLinesCapacity - 1) / 2); trackletsCapacity = maxTrackletsPerCluster * clustersPerLayerCapacity; diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h index 990ee224e78e9..336786677bd29 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h @@ -130,9 +130,9 @@ inline const int4 TrackerTraits::getBinsRect(const Cluster& currentCluster, int return getBinsRect(layerIndex, currentCluster.phi, maxdeltaphi, z1, z2, maxdeltaz); } -inline void TrackerTraits::initialiseTimeFrame(const int iteration, const MemoryParameters& memParams, const TrackingParameters& trackingParams) +inline void TrackerTraits::initialiseTimeFrame(const int iteration, const TrackingParameters& trackingParams) { - mTimeFrame->initialise(iteration, memParams, trackingParams, 7); + mTimeFrame->initialise(iteration, trackingParams, 7); setIsGPU(false); } diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h index 530b772abb97a..33656ef71a4bd 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h @@ -37,6 +37,7 @@ struct Tracklet final { return firstClusterIndex < 0 || secondClusterIndex < 0 && !tanLambda && !phi; } GPUhdi() void dump(); + GPUhdi() void dump() const; GPUhdi() unsigned char operator<(const Tracklet&) const; int firstClusterIndex; @@ -103,6 +104,11 @@ GPUhdi() void Tracklet::dump() { printf("fClIdx: %d sClIdx: %d rof1: %hu rof2: %hu\n", firstClusterIndex, secondClusterIndex, rof[0], rof[1]); } + +GPUhdi() void Tracklet::dump() const +{ + printf("fClIdx: %d sClIdx: %d rof1: %hu rof2: %hu\n", firstClusterIndex, secondClusterIndex, rof[0], rof[1]); +} // tanL: %f phi: %f // tanLambda, phi, diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 52e401d1e398f..61d0f9680fe23 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -107,7 +107,6 @@ void TrackerTraits::computeLayerTracklets(const int iteration) } for (int rof1{minRof}; rof1 <= maxRof; ++rof1) { gsl::span layer1 = tf->getClustersOnLayer(rof1, iLayer + 1); - // printf("%d %d %d \n", minRof, maxRof, layer1.size()); if (layer1.empty()) { continue; } @@ -167,15 +166,12 @@ void TrackerTraits::computeLayerTracklets(const int iteration) if (iLayer > 0) { tf->getTrackletsLookupTable()[iLayer - 1][currentSortedIndex]++; } - // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f %f %f \n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate, deltaPhi, deltaZ); - const float phi{o2::gpu::GPUCommonMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, currentCluster.xCoordinate - nextCluster.xCoordinate)}; const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / (currentCluster.radius - nextCluster.radius)}; - // printf("%d %d %d %d %d %d %d %d %d %d %f %f %f\n", maxBinIndex, firstBinIndex, iPhiBin, iPhiCount, phiBinsNum, rof1, rof0, firstRowClusterIndex, maxRowClusterIndex, iNextCluster, nextCluster.xCoordinate, nextCluster.yCoordinate, nextCluster.zCoordinate); - tf->getTracklets()[iLayer].emplace_back(currentSortedIndex, tf->getSortedIndex(rof1, iLayer + 1, iNextCluster), tanL, phi, rof0, rof1); - // printf("%d %d %lf %lf %hu %hu\n", tf->getTracklets()[iLayer].back().firstClusterIndex, tf->getTracklets()[iLayer].back().secondClusterIndex, tf->getTracklets()[iLayer].back().tanLambda, tf->getTracklets()[iLayer].back().phi, tf->getTracklets()[iLayer].back().rof[0], tf->getTracklets()[iLayer].back().rof[0]); + tf->getTracklets()[iLayer].emplace_back(currentSortedIndex, + tf->getSortedIndex(rof1, iLayer + 1, iNextCluster), tanL, phi, rof0, rof1); } } } @@ -203,7 +199,6 @@ void TrackerTraits::computeLayerTracklets(const int iteration) int count{0}; for (auto& trk : trkl) { if (trk.firstClusterIndex == id0 && trk.secondClusterIndex == id1) { - printf("layer %d: tracklet: %d/%d, decreasing index %d\n", iLayer + 1, count, trkl.size(), id0); lut[id0]--; } else { id0 = trk.firstClusterIndex; @@ -231,8 +226,6 @@ void TrackerTraits::computeLayerTracklets(const int iteration) id0 = trk.firstClusterIndex; id1 = trk.secondClusterIndex; newTrk.push_back(trk); - } else { - // printf("layer 0: tracklet: %d/%lu, decreasing index %d\n", count, tf->getTracklets()[0].size(), id0); } count++; } @@ -277,20 +270,19 @@ void TrackerTraits::computeLayerCells(const int iteration) tf->getTracklets()[iLayer].empty()) { continue; } - +#ifdef OPTIMISATION_OUTPUT float resolution{std::sqrt(Sq(mTrkParams[iteration].LayerMisalignment[iLayer]) + Sq(mTrkParams[iteration].LayerMisalignment[iLayer + 1]) + Sq(mTrkParams[iteration].LayerMisalignment[iLayer + 2])) / mTrkParams[iteration].LayerResolution[iLayer]}; resolution = resolution > 1.e-12 ? resolution : 1.f; - +#endif const int currentLayerTrackletsNum{static_cast(tf->getTracklets()[iLayer].size())}; for (int iTracklet{0}; iTracklet < currentLayerTrackletsNum; ++iTracklet) { const Tracklet& currentTracklet{tf->getTracklets()[iLayer][iTracklet]}; const int nextLayerClusterIndex{currentTracklet.secondClusterIndex}; - const int nextLayerFirstTrackletIndex{ - tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex]}; - const int nextLayerLastTrackletIndex{ - tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex + 1]}; + const int nextLayerFirstTrackletIndex{tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex]}; + const int nextLayerLastTrackletIndex{tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex + 1]}; + printf("%d, %d, %d\n", nextLayerClusterIndex, nextLayerFirstTrackletIndex, nextLayerLastTrackletIndex); if (nextLayerFirstTrackletIndex == nextLayerLastTrackletIndex) { continue; From 63a5c3a81b73f298c6740ef032782748a661b352 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Sun, 3 Jul 2022 11:45:27 +0200 Subject: [PATCH 20/30] Add Cell finder MClabel association, works on NVIDIA --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 9 +++-- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 10 ++++-- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 23 +++++++++++++ .../ITS/tracking/include/ITStracking/Cell.h | 33 +++++++++++++------ .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 2 +- 5 files changed, 60 insertions(+), 17 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index fa696c73aa86d..0e7d80dc98c6e 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -119,6 +119,7 @@ class TimeFrameGPU : public TimeFrame TimeFrameGPUConfig& getConfig() { return mConfig; } gpu::Stream& getStream(const int iLayer) { return mStreamArray[iLayer]; } std::vector& getTrackletSizeHost() { return mTrackletSizeHost; } + std::vector& getCellSizeHost() { return mCellSizeHost; } // Vertexer only int* getDeviceNTrackletsCluster(int rofId, int combId); @@ -134,7 +135,8 @@ class TimeFrameGPU : public TimeFrame int* getDeviceNFoundLines(const int rofId); int* getDeviceExclusiveNFoundLines(const int rofId); int* getDeviceCUBBuffer(const size_t rofId); - int* getDeviceNFoundTracklets() const { return mFoundTracklets; }; + int* getDeviceNFoundTracklets() const { return mDeviceFoundTracklets; }; + int* getDeviceNFoundCells() const { return mDeviceFoundCells; }; float* getDeviceXYCentroids(const int rofId); float* getDeviceZCentroids(const int rofId); int* getDeviceXHistograms(const int rofId); @@ -155,7 +157,7 @@ class TimeFrameGPU : public TimeFrame TimeFrameGPUConfig mConfig; std::array mStreamArray; std::vector mTrackletSizeHost; - + std::vector mCellSizeHost; // Per-layer information, do not expand at runtime std::array, NLayers> mClustersD; std::array, NLayers> mUsedClustersD; @@ -168,7 +170,8 @@ class TimeFrameGPU : public TimeFrame std::array, NLayers - 2> mCellsLookupTablesD; std::array, NLayers> mROframesClustersD; // layers x roframes int* mCUBTmpBuffers; - int* mFoundTracklets; + int* mDeviceFoundTracklets; + int* mDeviceFoundCells; gpu::StaticTrackingParameters* mDeviceTrackingParams; IndexTableUtils* mDeviceIndexTableUtils; diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 5088d6ed5b955..be57440b83664 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -105,6 +105,7 @@ template void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) { mTrackletSizeHost.resize(NLayers - 1, 0); + mCellSizeHost.resize(NLayers - 2, 0); for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { // Tracker and vertexer mTrackletsD[iLayer] = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; auto thrustTrackletsBegin = thrust::device_ptr(mTrackletsD[iLayer].get()); @@ -127,8 +128,10 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) mNExclusiveFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; mUsedTracklets = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; discardResult(cudaMalloc(&mCUBTmpBuffers, mConfig.nMaxROFs * mConfig.tmpCUBBufferSize)); - discardResult(cudaMalloc(&mFoundTracklets, (NLayers - 1) * sizeof(int))); - discardResult(cudaMemset(mFoundTracklets, 0, (NLayers - 1) * sizeof(int))); + discardResult(cudaMalloc(&mDeviceFoundTracklets, (NLayers - 1) * sizeof(int))); + discardResult(cudaMemset(mDeviceFoundTracklets, 0, (NLayers - 1) * sizeof(int))); + discardResult(cudaMalloc(&mDeviceFoundCells, (NLayers - 2) * sizeof(int))); + discardResult(cudaMemset(mDeviceFoundCells, 0, (NLayers - 2) * sizeof(int))); mXYCentroids = Vector{2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity, 2 * mConfig.nMaxROFs * mConfig.maxCentroidsXYCapacity}; mZCentroids = Vector{mConfig.nMaxROFs * mConfig.maxLinesCapacity, mConfig.nMaxROFs * mConfig.maxLinesCapacity}; for (size_t i{0}; i < 3; ++i) { @@ -185,9 +188,10 @@ template TimeFrameGPU::~TimeFrameGPU() { discardResult(cudaFree(mCUBTmpBuffers)); - discardResult(cudaFree(mFoundTracklets)); + discardResult(cudaFree(mDeviceFoundTracklets)); discardResult(cudaFree(mDeviceTrackingParams)); discardResult(cudaFree(mDeviceIndexTableUtils)); + discardResult(cudaFree(mDeviceFoundCells)); } template diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 36a606f2c2334..f9d8608e96b52 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -414,6 +414,13 @@ void TrackerTraitsGPU::computeLayerCells() mTimeFrameGPU->getDeviceCellsLookupTable(iLayer), mTimeFrameGPU->getDeviceTrackingParameters()); + // Compute number of found Cells + discardResult(cub::DeviceReduce::Sum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage + bufferSize, // temp_storage_bytes + mTimeFrameGPU->getDeviceCellsLookupTable(iLayer), // d_in + mTimeFrameGPU->getDeviceNFoundCells() + iLayer, // d_out + mTimeFrameGPU->getTrackletSizeHost()[iLayer], // num_items + mTimeFrameGPU->getStream(iLayer).get())); // Compute LUT discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage bufferSize, // temp_storage_bytes @@ -431,6 +438,22 @@ void TrackerTraitsGPU::computeLayerCells() mTimeFrameGPU->getDeviceCellsLookupTable(iLayer), mTimeFrameGPU->getDeviceTrackingParameters()); } + + checkGPUError(cudaMemcpy(mTimeFrameGPU->getCellSizeHost().data(), mTimeFrameGPU->getDeviceNFoundCells(), (NLayers - 2) * sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + /// Create cells labels + if (mTimeFrameGPU->hasMCinformation()) { + for (int iLayer{0}; iLayer < NLayers - 2; ++iLayer) { + std::cout << "layer " << iLayer << "found cells: " << mTimeFrameGPU->getCellSizeHost()[iLayer] << std::endl; + std::vector cells(mTimeFrameGPU->getCellSizeHost()[iLayer]); + checkGPUError(cudaMemcpy(cells.data(), mTimeFrameGPU->getDeviceCells(iLayer), mTimeFrameGPU->getCellSizeHost()[iLayer] * sizeof(o2::its::Cell), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + for (auto& cell : cells) { + MCCompLabel currentLab{mTimeFrameGPU->getTrackletsLabel(iLayer)[cell.getFirstTrackletIndex()]}; + MCCompLabel nextLab{mTimeFrameGPU->getTrackletsLabel(iLayer + 1)[cell.getSecondTrackletIndex()]}; + mTimeFrameGPU->getCellsLabel(iLayer).emplace_back(currentLab == nextLab ? currentLab : MCCompLabel()); + std::cout << mTimeFrameGPU->getCellsLabel(iLayer).back() << std::endl; + } + } + } } // void TrackerTraitsGPU::refitTracks(const std::vector>& tf, std::vector& tracks) diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Cell.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Cell.h index bcb449e261ae0..adf5bf095143c 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Cell.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Cell.h @@ -31,16 +31,17 @@ namespace its class Cell final { public: + GPUhd() Cell(); GPUd() Cell(const int, const int, const int, const int, const int, const float); - GPUhdni() int getFirstClusterIndex() const { return mFirstClusterIndex; }; - GPUhdni() int getSecondClusterIndex() const { return mSecondClusterIndex; }; - GPUhdni() int getThirdClusterIndex() const { return mThirdClusterIndex; }; - GPUhdni() int getFirstTrackletIndex() const { return mFirstTrackletIndex; }; - GPUhdni() int getSecondTrackletIndex() const { return mSecondTrackletIndex; }; - GPUhdni() float getTanLambda() const { return mTanLambda; }; - int getLevel() const { return mLevel; }; - void setLevel(const int level) { mLevel = level; }; + GPUhd() int getFirstClusterIndex() const { return mFirstClusterIndex; }; + GPUhd() int getSecondClusterIndex() const { return mSecondClusterIndex; }; + GPUhd() int getThirdClusterIndex() const { return mThirdClusterIndex; }; + GPUhd() int getFirstTrackletIndex() const { return mFirstTrackletIndex; }; + GPUhd() int getSecondTrackletIndex() const { return mSecondTrackletIndex; }; + GPUhd() float getTanLambda() const { return mTanLambda; }; + GPUhd() int getLevel() const { return mLevel; }; + GPUhd() void setLevel(const int level) { mLevel = level; }; private: const int mFirstClusterIndex; @@ -52,13 +53,25 @@ class Cell final int mLevel; }; +GPUhdi() Cell::Cell() + : mFirstClusterIndex{0}, + mSecondClusterIndex{0}, + mThirdClusterIndex{0}, + mFirstTrackletIndex{0}, + mSecondTrackletIndex{0}, + mTanLambda{0}, + mLevel{0} +{ + // Nothing to do +} + GPUdi() Cell::Cell(const int firstClusterIndex, const int secondClusterIndex, const int thirdClusterIndex, const int firstTrackletIndex, const int secondTrackletIndex, const float tanL) : mFirstClusterIndex{firstClusterIndex}, mSecondClusterIndex{secondClusterIndex}, mThirdClusterIndex{thirdClusterIndex}, - mFirstTrackletIndex(firstTrackletIndex), - mSecondTrackletIndex(secondTrackletIndex), + mFirstTrackletIndex{firstTrackletIndex}, + mSecondTrackletIndex{secondTrackletIndex}, mTanLambda{tanL}, mLevel{1} { diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 61d0f9680fe23..c41c950cfc0d3 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -282,7 +282,6 @@ void TrackerTraits::computeLayerCells(const int iteration) const int nextLayerClusterIndex{currentTracklet.secondClusterIndex}; const int nextLayerFirstTrackletIndex{tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex]}; const int nextLayerLastTrackletIndex{tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex + 1]}; - printf("%d, %d, %d\n", nextLayerClusterIndex, nextLayerFirstTrackletIndex, nextLayerLastTrackletIndex); if (nextLayerFirstTrackletIndex == nextLayerLastTrackletIndex) { continue; @@ -329,6 +328,7 @@ void TrackerTraits::computeLayerCells(const int iteration) MCCompLabel currentLab{tf->getTrackletsLabel(iLayer)[cell.getFirstTrackletIndex()]}; MCCompLabel nextLab{tf->getTrackletsLabel(iLayer + 1)[cell.getSecondTrackletIndex()]}; tf->getCellsLabel(iLayer).emplace_back(currentLab == nextLab ? currentLab : MCCompLabel()); + std::cout << tf->getCellsLabel(iLayer).back() << std::endl; } } } From aa63a79c8674da333877ebd164fb5eb7f476761b Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Sun, 3 Jul 2022 16:30:46 +0200 Subject: [PATCH 21/30] Make Cell finder work with HIP Remove plugin for CPU to GPU --- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 14 +----------- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 1 + .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 4 ++-- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 22 +++++++++---------- .../include/ITSWorkflow/TrackerSpec.h | 1 - .../ITSMFT/ITS/workflow/src/TrackerSpec.cxx | 3 +-- 6 files changed, 15 insertions(+), 30 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 0e7d80dc98c6e..bc105a61e05a5 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -45,7 +45,7 @@ template struct StaticTrackingParameters { StaticTrackingParameters& operator=(const StaticTrackingParameters& t) = default; void set(const TrackingParameters& pars); - // int CellMinimumLevel(); + /// General parameters int ClusterSharing = 0; int MinTrackLength = NLayers; @@ -54,18 +54,9 @@ struct StaticTrackingParameters { int DeltaROF = 0; int ZBins{256}; int PhiBins{128}; - /// Trackleting cuts - // float TrackletMaxDeltaPhi = 0.3f; - // float TrackletMaxDeltaZ[NLayers - 1] = {0.1f, 0.1f, 0.3f, 0.3f, 0.3f, 0.3f}; /// Cell finding cuts float CellDeltaTanLambdaSigma = 0.007f; - // float CellMaxDCA[NLayers - 2] = {0.05f, 0.04f, 0.05f, 0.2f, 0.4f}; - // float CellMaxDeltaPhi = 0.14f; - // float CellMaxDeltaZ[NLayers - 2] = {0.2f, 0.4f, 0.5f, 0.6f, 3.0f}; - // /// Neighbour finding cuts - // float NeighbourMaxDeltaCurvature[NLayers - 3] = {0.008f, 0.0025f, 0.003f, 0.0035f}; - // float NeighbourMaxDeltaN[NLayers - 3] = {0.002f, 0.0090f, 0.002f, 0.005f}; }; template @@ -79,9 +70,6 @@ void StaticTrackingParameters::set(const TrackingParameters& pars) ZBins = pars.ZBins; PhiBins = pars.PhiBins; CellDeltaTanLambdaSigma = pars.CellDeltaTanLambdaSigma; - // for (int i = 0; i < NLayers - 1; i++) { - // TrackletMaxDeltaZ[i] = pars.TrackletMaxDeltaZ[i]; - // } } template diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index be57440b83664..f50c9b991ef4e 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -115,6 +115,7 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) if (iLayer < NLayers - 2) { mCellsD[iLayer] = Vector{mConfig.validatedTrackletsCapacity, mConfig.validatedTrackletsCapacity}; mCellsLookupTablesD[iLayer] = Vector{mConfig.cellsLUTsize, mConfig.cellsLUTsize}; + mCellsLookupTablesD[iLayer].resetWithInt(mConfig.cellsLUTsize); } } diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index f9d8608e96b52..c094030ab25f6 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -421,6 +421,7 @@ void TrackerTraitsGPU::computeLayerCells() mTimeFrameGPU->getDeviceNFoundCells() + iLayer, // d_out mTimeFrameGPU->getTrackletSizeHost()[iLayer], // num_items mTimeFrameGPU->getStream(iLayer).get())); + // Compute LUT discardResult(cub::DeviceScan::ExclusiveSum(reinterpret_cast(mTimeFrameGPU->getDeviceCUBBuffer(iLayer)), // d_temp_storage bufferSize, // temp_storage_bytes @@ -428,6 +429,7 @@ void TrackerTraitsGPU::computeLayerCells() mTimeFrameGPU->getDeviceCellsLookupTable(iLayer), // d_out mTimeFrameGPU->getTrackletSizeHost()[iLayer], // num_items mTimeFrameGPU->getStream(iLayer).get())); + discardResult(cudaStreamSynchronize(mTimeFrameGPU->getStream(iLayer).get())); gpu::computeLayerCellsKernel<<getStream(iLayer).get()>>>( mTimeFrameGPU->getDeviceTrackletsAll(iLayer), @@ -443,14 +445,12 @@ void TrackerTraitsGPU::computeLayerCells() /// Create cells labels if (mTimeFrameGPU->hasMCinformation()) { for (int iLayer{0}; iLayer < NLayers - 2; ++iLayer) { - std::cout << "layer " << iLayer << "found cells: " << mTimeFrameGPU->getCellSizeHost()[iLayer] << std::endl; std::vector cells(mTimeFrameGPU->getCellSizeHost()[iLayer]); checkGPUError(cudaMemcpy(cells.data(), mTimeFrameGPU->getDeviceCells(iLayer), mTimeFrameGPU->getCellSizeHost()[iLayer] * sizeof(o2::its::Cell), cudaMemcpyDeviceToHost), __FILE__, __LINE__); for (auto& cell : cells) { MCCompLabel currentLab{mTimeFrameGPU->getTrackletsLabel(iLayer)[cell.getFirstTrackletIndex()]}; MCCompLabel nextLab{mTimeFrameGPU->getTrackletsLabel(iLayer + 1)[cell.getSecondTrackletIndex()]}; mTimeFrameGPU->getCellsLabel(iLayer).emplace_back(currentLab == nextLab ? currentLab : MCCompLabel()); - std::cout << mTimeFrameGPU->getCellsLabel(iLayer).back() << std::endl; } } } diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index c41c950cfc0d3..12879ff955814 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -81,7 +81,7 @@ void TrackerTraits::computeLayerTracklets(const int iteration) continue; } const float inverseR0{1.f / currentCluster.radius}; - int iPrimaryVertex{0}; + for (auto& primaryVertex : primaryVertices) { const float resolution = std::sqrt(Sq(mTrkParams[iteration].PVres) / primaryVertex.getNContributors() + Sq(tf->getPositionResolution(iLayer))); @@ -105,6 +105,7 @@ void TrackerTraits::computeLayerTracklets(const int iteration) if (phiBinsNum < 0) { phiBinsNum += mTrkParams[iteration].PhiBins; } + for (int rof1{minRof}; rof1 <= maxRof; ++rof1) { gsl::span layer1 = tf->getClustersOnLayer(rof1, iLayer + 1); if (layer1.empty()) { @@ -129,10 +130,11 @@ void TrackerTraits::computeLayerTracklets(const int iteration) const int maxRowClusterIndex = tf->getIndexTable(rof1, iLayer + 1)[maxBinIndex]; for (int iNextCluster{firstRowClusterIndex}; iNextCluster < maxRowClusterIndex; ++iNextCluster) { - // printf("%d %d\n", iNextCluster, (int)layer1.size()); + if (iNextCluster >= (int)layer1.size()) { break; } + const Cluster& nextCluster{layer1[iNextCluster]}; if (tf->isClusterUsed(iLayer + 1, nextCluster.clusterId)) { continue; @@ -170,8 +172,7 @@ void TrackerTraits::computeLayerTracklets(const int iteration) currentCluster.xCoordinate - nextCluster.xCoordinate)}; const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / (currentCluster.radius - nextCluster.radius)}; - tf->getTracklets()[iLayer].emplace_back(currentSortedIndex, - tf->getSortedIndex(rof1, iLayer + 1, iNextCluster), tanL, phi, rof0, rof1); + tf->getTracklets()[iLayer].emplace_back(currentSortedIndex, tf->getSortedIndex(rof1, iLayer + 1, iNextCluster), tanL, phi, rof0, rof1); } } } @@ -190,13 +191,11 @@ void TrackerTraits::computeLayerTracklets(const int iteration) std::sort(trkl.begin(), trkl.end(), [](const Tracklet& a, const Tracklet& b) { return a.firstClusterIndex < b.firstClusterIndex || (a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex < b.secondClusterIndex); }); - /// Remove duplicates auto& lut{tf->getTrackletsLookupTable()[iLayer]}; int id0{-1}, id1{-1}; std::vector newTrk; newTrk.reserve(trkl.size()); - int count{0}; for (auto& trk : trkl) { if (trk.firstClusterIndex == id0 && trk.secondClusterIndex == id1) { lut[id0]--; @@ -205,7 +204,6 @@ void TrackerTraits::computeLayerTracklets(const int iteration) id1 = trk.secondClusterIndex; newTrk.push_back(trk); } - count++; } trkl.swap(newTrk); @@ -220,14 +218,12 @@ void TrackerTraits::computeLayerTracklets(const int iteration) int id0{-1}, id1{-1}; std::vector newTrk; newTrk.reserve(tf->getTracklets()[0].size()); - int count{0}; for (auto& trk : tf->getTracklets()[0]) { if (trk.firstClusterIndex != id0 || trk.secondClusterIndex != id1) { id0 = trk.firstClusterIndex; id1 = trk.secondClusterIndex; newTrk.push_back(trk); } - count++; } tf->getTracklets()[0].swap(newTrk); @@ -270,6 +266,7 @@ void TrackerTraits::computeLayerCells(const int iteration) tf->getTracklets()[iLayer].empty()) { continue; } + #ifdef OPTIMISATION_OUTPUT float resolution{std::sqrt(Sq(mTrkParams[iteration].LayerMisalignment[iLayer]) + Sq(mTrkParams[iteration].LayerMisalignment[iLayer + 1]) + Sq(mTrkParams[iteration].LayerMisalignment[iLayer + 2])) / mTrkParams[iteration].LayerResolution[iLayer]}; resolution = resolution > 1.e-12 ? resolution : 1.f; @@ -280,8 +277,10 @@ void TrackerTraits::computeLayerCells(const int iteration) const Tracklet& currentTracklet{tf->getTracklets()[iLayer][iTracklet]}; const int nextLayerClusterIndex{currentTracklet.secondClusterIndex}; - const int nextLayerFirstTrackletIndex{tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex]}; - const int nextLayerLastTrackletIndex{tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex + 1]}; + const int nextLayerFirstTrackletIndex{ + tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex]}; + const int nextLayerLastTrackletIndex{ + tf->getTrackletsLookupTable()[iLayer][nextLayerClusterIndex + 1]}; if (nextLayerFirstTrackletIndex == nextLayerLastTrackletIndex) { continue; @@ -328,7 +327,6 @@ void TrackerTraits::computeLayerCells(const int iteration) MCCompLabel currentLab{tf->getTrackletsLabel(iLayer)[cell.getFirstTrackletIndex()]}; MCCompLabel nextLab{tf->getTrackletsLabel(iLayer + 1)[cell.getSecondTrackletIndex()]}; tf->getCellsLabel(iLayer).emplace_back(currentLab == nextLab ? currentLab : MCCompLabel()); - std::cout << tf->getCellsLabel(iLayer).back() << std::endl; } } } diff --git a/Detectors/ITSMFT/ITS/workflow/include/ITSWorkflow/TrackerSpec.h b/Detectors/ITSMFT/ITS/workflow/include/ITSWorkflow/TrackerSpec.h index 8e531a197044b..7c23f93067a1a 100644 --- a/Detectors/ITSMFT/ITS/workflow/include/ITSWorkflow/TrackerSpec.h +++ b/Detectors/ITSMFT/ITS/workflow/include/ITSWorkflow/TrackerSpec.h @@ -60,7 +60,6 @@ class TrackerDPL : public framework::Task std::shared_ptr mGGCCDBRequest; const o2::itsmft::TopologyDictionary* mDict = nullptr; std::unique_ptr mRecChain = nullptr; - std::unique_ptr mRecChainCPU = nullptr; std::unique_ptr mChainITS = nullptr; std::unique_ptr mTracker = nullptr; std::unique_ptr mVertexer = nullptr; diff --git a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx index b0070119d8755..7e7fb817142a2 100644 --- a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx +++ b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx @@ -61,8 +61,7 @@ void TrackerDPL::init(InitContext& ic) mTimer.Reset(); o2::base::GRPGeomHelper::instance().setRequest(mGGCCDBRequest); mChainITS.reset(mRecChain->AddChain()); - mChainITSCPU.reset(mRecChainCPU->AddChain()); - mVertexer = std::make_unique(mChainITSCPU->GetITSVertexerTraits()); + mVertexer = std::make_unique(mChainITS->GetITSVertexerTraits()); mTracker = std::make_unique(mChainITS->GetITSTrackerTraits()); mRunVertexer = true; mCosmicsProcessing = false; From 3a4c241ce19ab6665600f1f401cd357c1fb88f7e Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Thu, 7 Jul 2022 18:30:47 +0200 Subject: [PATCH 22/30] Fix IndexTable init for vertexer --- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 10 ++- .../ITSMFT/ITS/workflow/src/TrackerSpec.cxx | 66 +++++++++---------- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index f50c9b991ef4e..9110d0144ad17 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -43,7 +43,7 @@ template TimeFrameGPU::TimeFrameGPU() { mIsGPU = true; - getDeviceMemory(); + // getDeviceMemory(); To be updated } template @@ -159,13 +159,11 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) mTrackingFrameInfoD[iLayer].reset(mTrackingFrameInfo[iLayer].data(), static_cast(mTrackingFrameInfo[iLayer].size())); mClusterExternalIndicesD[iLayer].reset(mClusterExternalIndices[iLayer].data(), static_cast(mClusterExternalIndices[iLayer].size())); mROframesClustersD[iLayer].reset(mROframesClusters[iLayer].data(), static_cast(mROframesClusters[iLayer].size())); - if (iLayer < NLayers) { - mIndexTablesD[iLayer].reset(mIndexTables[iLayer].data(), static_cast(mIndexTables[iLayer].size())); - } + mIndexTablesD[iLayer].reset(mIndexTables[iLayer].data(), static_cast(mIndexTables[iLayer].size())); } } else { - mIndexTablesLayer0D.reset(getIndexTableWhole(0).data(), static_cast(getIndexTableWhole(0).size())); - mIndexTablesLayer2D.reset(getIndexTableWhole(2).data(), static_cast(getIndexTableWhole(2).size())); + mIndexTablesD[0].reset(getIndexTableWhole(0).data(), static_cast(getIndexTableWhole(0).size())); + mIndexTablesD[2].reset(getIndexTableWhole(2).data(), static_cast(getIndexTableWhole(2).size())); } gpuThrowOnError(); diff --git a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx index 7e7fb817142a2..d386376941166 100644 --- a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx +++ b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx @@ -209,7 +209,6 @@ void TrackerDPL::run(ProcessingContext& pc) std::vector processingMask; int cutVertexMult{0}, cutRandomMult = int(rofs.size()) - multEst.selectROFs(rofs, compClusters, physTriggers, processingMask); timeFrame->setMultiplicityCutMask(processingMask); - float vertexerElapsedTime{0.f}; if (mRunVertexer) { // Run seeding vertexer @@ -258,43 +257,42 @@ void TrackerDPL::run(ProcessingContext& pc) } else { timeFrame->setMultiplicityCutMask(processingMask); - mTracker->clustersToTracks(logger, errorLogger); + // mTracker->clustersToTracks(logger, errorLogger); if (timeFrame->hasBogusClusters()) { LOG(warning) << fmt::format(" - The processed timeframe had {} clusters with wild z coordinates, check the dictionaries", timeFrame->hasBogusClusters()); } - for (unsigned int iROF{0}; iROF < rofs.size(); ++iROF) { - - auto& rof{rofs[iROF]}; - tracks = timeFrame->getTracks(iROF); - trackLabels = timeFrame->getTracksLabel(iROF); - auto number{tracks.size()}; - auto first{allTracks.size()}; - int offset = -rof.getFirstEntry(); // cluster entry!!! - rof.setFirstEntry(first); - rof.setNEntries(number); - - if (processingMask[iROF]) { - irFrames.emplace_back(rof.getBCData(), rof.getBCData() + nBCPerTF - 1).info = tracks.size(); - } - - std::copy(trackLabels.begin(), trackLabels.end(), std::back_inserter(allTrackLabels)); - // Some conversions that needs to be moved in the tracker internals - for (unsigned int iTrk{0}; iTrk < tracks.size(); ++iTrk) { - auto& trc{tracks[iTrk]}; - trc.setFirstClusterEntry(allClusIdx.size()); // before adding tracks, create final cluster indices - int ncl = trc.getNumberOfClusters(), nclf = 0; - for (int ic = TrackITSExt::MaxClusters; ic--;) { // track internally keeps in->out cluster indices, but we want to store the references as out->in!!! - auto clid = trc.getClusterIndex(ic); - if (clid >= 0) { - allClusIdx.push_back(clid); - nclf++; - } - } - assert(ncl == nclf); - allTracks.emplace_back(trc); - } - } + // for (unsigned int iROF{0}; iROF < rofs.size(); ++iROF) { + // auto& rof{rofs[iROF]}; + // tracks = timeFrame->getTracks(iROF); + // trackLabels = timeFrame->getTracksLabel(iROF); + // auto number{tracks.size()}; + // auto first{allTracks.size()}; + // int offset = -rof.getFirstEntry(); // cluster entry!!! + // rof.setFirstEntry(first); + // rof.setNEntries(number); + + // if (processingMask[iROF]) { + // irFrames.emplace_back(rof.getBCData(), rof.getBCData() + nBCPerTF - 1).info = tracks.size(); + // } + + // std::copy(trackLabels.begin(), trackLabels.end(), std::back_inserter(allTrackLabels)); + // // Some conversions that needs to be moved in the tracker internals + // for (unsigned int iTrk{0}; iTrk < tracks.size(); ++iTrk) { + // auto& trc{tracks[iTrk]}; + // trc.setFirstClusterEntry(allClusIdx.size()); // before adding tracks, create final cluster indices + // int ncl = trc.getNumberOfClusters(), nclf = 0; + // for (int ic = TrackITSExt::MaxClusters; ic--;) { // track internally keeps in->out cluster indices, but we want to store the references as out->in!!! + // auto clid = trc.getClusterIndex(ic); + // if (clid >= 0) { + // allClusIdx.push_back(clid); + // nclf++; + // } + // } + // assert(ncl == nclf); + // allTracks.emplace_back(trc); + // } + // } LOGP(info, "ITSTracker pushed {} tracks and {} vertices", allTracks.size(), vertices.size()); if (mIsMC) { LOGP(info, "ITSTracker pushed {} track labels", allTrackLabels.size()); From 45e68e650ffe2352874264dc87120fcf0c82219f Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Fri, 8 Jul 2022 10:16:17 +0200 Subject: [PATCH 23/30] Restore CPU and plug GPU vertexer to tracker --- .../ITSMFT/ITS/workflow/src/TrackerSpec.cxx | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx index d386376941166..f0af3b090e25f 100644 --- a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx +++ b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx @@ -257,42 +257,42 @@ void TrackerDPL::run(ProcessingContext& pc) } else { timeFrame->setMultiplicityCutMask(processingMask); - // mTracker->clustersToTracks(logger, errorLogger); + mTracker->clustersToTracks(logger, errorLogger); if (timeFrame->hasBogusClusters()) { LOG(warning) << fmt::format(" - The processed timeframe had {} clusters with wild z coordinates, check the dictionaries", timeFrame->hasBogusClusters()); } - // for (unsigned int iROF{0}; iROF < rofs.size(); ++iROF) { - // auto& rof{rofs[iROF]}; - // tracks = timeFrame->getTracks(iROF); - // trackLabels = timeFrame->getTracksLabel(iROF); - // auto number{tracks.size()}; - // auto first{allTracks.size()}; - // int offset = -rof.getFirstEntry(); // cluster entry!!! - // rof.setFirstEntry(first); - // rof.setNEntries(number); - - // if (processingMask[iROF]) { - // irFrames.emplace_back(rof.getBCData(), rof.getBCData() + nBCPerTF - 1).info = tracks.size(); - // } - - // std::copy(trackLabels.begin(), trackLabels.end(), std::back_inserter(allTrackLabels)); - // // Some conversions that needs to be moved in the tracker internals - // for (unsigned int iTrk{0}; iTrk < tracks.size(); ++iTrk) { - // auto& trc{tracks[iTrk]}; - // trc.setFirstClusterEntry(allClusIdx.size()); // before adding tracks, create final cluster indices - // int ncl = trc.getNumberOfClusters(), nclf = 0; - // for (int ic = TrackITSExt::MaxClusters; ic--;) { // track internally keeps in->out cluster indices, but we want to store the references as out->in!!! - // auto clid = trc.getClusterIndex(ic); - // if (clid >= 0) { - // allClusIdx.push_back(clid); - // nclf++; - // } - // } - // assert(ncl == nclf); - // allTracks.emplace_back(trc); - // } - // } + for (unsigned int iROF{0}; iROF < rofs.size(); ++iROF) { + auto& rof{rofs[iROF]}; + tracks = timeFrame->getTracks(iROF); + trackLabels = timeFrame->getTracksLabel(iROF); + auto number{tracks.size()}; + auto first{allTracks.size()}; + int offset = -rof.getFirstEntry(); // cluster entry!!! + rof.setFirstEntry(first); + rof.setNEntries(number); + + if (processingMask[iROF]) { + irFrames.emplace_back(rof.getBCData(), rof.getBCData() + nBCPerTF - 1).info = tracks.size(); + } + + std::copy(trackLabels.begin(), trackLabels.end(), std::back_inserter(allTrackLabels)); + // Some conversions that needs to be moved in the tracker internals + for (unsigned int iTrk{0}; iTrk < tracks.size(); ++iTrk) { + auto& trc{tracks[iTrk]}; + trc.setFirstClusterEntry(allClusIdx.size()); // before adding tracks, create final cluster indices + int ncl = trc.getNumberOfClusters(), nclf = 0; + for (int ic = TrackITSExt::MaxClusters; ic--;) { // track internally keeps in->out cluster indices, but we want to store the references as out->in!!! + auto clid = trc.getClusterIndex(ic); + if (clid >= 0) { + allClusIdx.push_back(clid); + nclf++; + } + } + assert(ncl == nclf); + allTracks.emplace_back(trc); + } + } LOGP(info, "ITSTracker pushed {} tracks and {} vertices", allTracks.size(), vertices.size()); if (mIsMC) { LOGP(info, "ITSTracker pushed {} track labels", allTrackLabels.size()); From 529924156c0f47b3905922db026f0e0e0b300acf Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Fri, 8 Jul 2022 15:08:48 +0200 Subject: [PATCH 24/30] Use default streams for the moment --- Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h | 2 +- Detectors/ITSMFT/ITS/tracking/GPU/cuda/Stream.cu | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index bc105a61e05a5..1ecb35f089c92 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -143,7 +143,7 @@ class TimeFrameGPU : public TimeFrame private: TimeFrameGPUConfig mConfig; - std::array mStreamArray; + std::array mStreamArray; std::vector mTrackletSizeHost; std::vector mCellSizeHost; // Per-layer information, do not expand at runtime diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/Stream.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/Stream.cu index ae2a7b2a0263a..f5a2706fe5b8e 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/Stream.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/Stream.cu @@ -21,7 +21,7 @@ namespace gpu Stream::Stream() { - discardResult(cudaStreamCreateWithFlags(&mStream, cudaStreamNonBlocking)); + discardResult(cudaStreamCreateWithFlags(&mStream, cudaStreamDefault)); } // usles Stream::~Stream() From e51d4e64e1f745b5c2fb40dd1ee0f2b1a493d4b7 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Fri, 8 Jul 2022 21:51:04 +0200 Subject: [PATCH 25/30] Perform vertex fitting on CPU for consistency --- .../GPU/ITStrackingGPU/VertexerTraitsGPU.h | 14 +- .../ITS/tracking/GPU/cuda/TimeFrameGPU.cu | 4 +- .../tracking/GPU/cuda/VertexerTraitsGPU.cu | 132 +++++++++++++++++- .../tracking/include/ITStracking/TimeFrame.h | 12 ++ .../tracking/include/ITStracking/Vertexer.h | 49 +------ .../include/ITStracking/VertexerTraits.h | 51 ------- .../ITSMFT/ITS/tracking/src/TimeFrame.cxx | 14 ++ .../ITSMFT/ITS/tracking/src/Vertexer.cxx | 10 +- .../ITS/workflow/src/CookedTrackerSpec.cxx | 2 +- .../ITSMFT/ITS/workflow/src/TrackerSpec.cxx | 3 +- 10 files changed, 172 insertions(+), 119 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/VertexerTraitsGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/VertexerTraitsGPU.h index a08a2bad61a41..3ddbeb7206cfa 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/VertexerTraitsGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/VertexerTraitsGPU.h @@ -13,6 +13,7 @@ /// \brief /// \author matteo.concas@cern.ch +// #define VTX_DEBUG #ifndef ITSTRACKINGGPU_VERTEXERTRAITSGPU_H_ #define ITSTRACKINGGPU_VERTEXERTRAITSGPU_H_ @@ -45,25 +46,14 @@ class VertexerTraitsGPU : public VertexerTraits void computeTracklets() override; void computeTrackletMatching() override; void computeVertices() override; + void computeVerticesHist(); // void computeMCFiltering() override; - // GPU-specific getters - GPUd() static const int2 getBinsPhiRectWindow(const Cluster&, float maxdeltaphi); - protected: IndexTableUtils* mDeviceIndexTableUtils; gpu::TimeFrameGPU<7>* mTimeFrameGPU; }; -inline GPUd() const int2 VertexerTraitsGPU::getBinsPhiRectWindow(const Cluster& currentCluster, float phiCut) -{ - // This function returns the lowest PhiBin and the number of phi bins to be spanned, In the form int2{phiBinLow, PhiBinSpan} - const int phiBinMin{constants::its2::getPhiBinIndex( - math_utils::getNormalizedPhi(currentCluster.phi - phiCut))}; - const int phiBinSpan{static_cast(MATH_CEIL(phiCut * InversePhiBinSize))}; - return int2{phiBinMin, phiBinSpan}; -} - inline void VertexerTraitsGPU::adoptTimeFrame(TimeFrame* tf) { mTimeFrameGPU = static_cast*>(tf); } } // namespace its diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu index 9110d0144ad17..bf680252011fa 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TimeFrameGPU.cu @@ -122,11 +122,11 @@ void TimeFrameGPU::initialiseDevice(const TrackingParameters& trkParam) for (auto iComb{0}; iComb < 2; ++iComb) { // Vertexer only mNTrackletsPerClusterD[iComb] = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; } - mIndexTablesLayer0D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; - mIndexTablesLayer2D = Vector{mConfig.nMaxROFs * (ZBins * PhiBins + 1), mConfig.nMaxROFs * (ZBins * PhiBins + 1)}; mLines = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; mNFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + mNFoundLines.resetWithInt(mConfig.clustersPerLayerCapacity); mNExclusiveFoundLines = Vector{mConfig.clustersPerLayerCapacity, mConfig.clustersPerLayerCapacity}; + mNExclusiveFoundLines.resetWithInt(mConfig.clustersPerLayerCapacity); mUsedTracklets = Vector{mConfig.trackletsCapacity, mConfig.trackletsCapacity}; discardResult(cudaMalloc(&mCUBTmpBuffers, mConfig.nMaxROFs * mConfig.tmpCUBBufferSize)); discardResult(cudaMalloc(&mDeviceFoundTracklets, (NLayers - 1) * sizeof(int))); diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu index 6acd45cdb2264..b0980842f4043 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu @@ -385,7 +385,7 @@ void VertexerTraitsGPU::computeTracklets() for (int rofId{0}; rofId < mTimeFrameGPU->getNrof(); ++rofId) { const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getNClustersLayer(rofId, 1))}; const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getNClustersLayer(rofId, 1))}; - gpu::trackleterKernel<<>>( + gpu::trackleterKernel<<getStream(0).get()>>>( mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 0), mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 1), mTimeFrameGPU->getNClustersLayer(rofId, 0), @@ -397,7 +397,7 @@ void VertexerTraitsGPU::computeTracklets() mDeviceIndexTableUtils, mTimeFrameGPU->getConfig().maxTrackletsPerCluster); - gpu::trackleterKernel<<>>( + gpu::trackleterKernel<<getStream(1).get()>>>( mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 2), mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 1), mTimeFrameGPU->getNClustersLayer(rofId, 2), @@ -524,7 +524,16 @@ void VertexerTraitsGPU::computeTrackletMatching() mVrtParams.tanLambdaCut, mVrtParams.phiCut); gpuThrowOnError(); + + // Code to be removed in case of GPU vertex computing + int excLas, nLas, nLines; + checkGPUError(cudaMemcpy(&excLas, mTimeFrameGPU->getDeviceExclusiveNFoundLines(rofId) + mTimeFrameGPU->getNClustersLayer(rofId, 1) - 1, sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + checkGPUError(cudaMemcpy(&nLas, mTimeFrameGPU->getDeviceNFoundLines(rofId) + mTimeFrameGPU->getNClustersLayer(rofId, 1) - 1, sizeof(int), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + nLines = excLas + nLas; + mTimeFrameGPU->getLines(rofId).resize(nLines); + checkGPUError(cudaMemcpy(mTimeFrameGPU->getLines(rofId).data(), mTimeFrameGPU->getDeviceLines(rofId), sizeof(Line) * nLines, cudaMemcpyDeviceToHost), __FILE__, __LINE__); } + // discardResult(cudaDeviceSynchronize()); #ifdef VTX_DEBUG std::vector> NFoundLines(mTimeFrameGPU->getNrof()), ExcNFoundLines(mTimeFrameGPU->getNrof()); std::ofstream nlines_out("N_lines_gpu.txt"); @@ -560,6 +569,125 @@ void VertexerTraitsGPU::computeTrackletMatching() } void VertexerTraitsGPU::computeVertices() +{ +#ifdef VTX_DEBUG + std::vector> dbg_clusLines(mTimeFrameGPU->getNrof()); +#endif + std::vector noClustersVec(mTimeFrameGPU->getNrof(), 0); + for (int rofId{0}; rofId < mTimeFrameGPU->getNrof(); ++rofId) { + const int numTracklets{static_cast(mTimeFrameGPU->getLines(rofId).size())}; + std::vector usedTracklets(numTracklets, false); + for (int tracklet1{0}; tracklet1 < numTracklets; ++tracklet1) { + if (usedTracklets[tracklet1]) { + continue; + } + for (int tracklet2{tracklet1 + 1}; tracklet2 < numTracklets; ++tracklet2) { + if (usedTracklets[tracklet2]) { + continue; + } + if (Line::getDCA(mTimeFrameGPU->getLines(rofId)[tracklet1], mTimeFrameGPU->getLines(rofId)[tracklet2]) < mVrtParams.pairCut) { + mTimeFrameGPU->getTrackletClusters(rofId).emplace_back(tracklet1, mTimeFrameGPU->getLines(rofId)[tracklet1], tracklet2, mTimeFrameGPU->getLines(rofId)[tracklet2]); + std::array tmpVertex{mTimeFrameGPU->getTrackletClusters(rofId).back().getVertex()}; + if (tmpVertex[0] * tmpVertex[0] + tmpVertex[1] * tmpVertex[1] > 4.f) { + mTimeFrameGPU->getTrackletClusters(rofId).pop_back(); + break; + } + usedTracklets[tracklet1] = true; + usedTracklets[tracklet2] = true; + for (int tracklet3{0}; tracklet3 < numTracklets; ++tracklet3) { + if (usedTracklets[tracklet3]) { + continue; + } + if (Line::getDistanceFromPoint(mTimeFrameGPU->getLines(rofId)[tracklet3], tmpVertex) < mVrtParams.pairCut) { + mTimeFrameGPU->getTrackletClusters(rofId).back().add(tracklet3, mTimeFrameGPU->getLines(rofId)[tracklet3]); + usedTracklets[tracklet3] = true; + tmpVertex = mTimeFrameGPU->getTrackletClusters(rofId).back().getVertex(); + } + } + break; + } + } + } + std::sort(mTimeFrameGPU->getTrackletClusters(rofId).begin(), mTimeFrameGPU->getTrackletClusters(rofId).end(), + [](ClusterLines& cluster1, ClusterLines& cluster2) { return cluster1.getSize() > cluster2.getSize(); }); + noClustersVec[rofId] = static_cast(mTimeFrameGPU->getTrackletClusters(rofId).size()); + for (int iCluster1{0}; iCluster1 < noClustersVec[rofId]; ++iCluster1) { + std::array vertex1{mTimeFrameGPU->getTrackletClusters(rofId)[iCluster1].getVertex()}; + std::array vertex2{}; + for (int iCluster2{iCluster1 + 1}; iCluster2 < noClustersVec[rofId]; ++iCluster2) { + vertex2 = mTimeFrameGPU->getTrackletClusters(rofId)[iCluster2].getVertex(); + if (std::abs(vertex1[2] - vertex2[2]) < mVrtParams.clusterCut) { + float distance{(vertex1[0] - vertex2[0]) * (vertex1[0] - vertex2[0]) + + (vertex1[1] - vertex2[1]) * (vertex1[1] - vertex2[1]) + + (vertex1[2] - vertex2[2]) * (vertex1[2] - vertex2[2])}; + if (distance < mVrtParams.pairCut * mVrtParams.pairCut) { + for (auto label : mTimeFrameGPU->getTrackletClusters(rofId)[iCluster2].getLabels()) { + mTimeFrameGPU->getTrackletClusters(rofId)[iCluster1].add(label, mTimeFrameGPU->getLines(rofId)[label]); + vertex1 = mTimeFrameGPU->getTrackletClusters(rofId)[iCluster1].getVertex(); + } + } + mTimeFrameGPU->getTrackletClusters(rofId).erase(mTimeFrameGPU->getTrackletClusters(rofId).begin() + iCluster2); + --iCluster2; + --noClustersVec[rofId]; + } + } + } + } + + for (int rofId{0}; rofId < mTimeFrameGPU->getNrof(); ++rofId) { +#ifdef VTX_DEBUG + for (auto& cl : mTimeFrameGPU->getTrackletClusters(rofId)) { + dbg_clusLines[rofId].push_back(cl); + } +#endif + for (int iCluster{0}; iCluster < noClustersVec[rofId]; ++iCluster) { + if (mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getSize() < mVrtParams.clusterContributorsCut && noClustersVec[rofId] > 1) { + mTimeFrameGPU->getTrackletClusters(rofId).erase(mTimeFrameGPU->getTrackletClusters(rofId).begin() + iCluster); + noClustersVec[rofId]--; + continue; + } + if (mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getVertex()[0] * mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getVertex()[0] + + mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getVertex()[1] * mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getVertex()[1] < + 1.98 * 1.98) { + mVertices.emplace_back(mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getVertex()[0], + mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getVertex()[1], + mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getVertex()[2], + mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getRMS2(), // Symm matrix. Diagonal: RMS2 components, + // off-diagonal: square mean of projections on planes. + mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getSize(), // Contributors + mTimeFrameGPU->getTrackletClusters(rofId)[iCluster].getAvgDistance2(), // In place of chi2 + rofId); + } + } + + mTimeFrameGPU->addPrimaryVertices(mVertices); + mVertices.clear(); + } +#ifdef VTX_DEBUG + TFile* dbg_file = TFile::Open("artefacts_tf.root", "update"); + TTree* ln_clus_lines_tree = new TTree("clusterlines", "tf"); + std::vector cl_lines_vec_pre(0); + std::vector cl_lines_vec_post(0); + ln_clus_lines_tree->Branch("cllines_pre", &cl_lines_vec_pre); + ln_clus_lines_tree->Branch("cllines_post", &cl_lines_vec_post); + for (auto rofId{0}; rofId < mTimeFrameGPU->getNrof(); ++rofId) { + cl_lines_vec_pre.clear(); + cl_lines_vec_post.clear(); + for (auto& clln : mTimeFrameGPU->getTrackletClusters(rofId)) { + cl_lines_vec_post.push_back(clln); + } + for (auto& cl : dbg_clusLines[rofId]) { + cl_lines_vec_pre.push_back(cl); + } + ln_clus_lines_tree->Fill(); + } + dbg_file->cd(); + ln_clus_lines_tree->Write(); + dbg_file->Close(); +#endif +} + +void VertexerTraitsGPU::computeVerticesHist() { if (!mTimeFrameGPU->getClusters().size()) { return; diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h index c743ea9dd21e1..c24580d2a681a 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h @@ -58,6 +58,17 @@ namespace its { using Vertex = o2::dataformats::Vertex>; +struct lightVertex { + lightVertex(float x, float y, float z, std::array rms2, int cont, float avgdis2, int stamp); + float mX; + float mY; + float mZ; + std::array mRMS2; + float mAvgDistance2; + int mContributors; + int mTimeStamp; +}; + class TimeFrame { public: @@ -70,6 +81,7 @@ class TimeFrame int getPrimaryVerticesNum(int rofID = -1) const; void addPrimaryVertices(const std::vector& vertices); void addPrimaryVertices(const gsl::span& vertices); + void addPrimaryVertices(const std::vector&); void removePrimaryVerticesInROf(const int rofId); int loadROFrameData(const o2::itsmft::ROFRecord& rof, gsl::span clusters, const dataformats::MCTruthContainer* mcLabels = nullptr); diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Vertexer.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Vertexer.h index a4573c2f5d649..73e19a9dd55b5 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Vertexer.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Vertexer.h @@ -62,8 +62,7 @@ class Vertexer std::vector exportVertices(); VertexerTraits* getTraits() const { return mTraits; }; - float clustersToVertices( - const bool useMc = false, std::function = [](std::string s) { std::cout << s << std::endl; }); + float clustersToVertices(std::function = [](std::string s) { std::cout << s << std::endl; }); void filterMCTracklets(); void validateTracklets(); @@ -82,15 +81,7 @@ class Vertexer // Utils void dumpTraits(); template - float evaluateTask(void (Vertexer::*)(T...), bool, const char*, std::function logger, T&&... args); - - // debug - void setDebugCombinatorics(); - void setDebugTrackletSelection(); - void setDebugLines(); - void setDebugSummaryLines(); - void setDebugCentroidsHistograms(); - // \debug + float evaluateTask(void (Vertexer::*)(T...), const char*, std::function logger, T&&... args); private: std::uint32_t mROframe = 0; @@ -116,11 +107,6 @@ void Vertexer::findTracklets(T&&... args) mTraits->computeTracklets(std::forward(args)...); } -// inline void Vertexer::findTrivialMCTracklets() -// { -// mTraits->computeTrackletsPureMontecarlo(); -// } - inline VertexingParameters Vertexer::getVertParameters() const { return mTraits->getVertexingParameters(); @@ -152,7 +138,7 @@ inline std::vector Vertexer::exportVertices() } template -float Vertexer::evaluateTask(void (Vertexer::*task)(T...), bool verbose, const char* taskName, std::function logger, +float Vertexer::evaluateTask(void (Vertexer::*task)(T...), const char* taskName, std::function logger, T&&... args) { float diff{0.f}; @@ -171,9 +157,7 @@ float Vertexer::evaluateTask(void (Vertexer::*task)(T...), bool verbose, const c } else { sstream << std::setw(2) << " - " << taskName << " completed in: " << diff << " ms"; } - if (verbose) { - logger(sstream.str()); - } + logger(sstream.str()); } else { (this->*task)(std::forward(args)...); } @@ -181,31 +165,6 @@ float Vertexer::evaluateTask(void (Vertexer::*task)(T...), bool verbose, const c return diff; } -inline void Vertexer::setDebugCombinatorics() -{ - mTraits->setDebugFlag(VertexerDebug::CombinatoricsTreeAll); -} - -inline void Vertexer::setDebugTrackletSelection() -{ - mTraits->setDebugFlag(VertexerDebug::TrackletTreeAll); -} - -inline void Vertexer::setDebugLines() -{ - mTraits->setDebugFlag(VertexerDebug::LineTreeAll); -} - -inline void Vertexer::setDebugSummaryLines() -{ - mTraits->setDebugFlag(VertexerDebug::LineSummaryAll); -} - -inline void Vertexer::setDebugCentroidsHistograms() -{ - mTraits->setDebugFlag(VertexerDebug::HistCentroids); -} - } // namespace its } // namespace o2 #endif diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h index 358c5746575f4..763200d0a2fbc 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/VertexerTraits.h @@ -38,44 +38,13 @@ class MCCompLabel; namespace its { class ROframe; - using constants::its::LayersNumberVertexer; -struct lightVertex { - lightVertex(float x, float y, float z, std::array rms2, int cont, float avgdis2, int stamp); - float mX; - float mY; - float mZ; - std::array mRMS2; - float mAvgDistance2; - int mContributors; - int mTimeStamp; -}; - -struct ClusterMCLabelInfo { - int TrackId; - int MotherId; - int EventId; - float Pt; -}; - -enum class VertexerDebug : unsigned int { - TrackletTreeAll = 0x1 << 1, - LineTreeAll = 0x1 << 2, - CombinatoricsTreeAll = 0x1 << 3, - LineSummaryAll = 0x1 << 4, - HistCentroids = 0x1 << 5 -}; - enum class TrackletMode { Layer0Layer1 = 0, Layer1Layer2 = 2 }; -inline lightVertex::lightVertex(float x, float y, float z, std::array rms2, int cont, float avgdis2, int stamp) : mX{x}, mY{y}, mZ{z}, mRMS2{rms2}, mAvgDistance2{avgdis2}, mContributors{cont}, mTimeStamp{stamp} -{ -} - class VertexerTraits { public: @@ -113,13 +82,8 @@ class VertexerTraits unsigned char getIsGPU() const { return mIsGPU; }; void dumpVertexerTraits(); - void setDebugFlag(VertexerDebug flag, const unsigned char on); - unsigned char isDebugFlag(const VertexerDebug& flags) const; - unsigned int getDebugFlags() const { return static_cast(mDBGFlags); } - protected: unsigned char mIsGPU; - unsigned int mDBGFlags = 0; VertexingParameters mVrtParams; IndexTableUtils mIndexTableUtils; @@ -184,21 +148,6 @@ GPUhdi() const int4 VertexerTraits::getBinsRect(const Cluster& currentCluster, c return VertexerTraits::getBinsRect(currentCluster, layerIndex, directionZIntersection, maxdeltaz, maxdeltaphi, mIndexTableUtils); } -// debug -inline void VertexerTraits::setDebugFlag(VertexerDebug flag, const unsigned char on = true) -{ - if (on) { - mDBGFlags |= static_cast(flag); - } else { - mDBGFlags &= ~static_cast(flag); - } -} - -inline unsigned char VertexerTraits::isDebugFlag(const VertexerDebug& flags) const -{ - return mDBGFlags & static_cast(flags); -} - inline void VertexerTraits::adoptTimeFrame(TimeFrame* tf) { mTimeFrame = tf; } } // namespace its diff --git a/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx b/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx index 6464786bc4fb8..69fe1aca5ce5a 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx @@ -51,6 +51,10 @@ namespace o2 namespace its { +lightVertex::lightVertex(float x, float y, float z, std::array rms2, int cont, float avgdis2, int stamp) : mX{x}, mY{y}, mZ{z}, mRMS2{rms2}, mAvgDistance2{avgdis2}, mContributors{cont}, mTimeStamp{stamp} +{ +} + constexpr float DefClusErrorRow = o2::itsmft::SegmentationAlpide::PitchRow * 0.5; constexpr float DefClusErrorCol = o2::itsmft::SegmentationAlpide::PitchCol * 0.5; constexpr float DefClusError2Row = DefClusErrorRow * DefClusErrorRow; @@ -93,6 +97,16 @@ void TimeFrame::addPrimaryVertices(const gsl::span& vertices) mROframesPV.push_back(mPrimaryVertices.size()); } +void TimeFrame::addPrimaryVertices(const std::vector& lVertices) +{ + std::vector vertices; + for (auto& vertex : lVertices) { + vertices.emplace_back(o2::math_utils::Point3D(vertex.mX, vertex.mY, vertex.mZ), vertex.mRMS2, vertex.mContributors, vertex.mAvgDistance2); + vertices.back().setTimeStamp(vertex.mTimeStamp); + } + addPrimaryVertices(vertices); +} + int TimeFrame::loadROFrameData(const o2::itsmft::ROFRecord& rof, gsl::span clusters, const dataformats::MCTruthContainer* mcLabels) { diff --git a/Detectors/ITSMFT/ITS/tracking/src/Vertexer.cxx b/Detectors/ITSMFT/ITS/tracking/src/Vertexer.cxx index c530f676a0dd8..f00a5b78bbd11 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/Vertexer.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/Vertexer.cxx @@ -36,14 +36,14 @@ Vertexer::Vertexer(VertexerTraits* traits) mTraits = traits; } -float Vertexer::clustersToVertices(const bool useMc, std::function logger) +float Vertexer::clustersToVertices(std::function logger) { float total{0.f}; TrackingParameters trkPars; - total += evaluateTask(&Vertexer::initialiseVertexer, false, "Vertexer initialisation", logger, trkPars); - total += evaluateTask(&Vertexer::findTracklets, false, "Tracklet finding", logger); - total += evaluateTask(&Vertexer::validateTracklets, false, "Adjacent tracklets validation", logger); - total += evaluateTask(&Vertexer::findVertices, false, "Vertex finding", logger); + total += evaluateTask(&Vertexer::initialiseVertexer, "Vertexer initialisation", logger, trkPars); + total += evaluateTask(&Vertexer::findTracklets, "Tracklet finding", logger); + total += evaluateTask(&Vertexer::validateTracklets, "Adjacent tracklets validation", logger); + total += evaluateTask(&Vertexer::findVertices, "Vertex finding", logger); return total; } diff --git a/Detectors/ITSMFT/ITS/workflow/src/CookedTrackerSpec.cxx b/Detectors/ITSMFT/ITS/workflow/src/CookedTrackerSpec.cxx index 1195f02afffc6..b221f01fcde1d 100644 --- a/Detectors/ITSMFT/ITS/workflow/src/CookedTrackerSpec.cxx +++ b/Detectors/ITSMFT/ITS/workflow/src/CookedTrackerSpec.cxx @@ -139,7 +139,7 @@ void CookedTrackerDPL::run(ProcessingContext& pc) mTimeFrame.setMultiplicityCutMask(processingMask); float vertexerElapsedTime; if (mRunVertexer) { - vertexerElapsedTime = mVertexerPtr->clustersToVertices(false, [&](std::string s) { LOG(info) << s; }); + vertexerElapsedTime = mVertexerPtr->clustersToVertices([&](std::string s) { LOG(info) << s; }); } LOG(info) << fmt::format(" - Vertex seeding total elapsed time: {} ms in {} ROFs", vertexerElapsedTime, rofspan.size()); for (size_t iRof{0}; iRof < rofspan.size(); ++iRof) { diff --git a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx index f0af3b090e25f..1f6e23cacd5dd 100644 --- a/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx +++ b/Detectors/ITSMFT/ITS/workflow/src/TrackerSpec.cxx @@ -212,7 +212,7 @@ void TrackerDPL::run(ProcessingContext& pc) float vertexerElapsedTime{0.f}; if (mRunVertexer) { // Run seeding vertexer - vertexerElapsedTime = mVertexer->clustersToVertices(false, logger); + vertexerElapsedTime = mVertexer->clustersToVertices(logger); } const auto& multEstConf = FastMultEstConfig::Instance(); // parameters for mult estimation and cuts for (auto iRof{0}; iRof < rofspan.size(); ++iRof) { @@ -257,6 +257,7 @@ void TrackerDPL::run(ProcessingContext& pc) } else { timeFrame->setMultiplicityCutMask(processingMask); + // Run CA tracker mTracker->clustersToTracks(logger, errorLogger); if (timeFrame->hasBogusClusters()) { LOG(warning) << fmt::format(" - The processed timeframe had {} clusters with wild z coordinates, check the dictionaries", timeFrame->hasBogusClusters()); From f6ccef6fe801c336cd37bab5e8f3f53b3e142461 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Tue, 12 Jul 2022 12:08:22 +0200 Subject: [PATCH 26/30] Move TimeFrame checks to traits --- .../include/ITStracking/TrackerTraits.h | 6 ++++++ Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx | 8 ++++---- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 20 +++++++++++++++++++ macro/run_trac_ca_its.C | 2 +- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h index 336786677bd29..3ad2547f45eba 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h @@ -101,6 +101,12 @@ class TrackerTraits void setIsGPU(const unsigned char isgpu) { mIsGPU = isgpu; }; + // TimeFrame information forwarding + virtual int getTFNumberOfClusters() const; + virtual int getTFNumberOfTracklets() const; + virtual int getTFNumberOfCells() const; + // virtual bool checkTFMemory(const int iteration); + protected: TimeFrame* mTimeFrame; std::vector mTrkParams; diff --git a/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx b/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx index 0c191c013104b..3b84c3917fb6b 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx @@ -52,24 +52,24 @@ void Tracker::clustersToTracks(std::function logger, std::f for (int iteration = 0; iteration < (int)mTrkParams.size(); ++iteration) { total += evaluateTask(&Tracker::initialiseTimeFrame, "Timeframe initialisation", logger, iteration); total += evaluateTask(&Tracker::computeTracklets, "Tracklet finding", logger, iteration); - logger(fmt::format("\t- Number of tracklets: {}", mTimeFrame->getNumberOfTracklets())); + logger(fmt::format("\t- Number of tracklets: {}", mTraits->getTFNumberOfTracklets())); if (!mTimeFrame->checkMemory(mTrkParams[iteration].MaxMemory)) { error("Too much memory used during trackleting, check the detector status and/or the selections."); break; } - float trackletsPerCluster = mTimeFrame->getNumberOfClusters() > 0 ? float(mTimeFrame->getNumberOfTracklets()) / mTimeFrame->getNumberOfClusters() : 0.f; + float trackletsPerCluster = mTraits->getTFNumberOfClusters() > 0 ? float(mTraits->getTFNumberOfTracklets()) / mTraits->getTFNumberOfClusters() : 0.f; if (trackletsPerCluster > mTrkParams[iteration].TrackletsPerClusterLimit) { error(fmt::format("Too many tracklets per cluster ({}), check the detector status and/or the selections.", trackletsPerCluster)); break; } total += evaluateTask(&Tracker::computeCells, "Cell finding", logger, iteration); - logger(fmt::format("\t- Number of Cells: {}", mTimeFrame->getNumberOfCells())); + logger(fmt::format("\t- Number of Cells: {}", mTraits->getTFNumberOfCells())); if (!mTimeFrame->checkMemory(mTrkParams[iteration].MaxMemory)) { error("Too much memory used during cell finding, check the detector status and/or the selections."); break; } - float cellsPerCluster = mTimeFrame->getNumberOfClusters() > 0 ? float(mTimeFrame->getNumberOfCells()) / mTimeFrame->getNumberOfClusters() : 0.f; + float cellsPerCluster = mTraits->getTFNumberOfClusters() > 0 ? float(mTraits->getTFNumberOfCells()) / mTraits->getTFNumberOfClusters() : 0.f; if (cellsPerCluster > mTrkParams[iteration].CellsPerClusterLimit) { error(fmt::format("Too many cells per cluster ({}), check the detector status and/or the selections.", cellsPerCluster)); break; diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 12879ff955814..556df8e89f011 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -997,5 +997,25 @@ void TrackerTraits::setNThreads(int n) mNThreads = 1; #endif } +int TrackerTraits::getTFNumberOfClusters() const +{ + return mTimeFrame->getNumberOfClusters(); +} + +int TrackerTraits::getTFNumberOfTracklets() const +{ + return mTimeFrame->getNumberOfTracklets(); +} + +int TrackerTraits::getTFNumberOfCells() const +{ + return mTimeFrame->getNumberOfCells(); +} + +// bool TrackerTraits::checkTFMemory(const int iteration) +// { +// return mTimeFrame->checkMemory(mTrkParams[iteration].MaxMemory); +// } + } // namespace its } // namespace o2 diff --git a/macro/run_trac_ca_its.C b/macro/run_trac_ca_its.C index c135f81899732..2bac28c38fbc1 100644 --- a/macro/run_trac_ca_its.C +++ b/macro/run_trac_ca_its.C @@ -222,7 +222,7 @@ void run_trac_ca_its(bool cosmics = false, int rofId{0}; vertexer.adoptTimeFrame(tf); - vertexer.clustersToVertices(false); + vertexer.clustersToVertices(); tf.printVertices(); From 9e6c2e4a92ef98c53afa572a74c696bb22f53817 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Tue, 12 Jul 2022 14:56:03 +0200 Subject: [PATCH 27/30] Add TF forwarding overrides --- .../GPU/ITStrackingGPU/TrackerTraitsGPU.h | 5 +++++ .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 19 +++++++++++++++++++ macro/run_trac_its.C | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h index e19fb37b7bcc9..ee9c2303f1426 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h @@ -37,6 +37,11 @@ class TrackerTraitsGPU : public TrackerTraits void computeLayerCells(const int iteration) override; // void refitTracks(const std::vector>& tf, std::vector& tracks) override; + // TimeFrameGPU information forwarding + int getTFNumberOfClusters() const override; + int getTFNumberOfTracklets() const override; + int getTFNumberOfCells() const override; + private: gpu::TimeFrameGPU<7>* mTimeFrameGPU; gpu::StaticTrackingParameters* mStaticTrkPars; diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index c094030ab25f6..98e2e5ed14b66 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -470,6 +470,25 @@ void TrackerTraitsGPU::computeLayerCells() // //TODO: restore this // // mChainRunITSTrackFit(*mChain, mPrimaryVertexContext->getRoads(), clusters, cells, tf, tracks); // } + +template +int TrackerTraitsGPU::getTFNumberOfClusters() const +{ + return mTimeFrameGPU->getNumberOfClusters(); +} + +template +int TrackerTraitsGPU::getTFNumberOfTracklets() const +{ + return mTimeFrameGPU->getNumberOfTracklets(); +} + +template +int TrackerTraitsGPU::getTFNumberOfCells() const +{ + return mTimeFrameGPU->getNumberOfCells(); +} + template class TrackerTraitsGPU<7>; } // namespace its } // namespace o2 diff --git a/macro/run_trac_its.C b/macro/run_trac_its.C index c2ce2cd438bc5..67bc075c5754d 100644 --- a/macro/run_trac_its.C +++ b/macro/run_trac_its.C @@ -169,7 +169,7 @@ void run_trac_its(std::string path = "./", std::string outputfile = "o2trac_its. tf.loadROFrameData(rofspan, clSpan, pattIt_vertexer, dict, labels); tf.setMultiplicityCutMask(processingMask); vertexer.adoptTimeFrame(tf); - vertexer.clustersToVertices(mcTruth); + vertexer.clustersToVertices(); int iRof = 0; for (auto& rof : *rofs) { auto it = pattIt; From 7f91349b4d341c034fb7b7253e2eac52280af614 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Mon, 25 Jul 2022 19:28:52 +0200 Subject: [PATCH 28/30] Add comparison macro and fix bug in maxTracklets --- .../ITSMFT/ITS/macros/test/CMakeLists.txt | 7 +- .../ITSMFT/ITS/macros/test/CompareArtefacts.C | 91 +++++++++ .../ITSMFT/ITS/macros/test/RunGPUTracking.C | 186 ------------------ .../GPU/ITStrackingGPU/VertexerTraitsGPU.h | 4 +- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 14 +- .../tracking/GPU/cuda/VertexerTraitsGPU.cu | 43 ++-- .../include/ITStracking/Configuration.h | 4 +- .../include/ITStracking/Definitions.h | 1 + .../include/ITStracking/TrackingConfigParam.h | 2 +- .../include/ITStracking/VertexerTraits.h | 11 +- .../ITS/tracking/src/VertexerTraits.cxx | 11 +- 11 files changed, 146 insertions(+), 228 deletions(-) create mode 100644 Detectors/ITSMFT/ITS/macros/test/CompareArtefacts.C delete mode 100644 Detectors/ITSMFT/ITS/macros/test/RunGPUTracking.C diff --git a/Detectors/ITSMFT/ITS/macros/test/CMakeLists.txt b/Detectors/ITSMFT/ITS/macros/test/CMakeLists.txt index befc776289e25..ee9479edb17ec 100644 --- a/Detectors/ITSMFT/ITS/macros/test/CMakeLists.txt +++ b/Detectors/ITSMFT/ITS/macros/test/CMakeLists.txt @@ -66,10 +66,6 @@ o2_add_test_root_macro(CheckTracksCA.C O2::DataFormatsITSMFT LABELS its) -o2_add_test_root_macro(RunGPUTracking.C - PUBLIC_LINK_LIBRARIES O2::GPUTracking - LABELS its) - o2_add_test_root_macro(DisplayTrack.C PUBLIC_LINK_LIBRARIES O2::ITSBase O2::DataFormatsITSMFT @@ -95,3 +91,6 @@ o2_add_test_root_macro(ITSMisaligner.C PUBLIC_LINK_LIBRARIES O2::CCDB O2::ITSReconstruction LABELS its) + +o2_add_test_root_macro(CompareArtefacts.C + LABELS its) diff --git a/Detectors/ITSMFT/ITS/macros/test/CompareArtefacts.C b/Detectors/ITSMFT/ITS/macros/test/CompareArtefacts.C new file mode 100644 index 0000000000000..34da66d3890f6 --- /dev/null +++ b/Detectors/ITSMFT/ITS/macros/test/CompareArtefacts.C @@ -0,0 +1,91 @@ +#if !defined(__CLING__) || defined(__ROOTCLING__) +#include "TFile.h" +#include "TTree.h" +#include "TCanvas.h" +#include "TH1F.h" +#include "TLegend.h" +#include "TMath.h" +#include "TString.h" +#include "TStyle.h" + +#include +#include +#endif + +using std::string; +using std::vector; + +void CompareArtefacts(const string cpu_file = "artefacts_tf.root", const string gpu_file = "artefacts_tf_gpu.root") +{ + gStyle->SetOptStat(0); + + auto f_cpu = TFile::Open(cpu_file.data(), "r"); + auto f_gpu = TFile::Open(gpu_file.data(), "r"); + + auto tree_cpu_tracklets0 = (TTree*)f_cpu->Get("tracklets"); + auto tree_gpu_tracklets0 = (TTree*)f_gpu->Get("tracklets"); + + auto hist_cpu_tracklets0_phi = new TH1F("hist_cpu_tracklets0_phi", "hist_cpu_tracklets0_phi", 100, -TMath::Pi() - 1, TMath::Pi() + 1); + auto hist_gpu_tracklets0_phi = new TH1F("hist_gpu_tracklets0_phi", "hist_gpu_tracklets0_phi", 100, -TMath::Pi() - 1, TMath::Pi() + 1); + auto hist_cpu_tracklets0_tanL = new TH1F("hist_cpu_tracklets0_tanL", "hist_cpu_tracklets0_tanL", 100, -80, 80); + auto hist_gpu_tracklets0_tanL = new TH1F("hist_gpu_tracklets0_tanL", "hist_gpu_tracklets0_tanL", 100, -80, 80); + auto hist_cpu_tracklets0_firstClusterIndex = new TH1F("hist_cpu_tracklets0_firstClusterIndex", "hist_cpu_tracklets0_firstClusterIndex", 200, 0, 600); + auto hist_gpu_tracklets0_firstClusterIndex = new TH1F("hist_gpu_tracklets0_firstClusterIndex", "hist_gpu_tracklets0_firstClusterIndex", 200, 0, 600); + + hist_cpu_tracklets0_phi->SetLineColor(kRed); + hist_gpu_tracklets0_phi->SetLineColor(kBlue); + hist_cpu_tracklets0_tanL->SetLineColor(kRed); + hist_gpu_tracklets0_tanL->SetLineColor(kBlue); + hist_cpu_tracklets0_firstClusterIndex->SetLineColor(kRed); + hist_gpu_tracklets0_firstClusterIndex->SetLineColor(kBlue); + + auto c1 = new TCanvas("c1", "c1", 800, 800); + c1->cd(); + + tree_cpu_tracklets0->Draw("Tracklets0.phi >> hist_cpu_tracklets0_phi"); + tree_gpu_tracklets0->Draw("Tracklets0.phi >> hist_gpu_tracklets0_phi"); + + hist_gpu_tracklets0_phi->Draw(); + hist_cpu_tracklets0_phi->Draw("same"); + + auto legend = new TLegend(0.4, 0.4, 0.2, 0.2); + legend->SetHeader("Tracklets0 #varphi", "C"); + legend->AddEntry(hist_cpu_tracklets0_phi, Form("CPU: %1.f", hist_cpu_tracklets0_phi->GetEntries()), "l"); + legend->AddEntry(hist_gpu_tracklets0_phi, Form("GPU: %1.f", hist_gpu_tracklets0_phi->GetEntries()), "l"); + + legend->Draw(); + + // // Tan(L) + auto c2 = new TCanvas("c2", "c2", 800, 800); + c2->cd(); + + tree_cpu_tracklets0->Draw("Tracklets0.tanLambda >> hist_cpu_tracklets0_tanL"); + tree_gpu_tracklets0->Draw("Tracklets0.tanLambda >> hist_gpu_tracklets0_tanL"); + + hist_gpu_tracklets0_tanL->Draw(); + hist_cpu_tracklets0_tanL->Draw("same"); + + auto legend2 = new TLegend(0.4, 0.4, 0.2, 0.2); + legend2->SetHeader("Tracklets0 tan(#lambda)", "C"); + legend2->AddEntry(hist_cpu_tracklets0_tanL, Form("CPU: %1.f", hist_cpu_tracklets0_tanL->GetEntries()), "l"); + legend2->AddEntry(hist_gpu_tracklets0_tanL, Form("GPU: %1.f", hist_gpu_tracklets0_tanL->GetEntries()), "l"); + + legend2->Draw(); + + // first cluster index + auto c3 = new TCanvas("c3", "c3", 800, 800); + c3->cd(); + + tree_cpu_tracklets0->Draw("Tracklets0.firstClusterIndex >> hist_cpu_tracklets0_firstClusterIndex"); + tree_gpu_tracklets0->Draw("Tracklets0.firstClusterIndex >> hist_gpu_tracklets0_firstClusterIndex"); + + hist_gpu_tracklets0_firstClusterIndex->Draw(); + hist_cpu_tracklets0_firstClusterIndex->Draw("same"); + + auto legend3 = new TLegend(0.4, 0.4, 0.2, 0.2); + legend3->SetHeader("Tracklets0 first cluster index", "C"); + legend3->AddEntry(hist_cpu_tracklets0_firstClusterIndex, Form("CPU: %1.f", hist_cpu_tracklets0_firstClusterIndex->GetEntries()), "l"); + legend3->AddEntry(hist_gpu_tracklets0_firstClusterIndex, Form("GPU: %1.f", hist_gpu_tracklets0_firstClusterIndex->GetEntries()), "l"); + + legend3->Draw(); +} \ No newline at end of file diff --git a/Detectors/ITSMFT/ITS/macros/test/RunGPUTracking.C b/Detectors/ITSMFT/ITS/macros/test/RunGPUTracking.C deleted file mode 100644 index 6eab73649a68a..0000000000000 --- a/Detectors/ITSMFT/ITS/macros/test/RunGPUTracking.C +++ /dev/null @@ -1,186 +0,0 @@ -#if !defined(__CLING__) || defined(__ROOTCLING__) -#include "ITStracking/Vertexer.h" -#include "GPUO2Interface.h" -#include "GPUReconstruction.h" -#include "GPUChainITS.h" - -#include -#include -#include -#include -#include -#include -#include -#include "DetectorsCommonDataFormats/DetectorNameConf.h" -#include "SimulationDataFormat/MCEventHeader.h" - -#include "DataFormatsITSMFT/TopologyDictionary.h" -#include "DataFormatsITSMFT/CompCluster.h" -#include "DetectorsCommonDataFormats/DetID.h" -#include "DataFormatsParameters/GRPObject.h" -#include "DetectorsBase/GeometryManager.h" -#include "Field/MagneticField.h" -#include "ITSBase/GeometryTGeo.h" -#include "ITStracking/ROframe.h" -#include "ITStracking/IOUtils.h" -#include "ITStracking/Vertexer.h" -#include "ITStracking/Tracker.h" -#include "ITStrackingGPU/TimeFrameGPU.h" -#include "MathUtils/Utils.h" -#include "DetectorsBase/Propagator.h" -#include "CCDB/BasicCCDBManager.h" -#include "CCDB/CCDBTimeStampUtils.h" - -#include "SimulationDataFormat/MCCompLabel.h" -#include "SimulationDataFormat/MCTruthContainer.h" -#include "ITStracking/Configuration.h" - -using MCLabCont = o2::dataformats::MCTruthContainer; -using Vertex = o2::dataformats::Vertex>; - -void RunGPUTracking(bool useLUT = true, - std::string path = "./", - std::string outputfile = "o2trac_its.root", - std::string inputClustersITS = "o2clus_its.root", - std::string matLUTFile = "matbud.root", - std::string inputGRP = "o2sim_grp.root", - long timestamp = 0) -{ - o2::its::ROframe event(0, 7); - - if (path.back() != '/') { - path += '/'; - } - - //-------- init geometry and field --------// - const auto grp = o2::parameters::GRPObject::loadFrom(path + inputGRP); - if (!grp) { - LOG(fatal) << "Cannot run w/o GRP object"; - } - - o2::base::GeometryManager::loadGeometry(path); - o2::base::Propagator::initFieldFromGRP(grp); - auto field = static_cast(TGeoGlobalMagField::Instance()->GetField()); - if (!field) { - LOG(fatal) << "Failed to load ma"; - } - double origD[3] = {0., 0., 0.}; - // tracker.setBz(field->getBz(origD)); - - //-------- init lookuptable --------// - if (useLUT) { - auto* lut = o2::base::MatLayerCylSet::loadFromFile(matLUTFile); - o2::base::Propagator::Instance()->setMatLUT(lut); - } else { - // tracker.setCorrType(o2::base::PropagatorImpl::MatCorrType::USEMatCorrTGeo); - } - - // if (tracker.isMatLUT()) { - // LOG(info) << "Loaded material LUT from " << matLUTFile; - // } else { - // LOG(info) << "Material LUT " << matLUTFile << " file is absent, only TGeo can be used"; - // } - - auto gman = o2::its::GeometryTGeo::Instance(); - gman->fillMatrixCache(o2::math_utils::bit2Mask(o2::math_utils::TransformType::T2L, o2::math_utils::TransformType::T2GRot, - o2::math_utils::TransformType::L2G)); // request cached transforms - - auto& mgr = o2::ccdb::BasicCCDBManager::instance(); - mgr.setURL("http://alice-ccdb.cern.ch"); - mgr.setTimestamp(timestamp ? timestamp : o2::ccdb::getCurrentTimestamp()); - const o2::itsmft::TopologyDictionary* dict = mgr.get("ITS/Calib/ClusterDictionary"); - - //>>>---------- attach input data --------------->>> - TChain itsClusters("o2sim"); - itsClusters.AddFile((path + inputClustersITS).data()); - - if (!itsClusters.GetBranch("ITSClusterComp")) { - LOG(fatal) << "Did not find ITS clusters branch ITSClusterComp in the input tree"; - } - std::vector* cclusters = nullptr; - itsClusters.SetBranchAddress("ITSClusterComp", &cclusters); - - if (!itsClusters.GetBranch("ITSClusterPatt")) { - LOG(fatal) << "Did not find ITS cluster patterns branch ITSClusterPatt in the input tree"; - } - std::vector* patterns = nullptr; - itsClusters.SetBranchAddress("ITSClusterPatt", &patterns); - - MCLabCont* labels = nullptr; - if (!itsClusters.GetBranch("ITSClusterMCTruth")) { - LOG(warning) << "Did not find ITS clusters branch ITSClusterMCTruth in the input tree"; - } else { - itsClusters.SetBranchAddress("ITSClusterMCTruth", &labels); - } - - if (!itsClusters.GetBranch("ITSClustersROF")) { - LOG(fatal) << "Did not find ITS clusters branch ITSClustersROF in the input tree"; - } - - std::vector* mc2rofs = nullptr; - if (!itsClusters.GetBranch("ITSClustersMC2ROF")) { - LOG(fatal) << "Did not find ITS clusters branch ITSClustersROF in the input tree"; - } - itsClusters.SetBranchAddress("ITSClustersMC2ROF", &mc2rofs); - - std::vector* rofs = nullptr; - itsClusters.SetBranchAddress("ITSClustersROF", &rofs); - itsClusters.GetEntry(0); - - //------------------------------------------------- - std::unique_ptr recCUDA(o2::gpu::GPUReconstruction::CreateInstance(o2::gpu::GPUDataTypes::DeviceType::CUDA, true)); - auto* chainITSCUDA = recCUDA->AddChain(); - std::unique_ptr vertexerCUDA = std::make_unique(chainITSCUDA->GetITSVertexerTraits()); - std::unique_ptr trackerCUDA = std::make_unique(chainITSCUDA->GetITSTrackerTraits()); - - std::unique_ptr recHIP(o2::gpu::GPUReconstruction::CreateInstance(o2::gpu::GPUDataTypes::DeviceType::HIP, true)); - auto* chainITSHIP = recHIP->AddChain(); - std::unique_ptr vertexerHIP = std::make_unique(chainITSHIP->GetITSVertexerTraits()); - std::unique_ptr trackerHIP = std::make_unique(chainITSHIP->GetITSTrackerTraits()); - - o2::its::VertexingParameters parameters; - parameters.phiCut = 0.005f; - parameters.tanLambdaCut = 0.002f; - - vertexerCUDA->setParameters(parameters); - vertexerHIP->setParameters(parameters); - - gsl::span patt(patterns->data(), patterns->size()); - auto pattIt = patt.begin(); - auto clSpan = gsl::span(cclusters->data(), cclusters->size()); - - for (auto& rof : *rofs) { - - auto it = pattIt; - o2::its::ioutils::loadROFrameData(rof, event, clSpan, pattIt, dict, labels); - - // // CUDA - vertexerCUDA->initialiseVertexer(&event); - vertexerCUDA->findTracklets(); - // vertexerCUDA.filterMCTracklets(); // to use MC check - vertexerCUDA->validateTracklets(); - vertexerCUDA->findVertices(); - std::vector vertITSCU = vertexerCUDA->exportVertices(); - if (!vertITSCU.empty()) { - std::cout << " - Reconstructed vertex: x = " << vertITSCU[0].getX() << " y = " << vertITSCU[0].getY() << " x = " << vertITSCU[0].getZ() << std::endl; - event.addPrimaryVertex(vertITSCU[0].getX(), vertITSCU[0].getY(), vertITSCU[0].getZ()); - } else { - std::cout << " - Vertex not reconstructed" << std::endl; - } - - // // HIP - vertexerHIP->initialiseVertexer(&event); - vertexerHIP->findTracklets(); - // // vertexerHIP.filterMCTracklets(); // to use MC check - vertexerHIP->validateTracklets(); - vertexerHIP->findVertices(); - std::vector vertITSHIP = vertexerHIP->exportVertices(); - if (!vertITSHIP.empty()) { - std::cout << " - Reconstructed vertex: x = " << vertITSHIP[0].getX() << " y = " << vertITSHIP[0].getY() << " x = " << vertITSHIP[0].getZ() << std::endl; - event.addPrimaryVertex(vertITSHIP[0].getX(), vertITSHIP[0].getY(), vertITSHIP[0].getZ()); - } else { - std::cout << " - Vertex not reconstructed" << std::endl; - } - } -} -#endif diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/VertexerTraitsGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/VertexerTraitsGPU.h index 3ddbeb7206cfa..c9d923191d31f 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/VertexerTraitsGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/VertexerTraitsGPU.h @@ -46,9 +46,11 @@ class VertexerTraitsGPU : public VertexerTraits void computeTracklets() override; void computeTrackletMatching() override; void computeVertices() override; - void computeVerticesHist(); + void updateVertexingParameters(const VertexingParameters& vrtPar) override; // void computeMCFiltering() override; + void computeVerticesHist(); + protected: IndexTableUtils* mDeviceIndexTableUtils; gpu::TimeFrameGPU<7>* mTimeFrameGPU; diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 98e2e5ed14b66..406e7619d7f45 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -287,13 +287,13 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) rof0, mTimeFrameGPU->getNrof(), iLayer, - mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :check: - mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :check: - mTimeFrameGPU->getDeviceIndexTables(iLayer + 1), // :check: - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :check: - mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :check: - mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer), // :check: - mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer + 1), // :check: + mTimeFrameGPU->getDeviceClustersOnLayer(rof0, iLayer), // :checked: + mTimeFrameGPU->getDeviceClustersOnLayer(0, iLayer + 1), // :checked: + mTimeFrameGPU->getDeviceIndexTables(iLayer + 1), // :checked: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer), // :checked: + mTimeFrameGPU->getDeviceROframesClustersOnLayer(iLayer + 1), // :checked: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer), // :checked: + mTimeFrameGPU->getDeviceUsedClustersOnLayer(0, iLayer + 1), // :checked: mTimeFrameGPU->getDeviceVertices(rof0), mTimeFrameGPU->getDeviceTrackletsLookupTable(0, iLayer), mTimeFrameGPU->getDeviceTracklets(rof0, iLayer), diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu index b0980842f4043..53a34bcb2e5d9 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu @@ -177,7 +177,7 @@ GPUg() void trackleterKernel( // loop on clusters next layer for (int iNextLayerClusterIndex{firstRowClusterIndex}; iNextLayerClusterIndex < maxRowClusterIndex && iNextLayerClusterIndex < sizeNextLClusters; ++iNextLayerClusterIndex) { const Cluster& nextCluster = clustersNextLayer[iNextLayerClusterIndex]; - if (o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi) < phiCut) { + if (o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi) < phiCut) { if (storedTracklets < maxTrackletsPerCluster) { if constexpr (Mode == TrackletMode::Layer0Layer1) { new (Tracklets + stride + storedTracklets) Tracklet{iNextLayerClusterIndex, iCurrentLayerClusterIndex, nextCluster, currentCluster}; @@ -191,6 +191,9 @@ GPUg() void trackleterKernel( } } foundTracklets[iCurrentLayerClusterIndex] = storedTracklets; + if (storedTracklets >= maxTrackletsPerCluster) { + printf("gpu tracklet finder: some lines will be left behind for cluster %d. valid: %u max: %zu\n", iCurrentLayerClusterIndex, storedTracklets, maxTrackletsPerCluster); + } } } } @@ -377,6 +380,15 @@ GPUg() void computeVertexKernel( } } // namespace gpu +void VertexerTraitsGPU::updateVertexingParameters(const VertexingParameters& vrtPar) +{ + mVrtParams = vrtPar; + mIndexTableUtils.setTrackingParameters(vrtPar); + mVrtParams.phiSpan = static_cast(std::ceil(mIndexTableUtils.getNphiBins() * mVrtParams.phiCut / + constants::math::TwoPi)); + mVrtParams.zSpan = static_cast(std::ceil(mVrtParams.zCut * mIndexTableUtils.getInverseZCoordinate(0))); +} + void VertexerTraitsGPU::computeTracklets() { if (!mTimeFrameGPU->getClusters().size()) { @@ -395,8 +407,7 @@ void VertexerTraitsGPU::computeTracklets() mTimeFrameGPU->getDeviceTracklets(rofId, 0), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 0), mDeviceIndexTableUtils, - mTimeFrameGPU->getConfig().maxTrackletsPerCluster); - + mVrtParams.maxTrackletsPerCluster); gpu::trackleterKernel<<getStream(1).get()>>>( mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 2), mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 1), @@ -407,9 +418,10 @@ void VertexerTraitsGPU::computeTracklets() mTimeFrameGPU->getDeviceTracklets(rofId, 1), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 1), mDeviceIndexTableUtils, - mTimeFrameGPU->getConfig().maxTrackletsPerCluster); + mVrtParams.maxTrackletsPerCluster); } #ifdef VTX_DEBUG + discardResult(cudaDeviceSynchronize()); std::ofstream out01("NTC01.txt"), out12("NTC12.txt"); std::vector> NtrackletsClusters01(mTimeFrameGPU->getNrof()); std::vector> NtrackletsClusters12(mTimeFrameGPU->getNrof()); @@ -438,28 +450,28 @@ void VertexerTraitsGPU::computeTracklets() for (int iRof{0}; iRof < mTimeFrameGPU->getNrof(); ++iRof) { tracklets_clean01.clear(); tracklets_clean12.clear(); - tracklets_vec01.resize(mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * mTimeFrameGPU->getConfig().maxTrackletsPerCluster); // Nclusters * TrackletsPerCluster - tracklets_vec12.resize(mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * mTimeFrameGPU->getConfig().maxTrackletsPerCluster); + tracklets_vec01.resize(mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * mVrtParams.maxTrackletsPerCluster); // Nclusters * TrackletsPerCluster + tracklets_vec12.resize(mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * mVrtParams.maxTrackletsPerCluster); checkGPUError(cudaMemcpy(tracklets_vec01.data(), mTimeFrameGPU->getDeviceTracklets(iRof, 0), - mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * sizeof(Tracklet) * mTimeFrameGPU->getConfig().maxTrackletsPerCluster, + mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * sizeof(Tracklet) * mVrtParams.maxTrackletsPerCluster, cudaMemcpyDeviceToHost), __FILE__, __LINE__); checkGPUError(cudaMemcpy(tracklets_vec12.data(), mTimeFrameGPU->getDeviceTracklets(iRof, 1), - mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * sizeof(Tracklet) * mTimeFrameGPU->getConfig().maxTrackletsPerCluster, + mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * sizeof(Tracklet) * mVrtParams.maxTrackletsPerCluster, cudaMemcpyDeviceToHost), __FILE__, __LINE__); for (auto iCluster{0}; iCluster < NtrackletsClusters01[iRof].size(); ++iCluster) { auto nTracklets{NtrackletsClusters01[iRof][iCluster]}; for (auto iTracklet{0}; iTracklet < nTracklets; ++iTracklet) { - tracklets_clean01.push_back(tracklets_vec01[iCluster * mTimeFrameGPU->getConfig().maxTrackletsPerCluster + iTracklet]); + tracklets_clean01.push_back(tracklets_vec01[iCluster * mVrtParams.maxTrackletsPerCluster + iTracklet]); } } for (auto iCluster{0}; iCluster < NtrackletsClusters12[iRof].size(); ++iCluster) { auto nTracklets{NtrackletsClusters12[iRof][iCluster]}; for (auto iTracklet{0}; iTracklet < nTracklets; ++iTracklet) { - tracklets_clean12.push_back(tracklets_vec12[iCluster * mTimeFrameGPU->getConfig().maxTrackletsPerCluster + iTracklet]); + tracklets_clean12.push_back(tracklets_vec12[iCluster * mVrtParams.maxTrackletsPerCluster + iTracklet]); } } tr_tre->Fill(); @@ -482,7 +494,7 @@ void VertexerTraitsGPU::computeTrackletMatching() size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; // Reset used tracklets - checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedTracklets(rofId), false, sizeof(unsigned char) * mTimeFrameGPU->getConfig().maxTrackletsPerCluster * mTimeFrameGPU->getNClustersLayer(rofId, 1)), __FILE__, __LINE__); + checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedTracklets(rofId), false, sizeof(unsigned char) * mVrtParams.maxTrackletsPerCluster * mTimeFrameGPU->getNClustersLayer(rofId, 1)), __FILE__, __LINE__); gpu::trackletSelectionKernel<<>>( mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 0), @@ -496,7 +508,7 @@ void VertexerTraitsGPU::computeTrackletMatching() mTimeFrameGPU->getDeviceLines(rofId), mTimeFrameGPU->getDeviceNFoundLines(rofId), mTimeFrameGPU->getDeviceExclusiveNFoundLines(rofId), - mTimeFrameGPU->getConfig().maxTrackletsPerCluster, + mVrtParams.maxTrackletsPerCluster, mVrtParams.tanLambdaCut, mVrtParams.phiCut); @@ -506,7 +518,7 @@ void VertexerTraitsGPU::computeTrackletMatching() mTimeFrameGPU->getDeviceExclusiveNFoundLines(rofId), mTimeFrameGPU->getNClustersLayer(rofId, 1))); // Reset used tracklets - checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedTracklets(rofId), false, sizeof(unsigned char) * mTimeFrameGPU->getConfig().maxTrackletsPerCluster * mTimeFrameGPU->getNClustersLayer(rofId, 1)), __FILE__, __LINE__); + checkGPUError(cudaMemset(mTimeFrameGPU->getDeviceUsedTracklets(rofId), false, sizeof(unsigned char) * mVrtParams.maxTrackletsPerCluster * mTimeFrameGPU->getNClustersLayer(rofId, 1)), __FILE__, __LINE__); gpu::trackletSelectionKernel<<>>( mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 0), @@ -520,7 +532,7 @@ void VertexerTraitsGPU::computeTrackletMatching() mTimeFrameGPU->getDeviceLines(rofId), mTimeFrameGPU->getDeviceNFoundLines(rofId), mTimeFrameGPU->getDeviceExclusiveNFoundLines(rofId), - mTimeFrameGPU->getConfig().maxTrackletsPerCluster, + mVrtParams.maxTrackletsPerCluster, mVrtParams.tanLambdaCut, mVrtParams.phiCut); gpuThrowOnError(); @@ -533,8 +545,9 @@ void VertexerTraitsGPU::computeTrackletMatching() mTimeFrameGPU->getLines(rofId).resize(nLines); checkGPUError(cudaMemcpy(mTimeFrameGPU->getLines(rofId).data(), mTimeFrameGPU->getDeviceLines(rofId), sizeof(Line) * nLines, cudaMemcpyDeviceToHost), __FILE__, __LINE__); } - // discardResult(cudaDeviceSynchronize()); + #ifdef VTX_DEBUG + discardResult(cudaDeviceSynchronize()); std::vector> NFoundLines(mTimeFrameGPU->getNrof()), ExcNFoundLines(mTimeFrameGPU->getNrof()); std::ofstream nlines_out("N_lines_gpu.txt"); for (size_t rofId{0}; rofId < mTimeFrameGPU->getNrof(); ++rofId) { diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h index 8a6caf1b9da31..681647a35bd7c 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Configuration.h @@ -151,8 +151,8 @@ struct TimeFrameGPUConfig { size_t maxROFs); size_t tmpCUBBufferSize = 1e5; // In average in pp events there are required 4096 bytes - size_t maxTrackletsPerCluster = 50; - size_t clustersPerLayerCapacity = 5e5; + size_t maxTrackletsPerCluster = 1e2; + size_t clustersPerLayerCapacity = 2.5e5; size_t clustersPerROfCapacity = 1e4; size_t trackletsCapacity = maxTrackletsPerCluster * clustersPerLayerCapacity; size_t validatedTrackletsCapacity = 1e5; diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h index a6506f03b82bb..d75e4cad86eb5 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h @@ -16,6 +16,7 @@ #define TRACKINGITS_DEFINITIONS_H_ // #define CA_DEBUG +#define VTX_DEBUG template void discardResult(const T&) diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackingConfigParam.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackingConfigParam.h index 910439094e8ab..8d6ce5d55887c 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackingConfigParam.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackingConfigParam.h @@ -30,7 +30,7 @@ struct VertexerParamConfig : public o2::conf::ConfigurableParamHelper> selectClusters(const int* indexTable, const std::array& selectedBinsRect, @@ -102,15 +102,6 @@ inline void VertexerTraits::initialise(const TrackingParameters& trackingParams) setIsGPU(false); } -inline void VertexerTraits::updateVertexingParameters(const VertexingParameters& vrtPar) -{ - mVrtParams = vrtPar; - mIndexTableUtils.setTrackingParameters(vrtPar); - mVrtParams.phiSpan = static_cast(std::ceil(mIndexTableUtils.getNphiBins() * mVrtParams.phiCut / - constants::math::TwoPi)); - mVrtParams.zSpan = static_cast(std::ceil(mVrtParams.zCut * mIndexTableUtils.getInverseZCoordinate(0))); -} - GPUhdi() const int2 VertexerTraits::getPhiBins(float phi, float dPhi) { return VertexerTraits::getPhiBins(phi, dPhi, mIndexTableUtils); diff --git a/Detectors/ITSMFT/ITS/tracking/src/VertexerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/VertexerTraits.cxx index 967daef6f5e63..859414545a15a 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/VertexerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/VertexerTraits.cxx @@ -24,8 +24,6 @@ #include #endif -// #define VTX_DEBUG - namespace o2 { namespace its @@ -133,6 +131,15 @@ const std::vector> VertexerTraits::selectClusters(const int* return filteredBins; } +void VertexerTraits::updateVertexingParameters(const VertexingParameters& vrtPar) +{ + mVrtParams = vrtPar; + mIndexTableUtils.setTrackingParameters(vrtPar); + mVrtParams.phiSpan = static_cast(std::ceil(mIndexTableUtils.getNphiBins() * mVrtParams.phiCut / + constants::math::TwoPi)); + mVrtParams.zSpan = static_cast(std::ceil(mVrtParams.zCut * mIndexTableUtils.getInverseZCoordinate(0))); +} + void VertexerTraits::computeTracklets() { for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) { From a186688b195a770c1b1d5602ff588f352850795e Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Wed, 27 Jul 2022 19:26:09 +0200 Subject: [PATCH 29/30] Fix GPU vertexer --- .../ITSMFT/ITS/macros/test/CompareArtefacts.C | 89 +++++++++++++-- .../GPU/ITStrackingGPU/TimeFrameGPU.h | 7 ++ .../tracking/GPU/cuda/VertexerTraitsGPU.cu | 103 +++++++++++------- .../include/ITStracking/Definitions.h | 2 +- .../tracking/include/ITStracking/Tracklet.h | 2 +- 5 files changed, 154 insertions(+), 49 deletions(-) diff --git a/Detectors/ITSMFT/ITS/macros/test/CompareArtefacts.C b/Detectors/ITSMFT/ITS/macros/test/CompareArtefacts.C index 34da66d3890f6..26f0b718774c9 100644 --- a/Detectors/ITSMFT/ITS/macros/test/CompareArtefacts.C +++ b/Detectors/ITSMFT/ITS/macros/test/CompareArtefacts.C @@ -22,8 +22,8 @@ void CompareArtefacts(const string cpu_file = "artefacts_tf.root", const string auto f_cpu = TFile::Open(cpu_file.data(), "r"); auto f_gpu = TFile::Open(gpu_file.data(), "r"); - auto tree_cpu_tracklets0 = (TTree*)f_cpu->Get("tracklets"); - auto tree_gpu_tracklets0 = (TTree*)f_gpu->Get("tracklets"); + auto tree_cpu_tracklets = (TTree*)f_cpu->Get("tracklets"); + auto tree_gpu_tracklets = (TTree*)f_gpu->Get("tracklets"); auto hist_cpu_tracklets0_phi = new TH1F("hist_cpu_tracklets0_phi", "hist_cpu_tracklets0_phi", 100, -TMath::Pi() - 1, TMath::Pi() + 1); auto hist_gpu_tracklets0_phi = new TH1F("hist_gpu_tracklets0_phi", "hist_gpu_tracklets0_phi", 100, -TMath::Pi() - 1, TMath::Pi() + 1); @@ -32,6 +32,13 @@ void CompareArtefacts(const string cpu_file = "artefacts_tf.root", const string auto hist_cpu_tracklets0_firstClusterIndex = new TH1F("hist_cpu_tracklets0_firstClusterIndex", "hist_cpu_tracklets0_firstClusterIndex", 200, 0, 600); auto hist_gpu_tracklets0_firstClusterIndex = new TH1F("hist_gpu_tracklets0_firstClusterIndex", "hist_gpu_tracklets0_firstClusterIndex", 200, 0, 600); + auto hist_cpu_tracklets1_phi = new TH1F("hist_cpu_tracklets1_phi", "hist_cpu_tracklets1_phi", 100, -TMath::Pi() - 1, TMath::Pi() + 1); + auto hist_gpu_tracklets1_phi = new TH1F("hist_gpu_tracklets1_phi", "hist_gpu_tracklets1_phi", 100, -TMath::Pi() - 1, TMath::Pi() + 1); + auto hist_cpu_tracklets1_tanL = new TH1F("hist_cpu_tracklets1_tanL", "hist_cpu_tracklets1_tanL", 100, -80, 80); + auto hist_gpu_tracklets1_tanL = new TH1F("hist_gpu_tracklets1_tanL", "hist_gpu_tracklets1_tanL", 100, -80, 80); + auto hist_cpu_tracklets1_firstClusterIndex = new TH1F("hist_cpu_tracklets1_firstClusterIndex", "hist_cpu_tracklets1_firstClusterIndex", 200, 0, 600); + auto hist_gpu_tracklets1_firstClusterIndex = new TH1F("hist_gpu_tracklets1_firstClusterIndex", "hist_gpu_tracklets1_firstClusterIndex", 200, 0, 600); + hist_cpu_tracklets0_phi->SetLineColor(kRed); hist_gpu_tracklets0_phi->SetLineColor(kBlue); hist_cpu_tracklets0_tanL->SetLineColor(kRed); @@ -39,11 +46,18 @@ void CompareArtefacts(const string cpu_file = "artefacts_tf.root", const string hist_cpu_tracklets0_firstClusterIndex->SetLineColor(kRed); hist_gpu_tracklets0_firstClusterIndex->SetLineColor(kBlue); + hist_cpu_tracklets1_phi->SetLineColor(kRed); + hist_gpu_tracklets1_phi->SetLineColor(kBlue); + hist_cpu_tracklets1_tanL->SetLineColor(kRed); + hist_gpu_tracklets1_tanL->SetLineColor(kBlue); + hist_cpu_tracklets1_firstClusterIndex->SetLineColor(kRed); + hist_gpu_tracklets1_firstClusterIndex->SetLineColor(kBlue); + auto c1 = new TCanvas("c1", "c1", 800, 800); c1->cd(); - tree_cpu_tracklets0->Draw("Tracklets0.phi >> hist_cpu_tracklets0_phi"); - tree_gpu_tracklets0->Draw("Tracklets0.phi >> hist_gpu_tracklets0_phi"); + tree_cpu_tracklets->Draw("Tracklets0.phi >> hist_cpu_tracklets0_phi"); + tree_gpu_tracklets->Draw("Tracklets0.phi >> hist_gpu_tracklets0_phi"); hist_gpu_tracklets0_phi->Draw(); hist_cpu_tracklets0_phi->Draw("same"); @@ -59,8 +73,8 @@ void CompareArtefacts(const string cpu_file = "artefacts_tf.root", const string auto c2 = new TCanvas("c2", "c2", 800, 800); c2->cd(); - tree_cpu_tracklets0->Draw("Tracklets0.tanLambda >> hist_cpu_tracklets0_tanL"); - tree_gpu_tracklets0->Draw("Tracklets0.tanLambda >> hist_gpu_tracklets0_tanL"); + tree_cpu_tracklets->Draw("Tracklets0.tanLambda >> hist_cpu_tracklets0_tanL"); + tree_gpu_tracklets->Draw("Tracklets0.tanLambda >> hist_gpu_tracklets0_tanL"); hist_gpu_tracklets0_tanL->Draw(); hist_cpu_tracklets0_tanL->Draw("same"); @@ -76,11 +90,15 @@ void CompareArtefacts(const string cpu_file = "artefacts_tf.root", const string auto c3 = new TCanvas("c3", "c3", 800, 800); c3->cd(); - tree_cpu_tracklets0->Draw("Tracklets0.firstClusterIndex >> hist_cpu_tracklets0_firstClusterIndex"); - tree_gpu_tracklets0->Draw("Tracklets0.firstClusterIndex >> hist_gpu_tracklets0_firstClusterIndex"); + tree_cpu_tracklets->Draw("Tracklets0.firstClusterIndex >> hist_cpu_tracklets0_firstClusterIndex"); + tree_gpu_tracklets->Draw("Tracklets0.firstClusterIndex >> hist_gpu_tracklets0_firstClusterIndex"); + auto hist_cpu_tracklets0_firstClusterIndex_clone = (TH1F*)hist_cpu_tracklets0_firstClusterIndex->Clone("hist_cpu_tracklets0_firstClusterIndex_clone"); + hist_cpu_tracklets0_firstClusterIndex_clone->Add(hist_gpu_tracklets0_firstClusterIndex, -1); + hist_cpu_tracklets0_firstClusterIndex_clone->SetLineColor(kBlack); hist_gpu_tracklets0_firstClusterIndex->Draw(); hist_cpu_tracklets0_firstClusterIndex->Draw("same"); + hist_cpu_tracklets0_firstClusterIndex_clone->Draw("same"); auto legend3 = new TLegend(0.4, 0.4, 0.2, 0.2); legend3->SetHeader("Tracklets0 first cluster index", "C"); @@ -88,4 +106,59 @@ void CompareArtefacts(const string cpu_file = "artefacts_tf.root", const string legend3->AddEntry(hist_gpu_tracklets0_firstClusterIndex, Form("GPU: %1.f", hist_gpu_tracklets0_firstClusterIndex->GetEntries()), "l"); legend3->Draw(); + + // // Tracklets1 + auto c4 = new TCanvas("c4", "c4", 800, 800); + c4->cd(); + + tree_cpu_tracklets->Draw("Tracklets1.phi >> hist_cpu_tracklets1_phi"); + tree_gpu_tracklets->Draw("Tracklets1.phi >> hist_gpu_tracklets1_phi"); + + hist_cpu_tracklets1_phi->Draw(); + hist_gpu_tracklets1_phi->Draw("same"); + + auto legend4 = new TLegend(0.4, 0.4, 0.2, 0.2); + legend4->SetHeader("Tracklets1 #varphi", "C"); + legend4->AddEntry(hist_cpu_tracklets1_phi, Form("CPU: %1.f", hist_cpu_tracklets1_phi->GetEntries()), "l"); + legend4->AddEntry(hist_gpu_tracklets1_phi, Form("GPU: %1.f", hist_gpu_tracklets1_phi->GetEntries()), "l"); + + legend4->Draw(); + + // // Tan(L) + auto c5 = new TCanvas("c5", "c5", 800, 800); + c5->cd(); + + tree_cpu_tracklets->Draw("Tracklets1.tanLambda >> hist_cpu_tracklets1_tanL"); + tree_gpu_tracklets->Draw("Tracklets1.tanLambda >> hist_gpu_tracklets1_tanL"); + + hist_cpu_tracklets1_tanL->Draw(); + hist_gpu_tracklets1_tanL->Draw("same"); + + auto legend5 = new TLegend(0.4, 0.4, 0.2, 0.2); + legend5->SetHeader("Tracklets1 tan(#lambda)", "C"); + legend5->AddEntry(hist_cpu_tracklets1_tanL, Form("CPU: %1.f", hist_cpu_tracklets1_tanL->GetEntries()), "l"); + legend5->AddEntry(hist_gpu_tracklets1_tanL, Form("GPU: %1.f", hist_gpu_tracklets1_tanL->GetEntries()), "l"); + + legend5->Draw(); + + // first cluster index + auto c6 = new TCanvas("c6", "c6", 800, 800); + c6->cd(); + + tree_cpu_tracklets->Draw("Tracklets1.firstClusterIndex >> hist_cpu_tracklets1_firstClusterIndex"); + tree_gpu_tracklets->Draw("Tracklets1.firstClusterIndex >> hist_gpu_tracklets1_firstClusterIndex"); + auto hist_cpu_tracklets1_firstClusterIndex_clone = (TH1F*)hist_cpu_tracklets1_firstClusterIndex->Clone("hist_cpu_tracklets1_firstClusterIndex_clone"); + hist_cpu_tracklets1_firstClusterIndex_clone->Add(hist_gpu_tracklets1_firstClusterIndex, -1); + hist_cpu_tracklets1_firstClusterIndex_clone->SetLineColor(kBlack); + + hist_cpu_tracklets1_firstClusterIndex->Draw(); + hist_gpu_tracklets1_firstClusterIndex->Draw("same"); + hist_cpu_tracklets1_firstClusterIndex_clone->Draw("same"); + auto legend6 = new TLegend(0.4, 0.4, 0.2, 0.2); + + legend6->SetHeader("Tracklets1 first cluster index", "C"); + legend6->AddEntry(hist_cpu_tracklets1_firstClusterIndex, Form("CPU: %1.f", hist_cpu_tracklets1_firstClusterIndex->GetEntries()), "l"); + legend6->AddEntry(hist_gpu_tracklets1_firstClusterIndex, Form("GPU: %1.f", hist_gpu_tracklets1_firstClusterIndex->GetEntries()), "l"); + + legend6->Draw(); } \ No newline at end of file diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h index 1ecb35f089c92..60ed5cab65ef7 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TimeFrameGPU.h @@ -115,6 +115,7 @@ class TimeFrameGPU : public TimeFrame int* getDeviceIndexTableAtRof(const int layerId, const int rofId) { return mIndexTablesD[layerId].get() + rofId * (ZBins * PhiBins + 1); } unsigned char* getDeviceUsedTracklets(const int rofId); Line* getDeviceLines(const int rofId); + Tracklet* getDeviceTrackletsVertexerOnly(const int rofId, const int layerId); // this method uses the cluster table for layer 1 for any layer. It is used for the vertexer only. Tracklet* getDeviceTracklets(const int rofId, const int layerId); Tracklet* getDeviceTrackletsAll(const int layerId); Cell* getDeviceCells(const int layerId); @@ -219,6 +220,12 @@ inline Line* TimeFrameGPU::getDeviceLines(const int rofId) return getPtrFromRuler(rofId, mLines.get(), mROframesClusters[1].data()); } +template +inline Tracklet* TimeFrameGPU::getDeviceTrackletsVertexerOnly(const int rofId, const int layerId) +{ + return getPtrFromRuler(rofId, mTrackletsD[layerId].get(), mROframesClusters[1].data(), mConfig.maxTrackletsPerCluster); +} + template inline Tracklet* TimeFrameGPU::getDeviceTracklets(const int rofId, const int layerId) { diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu index 53a34bcb2e5d9..92d49c53e2ea7 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/VertexerTraitsGPU.cu @@ -132,6 +132,24 @@ GPUg() void printBufferOnThreadF(const float* v, size_t size, const unsigned int } } +GPUg() void resetTrackletsKernel(Tracklet* tracklets, const int nTracklets) +{ + for (int iCurrentLayerClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; iCurrentLayerClusterIndex < nTracklets; iCurrentLayerClusterIndex += blockDim.x * gridDim.x) { + new (tracklets + iCurrentLayerClusterIndex) Tracklet{}; + } +} + +GPUg() void dumpFoundTrackletsKernel(const Tracklet* tracklets, const int* nTracklet, const size_t nClustersMiddleLayer, const int maxTrackletsPerCluster) +{ + for (int iCurrentLayerClusterIndex = blockIdx.x * blockDim.x + threadIdx.x; iCurrentLayerClusterIndex < nClustersMiddleLayer; iCurrentLayerClusterIndex += blockDim.x * gridDim.x) { + const int stride{iCurrentLayerClusterIndex * maxTrackletsPerCluster}; + for (int iTracklet{0}; iTracklet < nTracklet[iCurrentLayerClusterIndex]; ++iTracklet) { + auto& t = tracklets[stride + iTracklet]; + t.dump(); + } + } +} + GPUg() void dumpMaximaKernel(const cub::KeyValuePair* tmpVertexBins, const int threadId) { if (blockIdx.x * blockDim.x + threadIdx.x == threadId) { @@ -153,7 +171,8 @@ GPUg() void trackleterKernel( Tracklet* Tracklets, int* foundTracklets, const IndexTableUtils* utils, - const size_t maxTrackletsPerCluster = 10) + const int rofId, + const size_t maxTrackletsPerCluster = 1e2) { const int phiBins{utils->getNphiBins()}; const int zBins{utils->getNzBins()}; @@ -177,12 +196,12 @@ GPUg() void trackleterKernel( // loop on clusters next layer for (int iNextLayerClusterIndex{firstRowClusterIndex}; iNextLayerClusterIndex < maxRowClusterIndex && iNextLayerClusterIndex < sizeNextLClusters; ++iNextLayerClusterIndex) { const Cluster& nextCluster = clustersNextLayer[iNextLayerClusterIndex]; - if (o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi) < phiCut) { + if (o2::gpu::GPUCommonMath::Abs(currentCluster.phi - nextCluster.phi) < phiCut) { if (storedTracklets < maxTrackletsPerCluster) { if constexpr (Mode == TrackletMode::Layer0Layer1) { - new (Tracklets + stride + storedTracklets) Tracklet{iNextLayerClusterIndex, iCurrentLayerClusterIndex, nextCluster, currentCluster}; + new (Tracklets + stride + storedTracklets) Tracklet{iNextLayerClusterIndex, iCurrentLayerClusterIndex, nextCluster, currentCluster, rofId, rofId}; } else { - new (Tracklets + stride + storedTracklets) Tracklet{iCurrentLayerClusterIndex, iNextLayerClusterIndex, currentCluster, nextCluster}; + new (Tracklets + stride + storedTracklets) Tracklet{iCurrentLayerClusterIndex, iNextLayerClusterIndex, currentCluster, nextCluster, rofId, rofId}; } ++storedTracklets; } @@ -211,7 +230,7 @@ GPUg() void trackletSelectionKernel( Line* lines, int* nFoundLines, int* nExclusiveFoundLines, - const int maxTrackletsPerCluster = 10, + const int maxTrackletsPerCluster = 1e2, const float tanLambdaCut = 0.025f, const float phiCut = 0.002f) { @@ -404,9 +423,10 @@ void VertexerTraitsGPU::computeTracklets() mTimeFrameGPU->getNClustersLayer(rofId, 1), mTimeFrameGPU->getDeviceIndexTableAtRof(0, rofId), mVrtParams.phiCut, - mTimeFrameGPU->getDeviceTracklets(rofId, 0), + mTimeFrameGPU->getDeviceTrackletsVertexerOnly(rofId, 0), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 0), mDeviceIndexTableUtils, + rofId, mVrtParams.maxTrackletsPerCluster); gpu::trackleterKernel<<getStream(1).get()>>>( mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 2), @@ -415,9 +435,10 @@ void VertexerTraitsGPU::computeTracklets() mTimeFrameGPU->getNClustersLayer(rofId, 1), mTimeFrameGPU->getDeviceIndexTableAtRof(2, rofId), mVrtParams.phiCut, - mTimeFrameGPU->getDeviceTracklets(rofId, 1), + mTimeFrameGPU->getDeviceTrackletsVertexerOnly(rofId, 1), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 1), mDeviceIndexTableUtils, + rofId, mVrtParams.maxTrackletsPerCluster); } #ifdef VTX_DEBUG @@ -426,10 +447,18 @@ void VertexerTraitsGPU::computeTracklets() std::vector> NtrackletsClusters01(mTimeFrameGPU->getNrof()); std::vector> NtrackletsClusters12(mTimeFrameGPU->getNrof()); for (int iRof{0}; iRof < mTimeFrameGPU->getNrof(); ++iRof) { - NtrackletsClusters01[iRof].resize(mTimeFrameGPU->getClustersOnLayer(iRof, 1).size()); - NtrackletsClusters12[iRof].resize(mTimeFrameGPU->getClustersOnLayer(iRof, 1).size()); - checkGPUError(cudaMemcpy(NtrackletsClusters01[iRof].data(), mTimeFrameGPU->getDeviceNTrackletsCluster(iRof, 0), sizeof(int) * mTimeFrameGPU->getClustersOnLayer(iRof, 1).size(), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - checkGPUError(cudaMemcpy(NtrackletsClusters12[iRof].data(), mTimeFrameGPU->getDeviceNTrackletsCluster(iRof, 1), sizeof(int) * mTimeFrameGPU->getClustersOnLayer(iRof, 1).size(), cudaMemcpyDeviceToHost), __FILE__, __LINE__); + NtrackletsClusters01[iRof].resize(mTimeFrameGPU->getNClustersLayer(iRof, 1)); + NtrackletsClusters12[iRof].resize(mTimeFrameGPU->getNClustersLayer(iRof, 1)); + checkGPUError(cudaMemcpy(NtrackletsClusters01[iRof].data(), + mTimeFrameGPU->getDeviceNTrackletsCluster(iRof, 0), + sizeof(int) * mTimeFrameGPU->getNClustersLayer(iRof, 1), + cudaMemcpyDeviceToHost), + __FILE__, __LINE__); + checkGPUError(cudaMemcpy(NtrackletsClusters12[iRof].data(), + mTimeFrameGPU->getDeviceNTrackletsCluster(iRof, 1), + sizeof(int) * mTimeFrameGPU->getNClustersLayer(iRof, 1), + cudaMemcpyDeviceToHost), + __FILE__, __LINE__); std::copy(NtrackletsClusters01[iRof].begin(), NtrackletsClusters01[iRof].end(), std::ostream_iterator(out01, "\t")); std::copy(NtrackletsClusters12[iRof].begin(), NtrackletsClusters12[iRof].end(), std::ostream_iterator(out12, "\t")); @@ -441,37 +470,33 @@ void VertexerTraitsGPU::computeTracklets() // Dump lines on root file TFile* trackletFile = TFile::Open("artefacts_tf_gpu.root", "recreate"); TTree* tr_tre = new TTree("tracklets", "tf"); - std::vector tracklets_vec01(0); - std::vector tracklets_vec12(0); - std::vector tracklets_clean01(0); - std::vector tracklets_clean12(0); - tr_tre->Branch("Tracklets0", &tracklets_clean01); - tr_tre->Branch("Tracklets1", &tracklets_clean12); + std::vector tracklets01; + std::vector tracklets12; + tr_tre->Branch("Tracklets0", &tracklets01); + tr_tre->Branch("Tracklets1", &tracklets12); for (int iRof{0}; iRof < mTimeFrameGPU->getNrof(); ++iRof) { - tracklets_clean01.clear(); - tracklets_clean12.clear(); - tracklets_vec01.resize(mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * mVrtParams.maxTrackletsPerCluster); // Nclusters * TrackletsPerCluster - tracklets_vec12.resize(mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * mVrtParams.maxTrackletsPerCluster); - checkGPUError(cudaMemcpy(tracklets_vec01.data(), - mTimeFrameGPU->getDeviceTracklets(iRof, 0), - mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * sizeof(Tracklet) * mVrtParams.maxTrackletsPerCluster, + tracklets01.clear(); + tracklets12.clear(); + std::vector rawTracklets0(mTimeFrameGPU->getNClustersLayer(iRof, 1) * mVrtParams.maxTrackletsPerCluster); + std::vector rawTracklets1(mTimeFrameGPU->getNClustersLayer(iRof, 1) * mVrtParams.maxTrackletsPerCluster); + checkGPUError(cudaMemcpy(rawTracklets0.data(), + mTimeFrameGPU->getDeviceTrackletsVertexerOnly(iRof, 0), + mTimeFrameGPU->getNClustersLayer(iRof, 1) * mVrtParams.maxTrackletsPerCluster * sizeof(o2::its::Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - checkGPUError(cudaMemcpy(tracklets_vec12.data(), - mTimeFrameGPU->getDeviceTracklets(iRof, 1), - mTimeFrameGPU->getClustersOnLayer(iRof, 1).size() * sizeof(Tracklet) * mVrtParams.maxTrackletsPerCluster, + checkGPUError(cudaMemcpy(rawTracklets1.data(), + mTimeFrameGPU->getDeviceTrackletsVertexerOnly(iRof, 1), + mTimeFrameGPU->getNClustersLayer(iRof, 1) * mVrtParams.maxTrackletsPerCluster * sizeof(o2::its::Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); - for (auto iCluster{0}; iCluster < NtrackletsClusters01[iRof].size(); ++iCluster) { - auto nTracklets{NtrackletsClusters01[iRof][iCluster]}; - for (auto iTracklet{0}; iTracklet < nTracklets; ++iTracklet) { - tracklets_clean01.push_back(tracklets_vec01[iCluster * mVrtParams.maxTrackletsPerCluster + iTracklet]); + for (int iCluster{0}; iCluster < mTimeFrameGPU->getNClustersLayer(iRof, 1); ++iCluster) { + for (int iTracklet{0}; iTracklet < NtrackletsClusters01[iRof][iCluster]; ++iTracklet) { + tracklets01.push_back(rawTracklets0[iCluster * mVrtParams.maxTrackletsPerCluster + iTracklet]); } } - for (auto iCluster{0}; iCluster < NtrackletsClusters12[iRof].size(); ++iCluster) { - auto nTracklets{NtrackletsClusters12[iRof][iCluster]}; - for (auto iTracklet{0}; iTracklet < nTracklets; ++iTracklet) { - tracklets_clean12.push_back(tracklets_vec12[iCluster * mVrtParams.maxTrackletsPerCluster + iTracklet]); + for (int iCluster{0}; iCluster < mTimeFrameGPU->getNClustersLayer(iRof, 1); ++iCluster) { + for (int iTracklet{0}; iTracklet < NtrackletsClusters12[iRof][iCluster]; ++iTracklet) { + tracklets12.push_back(rawTracklets1[iCluster * mVrtParams.maxTrackletsPerCluster + iTracklet]); } } tr_tre->Fill(); @@ -500,8 +525,8 @@ void VertexerTraitsGPU::computeTrackletMatching() mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 0), mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 1), mTimeFrameGPU->getNClustersLayer(rofId, 1), - mTimeFrameGPU->getDeviceTracklets(rofId, 0), - mTimeFrameGPU->getDeviceTracklets(rofId, 1), + mTimeFrameGPU->getDeviceTrackletsVertexerOnly(rofId, 0), + mTimeFrameGPU->getDeviceTrackletsVertexerOnly(rofId, 1), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 0), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 1), mTimeFrameGPU->getDeviceUsedTracklets(rofId), @@ -524,8 +549,8 @@ void VertexerTraitsGPU::computeTrackletMatching() mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 0), mTimeFrameGPU->getDeviceClustersOnLayer(rofId, 1), mTimeFrameGPU->getNClustersLayer(rofId, 1), - mTimeFrameGPU->getDeviceTracklets(rofId, 0), - mTimeFrameGPU->getDeviceTracklets(rofId, 1), + mTimeFrameGPU->getDeviceTrackletsVertexerOnly(rofId, 0), + mTimeFrameGPU->getDeviceTrackletsVertexerOnly(rofId, 1), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 0), mTimeFrameGPU->getDeviceNTrackletsCluster(rofId, 1), mTimeFrameGPU->getDeviceUsedTracklets(rofId), diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h index d75e4cad86eb5..de67d50344c62 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h @@ -16,7 +16,7 @@ #define TRACKINGITS_DEFINITIONS_H_ // #define CA_DEBUG -#define VTX_DEBUG +// #define VTX_DEBUG template void discardResult(const T&) diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h index 33656ef71a4bd..3508c541caa1c 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracklet.h @@ -34,7 +34,7 @@ struct Tracklet final { GPUhdi() bool operator!=(const Tracklet&) const; GPUhdi() unsigned char isEmpty() const { - return firstClusterIndex < 0 || secondClusterIndex < 0 && !tanLambda && !phi; + return firstClusterIndex < 0 || secondClusterIndex < 0; } GPUhdi() void dump(); GPUhdi() void dump() const; From f07cc5bab91b2cfd03c7f1860467ce85083068e7 Mon Sep 17 00:00:00 2001 From: Matteo Concas Date: Thu, 28 Jul 2022 17:49:44 +0200 Subject: [PATCH 30/30] Ultimate rebase --- .../GPU/ITStrackingGPU/TrackerTraitsGPU.h | 7 +++- .../ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu | 33 +++++++++++++++---- .../tracking/include/ITStracking/Tracker.h | 10 ------ .../include/ITStracking/TrackerTraits.h | 32 ++++++++---------- Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx | 5 ++- .../ITSMFT/ITS/tracking/src/TrackerTraits.cxx | 5 +++ 6 files changed, 53 insertions(+), 39 deletions(-) diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h index ee9c2303f1426..1f0e198836188 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h +++ b/Detectors/ITSMFT/ITS/tracking/GPU/ITStrackingGPU/TrackerTraitsGPU.h @@ -32,9 +32,14 @@ class TrackerTraitsGPU : public TrackerTraits // void computeLayerCells() final; void adoptTimeFrame(TimeFrame* tf) override; - void initialiseTimeFrame(const int iteration, const TrackingParameters& trackingParams) override; + void initialiseTimeFrame(const int iteration) override; void computeLayerTracklets(const int iteration) final; void computeLayerCells(const int iteration) override; + void setBz(float) override; + void findCellsNeighbours(const int iteration) override; + void findRoads(const int iteration) override; + void findTracks() override; + void extendTracks(const int iteration) override; // void refitTracks(const std::vector>& tf, std::vector& tracks) override; // TimeFrameGPU information forwarding diff --git a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu index 406e7619d7f45..8132e96399271 100644 --- a/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu +++ b/Detectors/ITSMFT/ITS/tracking/GPU/cuda/TrackerTraitsGPU.cu @@ -249,21 +249,21 @@ GPUg() void computeLayerCellsKernel( } // namespace gpu template -void TrackerTraitsGPU::initialiseTimeFrame(const int iteration, const TrackingParameters& trackingParams) +void TrackerTraitsGPU::initialiseTimeFrame(const int iteration) { - mTimeFrameGPU->initialise(iteration, trackingParams, NLayers); + mTimeFrameGPU->initialise(iteration, mTrkParams[iteration], NLayers); setIsGPU(true); } template void TrackerTraitsGPU::computeLayerTracklets(const int iteration) { - const Vertex diamondVert({mTrkParams.Diamond[0], mTrkParams.Diamond[1], mTrkParams.Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); + const Vertex diamondVert({mTrkParams[iteration].Diamond[0], mTrkParams[iteration].Diamond[1], mTrkParams[iteration].Diamond[2]}, {25.e-6f, 0.f, 0.f, 25.e-6f, 0.f, 36.f}, 1, 1.f); gsl::span diamondSpan(&diamondVert, 1); size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; for (int rof0{0}; rof0 < mTimeFrameGPU->getNrof(); ++rof0) { - gsl::span primaryVertices = mTrkParams.UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one + gsl::span primaryVertices = mTrkParams[iteration].UseDiamond ? diamondSpan : mTimeFrameGPU->getPrimaryVertices(rof0); // replace with GPU one std::vector paddedVertices; for (int iVertex{0}; iVertex < mTimeFrameGPU->getConfig().maxVerticesCapacity; ++iVertex) { if (iVertex < primaryVertices.size()) { @@ -276,7 +276,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) for (int iLayer{0}; iLayer < NLayers - 1; ++iLayer) { const dim3 threadsPerBlock{gpu::utils::host::getBlockSize(mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; const dim3 blocksGrid{gpu::utils::host::getBlocksGrid(threadsPerBlock, mTimeFrameGPU->getNClustersLayer(rof0, iLayer))}; - const float meanDeltaR{mTrkParams.LayerRadii[iLayer + 1] - mTrkParams.LayerRadii[iLayer]}; + const float meanDeltaR{mTrkParams[iteration].LayerRadii[iLayer + 1] - mTrkParams[iteration].LayerRadii[iLayer]}; if (!mTimeFrameGPU->getClustersOnLayer(rof0, iLayer).size()) { LOGP(info, "Skipping ROF0: {}, no clusters found on layer {}", rof0, iLayer); @@ -370,7 +370,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) // Create tracklets labels, at the moment on the host if (mTimeFrameGPU->hasMCinformation()) { - for (int iLayer{0}; iLayer < mTrkParams.TrackletsPerRoad(); ++iLayer) { + for (int iLayer{0}; iLayer < mTrkParams[iteration].TrackletsPerRoad(); ++iLayer) { std::vector tracklets(mTimeFrameGPU->getTrackletSizeHost()[iLayer]); checkGPUError(cudaMemcpy(tracklets.data(), mTimeFrameGPU->getDeviceTrackletsAll(iLayer), mTimeFrameGPU->getTrackletSizeHost()[iLayer] * sizeof(o2::its::Tracklet), cudaMemcpyDeviceToHost), __FILE__, __LINE__); for (auto& trk : tracklets) { @@ -395,7 +395,7 @@ void TrackerTraitsGPU::computeLayerTracklets(const int iteration) } template -void TrackerTraitsGPU::computeLayerCells() +void TrackerTraitsGPU::computeLayerCells(const int iteration) { size_t bufferSize = mTimeFrameGPU->getConfig().tmpCUBBufferSize; for (int iLayer{0}; iLayer < NLayers - 2; ++iLayer) { @@ -471,6 +471,25 @@ void TrackerTraitsGPU::computeLayerCells() // // mChainRunITSTrackFit(*mChain, mPrimaryVertexContext->getRoads(), clusters, cells, tf, tracks); // } +template +void TrackerTraitsGPU::findCellsNeighbours(const int iteration){}; + +template +void TrackerTraitsGPU::findRoads(const int iteration){}; + +template +void TrackerTraitsGPU::findTracks(){}; + +template +void TrackerTraitsGPU::extendTracks(const int iteration){}; + +template +void TrackerTraitsGPU::setBz(float bz) +{ + mBz = bz; + mTimeFrameGPU->setBz(bz); +} + template int TrackerTraitsGPU::getTFNumberOfClusters() const { diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracker.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracker.h index 7fb0816d180e7..7f08904c2d231 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracker.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/Tracker.h @@ -105,16 +105,6 @@ inline void Tracker::setParameters(const std::vector& trkPar mTrkParams = trkPars; } -inline void Tracker::initialiseTimeFrame(int& iteration) -{ - mTimeFrame->initialise(iteration, mTrkParams[iteration]); -} - -inline float Tracker::getBz() const -{ - return mBz; -} - template float Tracker::evaluateTask(void (Tracker::*task)(T...), const char* taskName, std::function logger, T&&... args) diff --git a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h index 3ad2547f45eba..fb0dfa198bbd9 100644 --- a/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h +++ b/Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h @@ -52,7 +52,8 @@ class TrackerTraits { public: virtual ~TrackerTraits() = default; - virtual void initialiseTimeFrame(const int iteration, const TrackingParameters& trackingParams); + virtual void adoptTimeFrame(TimeFrame* tf); + virtual void initialiseTimeFrame(const int iteration); virtual void computeLayerTracklets(const int iteration); virtual void computeLayerCells(const int iteration); virtual void findCellsNeighbours(const int iteration); @@ -61,12 +62,13 @@ class TrackerTraits virtual void extendTracks(const int iteration); virtual void findShortPrimaries(); virtual void refitTracks(const int iteration, const std::vector>&, std::vector&); + virtual void setBz(float bz); virtual bool trackFollowing(TrackITSExt* track, int rof, bool outward, const int iteration); void UpdateTrackingParameters(const std::vector& trkPars); TimeFrame* getTimeFrame() { return mTimeFrame; } - void adoptTimeFrame(TimeFrame* tf) { mTimeFrame = tf; } - void setBz(float bz); + + void setIsGPU(const unsigned char isgpu) { mIsGPU = isgpu; }; float getBz() const; void setCorrType(const o2::base::PropagatorImpl::MatCorrType type) { mCorrType = type; } bool isMatLUT() const; @@ -86,6 +88,13 @@ class TrackerTraits void setNThreads(int n); int getNThreads() const { return mNThreads; } + // TimeFrame information forwarding + virtual int getTFNumberOfClusters() const; + virtual int getTFNumberOfTracklets() const; + virtual int getTFNumberOfCells() const; + + float mBz = 5.f; + private: void traverseCellsTree(const int, const int); track::TrackParCov buildTrackSeed(const Cluster& cluster1, const Cluster& cluster2, const Cluster& cluster3, const TrackingFrameInfo& tf3, float resolution); @@ -94,23 +103,12 @@ class TrackerTraits int mNThreads = 1; bool mApplySmoothing = false; o2::base::PropagatorImpl::MatCorrType mCorrType = o2::base::PropagatorImpl::MatCorrType::USEMatCorrNONE; - float mBz = 5.f; - void UpdateTrackingParameters(const TrackingParameters& trkPar); - TimeFrame* getTimeFrame() { return mTimeFrame; } - - void setIsGPU(const unsigned char isgpu) { mIsGPU = isgpu; }; - - // TimeFrame information forwarding - virtual int getTFNumberOfClusters() const; - virtual int getTFNumberOfTracklets() const; - virtual int getTFNumberOfCells() const; // virtual bool checkTFMemory(const int iteration); protected: TimeFrame* mTimeFrame; std::vector mTrkParams; - bool mIsGPU = false; o2::gpu::GPUChainITS* mChain = nullptr; FuncRunITSTrackFit_t mChainRunITSTrackFit; @@ -136,14 +134,12 @@ inline const int4 TrackerTraits::getBinsRect(const Cluster& currentCluster, int return getBinsRect(layerIndex, currentCluster.phi, maxdeltaphi, z1, z2, maxdeltaz); } -inline void TrackerTraits::initialiseTimeFrame(const int iteration, const TrackingParameters& trackingParams) +inline void TrackerTraits::initialiseTimeFrame(const int iteration) { - mTimeFrame->initialise(iteration, trackingParams, 7); + mTimeFrame->initialise(iteration, mTrkParams[iteration], 7); setIsGPU(false); } -inline void TrackerTraits::adoptTimeFrame(TimeFrame* tf) { mTimeFrame = tf; } - inline const int4 TrackerTraits::getBinsRect(const int layerIndex, float phi, float maxdeltaphi, float z1, float z2, float maxdeltaz) { diff --git a/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx b/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx index 3b84c3917fb6b..e21fd29b778c5 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/Tracker.cxx @@ -99,10 +99,9 @@ void Tracker::clustersToTracks(std::function logger, std::f mNumberOfRuns++; } -template -void Tracker::initialiseTimeFrame(T&&... args) +void Tracker::initialiseTimeFrame(int& iteration) { - mTraits->initialiseTimeFrame(std::forward(args)...); + mTraits->initialiseTimeFrame(iteration); } void Tracker::computeTracklets(int& iteration) diff --git a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx index 556df8e89f011..c805c9e4ba11f 100644 --- a/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx +++ b/Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx @@ -1012,6 +1012,11 @@ int TrackerTraits::getTFNumberOfCells() const return mTimeFrame->getNumberOfCells(); } +void TrackerTraits::adoptTimeFrame(TimeFrame* tf) +{ + mTimeFrame = tf; +} + // bool TrackerTraits::checkTFMemory(const int iteration) // { // return mTimeFrame->checkMemory(mTrkParams[iteration].MaxMemory);