clDNN
event.hpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
18 #pragma once
19 #include "cldnn_defs.h"
20 #include "engine.hpp"
21 #include "profiling.hpp"
22 #include <algorithm>
23 #include <cassert>
24 
25 namespace cldnn
26 {
27 
30 
33 
35 struct event
36 {
38  static event create_user_event(const engine& engine)
39  {
40  return check_status<cldnn_event>("create user event failed", [&](status_t* status) { return cldnn_create_user_event(engine.get(), status); });
41  }
42 
44  event(cldnn_event impl) : _impl(impl)
45  {
46  if (_impl == nullptr) throw std::invalid_argument("implementation pointer should not be null");
47  }
48 
49  event(const event& other) : _impl(other._impl)
50  {
51  retain();
52  }
53 
54  event& operator=(const event& other)
55  {
56  if (_impl == other._impl) return *this;
57  release();
58  _impl = other._impl;
59  retain();
60  return *this;
61  }
62 
63  ~event()
64  {
65  release();
66  }
67 
68  friend bool operator==(const event& lhs, const event& rhs) { return lhs._impl == rhs._impl; }
69  friend bool operator!=(const event& lhs, const event& rhs) { return !(lhs == rhs); }
70 
72  void wait() const { check_status<void>("wait event failed", [=](status_t* status) { cldnn_wait_for_event(_impl, status); }); }
73 
75  void set() const { check_status<void>("set event failed", [=](status_t* status) { cldnn_set_event(_impl, status); }); }
76 
78  void set_event_handler(cldnn_event_handler handler, void* param) const
79  {
80  check_status<void>("set event handler failed", [=](status_t* status) { cldnn_add_event_handler(_impl, handler, param, status); });
81  }
82 
84  std::vector<instrumentation::profiling_interval> get_profiling_info() const
85  {
86  using namespace instrumentation;
87  wait();
88  size_t size_ret = 0;
89  status_t err_invalid_arg = CLDNN_SUCCESS;
90  cldnn_get_event_profiling_info(_impl, nullptr, 0, &size_ret, &err_invalid_arg);
91 
92  if (size_ret == 0)
93  {
94  return{};
95  }
96 
97  std::vector<cldnn_profiling_interval> profiling_info_ref(size_ret);
98 
99  check_status<void>("get event profiling info failed", [&](status_t* status)
100  {
101  cldnn_get_event_profiling_info(_impl, profiling_info_ref.data(), profiling_info_ref.size(), &size_ret, status);
102  });
103  assert(profiling_info_ref.size() == size_ret);
104 
105  std::vector<profiling_interval> result(profiling_info_ref.size());
106  std::transform(
107  std::begin(profiling_info_ref),
108  std::end(profiling_info_ref),
109  std::begin(result),
110  [](const cldnn_profiling_interval& ref) -> profiling_interval
111  {
112  return{
113  ref.name,
114  std::make_shared<profiling_period_basic>(std::chrono::nanoseconds(ref.nanoseconds))
115  };
116  }
117  );
118  return result;
119  }
120 
122  cldnn_event get() const { return _impl; }
123 private:
124  cldnn_event _impl;
125  void retain()
126  {
127  check_status<void>("retain event failed", [=](status_t* status) { cldnn_retain_event(_impl, status); });
128  }
129  void release()
130  {
131  check_status<void>("retain event failed", [=](status_t* status) { cldnn_release_event(_impl, status); });
132  }
133 };
134 CLDNN_API_CLASS(event)
135 
136 }
CLDNN_API cldnn_event cldnn_create_user_event(cldnn_engine engine, cldnn_status *status)
Creates an event which can be set by user.
Represents an clDNN Event object.
Definition: event.hpp:35
::cldnn_engine get() const
get C API engine handler.
Definition: engine.hpp:180
void(* cldnn_event_handler)(void *)
user-defined event handler callback.
Definition: cldnn.h:166
CLDNN_API void cldnn_release_event(cldnn_event event, cldnn_status *status)
Decrement reference counter for the event object. Deletes object when counter becomes zero...
struct cldnn_event_impl * cldnn_event
Event object.
Definition: cldnn.h:103
std::vector< instrumentation::profiling_interval > get_profiling_info() const
Get profiling info for the event associated with network output.
Definition: event.hpp:84
CLDNN_API void cldnn_add_event_handler(cldnn_event event, cldnn_event_handler handler, void *param, cldnn_status *status)
Register call back to be called on event completion.
void wait() const
Wait for event completion.
Definition: event.hpp:72
CLDNN_API void cldnn_get_event_profiling_info(cldnn_event event, cldnn_profiling_interval *profiling, size_t size, size_t *size_ret, cldnn_status *status)
Returns the profiling information for an network primitive associated with event. ...
CLDNN_API void cldnn_retain_event(cldnn_event event, cldnn_status *status)
Increment reference counter for the event object.
static event create_user_event(const engine &engine)
Create an event which can be set to &#39;completed&#39; by user.
Definition: event.hpp:38
void set_event_handler(cldnn_event_handler handler, void *param) const
Register call back to be called on event completion.
Definition: event.hpp:78
const char * name
Profiling interval name.
Definition: cldnn.h:173
event(cldnn_event impl)
Construct from C API handler cldnn_event.
Definition: event.hpp:44
Represents clDNN engine object.
Definition: engine.hpp:110
CLDNN_API void cldnn_wait_for_event(cldnn_event event, cldnn_status *status)
Waits for event completion or error.
CLDNN_API void cldnn_set_event(cldnn_event event, cldnn_status *status)
Set event status to completed.
Profiling information for an executed network primitive.
Definition: cldnn.h:171