summaryrefslogtreecommitdiff
path: root/libclapi/cl_device_id.c
blob: 30667698f5ad13cef18e98c0267da899cda9f416 (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
/*
 * 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 <string.h>
#include <dlfcn.h>
#include <assert.h>
#include <sys/types.h>
#include <dirent.h>
#include "cl_alloc.h"
#include "cl_device_id.h"
#include "cl_internals.h"
#include "cl_driver.h"
#include "cl_platform_id.h"
#include "backend/src/GBEConfig.h"

static cl_int cl_check_device_type(cl_device_type device_type)
{
  const cl_device_type valid =  CL_DEVICE_TYPE_GPU
                                | CL_DEVICE_TYPE_CPU
                                | CL_DEVICE_TYPE_ACCELERATOR
                                | CL_DEVICE_TYPE_DEFAULT
                                | CL_DEVICE_TYPE_CUSTOM;

  if((device_type & valid) == 0) {
    return CL_INVALID_DEVICE_TYPE;
  }

  return CL_SUCCESS;
}

typedef struct _cl_driver_backend {
  char* path_name;
  void* handle;
  cl_driver drv;
} _cl_driver_backend;
typedef _cl_driver_backend* cl_driver_backend;

static cl_driver_backend cl_all_drivers = NULL;
static int cl_driver_num = 0;
static int cl_driver_inited = 0;

static cl_int cl_get_device_ids(cl_platform_id platform, cl_device_type device_type,
                                cl_uint num_entries, cl_device_id *devices, cl_uint *num_devices)
{
  char* p;
  DIR* dir = NULL;
  struct dirent *dirp = NULL;
  cl_driver drv = NULL;
  cl_uint total_dev_num = 0;


  if (cl_driver_inited == 0) {
    char* dir_path = CL_MALLOC((strlen(OCL_DRIVER_DIRS) + 1) * sizeof(char));
    strcpy(dir_path, OCL_DRIVER_DIRS);

    cl_driver_inited = 1;

    p = strtok(dir_path,":");
    while (p) {
      dir = opendir(p);
      if (!dir) {
        p = strtok(NULL,":");
        continue;
      }

      while ((dirp = readdir(dir)) != NULL) {
        void *handle = NULL;
        char driver_name[64];
        char* path;

        int len = strlen(dirp->d_name);
        /* Should end with libclxx.so */
        if (len < 9)
          continue;

        if (dirp->d_name[len - 1] != 'o' || dirp->d_name[len - 2] != 's'
            || dirp->d_name[len - 3] != '.' || dirp->d_name[0] != 'l'
            || dirp->d_name[1] != 'i' || dirp->d_name[2] != 'b'
            || dirp->d_name[3] != 'c' || dirp->d_name[4] != 'l')
          continue;

        /* Try to dlopen it. */
        path = CL_MALLOC(len + strlen(p) + 1 + 1);
        sprintf(path, "%s/%s", p, dirp->d_name);
        handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
        if (!handle) {
          CL_FREE(path);
          continue;
        }

        /* global var named clxxxDriver */
        snprintf(driver_name, 60, "%s", &dirp->d_name[3]);
        len = strlen(driver_name);
        driver_name[len - 3] = 'D';
        driver_name[len - 2] = 'r';
        driver_name[len - 1] = 'i';
        driver_name[len] = 'v';
        driver_name[len + 1] = 'e';
        driver_name[len + 2] = 'r';
        driver_name[len + 3] = 0;

        drv = (cl_driver)dlsym(handle, driver_name);
        if (drv == NULL) {
          dlclose(handle);
          CL_FREE(path);
          continue;
        }

        /* Try it.*/
        if (cl_driver_check(drv) != CL_SUCCESS || drv->init(platform) != CL_SUCCESS) {
          dlclose(handle);
          CL_FREE(path);
          continue;
        }

        cl_driver_num++;
        cl_driver_backend tmp_d = CL_REALLOC(cl_all_drivers, cl_driver_num * sizeof(_cl_driver_backend));
        cl_all_drivers = tmp_d;
        cl_all_drivers[cl_driver_num - 1].path_name = path;
        cl_all_drivers[cl_driver_num - 1].handle = handle;
        cl_all_drivers[cl_driver_num - 1].drv = drv;
      }

      closedir(dir);
      p = strtok(NULL,":");
    }

    CL_FREE(dir_path);
  }

  if (cl_driver_num) {
    int i,j;
    cl_uint tmp_n;
    for (i = 0; i < cl_driver_num; i++) {
      if (cl_all_drivers[i].drv->get_device_ids(platform, device_type, 0, NULL, &tmp_n) == CL_SUCCESS) {
        if (devices && tmp_n) {
          cl_device_id *tmp_devices = CL_CALLOC(tmp_n, sizeof(cl_device_id));
          if (cl_all_drivers[i].drv->get_device_ids(
                platform, device_type, tmp_n, tmp_devices, &tmp_n) == CL_SUCCESS) {
            for (j = 0; j < tmp_n; j++) {
              if (total_dev_num >= num_entries)
                goto OUT;

              devices[total_dev_num] = tmp_devices[j];
              total_dev_num++;
            }
          }
          CL_FREE(tmp_devices);
        } else {
          total_dev_num += tmp_n;
        }
      }
    }
  }

