summaryrefslogtreecommitdiff
path: root/libclapi/cl_context.c
blob: 2e6073daa78faf2eab45d68c54f841965a2189dd (plain)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * 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.1 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/>.
 *
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "CL/cl.h"
#include "CL/cl_gl.h"
#include "cl_platform_id.h"
#include "cl_device_id.h"
#include "cl_context.h"
#include "cl_alloc.h"
#include "cl_mutex.h"
#include "cl_internals.h"
#include "cl_driver.h"

#define CHECK(var) \
  if (var) \
    return CL_INVALID_PROPERTY; \
  else \
    var = 1;

static cl_int cl_context_properties_process(const cl_context_properties *prop,
    struct _cl_context_prop *cl_props, cl_uint * prop_len)
{
  int set_cl_context_platform = 0,
      set_cl_gl_context_khr = 0,
      set_cl_egl_display_khr = 0,
      set_cl_glx_display_khr = 0,
      set_cl_wgl_hdc_khr = 0,
      set_cl_cgl_sharegroup_khr = 0;
  cl_int err = CL_SUCCESS;

  cl_props->gl_type = CL_GL_NOSHARE;
  cl_props->platform_id = 0;

  if (prop == NULL)
    goto exit;

  while(*prop) {
    switch (*prop) {
      case CL_CONTEXT_PLATFORM:
        CHECK (set_cl_context_platform);
        cl_props->platform_id = *(prop + 1);
        if (UNLIKELY(((cl_platform_id) (cl_props->platform_id))->magic != CL_MAGIC_PLATFORM_HEADER)) {
          err = CL_INVALID_PLATFORM;
          goto exit;
        }
        break;
      case CL_GL_CONTEXT_KHR:
        CHECK (set_cl_gl_context_khr);
        cl_props->gl_context = *(prop + 1);
        break;
      case CL_EGL_DISPLAY_KHR:
        CHECK (set_cl_egl_display_khr);
        cl_props->gl_type = CL_GL_EGL_DISPLAY;
        cl_props->egl_display = *(prop + 1);
        break;
      case CL_GLX_DISPLAY_KHR:
        CHECK (set_cl_glx_display_khr);
        cl_props->gl_type = CL_GL_GLX_DISPLAY;
        cl_props->glx_display = *(prop + 1);
        break;
      case CL_WGL_HDC_KHR:
        CHECK (set_cl_wgl_hdc_khr);
        cl_props->gl_type = CL_GL_WGL_HDC;
        cl_props->wgl_hdc = *(prop + 1);
        break;
      case CL_CGL_SHAREGROUP_KHR:
        CHECK (set_cl_cgl_sharegroup_khr);
        cl_props->gl_type = CL_GL_CGL_SHAREGROUP;
        cl_props->cgl_sharegroup = *(prop + 1);
        break;
      default:
        err = CL_INVALID_PROPERTY;
        goto exit;
    }
    prop += 2;
    *prop_len += 2;
  }
  (*prop_len)++;

exit:
  return err;
}

static cl_context cl_context_new(const cl_context_properties *properties,
                                 struct _cl_context_prop* props, cl_uint prop_len,
                                 cl_uint num_devices, const cl_device_id *devices,
                                 void (CL_CALLBACK * pfn_notify) (const char*, const void*, size_t, void*),
                                 void *user_data)
{
  cl_context ctx = NULL;
  cl_uint i;

  ctx = CL_CALLOC(1, sizeof(_cl_context));
  if (ctx == NULL)
    return NULL;

  ctx->devices = CL_CALLOC(num_devices, sizeof(cl_device_id));
  if (ctx->devices == NULL) {
    CL_FREE(ctx);
    return NULL;
  }

  /* Create the private pointer array if device > 1 */
  if (num_devices > 1) {
    ctx->pdata = CL_CALLOC(num_devices, sizeof(void*));
    if (ctx->pdata == NULL) {
      CL_FREE(ctx->devices);
      CL_FREE(ctx);
      return NULL;
    }
  }

  if(properties != NULL && prop_len > 0) {
    ctx->prop_user = CL_CALLOC(prop_len, sizeof(cl_context_properties));
    if (ctx->prop_user == NULL) {
      CL_FREE(ctx->devices);
      if (ctx->device_num > 1) {
        CL_FREE(ctx->pdata);
      }
      CL_FREE(ctx);
      return NULL;
    }

    memcpy(ctx->prop_user, properties, sizeof(cl_context_properties)*prop_len);
  }

  ctx->props = *props;
  ctx->magic = CL_MAGIC_CONTEXT_HEADER;
  cl_ref_set_val(&ctx->ref_n, 1);
  CL_MUTEX_INIT(&ctx->lock);
  CL_COND_INIT(&ctx->cond);

  memcpy(ctx->devices, devices, sizeof(cl_device_id)*num_devices);
  ctx->device_num = num_devices;
  /* Add all refs to devices. */
  for (i = 0; i < num_devices; i++) {
    cl_retain_device_id(devices[i]);
  }

  ctx->prop_len = prop_len;
  /* Save the user callback and user data*/
  ctx->pfn_notify = pfn_notify;
  ctx->user_data = user_data;
  return ctx;
}

