forked from cms-patatrack/pixeltrack-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventCache.cc
68 lines (61 loc) · 2.21 KB
/
EventCache.cc
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
#include "CUDACore/EventCache.h"
#include "CUDACore/cudaCheck.h"
#include "CUDACore/currentDevice.h"
#include "CUDACore/deviceCount.h"
#include "CUDACore/eventWorkHasCompleted.h"
#include "CUDACore/ScopedSetDevice.h"
namespace cms::cuda {
void EventCache::Deleter::operator()(cudaEvent_t event) const {
if (device_ != -1) {
ScopedSetDevice deviceGuard{device_};
cudaCheck(cudaEventDestroy(event));
}
}
// EventCache should be constructed by the first call to
// getEventCache() only if we have CUDA devices present
EventCache::EventCache() : cache_(deviceCount()) {}
SharedEventPtr EventCache::get() {
const auto dev = currentDevice();
auto event = makeOrGet(dev);
// captured work has completed, or a just-created event
if (eventWorkHasCompleted(event.get())) {
return event;
}
// Got an event with incomplete captured work. Try again until we
// get a completed (or a just-created) event. Need to keep all
// incomplete events until a completed event is found in order to
// avoid ping-pong with an incomplete event.
std::vector<SharedEventPtr> ptrs{std::move(event)};
bool completed;
do {
event = makeOrGet(dev);
completed = eventWorkHasCompleted(event.get());
if (not completed) {
ptrs.emplace_back(std::move(event));
}
} while (not completed);
return event;
}
SharedEventPtr EventCache::makeOrGet(int dev) {
return cache_[dev].makeOrGet([dev]() {
cudaEvent_t event;
// it should be a bit faster to ignore timings
cudaCheck(cudaEventCreateWithFlags(&event, cudaEventDisableTiming));
return std::unique_ptr<BareEvent, Deleter>(event, Deleter{dev});
});
}
void EventCache::clear() {
// Reset the contents of the caches, but leave an
// edm::ReusableObjectHolder alive for each device. This is needed
// mostly for the unit tests, where the function-static
// EventCache lives through multiple tests (and go through
// multiple shutdowns of the framework).
cache_.clear();
cache_.resize(deviceCount());
}
EventCache& getEventCache() {
// the public interface is thread safe
static EventCache cache;
return cache;
}
} // namespace cms::cuda