OUT:
  if (num_devices)
    *num_devices = total_dev_num;

  if (total_dev_num)
    return CL_SUCCESS;

  return CL_DEVICE_NOT_FOUND;
}

#define DECL_FIELD(CASE,FIELD)                                      \
  case JOIN(CL_DEVICE_,CASE):                                       \
    if (param_value_size_ret) {                                     \
      *param_value_size_ret = sizeof device->FIELD;                 \
      if (!param_value)                                             \
        return CL_SUCCESS;                                          \
    }                                                               \
    if (param_value_size < sizeof device->FIELD)                    \
      return CL_INVALID_VALUE;                                      \
    memcpy(param_value, &device->FIELD, sizeof device->FIELD);      \
    return CL_SUCCESS;

#define DECL_STRING_FIELD(CASE,FIELD)                               \
  case JOIN(CL_DEVICE_,CASE):                                       \
    if (param_value_size_ret) {                                     \
      *param_value_size_ret = device->JOIN(FIELD,_sz);              \
      if (!param_value)                                             \
        return CL_SUCCESS;                                          \
    }                                                               \
    if (param_value_size < device->JOIN(FIELD,_sz))                 \
      return CL_INVALID_VALUE;                                      \
    memcpy(param_value, device->FIELD, device->JOIN(FIELD,_sz));    \
    return CL_SUCCESS;