static void cl_context_delete(cl_context ctx)
{
  cl_uint i;

  assert(ctx);

  CL_MUTEX_DESTROY(&ctx->lock);
  CL_COND_DESTROY(&ctx->cond);
  /* delete  refs to devices. */
  for (i = 0; i < ctx->device_num; i++) {
    cl_release_device_id(ctx->devices[i]);
  }

  if (ctx->device_num > 1) {
    CL_FREE(ctx->pdata);
  }
  CL_FREE(ctx->prop_user);
  CL_FREE(ctx->devices);
  ctx->magic = CL_MAGIC_DEAD_HEADER; /* For safety */
  CL_FREE(ctx);
}

LOCAL void cl_release_context(cl_context ctx)
{
  cl_uint i;
  cl_int err;

  assert(ctx);
  cl_int barrier_events_num = 0;
  cl_event* barrier_events_list = NULL;

  /* Add a check point here. */
  barrier_events_list = cl_check_context_barrier_events(ctx, &barrier_events_num, &err);
  assert(err == CL_SUCCESS);
  if (barrier_events_list) {
    assert(barrier_events_num > 0);
    CL_FREE(barrier_events_list);
  }

  /* We are not done yet */
  if (cl_ref_dec(&ctx->ref_n) > 1)
    return;

  /* All object lists should have been freed. Otherwise, the reference counter
   * of the context cannot be 0 */
  assert(ctx->queues == NULL);
  assert(ctx->programs == NULL);
  assert(ctx->buffers == NULL);
  assert(ctx->events == NULL);
  assert(ctx->barrier_events == NULL);
  assert(ctx->barrier_events_num == 0);

  for (i = 0; i < ctx->device_num; i++) {
    ctx->devices[i]->driver->release_context(ctx, ctx->devices[i]);
  }

  cl_context_delete(ctx);
}

static cl_context cl_create_context(const cl_context_properties *properties, cl_uint num_devices,
                                    const cl_device_id *devices,
                                    void (CL_CALLBACK * pfn_notify) (const char*, const void*, size_t, void*),
                                    void *user_data, cl_int *errcode_ret)
{
  /* cl_platform_id platform = NULL; */
  struct _cl_context_prop props;
  cl_context ctx = NULL;
  cl_int err = CL_SUCCESS;
  cl_uint prop_len = 0;
  cl_uint i;

  /* Check that we are getting the right platform */
  if (UNLIKELY(((err = cl_context_properties_process(properties, &props, &prop_len)) != CL_SUCCESS)))
    goto exit;

  /* We are good */
  if (UNLIKELY((ctx = cl_context_new(properties, &props, prop_len, num_devices, devices,
                                     pfn_notify, user_data)) == NULL)) {
    err = CL_OUT_OF_HOST_MEMORY;
    goto exit;
  }

  for (i = 0; i < num_devices; i++) {
    err = ctx->devices[i]->driver->create_context(ctx, ctx->devices[i]);
    if (err != CL_SUCCESS)
      break;
  }

  if (err != CL_SUCCESS)
    cl_release_context(ctx);

exit:
  if (errcode_ret != NULL)
    *errcode_ret = err;
  return ctx;
}

LOCAL cl_int cl_retain_context(cl_context ctx)
{
  assert(ctx);
  int ret = cl_ref_inc_if_positive(&ctx->ref_n);
  if (ret > 0)
    return ret;

  return 0;
}

