-
Notifications
You must be signed in to change notification settings - Fork 35
/
ScopedContext.h
239 lines (194 loc) · 9.75 KB
/
ScopedContext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifndef HeterogeneousCore_CUDACore_ScopedContext_h
#define HeterogeneousCore_CUDACore_ScopedContext_h
#include <optional>
#include "CUDACore/Product.h"
#include "Framework/WaitingTaskWithArenaHolder.h"
#include "Framework/Event.h"
#include "Framework/EDGetToken.h"
#include "Framework/EDPutToken.h"
#include "CUDACore/ContextState.h"
#include "CUDACore/EventCache.h"
#include "CUDACore/SharedEventPtr.h"
#include "CUDACore/SharedStreamPtr.h"
namespace cms {
namespace cudatest {
class TestScopedContext;
}
namespace cuda {
namespace impl {
// This class is intended to be derived by other ScopedContext*, not for general use
class ScopedContextBase {
public:
int device() const { return currentDevice_; }
// cudaStream_t is a pointer to a thread-safe object, for which a
// mutable access is needed even if the ScopedContext itself
// would be const. Therefore it is ok to return a non-const
// pointer from a const method here.
cudaStream_t stream() const { return stream_.get(); }
const SharedStreamPtr& streamPtr() const { return stream_; }
protected:
// The constructors set the current device, but the device
// is not set back to the previous value at the destructor. This
// should be sufficient (and tiny bit faster) as all CUDA API
// functions relying on the current device should be called from
// the scope where this context is. The current device doesn't
// really matter between modules (or across TBB tasks).
explicit ScopedContextBase(edm::StreamID streamID);
explicit ScopedContextBase(const ProductBase& data);
explicit ScopedContextBase(int device, SharedStreamPtr stream);
private:
int currentDevice_;
SharedStreamPtr stream_;
};
class ScopedContextGetterBase : public ScopedContextBase {
public:
template <typename T>
const T& get(const Product<T>& data) {
synchronizeStreams(data.device(), data.stream(), data.isAvailable(), data.event());
return data.data_;
}
template <typename T>
const T& get(const edm::Event& iEvent, edm::EDGetTokenT<Product<T>> token) {
return get(iEvent.get(token));
}
protected:
template <typename... Args>
ScopedContextGetterBase(Args&&... args) : ScopedContextBase(std::forward<Args>(args)...) {}
void synchronizeStreams(int dataDevice, cudaStream_t dataStream, bool available, cudaEvent_t dataEvent);
};
class ScopedContextHolderHelper {
public:
ScopedContextHolderHelper(edm::WaitingTaskWithArenaHolder waitingTaskHolder)
: waitingTaskHolder_{std::move(waitingTaskHolder)} {}
template <typename F>
void pushNextTask(F&& f, ContextState const* state);
void replaceWaitingTaskHolder(edm::WaitingTaskWithArenaHolder waitingTaskHolder) {
waitingTaskHolder_ = std::move(waitingTaskHolder);
}
void enqueueCallback(int device, cudaStream_t stream);
private:
edm::WaitingTaskWithArenaHolder waitingTaskHolder_;
};
} // namespace impl
/**
* The aim of this class is to do necessary per-event "initialization" in ExternalWork acquire():
* - setting the current device
* - calling edm::WaitingTaskWithArenaHolder::doneWaiting() when necessary
* - synchronizing between CUDA streams if necessary
* and enforce that those get done in a proper way in RAII fashion.
*/
class ScopedContextAcquire : public impl::ScopedContextGetterBase {
public:
/// Constructor to create a new CUDA stream (no need for context beyond acquire())
explicit ScopedContextAcquire(edm::StreamID streamID, edm::WaitingTaskWithArenaHolder waitingTaskHolder)
: ScopedContextGetterBase(streamID), holderHelper_{std::move(waitingTaskHolder)} {}
/// Constructor to create a new CUDA stream, and the context is needed after acquire()
explicit ScopedContextAcquire(edm::StreamID streamID,
edm::WaitingTaskWithArenaHolder waitingTaskHolder,
ContextState& state)
: ScopedContextGetterBase(streamID), holderHelper_{std::move(waitingTaskHolder)}, contextState_{&state} {}
/// Constructor to (possibly) re-use a CUDA stream (no need for context beyond acquire())
explicit ScopedContextAcquire(const ProductBase& data, edm::WaitingTaskWithArenaHolder waitingTaskHolder)
: ScopedContextGetterBase(data), holderHelper_{std::move(waitingTaskHolder)} {}
/// Constructor to (possibly) re-use a CUDA stream, and the context is needed after acquire()
explicit ScopedContextAcquire(const ProductBase& data,
edm::WaitingTaskWithArenaHolder waitingTaskHolder,
ContextState& state)
: ScopedContextGetterBase(data), holderHelper_{std::move(waitingTaskHolder)}, contextState_{&state} {}
~ScopedContextAcquire();
template <typename F>
void pushNextTask(F&& f) {
if (contextState_ == nullptr)
throwNoState();
holderHelper_.pushNextTask(std::forward<F>(f), contextState_);
}
void replaceWaitingTaskHolder(edm::WaitingTaskWithArenaHolder waitingTaskHolder) {
holderHelper_.replaceWaitingTaskHolder(std::move(waitingTaskHolder));
}
private:
void throwNoState();
impl::ScopedContextHolderHelper holderHelper_;
ContextState* contextState_ = nullptr;
};
/**
* The aim of this class is to do necessary per-event "initialization" in ExternalWork produce() or normal produce():
* - setting the current device
* - synchronizing between CUDA streams if necessary
* and enforce that those get done in a proper way in RAII fashion.
*/
class ScopedContextProduce : public impl::ScopedContextGetterBase {
public:
/// Constructor to create a new CUDA stream (non-ExternalWork module)
explicit ScopedContextProduce(edm::StreamID streamID) : ScopedContextGetterBase(streamID) {}
/// Constructor to (possibly) re-use a CUDA stream (non-ExternalWork module)
explicit ScopedContextProduce(const ProductBase& data) : ScopedContextGetterBase(data) {}
/// Constructor to re-use the CUDA stream of acquire() (ExternalWork module)
explicit ScopedContextProduce(ContextState& state)
: ScopedContextGetterBase(state.device(), state.releaseStreamPtr()) {}
/// Record the CUDA event, all asynchronous work must have been queued before the destructor
~ScopedContextProduce();
template <typename T>
std::unique_ptr<Product<T>> wrap(T data) {
// make_unique doesn't work because of private constructor
return std::unique_ptr<Product<T>>(new Product<T>(device(), streamPtr(), event_, std::move(data)));
}
template <typename T, typename... Args>
auto emplace(edm::Event& iEvent, edm::EDPutTokenT<T> token, Args&&... args) {
return iEvent.emplace(token, device(), streamPtr(), event_, std::forward<Args>(args)...);
}
private:
friend class cudatest::TestScopedContext;
// This construcor is only meant for testing
explicit ScopedContextProduce(int device, SharedStreamPtr stream, SharedEventPtr event)
: ScopedContextGetterBase(device, std::move(stream)), event_{std::move(event)} {}
// create the CUDA Event upfront to catch possible errors from its creation
SharedEventPtr event_ = getEventCache().get();
};
/**
* The aim of this class is to do necessary per-task "initialization" tasks created in ExternalWork acquire():
* - setting the current device
* - calling edm::WaitingTaskWithArenaHolder::doneWaiting() when necessary
* and enforce that those get done in a proper way in RAII fashion.
*/
class ScopedContextTask : public impl::ScopedContextBase {
public:
/// Constructor to re-use the CUDA stream of acquire() (ExternalWork module)
explicit ScopedContextTask(ContextState const* state, edm::WaitingTaskWithArenaHolder waitingTaskHolder)
: ScopedContextBase(state->device(), state->streamPtr()), // don't move, state is re-used afterwards
holderHelper_{std::move(waitingTaskHolder)},
contextState_{state} {}
~ScopedContextTask();
template <typename F>
void pushNextTask(F&& f) {
holderHelper_.pushNextTask(std::forward<F>(f), contextState_);
}
void replaceWaitingTaskHolder(edm::WaitingTaskWithArenaHolder waitingTaskHolder) {
holderHelper_.replaceWaitingTaskHolder(std::move(waitingTaskHolder));
}
private:
impl::ScopedContextHolderHelper holderHelper_;
ContextState const* contextState_;
};
/**
* The aim of this class is to do necessary per-event "initialization" in analyze()
* - setting the current device
* - synchronizing between CUDA streams if necessary
* and enforce that those get done in a proper way in RAII fashion.
*/
class ScopedContextAnalyze : public impl::ScopedContextGetterBase {
public:
/// Constructor to (possibly) re-use a CUDA stream
explicit ScopedContextAnalyze(const ProductBase& data) : ScopedContextGetterBase(data) {}
};
namespace impl {
template <typename F>
void ScopedContextHolderHelper::pushNextTask(F&& f, ContextState const* state) {
replaceWaitingTaskHolder(edm::WaitingTaskWithArenaHolder{edm::make_waiting_task_with_holder(
std::move(waitingTaskHolder_), [state, func = std::forward<F>(f)](edm::WaitingTaskWithArenaHolder h) {
func(ScopedContextTask{state, std::move(h)});
})});
}
} // namespace impl
} // namespace cuda
} // namespace cms
#endif