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; }
39 uint64_t _queue_stamp = 0;
42 struct base_event : virtual public ocl_base_event {
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) {}
47 explicit base_event(std::shared_ptr<gpu_toolkit> ctx) : ocl_base_event(0, false), _ctx(ctx) {}
49 void attach_ocl_event(const cl::Event& ev, const uint64_t q_stamp) {
51 _queue_stamp = q_stamp;
56 std::shared_ptr<gpu_toolkit> get_context() const { return _ctx; }
57 cl::Event get() { return _event; }
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);
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;
71 friend struct base_events;
77 struct base_events : virtual public ocl_base_event {
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) {
84 explicit base_events(std::shared_ptr<gpu_toolkit> ctx) : ocl_base_event(0, false), _ctx(ctx) {}
86 void attach_events(const std::vector<event_impl::ptr>& ev) {
88 throw std::runtime_error("Trying to attach events to valid event object.");
94 std::shared_ptr<gpu_toolkit> get_context() const { return _ctx; }
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();
104 _queue_stamp = _queue_stamp_max;
106 void wait_impl() override;
107 bool is_set_impl() override;
109 bool get_profiling_info_impl(std::list<instrumentation::profiling_interval>& info) override;
111 std::shared_ptr<gpu_toolkit> _ctx;
112 std::vector<event_impl::ptr> _events;