summaryrefslogtreecommitdiff
path: root/libclapi/cl_platform_id.c
blob: 493b3957b120391de4f76da7c90d4da20d61308c (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
/*
 * 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 "CL/cl.h"
#include "CL/cl_ext.h"
#include "cl_platform_id.h"
#include "cl_extension.h"
#include "cl_internals.h"
#include "cl_alloc.h"
#include "cl_mutex.h"

#define _STR(x) #x
#define _JOINT(x, y) _STR(x) "." _STR(y)
#define _JOINT3(x, y, z) _STR(x) "." _STR(y) "." _STR(z)
#define LIBCL_VERSION_STRING "OpenCL " _JOINT(1, 2) " beignet "


#define DECL_FIELD(CASE,FIELD)                                  \
  case JOIN(CL_,CASE):                                          \
    if (param_value_size < platform->JOIN(FIELD,_sz))           \
      return CL_INVALID_VALUE;                                  \
    if (param_value_size_ret != NULL)                           \
      *param_value_size_ret = platform->JOIN(FIELD,_sz);        \
    memcpy(param_value,                                         \
           platform->FIELD,                                     \
           platform->JOIN(FIELD,_sz));                          \
      return CL_SUCCESS;

#define GET_FIELD_SZ(CASE,FIELD)                                \
  case JOIN(CL_,CASE):                                          \
    if (param_value_size_ret != NULL)                           \
      *param_value_size_ret = platform->JOIN(FIELD,_sz);        \
    return CL_SUCCESS;

cl_int cl_get_platform_info(cl_platform_id platform, cl_platform_info param_name,
                            size_t param_value_size, void *param_value, size_t *param_value_size_ret)
{
  if (param_value == NULL) {
    switch (param_name) {
        GET_FIELD_SZ(PLATFORM_PROFILE, profile);
        GET_FIELD_SZ(PLATFORM_VERSION, version);
        GET_FIELD_SZ(PLATFORM_NAME, name);
        GET_FIELD_SZ(PLATFORM_VENDOR, vendor);
        GET_FIELD_SZ(PLATFORM_EXTENSIONS, extensions);
        GET_FIELD_SZ(PLATFORM_ICD_SUFFIX_KHR, icd_suffix_khr);
      default:
        return CL_INVALID_VALUE;
    }
  }

  /* Fetch the platform inform */
  switch (param_name) {
      DECL_FIELD(PLATFORM_PROFILE, profile);
      DECL_FIELD(PLATFORM_VERSION, version);
      DECL_FIELD(PLATFORM_NAME, name);
      DECL_FIELD(PLATFORM_VENDOR, vendor);
      DECL_FIELD(PLATFORM_EXTENSIONS, extensions);
      DECL_FIELD(PLATFORM_ICD_SUFFIX_KHR, icd_suffix_khr);
    default:
      return CL_INVALID_VALUE;
  }

}
#undef DECL_FIELD
#undef GET_FIELD_SZ

#define DECL_INFO_STRING(FIELD, STRING) \
    .FIELD = STRING,                    \
    .FIELD##_sz = sizeof(STRING),

/* This extension should be common for all the intel platform.
   Every device may have its own additional externsions. */
static _cl_extensions intel_platform_extensions = {
  {
#define DECL_EXT(name) \
  {(struct cl_extension_base){.ext_id = cl_##name##_ext_id, .ext_name = "cl_" #name, .ext_enabled = 0}},
    DECL_ALL_EXTENSIONS
  },
#undef DECL_EXT
};

static _cl_platform_id intel_platform = {
  .magic = CL_MAGIC_PLATFORM_HEADER,
  DECL_INFO_STRING(profile, "FULL_PROFILE")
  DECL_INFO_STRING(version, LIBCL_VERSION_STRING)
  DECL_INFO_STRING(name, "Intel Platform OCL Implementation")
  DECL_INFO_STRING(vendor, "Intel")
  DECL_INFO_STRING(icd_suffix_khr, "Intel")
  .all_extensions = &intel_platform_extensions,
};

#undef DECL_INFO_STRING

void static cl_init_platform(void)
{
  static int inited = 0;

  if (!inited) {
    CL_ALLOC_DEBUG_INIT();
    CL_MUTEX_DEBUG_INIT();
    cl_platform_extension_init(intel_platform.all_extensions,
                               intel_platform.extensions, CL_MAX_EXTENSION_LENGTH);
    intel_platform.extensions_sz = strlen(intel_platform.extensions);
    inited = 1;
  }
}

/**************************************************************************************
 *************************           CL APIs           ********************************
 **************************************************************************************/
cl_int
clGetPlatformIDs(cl_uint          num_entries,
                 cl_platform_id * platforms,
                 cl_uint *        num_platforms)
{
  if(UNLIKELY(platforms == NULL && num_platforms == NULL))
    return CL_INVALID_VALUE;
  if(UNLIKELY(num_entries == 0 && platforms != NULL))
    return CL_INVALID_VALUE;

  cl_init_platform();

  if (platforms)
    *platforms = &intel_platform;

  if (num_platforms)
    *num_platforms = 1;

  return CL_SUCCESS;
}


cl_int
clGetPlatformInfo(cl_platform_id    platform,
                  cl_platform_info  param_name,
                  size_t            param_value_size,
                  void *            param_value,
                  size_t *          param_value_size_ret)
{
  cl_int err = CL_SUCCESS;
  CHECK_PLATFORM(platform);

  err = cl_get_platform_info(platform, param_name, param_value_size,
                             param_value, param_value_size_ret);
error:
  return err;
}