/
command_dispatcher.cpp
298 lines (270 loc) · 11.2 KB
/
command_dispatcher.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \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/>. *
\******************************************************************************/
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include "cppa/cppa.hpp"
#include "cppa/opencl/command_dispatcher.hpp"
using namespace std;
namespace cppa { namespace opencl {
struct command_dispatcher::worker {
command_dispatcher* m_parent;
typedef command_ptr job_ptr;
job_queue* m_job_queue;
thread m_thread;
job_ptr m_dummy;
worker(command_dispatcher* parent, job_queue* jq, job_ptr dummy)
: m_parent(parent), m_job_queue(jq), m_dummy(dummy) { }
void start() {
m_thread = thread(&command_dispatcher::worker_loop, this);
}
worker(const worker&) = delete;
worker& operator=(const worker&) = delete;
void operator()() {
job_ptr job;
for (;;) {
/*
* todo:
* manage device usage
* wait for device
*/
// adopt reference count of job queue
job.adopt(m_job_queue->pop());
if(job != m_dummy) {
try {
cl_command_queue cmd_q =
m_parent->m_devices.front().cmd_queue.get();
job->enqueue(cmd_q);
cl_int err{clFlush(cmd_q)};
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "clFlush: '"
<< get_opencl_error(err)
<< "'.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
}
catch (exception& e) {
ostringstream oss;
oss << "worker loop, e.what(): '" << e.what() << "'.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
}
else {
CPPA_LOG_TRACE("worker done");
return;
}
}
}
};
void command_dispatcher::worker_loop(command_dispatcher::worker* w) {
(*w)();
}
void command_dispatcher::supervisor_loop(command_dispatcher* scheduler,
job_queue* jq, command_ptr m_dummy) {
unique_ptr<command_dispatcher::worker> worker;
worker.reset(new command_dispatcher::worker(scheduler, jq, m_dummy));
worker->start();
worker->m_thread.join();
worker.reset();
CPPA_LOG_TRACE("supervisor done");
}
void command_dispatcher::initialize() {
m_dummy = make_counted<command_dummy>();
cl_int err{0};
/* find up to two available platforms */
vector<cl_platform_id> ids(2);
cl_uint number_of_platforms;
err = clGetPlatformIDs(ids.size(), ids.data(), &number_of_platforms);
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "clGetPlatformIDs: '"
<< get_opencl_error(err)
<< "'.";
CPPA_LOG_ERROR(oss.str());
throw logic_error(oss.str());
}
else if (number_of_platforms < 1) {
ostringstream oss;
oss << "clGetPlatformIDs: 'no platforms found'.";
CPPA_LOG_ERROR(oss.str());
throw logic_error(oss.str());
}
/* find gpu devices on our platform */
int pid{0};
cl_uint num_devices{0};
cl_device_type dev_type{CL_DEVICE_TYPE_GPU};
err = clGetDeviceIDs(ids[pid], dev_type, 0, nullptr, &num_devices);
if (err == CL_DEVICE_NOT_FOUND) {
CPPA_LOG_TRACE("No gpu devices found. Looking for cpu devices.");
cout << "No gpu devices found. Looking for cpu devices." << endl;
dev_type = CL_DEVICE_TYPE_CPU;
err = clGetDeviceIDs(ids[pid], dev_type, 0, nullptr, &num_devices);
}
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "clGetDeviceIDs: '"
<< get_opencl_error(err)
<< "'.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
vector<cl_device_id> devices(num_devices);
err = clGetDeviceIDs(ids[pid], dev_type, num_devices, devices.data(), nullptr);
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "clGetDeviceIDs: '"
<< get_opencl_error(err)
<< "'.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
/* create a context */
m_context.adopt(clCreateContext(0, 1, devices.data(), nullptr, nullptr, &err));
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "clCreateContext: '"
<< get_opencl_error(err)
<< "'.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
for (auto& d : devices) {
CPPA_LOG_TRACE("Creating command queue for device(s).");
device_ptr device;
device.adopt(d);
unsigned id{++dev_id_gen};
size_t return_size{0};
static constexpr size_t buf_size = 128;
char buf[buf_size];
err = clGetDeviceInfo(device.get(), CL_DEVICE_NAME, buf_size, buf, &return_size);
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "clGetDeviceInfo (CL_DEVICE_NAME): '"
<< get_opencl_error(err)
<< "'.";
CPPA_LOG_ERROR(oss.str());
fill(buf, buf+buf_size, 0);
}
command_queue_ptr cmd_queue;
cmd_queue.adopt(clCreateCommandQueue(m_context.get(),
device.get(),
CL_QUEUE_PROFILING_ENABLE,
&err));
if (err != CL_SUCCESS) {
CPPA_LOG_DEBUG("Could not create command queue for device "
<< buf << ": '" << get_opencl_error(err)
<< "'.");
}
else {
size_t max_work_group_size{0};
err = clGetDeviceInfo(device.get(),
CL_DEVICE_MAX_WORK_GROUP_SIZE,
sizeof(size_t),
&max_work_group_size,
&return_size);
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "[!!!] clGetDeviceInfo ("
<< id
<< ":CL_DEVICE_MAX_WORK_GROUP_SIZE): '"
<< get_opencl_error(err) << "'.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
cl_uint max_work_item_dimensions = 0;
err = clGetDeviceInfo(device.get(),
CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
sizeof(cl_uint),
&max_work_item_dimensions,
&return_size);
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "[!!!] clGetDeviceInfo ("
<< id
<< ":CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS): '"
<< get_opencl_error(err) << "'.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
vector<size_t> max_work_items_per_dim(max_work_item_dimensions);
err = clGetDeviceInfo(device.get(),
CL_DEVICE_MAX_WORK_ITEM_SIZES,
sizeof(size_t)*max_work_item_dimensions,
max_work_items_per_dim.data(),
&return_size);
if (err != CL_SUCCESS) {
ostringstream oss;
oss << "[!!!] clGetDeviceInfo ("
<< id
<< ":CL_DEVICE_MAX_WORK_ITEM_SIZES): '"
<< get_opencl_error(err) << "'.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
device_info dev_info{id,
cmd_queue,
device,
max_work_group_size,
max_work_item_dimensions,
move(max_work_items_per_dim)};
m_devices.push_back(move(dev_info));
}
}
if (m_devices.empty()) {
ostringstream oss;
oss << "[!!!] Could not create a command queue for "
<< "any of the present devices.";
CPPA_LOG_ERROR(oss.str());
throw runtime_error(oss.str());
}
else {
m_supervisor = thread(&command_dispatcher::supervisor_loop,
this,
&m_job_queue,
m_dummy);
}
}
void command_dispatcher::destroy() {
m_dummy->ref(); // reference of m_job_queue
m_job_queue.push_back(m_dummy.get());
m_supervisor.join();
delete this;
}
void command_dispatcher::dispose() {
delete this;
}
command_dispatcher* get_command_dispatcher() {
return detail::singleton_manager::get_command_dispatcher();
}
} } // namespace cppa::opencl