2 // Copyright (c) 2019 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ocl_toolkit.h"
27 struct profiling_period_ocl_start_stop {
29 cl_profiling_info start;
30 cl_profiling_info stop;
33 struct ocl_base_event : virtual public event_impl {
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;
40 uint64_t _queue_stamp = 0;
43 struct base_event : virtual public ocl_base_event {
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) {}
48 explicit base_event(std::shared_ptr<gpu_toolkit> ctx) : ocl_base_event(0, false), _ctx(ctx) {}
50 void attach_ocl_event(const cl::Event& ev, const uint64_t q_stamp) {
52 _queue_stamp = q_stamp;
57 std::shared_ptr<gpu_toolkit> get_context() const { return _ctx; }
58 cl::Event get() override { return _event; }
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);
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;
72 friend struct base_events;
78 struct base_events : virtual public ocl_base_event {
80 base_events(std::shared_ptr<gpu_toolkit> ctx, std::vector<event_impl::ptr> const& ev)
81 : ocl_base_event(0, true), _ctx(ctx) {
85 explicit base_events(std::shared_ptr<gpu_toolkit> ctx) : ocl_base_event(0, false), _ctx(ctx) {}
87 void attach_events(const std::vector<event_impl::ptr>& ev) {
89 throw std::runtime_error("Trying to attach events to valid event object.");
94 cl::Event get() override { return _last_ocl_event; }
95 std::shared_ptr<gpu_toolkit> get_context() const { return _ctx; }
97 void reset() override {
98 ocl_base_event::reset();
103 void wait_impl() override;
104 bool is_set_impl() override;
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();
118 _events.push_back(multiple_events->_events[j]);
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();
128 _events.push_back(ev[i]);
133 bool get_profiling_info_impl(std::list<instrumentation::profiling_interval>& info) override;
135 cl::Event _last_ocl_event;
136 std::shared_ptr<gpu_toolkit> _ctx;
137 std::vector<event_impl::ptr> _events;