-
Notifications
You must be signed in to change notification settings - Fork 540
/
command_dispatcher.hpp
180 lines (147 loc) · 6.93 KB
/
command_dispatcher.hpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011-2013 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* Raphael Hiesgen <raphael.hiesgen@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef CPPA_OPENCL_COMMAND_DISPATCHER_HPP
#define CPPA_OPENCL_COMMAND_DISPATCHER_HPP
#include <atomic>
#include <vector>
#include <algorithm>
#include <functional>
#include "cppa/option.hpp"
#include "cppa/channel.hpp"
#include "cppa/cow_tuple.hpp"
#include "cppa/logging.hpp"
#include "cppa/opencl/global.hpp"
#include "cppa/opencl/command.hpp"
#include "cppa/opencl/program.hpp"
#include "cppa/opencl/smart_ptr.hpp"
#include "cppa/opencl/actor_facade.hpp"
#include "cppa/detail/singleton_mixin.hpp"
#include "cppa/detail/singleton_manager.hpp"
#include "cppa/intrusive/blocking_single_reader_queue.hpp"
namespace cppa { namespace opencl {
struct dereferencer {
inline void operator()(ref_counted* ptr) { ptr->deref(); }
};
template<typename... Ts>
option<cow_tuple<Ts...>> default_map_args(any_tuple msg) {
return tuple_cast<Ts...>(msg);
}
class command_dispatcher {
struct worker;
friend struct worker;
friend class detail::singleton_manager;
friend class program;
friend void enqueue_to_dispatcher(command_dispatcher* dispatcher,
command_ptr cmd);
public:
void enqueue();
template<typename Ret, typename... Args>
actor_ptr spawn(const program& prog,
const char* kernel_name,
std::vector<size_t> global_dims,
std::vector<size_t> global_offs,
std::vector<size_t> local_dims,
std::function<option<cow_tuple<typename util::rm_ref<Args>::type...>>(any_tuple)> map_args,
std::function<any_tuple(Ret&)> map_result)
{
return actor_facade<Ret (Args...)>::create(this,
prog,
kernel_name,
std::move(global_dims),
std::move(global_offs),
std::move(local_dims),
std::move(map_args),
std::move(map_result));
}
template<typename Ret, typename... Args>
actor_ptr spawn(const program& prog,
const char* kernel_name,
std::vector<size_t> global_dims,
std::vector<size_t> global_offs = {},
std::vector<size_t> local_dims = {})
{
std::function<option<cow_tuple<typename util::rm_ref<Args>::type...>>(any_tuple)>
map_args = [] (any_tuple msg) {
return tuple_cast<typename util::rm_ref<Args>::type...>(msg);
};
std::function<any_tuple(Ret&)> map_result = [] (Ret& result) {
return make_any_tuple(std::move(result));
};
return this->spawn<Ret,Args...>(prog,
kernel_name,
std::move(global_dims),
std::move(global_offs),
std::move(local_dims),
std::move(map_args),
std::move(map_result));
}
private:
struct device_info {
unsigned id;
command_queue_ptr cmd_queue;
device_ptr dev_id;
size_t max_itms_per_grp;
cl_uint max_dim;
std::vector<size_t> max_itms_per_dim;
device_info(unsigned id,
command_queue_ptr queue,
device_ptr device_id,
size_t max_itms_per_grp,
cl_uint max_dim,
std::vector<size_t> max_itms_per_dim)
: id(id)
, cmd_queue(queue)
, dev_id(device_id)
, max_itms_per_grp(max_itms_per_grp)
, max_dim(max_dim)
, max_itms_per_dim(std::move(max_itms_per_dim)) { }
};
typedef intrusive::blocking_single_reader_queue<command,dereferencer>
job_queue;
static inline command_dispatcher* create_singleton() {
return new command_dispatcher;
}
void initialize();
void dispose();
void destroy();
std::atomic<unsigned> dev_id_gen;
job_queue m_job_queue;
command_ptr m_dummy;
std::thread m_supervisor;
std::vector<device_info> m_devices;
context_ptr m_context;
static void worker_loop(worker*);
static void supervisor_loop(command_dispatcher *scheduler,
job_queue*,
command_ptr);
};
command_dispatcher* get_command_dispatcher();
} } // namespace cppa::opencl
#endif // CPPA_OPENCL_COMMAND_DISPATCHER_HPP