Publishing R3
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / api / CPP / 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
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
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
28 /// @addtogroup cpp_api C++ API
29 /// @{
30
31 /// @addtogroup cpp_event Events Support
32 /// @{
33
34 /// @brief Represents an clDNN Event object
35 struct event
36 {
37     /// @brief Create an event which can be set to 'completed' by user.
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     
43     /// @brief Construct from C API handler @ref ::cldnn_event.
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
71     /// @brief Wait for event completion.
72     void wait() const { check_status<void>("wait event failed", [=](status_t* status) { cldnn_wait_for_event(_impl, status); }); }
73
74     /// @brief Set event status to 'completed'.
75     void set() const { check_status<void>("set event failed", [=](status_t* status) { cldnn_set_event(_impl, status); }); }
76
77     /// @brief Register call back to be called on event completion.
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
83     /// @brief Get profiling info for the event associated with network output.
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
121     /// @brief Returns C API event handler.
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 /// @}
137 /// @}
138 }