blob: 3d313ce89694ce2c40b861bc3eb02d2afe396c0a [file] [log] [blame]
Francisco Jerezc6db1b32012-04-20 16:56:19 +02001//
2// Copyright 2012 Francisco Jerez
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Kenneth Graunkef0cb66b2013-04-21 13:52:08 -070017// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20// OTHER DEALINGS IN THE SOFTWARE.
Francisco Jerezc6db1b32012-04-20 16:56:19 +020021//
22
23#include "core/event.hpp"
24#include "pipe/p_screen.h"
25
26using namespace clover;
27
Francisco Jerezc4578d22014-02-18 15:07:11 +010028event::event(clover::context &ctx, const ref_vector<event> &deps,
Francisco Jerez257781f2013-10-01 11:54:07 -070029 action action_ok, action action_fail) :
Francisco Jerez02f8ac62015-06-09 22:52:25 +030030 context(ctx), _wait_count(1), _status(0),
Francisco Jerezc6db1b32012-04-20 16:56:19 +020031 action_ok(action_ok), action_fail(action_fail) {
Francisco Jerez257781f2013-10-01 11:54:07 -070032 for (auto &ev : deps)
Francisco Jerezc4578d22014-02-18 15:07:11 +010033 ev.chain(*this);
Francisco Jerezc6db1b32012-04-20 16:56:19 +020034}
35
Francisco Jerez257781f2013-10-01 11:54:07 -070036event::~event() {
Francisco Jerezc6db1b32012-04-20 16:56:19 +020037}
38
Francisco Jerez2232b922015-05-09 14:47:38 +030039std::vector<intrusive_ref<event>>
40event::trigger_self() {
Francisco Jereza533d4e2015-05-09 16:01:23 +030041 std::lock_guard<std::mutex> lock(mutex);
Francisco Jerez2232b922015-05-09 14:47:38 +030042 std::vector<intrusive_ref<event>> evs;
43
Jan Vesely1e8b8e02018-07-24 22:17:28 -040044 if (_wait_count && !--_wait_count)
Francisco Jerez2232b922015-05-09 14:47:38 +030045 std::swap(_chain, evs);
46
Francisco Jerezbc4000e2015-06-09 22:59:43 +030047 cv.notify_all();
Francisco Jerez2232b922015-05-09 14:47:38 +030048 return evs;
49}
50
Francisco Jerezc6db1b32012-04-20 16:56:19 +020051void
Jan Vesely866b25f2018-07-17 02:05:02 -040052event::trigger() try {
Francisco Jerezbc4000e2015-06-09 22:59:43 +030053 if (wait_count() == 1)
Francisco Jerez2232b922015-05-09 14:47:38 +030054 action_ok(*this);
Francisco Jerez2232b922015-05-09 14:47:38 +030055
Francisco Jerezbc4000e2015-06-09 22:59:43 +030056 for (event &ev : trigger_self())
Francisco Jerez2232b922015-05-09 14:47:38 +030057 ev.trigger();
Jan Vesely866b25f2018-07-17 02:05:02 -040058} catch (error &e) {
59 abort(e.get());
Francisco Jerez2232b922015-05-09 14:47:38 +030060}
61
62std::vector<intrusive_ref<event>>
63event::abort_self(cl_int status) {
Francisco Jereza533d4e2015-05-09 16:01:23 +030064 std::lock_guard<std::mutex> lock(mutex);
Francisco Jerez2232b922015-05-09 14:47:38 +030065 std::vector<intrusive_ref<event>> evs;
66
67 _status = status;
Jan Vesely1e8b8e02018-07-24 22:17:28 -040068 _wait_count = 0;
Francisco Jerez2232b922015-05-09 14:47:38 +030069 std::swap(_chain, evs);
70
Jan Vesely1e8b8e02018-07-24 22:17:28 -040071 cv.notify_all();
Francisco Jerez2232b922015-05-09 14:47:38 +030072 return evs;
Francisco Jerezebfdce02013-10-06 13:48:23 -070073}
74
75void
Francisco Jerez257781f2013-10-01 11:54:07 -070076event::abort(cl_int status) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +020077 action_fail(*this);
78
Francisco Jerezbc4000e2015-06-09 22:59:43 +030079 for (event &ev : abort_self(status))
Francisco Jerez2232b922015-05-09 14:47:38 +030080 ev.abort(status);
Francisco Jerezc6db1b32012-04-20 16:56:19 +020081}
82
Francisco Jerez02f8ac62015-06-09 22:52:25 +030083unsigned
84event::wait_count() const {
85 std::lock_guard<std::mutex> lock(mutex);
86 return _wait_count;
87}
88
Francisco Jerezc6db1b32012-04-20 16:56:19 +020089bool
Francisco Jerez257781f2013-10-01 11:54:07 -070090event::signalled() const {
Francisco Jerez02f8ac62015-06-09 22:52:25 +030091 return !wait_count();
Francisco Jerezc6db1b32012-04-20 16:56:19 +020092}
93
Francisco Jerez4022a462015-05-09 16:22:33 +030094cl_int
95event::status() const {
Francisco Jereza533d4e2015-05-09 16:01:23 +030096 std::lock_guard<std::mutex> lock(mutex);
Francisco Jerez4022a462015-05-09 16:22:33 +030097 return _status;
98}
99
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200100void
Francisco Jerezc4578d22014-02-18 15:07:11 +0100101event::chain(event &ev) {
Francisco Jereza533d4e2015-05-09 16:01:23 +0300102 std::unique_lock<std::mutex> lock(mutex, std::defer_lock);
103 std::unique_lock<std::mutex> lock_ev(ev.mutex, std::defer_lock);
104 std::lock(lock, lock_ev);
105
Francisco Jerez02f8ac62015-06-09 22:52:25 +0300106 if (_wait_count) {
107 ev._wait_count++;
Francisco Jerez8e14b822013-09-17 23:13:48 -0700108 _chain.push_back(ev);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200109 }
Francisco Jerezc4578d22014-02-18 15:07:11 +0100110 ev.deps.push_back(*this);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200111}
112
Tom Stellard9c4dc982015-03-26 19:33:24 +0000113void
Francisco Jerezbc4000e2015-06-09 22:59:43 +0300114event::wait_signalled() const {
115 std::unique_lock<std::mutex> lock(mutex);
116 cv.wait(lock, [=]{ return !_wait_count; });
117}
118
119void
Tom Stellard9c4dc982015-03-26 19:33:24 +0000120event::wait() const {
121 for (event &ev : deps)
122 ev.wait();
123
Francisco Jerezbc4000e2015-06-09 22:59:43 +0300124 wait_signalled();
Tom Stellard9c4dc982015-03-26 19:33:24 +0000125}
126
Francisco Jerez257781f2013-10-01 11:54:07 -0700127hard_event::hard_event(command_queue &q, cl_command_type command,
128 const ref_vector<event> &deps, action action) :
Francisco Jerezc4578d22014-02-18 15:07:11 +0100129 event(q.context(), deps, profile(q, action), [](event &ev){}),
Francisco Jerez8e14b822013-09-17 23:13:48 -0700130 _queue(q), _command(command), _fence(NULL) {
Francisco Jerezebfdce02013-10-06 13:48:23 -0700131 if (q.profiling_enabled())
132 _time_queued = timestamp::current(q);
133
Francisco Jerezc4578d22014-02-18 15:07:11 +0100134 q.sequence(*this);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200135 trigger();
136}
137
138hard_event::~hard_event() {
Francisco Jerezc4578d22014-02-18 15:07:11 +0100139 pipe_screen *screen = queue()->device().pipe;
Francisco Jerez8e14b822013-09-17 23:13:48 -0700140 screen->fence_reference(screen, &_fence, NULL);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200141}
142
143cl_int
144hard_event::status() const {
Francisco Jerezc4578d22014-02-18 15:07:11 +0100145 pipe_screen *screen = queue()->device().pipe;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200146
Francisco Jerez4022a462015-05-09 16:22:33 +0300147 if (event::status() < 0)
148 return event::status();
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200149
Francisco Jerez8e14b822013-09-17 23:13:48 -0700150 else if (!_fence)
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200151 return CL_QUEUED;
152
Marek Olšák54272e12016-08-06 16:41:42 +0200153 else if (!screen->fence_finish(screen, NULL, _fence, 0))
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200154 return CL_SUBMITTED;
155
156 else
157 return CL_COMPLETE;
158}
159
Francisco Jerez257781f2013-10-01 11:54:07 -0700160command_queue *
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200161hard_event::queue() const {
Francisco Jerezc4578d22014-02-18 15:07:11 +0100162 return &_queue();
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200163}
164
165cl_command_type
166hard_event::command() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700167 return _command;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200168}
169
170void
171hard_event::wait() const {
Francisco Jerezc4578d22014-02-18 15:07:11 +0100172 pipe_screen *screen = queue()->device().pipe;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200173
Tom Stellard9c4dc982015-03-26 19:33:24 +0000174 event::wait();
175
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200176 if (status() == CL_QUEUED)
177 queue()->flush();
178
Francisco Jerez8e14b822013-09-17 23:13:48 -0700179 if (!_fence ||
Marek Olšák54272e12016-08-06 16:41:42 +0200180 !screen->fence_finish(screen, NULL, _fence, PIPE_TIMEOUT_INFINITE))
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200181 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
182}
183
Francisco Jerezebfdce02013-10-06 13:48:23 -0700184const lazy<cl_ulong> &
185hard_event::time_queued() const {
186 return _time_queued;
Niels Ole Salscheider4a3505d2013-08-09 11:59:25 +0200187}
188
Francisco Jerezebfdce02013-10-06 13:48:23 -0700189const lazy<cl_ulong> &
190hard_event::time_submit() const {
191 return _time_submit;
Niels Ole Salscheider4a3505d2013-08-09 11:59:25 +0200192}
193
Francisco Jerezebfdce02013-10-06 13:48:23 -0700194const lazy<cl_ulong> &
195hard_event::time_start() const {
196 return _time_start;
Niels Ole Salscheider4a3505d2013-08-09 11:59:25 +0200197}
198
Francisco Jerezebfdce02013-10-06 13:48:23 -0700199const lazy<cl_ulong> &
200hard_event::time_end() const {
201 return _time_end;
Niels Ole Salscheider4a3505d2013-08-09 11:59:25 +0200202}
203
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200204void
205hard_event::fence(pipe_fence_handle *fence) {
Francisco Jerezc4578d22014-02-18 15:07:11 +0100206 pipe_screen *screen = queue()->device().pipe;
Francisco Jerez8e14b822013-09-17 23:13:48 -0700207 screen->fence_reference(screen, &_fence, fence);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200208}
209
Francisco Jerezebfdce02013-10-06 13:48:23 -0700210event::action
211hard_event::profile(command_queue &q, const action &action) const {
212 if (q.profiling_enabled()) {
213 return [&q, action] (event &ev) {
214 auto &hev = static_cast<hard_event &>(ev);
215
216 hev._time_submit = timestamp::current(q);
217 hev._time_start = timestamp::query(q);
218
219 action(ev);
220
221 hev._time_end = timestamp::query(q);
222 };
223
224 } else {
225 return action;
226 }
227}
228
Francisco Jerezc4578d22014-02-18 15:07:11 +0100229soft_event::soft_event(clover::context &ctx, const ref_vector<event> &deps,
Francisco Jerez8e14b822013-09-17 23:13:48 -0700230 bool _trigger, action action) :
Francisco Jerez257781f2013-10-01 11:54:07 -0700231 event(ctx, deps, action, action) {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700232 if (_trigger)
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200233 trigger();
234}
235
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200236cl_int
237soft_event::status() const {
Francisco Jerez4022a462015-05-09 16:22:33 +0300238 if (event::status() < 0)
239 return event::status();
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200240
241 else if (!signalled() ||
Francisco Jerezc4578d22014-02-18 15:07:11 +0100242 any_of([](const event &ev) {
243 return ev.status() != CL_COMPLETE;
Francisco Jerez7d617692013-10-06 13:49:05 -0700244 }, deps))
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200245 return CL_SUBMITTED;
246
247 else
248 return CL_COMPLETE;
249}
250
Francisco Jerez257781f2013-10-01 11:54:07 -0700251command_queue *
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200252soft_event::queue() const {
253 return NULL;
254}
255
256cl_command_type
257soft_event::command() const {
258 return CL_COMMAND_USER;
259}
260
261void
262soft_event::wait() const {
Tom Stellard9c4dc982015-03-26 19:33:24 +0000263 event::wait();
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200264
265 if (status() != CL_COMPLETE)
266 throw error(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
267}