forked from plaidml/plaidml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.h
71 lines (56 loc) · 2.57 KB
/
event.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2017-2018 Intel Corporation.
#pragma once
#include <initializer_list>
#include <memory>
#include <utility>
#include <vector>
#include "tile/base/hal.h"
#include "tile/hal/opencl/buffer.h"
#include "tile/hal/opencl/device_state.h"
#include "tile/hal/opencl/ocl.h"
namespace vertexai {
namespace tile {
namespace hal {
namespace opencl {
// Implements hal::Event in terms of OpenCL events.
class Event final : public hal::Event {
public:
// Casts a hal::Event to an Event, throwing an exception if the supplied hal::Event isn't an
// OpenCL event, or if it's an event for a different context.
static std::shared_ptr<Event> Downcast(const std::shared_ptr<hal::Event>& event, const CLObj<cl_context>& cl_ctx);
// Casts a vector of hal::Event to a vector of cl_event, throwing an exception if the supplied
// hal::Event objects aren't OpenCL events, or if any are events for a different context. This
// method does not retain the returned events; callers must hold onto the input vector to keep the
// events alive.
static std::vector<cl_event> Downcast(const std::vector<std::shared_ptr<hal::Event>>& events,
const CLObj<cl_context>& cl_ctx, const DeviceState::Queue& queue);
// Returns a future that waits for all of the supplied events to complete.
static boost::future<std::vector<std::shared_ptr<hal::Result>>> WaitFor(
const std::vector<std::shared_ptr<hal::Event>>& events, const std::shared_ptr<DeviceState>& device_state);
Event(const context::Context& ctx, const std::shared_ptr<DeviceState>& device_state, CLObj<cl_event> cl_event,
const DeviceState::Queue& queue);
Event(const context::Context& ctx, const std::shared_ptr<DeviceState>& device_state, CLObj<cl_event> cl_event,
const DeviceState::Queue& queue, const std::shared_ptr<hal::Result>& result);
~Event() final;
boost::shared_future<std::shared_ptr<hal::Result>> GetFuture() final;
private:
struct FutureState {
std::mutex mu;
bool completed = false;
std::shared_ptr<FutureState> self; // Set iff clSetEventCallback is in flight
std::shared_ptr<hal::Result> result;
boost::promise<std::shared_ptr<hal::Result>> prom;
};
static void EventComplete(cl_event evt, cl_int status, void* data);
const DeviceState::Queue* queue_;
std::mutex mu_;
bool started_ = false;
CLObj<cl_context> cl_ctx_;
CLObj<cl_event> cl_event_;
std::shared_ptr<FutureState> state_;
boost::shared_future<std::shared_ptr<hal::Result>> fut_;
};
} // namespace opencl
} // namespace hal
} // namespace tile
} // namespace vertexai