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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
* Copyright © 2012 Intel Corporation
*
* This library 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 of the License, or (at your option) any later version.
*
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Benjamin Segovia <benjamin.segovia@intel.com>
*/
#include "cl_command_queue.h"
#include "cl_context.h"
#include "cl_program.h"
#include "cl_kernel.h"
#include "cl_device_id.h"
#include "cl_mem.h"
#include "cl_utils.h"
#include "cl_alloc.h"
#include "cl_driver.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
LOCAL cl_command_queue
cl_command_queue_new(cl_context ctx)
{
cl_command_queue queue = NULL;
assert(ctx);
TRY_ALLOC_NO_ERR (queue, CALLOC(struct _cl_command_queue));
queue->magic = CL_MAGIC_QUEUE_HEADER;
queue->ref_n = 1;
queue->ctx = ctx;
TRY_ALLOC_NO_ERR (queue->gpgpu, cl_gpgpu_new(ctx->drv));
/* Append the command queue in the list */
pthread_mutex_lock(&ctx->queue_lock);
queue->next = ctx->queues;
if (ctx->queues != NULL)
ctx->queues->prev = queue;
ctx->queues = queue;
pthread_mutex_unlock(&ctx->queue_lock);
/* The queue also belongs to its context */
cl_context_add_ref(ctx);
exit:
return queue;
error:
cl_command_queue_delete(queue);
queue = NULL;
goto exit;
}
LOCAL void
cl_command_queue_delete(cl_command_queue queue)
{
assert(queue);
if (atomic_dec(&queue->ref_n) != 1) return;
/* Remove it from the list */
assert(queue->ctx);
pthread_mutex_lock(&queue->ctx->queue_lock);
if (queue->prev)
queue->prev->next = queue->next;
if (queue->next)
queue->next->prev = queue->prev;
if (queue->next == NULL && queue->prev == NULL)
queue->ctx->queues = NULL;
pthread_mutex_unlock(&queue->ctx->queue_lock);
if (queue->fulsim_out != NULL) {
cl_mem_delete(queue->fulsim_out);
queue->fulsim_out = NULL;
}
cl_buffer_unreference(queue->last_batch);
cl_mem_delete(queue->perf);
cl_context_delete(queue->ctx);
cl_gpgpu_delete(queue->gpgpu);
queue->magic = CL_MAGIC_DEAD_HEADER; /* For safety */
cl_free(queue);
}
LOCAL void
cl_command_queue_add_ref(cl_command_queue queue)
{
atomic_inc(&queue->ref_n);
}
LOCAL cl_int
cl_command_queue_bind_surface(cl_command_queue queue, cl_kernel k)
{
/* Bind all user buffers (given by clSetKernelArg) */
uint32_t i;
enum gbe_arg_type arg_type; /* kind of argument */
for (i = 0; i < k->arg_n; ++i) {
uint32_t offset; // location of the address in the curbe
arg_type = gbe_kernel_get_arg_type(k->opaque, i);
if (arg_type != GBE_ARG_GLOBAL_PTR &&
arg_type != GBE_ARG_CONSTANT_PTR &&
arg_type != GBE_ARG_IMAGE &&
arg_type != GBE_ARG_SAMPLER)
continue;
offset = gbe_kernel_get_curbe_offset(k->opaque, GBE_CURBE_KERNEL_ARGUMENT, i);
if (arg_type == GBE_ARG_IMAGE) {
uint32_t *curbe_index = (uint32_t*)(k->curbe + offset);
cl_gpgpu_bind_image(queue->gpgpu, curbe_index, k->args[i].mem->bo,
k->args[i].mem->intel_fmt, k->args[i].mem->type,
k->args[i].mem->w, k->args[i].mem->h,
k->args[i].mem->pitch, k->args[i].mem->tiling);
} else if (arg_type == GBE_ARG_SAMPLER) {
uint32_t *curbe_index = (uint32_t*)(k->curbe + offset);
cl_gpgpu_insert_sampler(queue->gpgpu, curbe_index, k->args[i].sampler);
} else
cl_gpgpu_bind_buf(queue->gpgpu, k->args[i].mem->bo, offset, cc_llc_l3);
}
return CL_SUCCESS;
}
#if USE_FULSIM
extern void drm_intel_bufmgr_gem_stop_aubfile(cl_buffer_mgr);
extern void drm_intel_bufmgr_gem_set_aubfile(cl_buffer_mgr, FILE*);
extern void aub_exec_dump_raw_file(cl_buffer, size_t offset, size_t sz);
static void
cl_run_fulsim(void)
{
const char *run_it = getenv("OCL_SIMULATOR");
const char *debug_mode = getenv("OCL_FULSIM_DEBUG_MODE");
if (run_it == NULL || strcmp(run_it, "1")) return;
#if EMULATE_GEN == 7 /* IVB */
if (debug_mode == NULL || strcmp(debug_mode, "1"))
system("wine AubLoad.exe dump.aub -device ivbB0");
else
system("wine AubLoad.exe dump.aub -device ivbB0 -debug");
#elif EMULATE_GEN == 75 /* HSW */
if (debug_mode == NULL || strcmp(debug_mode, "1"))
system("wine AubLoad.exe dump.aub -device hsw.h.a0");
else
system("wine AubLoad.exe dump.aub -device hsw.h.a0 -debug");
#else
#error "Unknown device"
#endif
}
/* Each buffer is dump using several chunks of this size */
static const size_t chunk_sz = 8192u;
static cl_int
cl_fulsim_dump_all_surfaces(cl_command_queue queue, cl_kernel k)
{
cl_int err = CL_SUCCESS;
cl_mem mem = NULL;
int i;
size_t j;
/* Bind user defined surface */
for (i = 0; i < k->arg_n; ++i) {
size_t chunk_n, chunk_remainder;
if (gbe_kernel_get_arg_type(k->opaque, i) != GBE_ARG_GLOBAL_PTR)
continue;
mem = (cl_mem) k->args[i].mem;
CHECK_MEM(mem);
chunk_n = cl_buffer_get_size(mem->bo) / chunk_sz;
chunk_remainder = cl_buffer_get_size(mem->bo) % chunk_sz;
for (j = 0; j < chunk_n; ++j)
aub_exec_dump_raw_file(mem->bo, j * chunk_sz, chunk_sz);
if (chunk_remainder)
aub_exec_dump_raw_file(mem->bo, chunk_n * chunk_sz, chunk_remainder);
}
error:
return err;
}
struct bmphdr {
/* 2 bytes of magic here, "BM", total header size is 54 bytes! */
int filesize; /* 4 total file size incl header */
short as0, as1; /* 8 app specific */
int bmpoffset; /* 12 ofset of bmp data */
int headerbytes; /* 16 bytes in header from this point (40 actually) */
int width; /* 20 */
int height; /* 24 */
short nplanes; /* 26 no of color planes */
short bpp; /* 28 bits/pixel */
int compression; /* 32 BI_RGB = 0 = no compression */
int sizeraw; /* 36 size of raw bmp file, excluding header, incl padding */
int hres; /* 40 horz resolutions pixels/meter */
int vres; /* 44 */
int npalcolors; /* 48 No of colors in palette */
int nimportant; /* 52 No of important colors */
/* raw b, g, r data here, dword aligned per scan line */
};
static int*
cl_read_bmp(const char *filename, int *width, int *height)
{
int n;
struct bmphdr hdr;
FILE *fp = fopen(filename, "rb");
assert(fp);
char magic[2];
n = fread(&magic[0], 1, 2, fp);
assert(n == 2 && magic[0] == 'B' && magic[1] == 'M');
n = fread(&hdr, 1, sizeof(hdr), fp);
assert(n == sizeof(hdr));
assert(hdr.width > 0 &&
hdr.height > 0 &&
hdr.nplanes == 1
&& hdr.compression == 0);
int *rgb32 = (int *) cl_malloc(hdr.width * hdr.height * sizeof(int));
assert(rgb32);
int x, y;
int *dst = rgb32;
for (y = 0; y < hdr.height; y++) {
for (x = 0; x < hdr.width; x++) {
assert(!feof(fp));
int b = (getc(fp) & 0x0ff);
int g = (getc(fp) & 0x0ff);
int r = (getc(fp) & 0x0ff);
*dst++ = (r | (g << 8) | (b << 16) | 0xff000000); /* abgr */
}
while (x & 3) {
getc(fp);
x++;
}
}
fclose(fp);
*width = hdr.width;
*height = hdr.height;
return rgb32;
}
static char*
cl_read_dump(const char *name, size_t *size)
{
char *raw = NULL, *dump = NULL;
size_t i, sz;
int w, h;
if ((raw = (char*) cl_read_bmp(name, &w, &h)) == NULL)
return NULL;
sz = w * h;
dump = (char*) cl_malloc(sz);
assert(dump);
for (i = 0; i < sz; ++i)
dump[i] = raw[4*i];
cl_free(raw);
if (size)
*size = sz;
return dump;
}
static cl_int
cl_fulsim_read_all_surfaces(cl_command_queue queue, cl_kernel k)
{
cl_int err = CL_SUCCESS;
cl_mem mem = NULL;
char *from = NULL, *to = NULL;
size_t size, j, chunk_n, chunk_remainder;
int i, curr = 0;
/* Bind user defined surface */
for (i = 0; i < k->arg_n; ++i) {
if (gbe_kernel_get_arg_type(k->opaque, i) != GBE_ARG_GLOBAL_PTR)
continue;
mem = (cl_mem) k->args[i].mem;
CHECK_MEM(mem);
assert(mem->bo);
chunk_n = cl_buffer_get_size(mem->bo) / chunk_sz;
chunk_remainder = cl_buffer_get_size(mem->bo) % chunk_sz;
to = cl_mem_map(mem);
for (j = 0; j < chunk_n; ++j) {
char name[256];
sprintf(name, "dump%03i.bmp", curr);
#ifdef NDEBUG
from = cl_read_dump(name, NULL);
#else
from = cl_read_dump(name, &size);
assert(size == chunk_sz);
#endif /* NDEBUG */
memcpy(to + j*chunk_sz, from, chunk_sz);
cl_free(from);
curr++;
}
if (chunk_remainder) {
char name[256];
sprintf(name, "dump%03i.bmp", curr);
#ifdef NDEBUG
from = cl_read_dump(name, NULL);
#else
from = cl_read_dump(name, &size);
assert(size == chunk_remainder);
#endif /* NDEBUG */
memcpy(to + chunk_n*chunk_sz, from, chunk_remainder);
cl_free(from);
curr++;
}
cl_mem_unmap(mem);
}
error:
return err;
}
#endif
extern cl_int cl_command_queue_ND_range_gen7(cl_command_queue, cl_kernel, const size_t *, const size_t *, const size_t *);
static cl_int
cl_kernel_check_args(cl_kernel k)
{
uint32_t i;
for (i = 0; i < k->arg_n; ++i)
if (k->args[i].is_set == CL_FALSE)
return CL_INVALID_KERNEL_ARGS;
return CL_SUCCESS;
}
LOCAL cl_int
cl_command_queue_ND_range(cl_command_queue queue,
cl_kernel k,
const size_t *global_wk_off,
const size_t *global_wk_sz,
const size_t *local_wk_sz)
{
const int32_t ver = cl_driver_get_ver(queue->ctx->drv);
cl_int err = CL_SUCCESS;
/* Check that the user did not forget any argument */
TRY (cl_kernel_check_args, k);
#if USE_FULSIM
cl_buffer_mgr bufmgr = NULL;
FILE *file = NULL;
const char *run_it = getenv("OCL_SIMULATOR");
if (run_it != NULL && strcmp(run_it, "1") == 0) {
file = fopen("dump.aub", "wb");
FATAL_IF (file == NULL, "Unable to open file dump.aub");
bufmgr = cl_context_get_bufmgr(queue->ctx);
drm_intel_bufmgr_gem_set_aubfile(bufmgr, file);
}
#endif /* USE_FULSIM */
if (ver == 7 || ver == 75)
TRY (cl_command_queue_ND_range_gen7, queue, k, global_wk_off, global_wk_sz, local_wk_sz);
else
FATAL ("Unknown Gen Device");
#if USE_FULSIM
if (run_it != NULL && strcmp(run_it, "1") == 0) {
TRY (cl_fulsim_dump_all_surfaces, queue, k);
drm_intel_bufmgr_gem_stop_aubfile(bufmgr);
fclose(file);
cl_run_fulsim();
TRY (cl_fulsim_read_all_surfaces, queue, k);
}
#endif /* USE_FULSIM */
error:
return err;
}
LOCAL cl_int
cl_command_queue_finish(cl_command_queue queue)
{
if (queue->last_batch == NULL)
return CL_SUCCESS;
cl_buffer_wait_rendering(queue->last_batch);
cl_buffer_unreference(queue->last_batch);
queue->last_batch = NULL;
return CL_SUCCESS;
}
|