From 6654966d28313f9ecff49a09751eb84366492fcf Mon Sep 17 00:00:00 2001
From: Felix Weiglhofer <weiglhofer@fias.uni-frankfurt.de>
Date: Thu, 25 Aug 2022 12:19:18 +0000
Subject: [PATCH] GpuRecoSts: Small fixes.

---
 reco/detectors/sts/CbmRecoSts.cxx                 | 10 ++++++++++
 reco/detectors/sts/experimental/CbmGpuRecoSts.cxx |  7 ++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/reco/detectors/sts/CbmRecoSts.cxx b/reco/detectors/sts/CbmRecoSts.cxx
index 49be2bf19d..1405fbf130 100644
--- a/reco/detectors/sts/CbmRecoSts.cxx
+++ b/reco/detectors/sts/CbmRecoSts.cxx
@@ -33,6 +33,8 @@
 
 #include <iomanip>
 
+#include <xpu/host.h>
+
 #if __has_include(<omp.h>)
 #include <omp.h>
 #endif
@@ -334,6 +336,14 @@ InitStatus CbmRecoSts::Init()
   LOG(info) << "==========================================================";
   LOG(info) << GetName() << ": Initialising ";
 
+  // Initialize xpu.
+  // TODO: This call can be relatively expensive.
+  //       We need a way to ensure this happens only once at the beginning.
+  if (fUseGpuReco) {
+    setenv("XPU_PROFILE", "1", 1);  // Always enable profiling in xpu
+    xpu::initialize();
+  }
+
   // --- Check IO-Manager
   FairRootManager* ioman = FairRootManager::Instance();
   assert(ioman);
diff --git a/reco/detectors/sts/experimental/CbmGpuRecoSts.cxx b/reco/detectors/sts/experimental/CbmGpuRecoSts.cxx
index 923cb2ce6d..1001a5ba8b 100644
--- a/reco/detectors/sts/experimental/CbmGpuRecoSts.cxx
+++ b/reco/detectors/sts/experimental/CbmGpuRecoSts.cxx
@@ -190,9 +190,6 @@ void CbmGpuRecoSts::Setup(size_t maxDigisPerModule, size_t nDigitsTotal)
 
 void CbmGpuRecoSts::RunHitFinder()
 {
-  setenv("XPU_PROFILE", "1", 1);
-  xpu::initialize();
-
   auto& hfc = fHitfinderCpu;
 
   // ROCm bug: Mi100 name is an emtpy string...
@@ -368,6 +365,10 @@ void CbmGpuRecoSts::FetchDigis(size_t& maxDigisPerModule, size_t& nDigisTotal)
   CbmDigiManager* digis = CbmDigiManager::Instance();
   auto& hfc             = fHitfinderCpu;
 
+  // Remove digis from previous timeslice
+  fDigisByModuleB.clear();
+  fDigisByModuleF.clear();
+
   // FIXME: GPU reco should use regular digi class too
   nDigis      = digis->GetNofDigis(ECbmModuleId::kSts);
   nDigisTotal = 0;
-- 
GitLab