Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / gpu / events_pool.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 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include "refcounted_obj.h"
19 #include "event_impl.h"
20 #include "meta_utils.h"
21 #include <iostream>
22
23 namespace cldnn {
24     namespace gpu {
25
26         class gpu_toolkit;
27
28         template<typename Type,
29             typename U = typename std::enable_if<
30             meta::is_any_of<Type, base_event, user_event, base_events>::value>::type>
31         class event_pool_impl
32         {
33         protected:
34             event_pool_impl() = default;
35
36             using type = Type;
37
38             event_impl::ptr get_from_pool(std::shared_ptr<gpu_toolkit>& ctx)
39             {
40                 for (auto& ev : _events)
41                 {
42                     if (!ev->is_valid())
43                         return ev;
44                 }
45                 return allocate({ new Type(ctx), false });
46             }
47
48             void reset_events()
49             {
50                 for (auto& ev : _events)
51                     ev->reset();
52             }
53
54         private:
55             std::vector<event_impl::ptr> _events;
56
57             event_impl::ptr allocate(const event_impl::ptr& obj)
58             {
59                 _events.emplace_back(obj);
60                 return _events.back();
61             }
62         };
63
64         struct base_event_pool : event_pool_impl<base_event>
65         {
66             event_impl::ptr get(std::shared_ptr<gpu_toolkit>& ctx, const cl::Event& ev, const uint64_t q_stamp)
67             {
68                 auto ret = get_from_pool(ctx);
69                 dynamic_cast<type*>(ret.get())->attach_ocl_event(ev, q_stamp);
70                 return ret;
71             }
72             void reset()
73             {
74                 reset_events();
75             }
76         };
77
78         struct user_event_pool : event_pool_impl<user_event>
79         {
80             event_impl::ptr get(std::shared_ptr<gpu_toolkit>& ctx, bool set = false)
81             {
82                 auto ret = get_from_pool(ctx);
83                 dynamic_cast<type*>(ret.get())->attach_event(set);
84                 return ret;
85             }
86             void reset()
87             {
88                 reset_events();
89             }
90         };
91
92         struct group_event_pool : event_pool_impl<base_events>
93         {
94             event_impl::ptr get(std::shared_ptr<gpu_toolkit>& ctx, const std::vector<event_impl::ptr>& deps)
95             {
96                 auto ret_ev = get_from_pool(ctx);
97                 dynamic_cast<type*>(ret_ev.get())->attach_events(deps);
98                 return ret_ev;
99             }
100             void reset()
101             {
102                 reset_events();
103             }
104         };
105
106         class events_pool
107         {
108         public:
109             events_pool() = default;
110
111             event_impl::ptr get_from_base_pool(std::shared_ptr<gpu_toolkit> ctx, const cl::Event& ev, const uint64_t q_stamp)
112             {
113                 return _base_pool.get(ctx, ev, q_stamp);
114             }
115
116             event_impl::ptr get_from_user_pool(std::shared_ptr<gpu_toolkit> ctx, bool set = false)
117             {
118                 return _user_pool.get(ctx, set);
119             }
120
121             event_impl::ptr get_from_group_pool(std::shared_ptr<gpu_toolkit> ctx, const std::vector<event_impl::ptr>& deps)
122             {
123                 return _group_pool.get(ctx, deps);
124             }
125
126             void reset_events()
127             {
128                 _base_pool.reset();
129                 _user_pool.reset();
130                 _group_pool.reset();
131             }
132
133         private:
134             base_event_pool _base_pool;
135             user_event_pool _user_pool;
136             group_event_pool _group_pool;
137         };
138     }
139 }