LOCAL cl_int cl_get_device_info(cl_device_id device, cl_device_info param_name, size_t param_value_size,
                                void *param_value, size_t *param_value_size_ret)
{
  /* Find the correct parameter */
  switch (param_name) {
      DECL_FIELD(TYPE, device_type)
      DECL_FIELD(VENDOR_ID, vendor_id)
      DECL_FIELD(MAX_COMPUTE_UNITS, max_compute_unit)
      DECL_FIELD(MAX_WORK_ITEM_DIMENSIONS, max_work_item_dimensions)
      DECL_FIELD(MAX_WORK_ITEM_SIZES, max_work_item_sizes)
      DECL_FIELD(MAX_WORK_GROUP_SIZE, max_work_group_size)
      DECL_FIELD(PREFERRED_VECTOR_WIDTH_CHAR, preferred_vector_width_char)
      DECL_FIELD(PREFERRED_VECTOR_WIDTH_SHORT, preferred_vector_width_short)
      DECL_FIELD(PREFERRED_VECTOR_WIDTH_INT, preferred_vector_width_int)
      DECL_FIELD(PREFERRED_VECTOR_WIDTH_LONG, preferred_vector_width_long)
      DECL_FIELD(PREFERRED_VECTOR_WIDTH_FLOAT, preferred_vector_width_float)
      DECL_FIELD(PREFERRED_VECTOR_WIDTH_DOUBLE, preferred_vector_width_double)
      DECL_FIELD(PREFERRED_VECTOR_WIDTH_HALF, preferred_vector_width_half)
      DECL_FIELD(NATIVE_VECTOR_WIDTH_CHAR, native_vector_width_char)
      DECL_FIELD(NATIVE_VECTOR_WIDTH_SHORT, native_vector_width_short)
      DECL_FIELD(NATIVE_VECTOR_WIDTH_INT, native_vector_width_int)
      DECL_FIELD(NATIVE_VECTOR_WIDTH_LONG, native_vector_width_long)
      DECL_FIELD(NATIVE_VECTOR_WIDTH_FLOAT, native_vector_width_float)
      DECL_FIELD(NATIVE_VECTOR_WIDTH_DOUBLE, native_vector_width_double)
      DECL_FIELD(NATIVE_VECTOR_WIDTH_HALF, native_vector_width_half)
      DECL_FIELD(MAX_CLOCK_FREQUENCY, max_clock_frequency)
      DECL_FIELD(ADDRESS_BITS, address_bits)
      DECL_FIELD(MAX_MEM_ALLOC_SIZE, max_mem_alloc_size)
      DECL_FIELD(IMAGE_SUPPORT, image_support)
      DECL_FIELD(MAX_READ_IMAGE_ARGS, max_read_image_args)
      DECL_FIELD(MAX_WRITE_IMAGE_ARGS, max_write_image_args)
      DECL_FIELD(IMAGE_MAX_ARRAY_SIZE, image_max_array_size)
      DECL_FIELD(IMAGE2D_MAX_WIDTH, image2d_max_width)
      DECL_FIELD(IMAGE2D_MAX_HEIGHT, image2d_max_height)
      DECL_FIELD(IMAGE3D_MAX_WIDTH, image3d_max_width)
      DECL_FIELD(IMAGE3D_MAX_HEIGHT, image3d_max_height)
      DECL_FIELD(IMAGE3D_MAX_DEPTH, image3d_max_depth)
      DECL_FIELD(MAX_SAMPLERS, max_samplers)
      DECL_FIELD(MAX_PARAMETER_SIZE, max_parameter_size)
      DECL_FIELD(MEM_BASE_ADDR_ALIGN, mem_base_addr_align)
      DECL_FIELD(MIN_DATA_TYPE_ALIGN_SIZE, min_data_type_align_size)
      DECL_FIELD(SINGLE_FP_CONFIG, single_fp_config)
      DECL_FIELD(HALF_FP_CONFIG, half_fp_config)
      DECL_FIELD(DOUBLE_FP_CONFIG, double_fp_config)
      DECL_FIELD(GLOBAL_MEM_CACHE_TYPE, global_mem_cache_type)
      DECL_FIELD(GLOBAL_MEM_CACHELINE_SIZE, global_mem_cache_line_size)
      DECL_FIELD(GLOBAL_MEM_CACHE_SIZE, global_mem_cache_size)
      DECL_FIELD(GLOBAL_MEM_SIZE, global_mem_size)
      DECL_FIELD(MAX_CONSTANT_BUFFER_SIZE, max_constant_buffer_size)
      DECL_FIELD(IMAGE_MAX_BUFFER_SIZE, image_max_mem_size)
      DECL_FIELD(MAX_CONSTANT_ARGS, max_constant_args)
      DECL_FIELD(LOCAL_MEM_TYPE, local_mem_type)
      DECL_FIELD(LOCAL_MEM_SIZE, local_mem_size)
      DECL_FIELD(ERROR_CORRECTION_SUPPORT, error_correction_support)
      DECL_FIELD(HOST_UNIFIED_MEMORY, host_unified_memory)
      DECL_FIELD(PROFILING_TIMER_RESOLUTION, profiling_timer_resolution)
      DECL_FIELD(ENDIAN_LITTLE, endian_little)
      DECL_FIELD(AVAILABLE, available)
      DECL_FIELD(COMPILER_AVAILABLE, compiler_available)
      DECL_FIELD(LINKER_AVAILABLE, linker_available)
      DECL_FIELD(EXECUTION_CAPABILITIES, execution_capabilities)
      DECL_FIELD(QUEUE_PROPERTIES, queue_properties)
      DECL_FIELD(PLATFORM, platform)
      DECL_FIELD(PRINTF_BUFFER_SIZE, printf_buffer_size)
      DECL_FIELD(PREFERRED_INTEROP_USER_SYNC, interop_user_sync)
      DECL_STRING_FIELD(NAME, name)
      DECL_STRING_FIELD(VENDOR, vendor)
      DECL_STRING_FIELD(VERSION, version)
      DECL_STRING_FIELD(PROFILE, profile)
      DECL_STRING_FIELD(OPENCL_C_VERSION, opencl_c_version)
      DECL_STRING_FIELD(SPIR_VERSIONS, spir_versions)
      DECL_STRING_FIELD(EXTENSIONS, extensions);
      DECL_STRING_FIELD(BUILT_IN_KERNELS, built_in_kernels)
      DECL_FIELD(PARENT_DEVICE, parent)
      DECL_FIELD(PARTITION_MAX_SUB_DEVICES, partition_max_sub_device)
      DECL_FIELD(PARTITION_PROPERTIES, partition_property)
      DECL_FIELD(PARTITION_AFFINITY_DOMAIN, affinity_domain)
      DECL_FIELD(PARTITION_TYPE, partition_type)
      DECL_FIELD(IMAGE_PITCH_ALIGNMENT, image_pitch_alignment)
      DECL_FIELD(IMAGE_BASE_ADDRESS_ALIGNMENT, image_base_address_alignment)

    case CL_DEVICE_REFERENCE_COUNT:
      if (param_value_size_ret) {
        *param_value_size_ret = sizeof(int);
        if (!param_value)
          return CL_SUCCESS;
      }
      if (param_value_size < sizeof(int))
        return CL_INVALID_VALUE;
      *((int *)param_value) = cl_ref_get_val(&device->ref_n);
      return CL_SUCCESS;

    case CL_DRIVER_VERSION:
      if (param_value_size_ret) {
        *param_value_size_ret = device->driver_version_sz;
        if (!param_value)
          return CL_SUCCESS;
      }
      if (param_value_size < device->driver_version_sz)
        return CL_INVALID_VALUE;
      memcpy(param_value, device->driver_version, device->driver_version_sz);
      return CL_SUCCESS;

    default:
      return CL_INVALID_VALUE;
  };
}

