[IE CLDNN] Fixed mem leak (#3153)
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / gpu / ocl_base_event.h
1 /*
2 // Copyright (c) 2019 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 #pragma once
18
19 #include "ocl_toolkit.h"
20 #include <vector>
21 #include <memory>
22 #include <list>
23
24 namespace cldnn {
25 namespace gpu {
26
27 struct profiling_period_ocl_start_stop {
28     const char* name;
29     cl_profiling_info start;
30     cl_profiling_info stop;
31 };
32
33 struct ocl_base_event : virtual public event_impl {
34 public:
35     explicit ocl_base_event(uint64_t queue_stamp = 0, bool valid = false) : _queue_stamp(queue_stamp) { _attached = valid; }
36     uint64_t get_queue_stamp() const { return _queue_stamp; }
37     virtual cl::Event get() = 0;
38
39 protected:
40     uint64_t _queue_stamp = 0;
41 };
42
43 struct base_event : virtual public ocl_base_event {
44 public:
45     base_event(std::shared_ptr<gpu_toolkit> ctx, cl::Event const& ev, uint64_t queue_stamp = 0)
46         : ocl_base_event(queue_stamp, true), _ctx(ctx), _event(ev) {}
47
48     explicit base_event(std::shared_ptr<gpu_toolkit> ctx) : ocl_base_event(0, false), _ctx(ctx) {}
49
50     void attach_ocl_event(const cl::Event& ev, const uint64_t q_stamp) {
51         _event = ev;
52         _queue_stamp = q_stamp;
53         _attached = true;
54         _set = false;
55     }
56
57     std::shared_ptr<gpu_toolkit> get_context() const { return _ctx; }
58     cl::Event get() override { return _event; }
59
60 private:
61     std::shared_ptr<gpu_toolkit> _ctx;
62     bool _callback_set = false;
63     void set_ocl_callback();
64     static void CL_CALLBACK ocl_event_completion_callback(cl_event, cl_int, void* me);
65
66 private:
67     void wait_impl() override;
68     bool is_set_impl() override;
69     bool add_event_handler_impl(event_handler, void*) override;
70     bool get_profiling_info_impl(std::list<instrumentation::profiling_interval>& info) override;
71
72     friend struct base_events;
73
74 protected:
75     cl::Event _event;
76 };
77
78 struct base_events : virtual public ocl_base_event {
79 public:
80     base_events(std::shared_ptr<gpu_toolkit> ctx, std::vector<event_impl::ptr> const& ev)
81         : ocl_base_event(0, true), _ctx(ctx) {
82         process_events(ev);
83     }
84
85     explicit base_events(std::shared_ptr<gpu_toolkit> ctx) : ocl_base_event(0, false), _ctx(ctx) {}
86
87     void attach_events(const std::vector<event_impl::ptr>& ev) {
88         if (_attached)
89             throw std::runtime_error("Trying to attach events to valid event object.");
90         process_events(ev);
91         _attached = true;
92     }
93
94     cl::Event get() override { return _last_ocl_event; }
95     std::shared_ptr<gpu_toolkit> get_context() const { return _ctx; }
96
97     void reset() override {
98         ocl_base_event::reset();
99         _events.clear();
100     }
101
102 private:
103     void wait_impl() override;
104     bool is_set_impl() override;
105
106     void process_events(const std::vector<event_impl::ptr>& ev) {
107         for (size_t i = 0; i < ev.size(); i++) {
108             auto multiple_events = dynamic_cast<base_events*>(ev[i].get());
109             if (multiple_events) {
110                 for (size_t j = 0; j < multiple_events->_events.size(); j++) {
111                     if (auto base_ev = dynamic_cast<base_event*>(multiple_events->_events[j].get())) {
112                         auto current_ev_queue_stamp = base_ev->get_queue_stamp();
113                         if ((_queue_stamp == 0) || (current_ev_queue_stamp > _queue_stamp)) {
114                             _queue_stamp = current_ev_queue_stamp;
115                             _last_ocl_event = base_ev->get();
116                         }
117                     }
118                     _events.push_back(multiple_events->_events[j]);
119                 }
120             } else {
121                 if (auto base_ev = dynamic_cast<base_event*>(ev[i].get())) {
122                     auto current_ev_queue_stamp = base_ev->get_queue_stamp();
123                     if ((_queue_stamp == 0) || (current_ev_queue_stamp > _queue_stamp)) {
124                         _queue_stamp = current_ev_queue_stamp;
125                         _last_ocl_event = base_ev->get();
126                     }
127                 }
128                 _events.push_back(ev[i]);
129             }
130         }
131     }
132
133     bool get_profiling_info_impl(std::list<instrumentation::profiling_interval>& info) override;
134
135     cl::Event _last_ocl_event;
136     std::shared_ptr<gpu_toolkit> _ctx;
137     std::vector<event_impl::ptr> _events;
138 };
139
140 }  // namespace gpu
141 }  // namespace cldnn