CMS 3D CMS Logo

EventCache.cc
Go to the documentation of this file.
8 
9 namespace cms::cuda {
10  void EventCache::Deleter::operator()(cudaEvent_t event) const {
11  if (device_ != -1) {
12  ScopedSetDevice deviceGuard{device_};
13  cudaCheck(cudaEventDestroy(event));
14  }
15  }
16 
17  // EventCache should be constructed by the first call to
18  // getEventCache() only if we have CUDA devices present
20 
22  const auto dev = currentDevice();
23  auto event = makeOrGet(dev);
24  // captured work has completed, or a just-created event
25  if (eventWorkHasCompleted(event.get())) {
26  return event;
27  }
28 
29  // Got an event with incomplete captured work. Try again until we
30  // get a completed (or a just-created) event. Need to keep all
31  // incomplete events until a completed event is found in order to
32  // avoid ping-pong with an incomplete event.
33  std::vector<SharedEventPtr> ptrs{std::move(event)};
34  bool completed;
35  do {
36  event = makeOrGet(dev);
37  completed = eventWorkHasCompleted(event.get());
38  if (not completed) {
39  ptrs.emplace_back(std::move(event));
40  }
41  } while (not completed);
42  return event;
43  }
44 
46  return cache_[dev].makeOrGet([dev]() {
47  cudaEvent_t event;
48  // it should be a bit faster to ignore timings
49  cudaCheck(cudaEventCreateWithFlags(&event, cudaEventDisableTiming));
50  return std::unique_ptr<BareEvent, Deleter>(event, Deleter{dev});
51  });
52  }
53 
55  // Reset the contents of the caches, but leave an
56  // edm::ReusableObjectHolder alive for each device. This is needed
57  // mostly for the unit tests, where the function-static
58  // EventCache lives through multiple tests (and go through
59  // multiple shutdowns of the framework).
60  cache_.clear();
61  cache_.resize(deviceCount());
62  }
63 
65  // the public interface is thread safe
67  return cache;
68  }
69 } // namespace cms::cuda
cms::cuda
Definition: Product.h:14
cms::cuda::SharedEventPtr
std::shared_ptr< std::remove_pointer_t< cudaEvent_t > > SharedEventPtr
Definition: SharedEventPtr.h:14
cms::cuda::EventCache
Definition: EventCache.h:15
cms::cuda::EventCache::makeOrGet
SharedEventPtr makeOrGet(int dev)
Definition: EventCache.cc:45
cms::cuda::EventCache::cache_
std::vector< edm::ReusableObjectHolder< BareEvent, Deleter > > cache_
Definition: EventCache.h:48
deviceCount.h
cms::cuda::EventCache::get
SharedEventPtr get()
Definition: EventCache.cc:21
cms::cuda::ScopedSetDevice
Definition: ScopedSetDevice.h:10
cms::cuda::EventCache::clear
void clear()
Definition: EventCache.cc:54
cms::cuda::EventCache::EventCache
EventCache()
Definition: EventCache.cc:19
CMS_THREAD_SAFE
#define CMS_THREAD_SAFE
Definition: thread_safety_macros.h:4
utilities.cache
def cache(function)
Definition: utilities.py:3
cms::cuda::currentDevice
int currentDevice()
Definition: currentDevice.h:10
cms::cuda::eventWorkHasCompleted
bool eventWorkHasCompleted(cudaEvent_t event)
Definition: eventWorkHasCompleted.h:18
thread_safety_macros.h
cms::cuda::deviceCount
int deviceCount()
Definition: deviceCount.h:10
edmPickEvents.event
event
Definition: edmPickEvents.py:273
cudaCheck.h
eventWorkHasCompleted.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
cudaCheck
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:62
EventCache.h
cms::cuda::EventCache::Deleter::device_
int device_
Definition: EventCache.h:45
currentDevice.h
ScopedSetDevice.h
event
Definition: event.py:1
cms::cuda::EventCache::Deleter
Definition: EventCache.h:38
cms::cuda::getEventCache
EventCache & getEventCache()
Definition: EventCache.cc:64
cms::cuda::EventCache::Deleter::operator()
void operator()(cudaEvent_t event) const
Definition: EventCache.cc:10