LOCAL cl_int cl_context_get_device_index(cl_context ctx, const cl_device_id device)
{
  cl_uint i = 0;

  assert(device);

  for (i = 0; i < ctx->device_num; i++) {
    if (device == ctx->devices[i]) {
      return i;
    }
  }

  return -1;
}

static cl_int cl_get_context_info(cl_context context, cl_context_info param_name, size_t param_value_size,
                                  void *param_value, size_t *param_value_size_ret)
{

  if (param_name == CL_CONTEXT_DEVICES) {
    FILL_GETINFO_RET(cl_device_id, context->device_num, context->devices, CL_SUCCESS);
  } else if (param_name == CL_CONTEXT_NUM_DEVICES) {
    cl_uint n = context->device_num;
    FILL_GETINFO_RET(cl_uint, 1, &n, CL_SUCCESS);
  } else if (param_name == CL_CONTEXT_REFERENCE_COUNT) {
    cl_uint ref = cl_ref_get_val(&context->ref_n);
    FILL_GETINFO_RET(cl_uint, 1, &ref, CL_SUCCESS);
  } else if (param_name == CL_CONTEXT_PROPERTIES) {
    if(context->prop_len > 0) {
      FILL_GETINFO_RET(cl_context_properties, context->prop_len, context->prop_user, CL_SUCCESS);
    } else {
      cl_context_properties n = 0;
      FILL_GETINFO_RET(cl_context_properties, 1, &n, CL_SUCCESS);
    }
  } else {
    return CL_INVALID_VALUE;
  }
}

/**************************************************************************************
 *************************           CL APIs           ********************************
 **************************************************************************************/
cl_context
clCreateContext(const cl_context_properties *  properties,
                cl_uint                        num_devices,
                const cl_device_id *           devices,
                void (* pfn_notify) (const char*, const void*, size_t, void*),
                void *                         user_data,
                cl_int *                       errcode_ret)
{
  cl_int err = CL_SUCCESS;
  cl_context context = NULL;

  /* Assert parameters correctness */
  INVALID_VALUE_IF(devices == NULL);
  INVALID_VALUE_IF(num_devices == 0);
  INVALID_VALUE_IF(pfn_notify == NULL && user_data != NULL);

  context = cl_create_context(properties, num_devices, devices, pfn_notify, user_data, &err);
error:
  if (errcode_ret)
    *errcode_ret = err;
  return context;
}

cl_context
clCreateContextFromType(const cl_context_properties *  properties,
                        cl_device_type                 device_type,
                        void (CL_CALLBACK *pfn_notify) (const char *, const void *, size_t, void *),
                        void *                         user_data,
                        cl_int *                       errcode_ret)
{
  cl_context context = NULL;
  cl_int err = CL_SUCCESS;
  cl_device_id devices[1];
  cl_uint num_devices = 1;

  INVALID_VALUE_IF(pfn_notify == NULL && user_data != NULL);

// err = cl_check_device_type(device_type);
  if(err != CL_SUCCESS) {
    goto error;
  }









  //err = cl_get_device_ids(NULL,
  //                        device_type,
  //                        1,
  //                       &devices[0],
  //                      &num_devices);
  if (err != CL_SUCCESS) {
    goto error;
  }

  context = cl_create_context(properties, num_devices, devices, pfn_notify, user_data, &err);
error:
  if (errcode_ret)
    *errcode_ret = err;
  return context;
}

cl_int
clRetainContext(cl_context context)
{
  cl_int err = CL_SUCCESS;
  CHECK_CONTEXT(context);

  if (cl_retain_context(context) == 0) {
    err = CL_INVALID_CONTEXT;
  }

error:
  return err;
}

cl_int
clReleaseContext(cl_context context)
{
  cl_int err = CL_SUCCESS;
  CHECK_CONTEXT(context);
  cl_release_context(context);
error:
  return err;
}

cl_int
clGetContextInfo(cl_context      context,
                 cl_context_info param_name,
                 size_t          param_value_size,
                 void *          param_value,
                 size_t *        param_value_size_ret)
{
  cl_int err = CL_SUCCESS;
  CHECK_CONTEXT(context);

  err = cl_get_context_info(context, param_name, param_value_size,
                            param_value, param_value_size_ret);

error:
  return err;
}