LOCAL void cl_retain_device_id(cl_device_id device)
{
  assert(device);

  if (device->root)
    return;

  assert(0); // TODO: no sub device implemented, can to get here now.
}

LOCAL void cl_release_device_id(cl_device_id device)
{

  if (UNLIKELY(device == NULL))
    return;

  if (device->root)
    return;

  if (cl_ref_dec(&device->ref_n) > 1)
    return;

  assert(0); // TODO: no sub device implemented, can to get here now.
  //return CLDriver->release_device(device);
}

/**************************************************************************************
 *************************           CL APIs           ********************************
 **************************************************************************************/
cl_int
clGetDeviceInfo(cl_device_id   device,
                cl_device_info param_name,
                size_t         param_value_size,
                void *         param_value,
                size_t *       param_value_size_ret)
{
  return cl_get_device_info(device, param_name, param_value_size,
                            param_value, param_value_size_ret);
}

cl_int
clGetDeviceIDs(cl_platform_id platform,
               cl_device_type device_type,
               cl_uint        num_entries,
               cl_device_id * devices,
               cl_uint *      num_devices)
{
  cl_int err = CL_SUCCESS;

  /* Check parameter consistency */
  if (UNLIKELY(devices == NULL && num_devices == NULL))
    return CL_INVALID_VALUE;
  if (UNLIKELY(devices && num_entries == 0))
    return CL_INVALID_VALUE;

  err = cl_check_device_type(device_type);
  if(err != CL_SUCCESS)
    return err;

  return cl_get_device_ids(platform, device_type, num_entries, devices, num_devices);
}

cl_int
clRetainDevice(cl_device_id device)
{
  cl_int err = CL_SUCCESS;
  CHECK_DEVICE(device);
  cl_retain_device_id(device);
error:
  return err;
}

cl_int
clReleaseDevice(cl_device_id device)
{
  cl_int err = CL_SUCCESS;
  CHECK_DEVICE(device);
  cl_release_device_id(device);
error:
  return err;
}

cl_int
clCreateSubDevices(cl_device_id                         in_device,
                   const cl_device_partition_property * properties,
                   cl_uint                              num_devices,
                   cl_device_id *                       out_devices,
                   cl_uint *                            num_devices_ret)
{
  /* Check parameter consistency */
  if (UNLIKELY(out_devices == NULL && num_devices_ret == NULL))
    return CL_INVALID_VALUE;
  if (UNLIKELY(in_device == NULL && properties == NULL))
    return CL_INVALID_VALUE;

  // TODO: should implement create_sub_devices.
  //return CLDriver->create_sub_devices(platform, device_type, num_entries, devices, num_devices);

  *num_devices_ret = 0;
  return CL_INVALID_DEVICE_PARTITION_COUNT;
}