Skip to content

Commit 3a36b4f

Browse files
committed
GPU: Make GPUROOTDump thread-safe
1 parent 4eddbc9 commit 3a36b4f

3 files changed

Lines changed: 28 additions & 26 deletions

File tree

GPU/GPUTracking/Debug/GPUROOTDump.h

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ struct internal_Branch<TTree> {
5050
};
5151
} // namespace
5252

53+
template <typename... Args>
54+
class GPUROOTDump;
55+
5356
template <class T, typename... Args>
54-
class GPUROOTDump : public GPUROOTDump<Args...>
57+
class GPUROOTDump<T, Args...> : public GPUROOTDump<Args...>
5558
{
5659
public:
5760
template <typename... Names>
@@ -65,59 +68,53 @@ class GPUROOTDump : public GPUROOTDump<Args...>
6568
{
6669
return GPUROOTDump<T, Args...>(name1, names...);
6770
}
68-
void Fill(const T& o, Args... args)
71+
72+
void Fill(const T& o, const Args&... args)
73+
{
74+
stdspinlock spinlock(GPUROOTDumpBase::mMutex);
75+
FillInternal(o, args...);
76+
}
77+
78+
protected:
79+
void FillInternal(const T& o, const Args&... args)
6980
{
7081
mObj = o;
7182
GPUROOTDump<Args...>::Fill(args...);
7283
}
7384

74-
protected:
7585
using GPUROOTDump<Args...>::mTree;
7686
template <typename... Names>
7787
GPUROOTDump(const char* name1, Names... names) : GPUROOTDump<Args...>(names...)
7888
{
89+
stdspinlock spinlock(GPUROOTDumpBase::mMutex);
7990
mTree->Branch(name1, &mObj);
8091
}
8192

8293
private:
8394
T mObj;
8495
};
8596

86-
template <class T>
87-
class GPUROOTDump<T> : public GPUROOTDumpBase
97+
template <>
98+
class GPUROOTDump<> : public GPUROOTDumpBase
8899
{
89100
public:
90-
static GPUROOTDump<T>& get(const char* name) // return always the same instance, identified by template
91-
{
92-
static GPUROOTDump<T> instance(name);
93-
return instance;
94-
}
95-
static GPUROOTDump<T> getNew(const char* name) // return new individual instance
96-
{
97-
return GPUROOTDump<T>(name);
98-
}
99-
100101
void write() override { mTree->Write(); }
101102

102-
void Fill(const T& o)
103+
protected:
104+
void Fill()
103105
{
104-
mObj = o;
105106
mTree->Fill();
106107
}
107108

108-
protected:
109109
GPUROOTDump(const char* name1, const char* nameTree = nullptr)
110110
{
111111
if (nameTree == nullptr) {
112112
nameTree = name1;
113113
}
114+
stdspinlock spinlock(GPUROOTDumpBase::mMutex);
114115
mTree = new TTree(nameTree, nameTree);
115-
mTree->Branch(name1, &mObj);
116116
}
117117
TTree* mTree = nullptr;
118-
119-
private:
120-
T mObj;
121118
};
122119

123120
template <>
@@ -137,14 +134,16 @@ class GPUROOTDump<TNtuple> : public GPUROOTDumpBase
137134
void write() override { mNTuple->Write(); }
138135

139136
template <typename... Args>
140-
void Fill(Args... args)
137+
void Fill(const Args&... args)
141138
{
139+
stdspinlock spinlock(GPUROOTDumpBase::mMutex);
142140
mNTuple->Fill(args...);
143141
}
144142

145143
private:
146144
GPUROOTDump(const char* name, const char* options)
147145
{
146+
stdspinlock spinlock(GPUROOTDumpBase::mMutex);
148147
mNTuple = new TNtuple(name, name, options);
149148
}
150149
TNtuple* mNTuple;
@@ -155,18 +154,18 @@ class GPUROOTDump
155154
{
156155
public:
157156
template <typename... Names>
158-
GPUd() void Fill(Args... args) const
157+
GPUd() static void Fill(Args... args)
159158
{
160159
}
161160
template <typename... Names>
162161
GPUd() static GPUROOTDump<Args...>& get(Args... args)
163162
{
164-
return *(GPUROOTDump<Args...>*)(size_t)(1024); // Will never be used, return just some reference, which must not be nullptr by specification
163+
return GPUROOTDump<Args...>();
165164
}
166165
template <typename... Names>
167166
GPUd() static GPUROOTDump<Args...>& getNew(Args... args)
168167
{
169-
return *(GPUROOTDump<Args...>*)(size_t)(1024); // Will never be used, return just some reference, which must not be nullptr by specification
168+
return GPUROOTDump<Args...>();
170169
}
171170
};
172171
#endif

GPU/GPUTracking/Debug/GPUROOTDumpCore.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using namespace o2::gpu;
2323

2424
std::weak_ptr<GPUROOTDumpCore> GPUROOTDumpCore::sInstance;
25+
std::atomic_flag GPUROOTDumpBase::mMutex = ATOMIC_FLAG_INIT;
2526

2627
GPUROOTDumpCore::GPUROOTDumpCore(GPUROOTDumpCore::GPUROOTDumpCorePrivate)
2728
{

GPU/GPUTracking/Debug/GPUROOTDumpCore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define GPUROOTDUMPCORE_H
1717

1818
#include "GPUCommonDef.h"
19+
#include "utils/stdspinlock.h"
1920
#include <memory>
2021
#include <vector>
2122

@@ -32,6 +33,7 @@ class GPUROOTDumpBase
3233

3334
protected:
3435
GPUROOTDumpBase();
36+
static std::atomic_flag mMutex;
3537
std::weak_ptr<GPUROOTDumpCore> mCore;
3638
};
3739

0 commit comments

Comments
 (0)