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  // cudaEventBlockingSync is needed to let the thread calling
50  // cudaEventSynchronize() to sleep instead of spinning the CPU
51  cudaCheck(cudaEventCreateWithFlags(&event, cudaEventDisableTiming | cudaEventBlockingSync));
52  return std::unique_ptr<BareEvent, Deleter>(event, Deleter{dev});
53  });
54  }
55 
57  // Reset the contents of the caches, but leave an
58  // edm::ReusableObjectHolder alive for each device. This is needed
59  // mostly for the unit tests, where the function-static
60  // EventCache lives through multiple tests (and go through
61  // multiple shutdowns of the framework).
62  cache_.clear();
63  cache_.resize(deviceCount());
64  }
65 
67  // the public interface is thread safe
69  return cache;
70  }
71 } // namespace cms::cuda
int deviceCount()
Definition: deviceCount.h:10
SharedEventPtr makeOrGet(int dev)
Definition: EventCache.cc:45
std::shared_ptr< std::remove_pointer_t< cudaEvent_t > > SharedEventPtr
SharedEventPtr get()
Definition: EventCache.cc:21
std::vector< edm::ReusableObjectHolder< BareEvent, Deleter > > cache_
Definition: EventCache.h:48
#define CMS_THREAD_SAFE
void operator()(cudaEvent_t event) const
Definition: EventCache.cc:10
def cache(function)
Definition: utilities.py:3
EventCache & getEventCache()
Definition: EventCache.cc:66
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69
def move(src, dest)
Definition: eostools.py:511
bool eventWorkHasCompleted(cudaEvent_t event)
Definition: event.py:1
int currentDevice()
Definition: currentDevice.h:10