summaryrefslogtreecommitdiff
path: root/src/cl_api_command_queue.c
blob: 3fdfbc6e96ae08c0f41ba46bcf887eb362d3f372 (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
/*
 * 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 "cl_command_queue.h"
#include "cl_device_id.h"
#include "CL/cl.h"
#include <stdio.h>

/* Depreciated in 2.0 later */
cl_command_queue
clCreateCommandQueue(cl_context context,
                     cl_device_id device,
                     cl_command_queue_properties properties,
                     cl_int *errcode_ret)
{
  cl_queue_properties props[3];
  props[0] = CL_QUEUE_PROPERTIES;
  props[1] = properties;
  props[2] = 0;
  return clCreateCommandQueueWithProperties(context, device, props, errcode_ret);
}

/* 2.0 new API for create command queue. */
cl_command_queue
clCreateCommandQueueWithProperties(cl_context context,
                                   cl_device_id device,
                                   const cl_queue_properties *properties,
                                   cl_int *errcode_ret)
{
  cl_command_queue queue = NULL;
  cl_int err = CL_SUCCESS;
  cl_command_queue_properties prop = 0xFFFFFFFF;
  cl_uint queue_sz = 0xFFFFFFFF;

  do {
    if (!CL_OBJECT_IS_CONTEXT(context)) {
      err = CL_INVALID_CONTEXT;
      break;
    }

    err = cl_devices_list_include_check(context->device_num, context->devices, 1, &device);
    if (err)
      break;

    if (properties) {
      cl_ulong que_type;
      cl_ulong que_val;
      cl_uint i;
      for (i = 0; (que_type = properties[i++]) != 0; i++) {
        que_val = properties[i];
        switch (que_type) {
        case CL_QUEUE_PROPERTIES:
          if (prop != 0xFFFFFFFF)
            err = CL_INVALID_VALUE;
          else {
            switch (que_val) {
            case 0:
            case CL_QUEUE_PROFILING_ENABLE:
            case CL_QUEUE_PROFILING_ENABLE |
              CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE:
            case CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE:
            case CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_ON_DEVICE:
            case CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_ON_DEVICE |
              CL_QUEUE_ON_DEVICE_DEFAULT:
            case CL_QUEUE_PROFILING_ENABLE |
              CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_ON_DEVICE:
            case CL_QUEUE_PROFILING_ENABLE |
              CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_ON_DEVICE |
              CL_QUEUE_ON_DEVICE_DEFAULT:
              prop = que_val;
              break;
            default:
              err = CL_INVALID_VALUE;
              break;
            }
          }
          break;
        case CL_QUEUE_SIZE:
          queue_sz = que_val;
          break;
        default:
          err = CL_INVALID_VALUE;
          break;
        }
      }

      if (err) /* break the while and return some err. */
        break;
    }

    /* Set some paramters to default val. */
    if (prop == 0xFFFFFFFF)
      prop = 0;
    if (queue_sz != 0xFFFFFFFF)
      if (!(prop & CL_QUEUE_ON_DEVICE)) {
        err = CL_INVALID_VALUE;
        break;
      }
    if (queue_sz == 0xFFFFFFFF)
      queue_sz = device->queue_on_device_preferred_size;

    if (queue_sz > device->queue_on_device_max_size) {
      err = CL_INVALID_VALUE;
      break;
    }

    queue = cl_create_command_queue(context, device, prop, queue_sz, &err);
  } while (0);

  if (errcode_ret)
    *errcode_ret = err;
  return queue;
}

cl_int
clGetCommandQueueInfo(cl_command_queue command_queue,
                      cl_command_queue_info param_name,
                      size_t param_value_size,
                      void *param_value,
                      size_t *param_value_size_ret)
{
  const void *src_ptr = NULL;
  size_t src_size = 0;
  cl_int ref;

  if (!CL_OBJECT_IS_COMMAND_QUEUE(command_queue)) {
    return CL_INVALID_COMMAND_QUEUE;
  }

  if (param_name == CL_QUEUE_CONTEXT) {
    src_ptr = &command_queue->ctx;
    src_size = sizeof(cl_context);
  } else if (param_name == CL_QUEUE_DEVICE) {
    src_ptr = &command_queue->device;
    src_size = sizeof(cl_device_id);
  } else if (param_name == CL_QUEUE_REFERENCE_COUNT) {
    ref = CL_OBJECT_GET_REF(command_queue);
    src_ptr = &ref;
    src_size = sizeof(cl_int);
  } else if (param_name == CL_QUEUE_PROPERTIES) {
    src_ptr = &command_queue->props;
    src_size = sizeof(cl_command_queue_properties);
  } else if (param_name == CL_QUEUE_SIZE) {
    src_ptr = &command_queue->size;
    src_size = sizeof(command_queue->size);
  } else {
    return CL_INVALID_VALUE;
  }

  return cl_get_info_helper(src_ptr, src_size,
                            param_value, param_value_size, param_value_size_ret);
}

cl_int
clFlush(cl_command_queue command_queue)
{
  if (!CL_OBJECT_IS_COMMAND_QUEUE(command_queue)) {
    return CL_INVALID_COMMAND_QUEUE;
  }

  return cl_command_queue_wait_flush(command_queue);
}

cl_int
clFinish(cl_command_queue command_queue)
{
  if (!CL_OBJECT_IS_COMMAND_QUEUE(command_queue)) {
    return CL_INVALID_COMMAND_QUEUE;
  }

  return cl_command_queue_wait_finish(command_queue);
}

cl_int
clRetainCommandQueue(cl_command_queue command_queue)
{
  if (!CL_OBJECT_IS_COMMAND_QUEUE(command_queue)) {
    return CL_INVALID_COMMAND_QUEUE;
  }
  cl_command_queue_add_ref(command_queue);
  return CL_SUCCESS;
}

cl_int
clReleaseCommandQueue(cl_command_queue command_queue)
{
  if (!CL_OBJECT_IS_COMMAND_QUEUE(command_queue)) {
    return CL_INVALID_COMMAND_QUEUE;
  }

  cl_command_queue_wait_flush(command_queue);

  cl_command_queue_delete(command_queue);
  return CL_SUCCESS;
}