forked from cms-patatrack/pixeltrack-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamCache.cc
43 lines (38 loc) · 1.38 KB
/
StreamCache.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
#include "CUDACore/StreamCache.h"
#include "CUDACore/cudaCheck.h"
#include "CUDACore/currentDevice.h"
#include "CUDACore/deviceCount.h"
#include "CUDACore/ScopedSetDevice.h"
namespace cms::cuda {
void StreamCache::Deleter::operator()(cudaStream_t stream) const {
if (device_ != -1) {
ScopedSetDevice deviceGuard{device_};
cudaCheck(cudaStreamDestroy(stream));
}
}
// StreamCache should be constructed by the first call to
// getStreamCache() only if we have CUDA devices present
StreamCache::StreamCache() : cache_(deviceCount()) {}
SharedStreamPtr StreamCache::get() {
const auto dev = currentDevice();
return cache_[dev].makeOrGet([dev]() {
cudaStream_t stream;
cudaCheck(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
return std::unique_ptr<BareStream, Deleter>(stream, Deleter{dev});
});
}
void StreamCache::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
// StreamCache lives through multiple tests (and go through
// multiple shutdowns of the framework).
cache_.clear();
cache_.resize(deviceCount());
}
StreamCache& getStreamCache() {
// the public interface is thread safe
static StreamCache cache;
return cache;
}
} // namespace cms::cuda