updated readme file due to moving CMake scripts to the root folder
[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
38 protected:
39     uint64_t _queue_stamp = 0;
40 };
41
42 struct base_event : virtual public ocl_base_event {
43 public:
44     base_event(std::shared_ptr<gpu_toolkit> ctx, cl::Event const& ev, uint64_t queue_stamp = 0)
45         : ocl_base_event(queue_stamp, true), _ctx(ctx), _event(ev) {}
46
47     explicit base_event(std::shared_ptr<gpu_toolkit> ctx) : ocl_base_event(0, false), _ctx(ctx) {}
48
49     void attach_ocl_event(const cl::Event& ev, const uint64_t q_stamp) {
50         _event = ev;
51         _queue_stamp = q_stamp;
52         _attached = true;
53         _set = false;
54     }
55
56     std::shared_ptr<gpu_toolkit> get_context() const { return _ctx; }
57     cl::Event get() { return _event; }
58
59 private:
60     std::shared_ptr<gpu_toolkit> _ctx;
61     bool _callback_set = false;
62     void set_ocl_callback();
63     static void CL_CALLBACK ocl_event_completion_callback(cl_event, cl_int, void* me);
64
65 private:
66     void wait_impl() override;
67     bool is_set_impl() override;
68     bool add_event_handler_impl(event_handler, void*) override;
69     bool get_profiling_info_impl(std::list<instrumentation::profiling_interval>& info) override;
70
71     friend struct base_events;
72
73 protected:
74     cl::Event _event;
75 };
76
77 struct base_events : virtual public ocl_base_event {
78 public:
79     base_events(std::shared_ptr<gpu_toolkit> ctx, std::vector<event_impl::ptr> const& ev)
80         : ocl_base_event(0, true), _ctx(ctx), _events(ev) {
81         set_queue_stamp();
82     }
83
84     explicit base_events(std::shared_ptr<gpu_toolkit> ctx) : ocl_base_event(0, false), _ctx(ctx) {}
85
86     void attach_events(const std::vector<event_impl::ptr>& ev) {
87         if (_attached)
88             throw std::runtime_error("Trying to attach events to valid event object.");
89         _events = ev;
90         _attached = true;
91         set_queue_stamp();
92     }
93
94     std::shared_ptr<gpu_toolkit> get_context() const { return _ctx; }
95
96 private:
97     void set_queue_stamp() {
98         uint64_t _queue_stamp_max = 0;
99         for (size_t i = 0; i < _events.size(); i++) {
100             auto* _base_event = dynamic_cast<base_event*>(_events[i].get());
101             if (_base_event->get_queue_stamp() > _queue_stamp_max)
102                 _queue_stamp_max = _base_event->get_queue_stamp();
103         }
104         _queue_stamp = _queue_stamp_max;
105     }
106     void wait_impl() override;
107     bool is_set_impl() override;
108
109     bool get_profiling_info_impl(std::list<instrumentation::profiling_interval>& info) override;
110
111     std::shared_ptr<gpu_toolkit> _ctx;
112     std::vector<event_impl::ptr> _events;
113 };
114
115 }  // namespace gpu
116 }  // namespace cldnn