forked from OCL-dev/ocl-icd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libdummy_icd.c
266 lines (248 loc) · 8.35 KB
/
libdummy_icd.c
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
/**
Copyright (c) 2012-2020, Brice Videau <[email protected]>
Copyright (c) 2012-2020, Vincent Danjean <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ocl_icd_debug.h"
#include "libdummy_icd.h"
#include "libdummy_icd_gen.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define NUM_PLATFORMS 1
#if defined(__APPLE__) || defined(__MACOSX)
#define SYMB(f)
#else
#define SYMB(f) \
typeof(INT##f) f __attribute__ ((alias ("INT" #f), visibility("default")))
#endif
#pragma GCC visibility push(hidden)
int debug_ocl_icd_mask;
cl_uint const num_master_platforms = NUM_PLATFORMS;
struct _cl_platform_id master_platforms[NUM_PLATFORMS] = { {&master_dispatch} };
static cl_int _GetPlatformIDs(
cl_uint num_entries,
cl_platform_id *platforms,
cl_uint *num_platforms) {
debug_trace();
if( platforms == NULL && num_platforms == NULL )
return CL_INVALID_VALUE;
if( num_entries == 0 && platforms != NULL )
return CL_INVALID_VALUE;
if( num_master_platforms == 0)
return CL_PLATFORM_NOT_FOUND_KHR;
if( num_platforms != NULL ){
debug(D_LOG, " asked num_platforms");
*num_platforms = num_master_platforms; }
if( platforms != NULL ) {
debug(D_LOG, " asked %i platforms", num_entries);
cl_uint i;
for( i=0; i<(num_master_platforms<num_entries?num_master_platforms:num_entries); i++)
platforms[i] = &master_platforms[i];
}
return CL_SUCCESS;
}
#if defined(__APPLE__) || defined(__MACOSX)
CL_API_ENTRY cl_int __attribute__((visibility("default"))) CL_API_CALL clGetPlatformIDs(
cl_uint num_entries,
cl_platform_id *platforms,
cl_uint *num_platforms) {
#else
CL_API_ENTRY cl_int CL_API_CALL INTclGetPlatformIDs(
cl_uint num_entries,
cl_platform_id *platforms,
cl_uint *num_platforms) {
#endif
debug_init();
debug_trace();
return _GetPlatformIDs(num_entries, platforms, num_platforms);
}
SYMB(clGetPlatformIDs);
#if defined(__APPLE__) || defined(__MACOSX)
CL_API_ENTRY cl_int __attribute__((visibility("default"))) CL_API_CALL clIcdGetPlatformIDsKHR(
cl_uint num_entries,
cl_platform_id *platforms,
cl_uint *num_platforms) {
#else
CL_API_ENTRY cl_int CL_API_CALL INTclIcdGetPlatformIDsKHR(
cl_uint num_entries,
cl_platform_id *platforms,
cl_uint *num_platforms) {
#endif
debug_init();
debug_trace();
return _GetPlatformIDs(num_entries, platforms, num_platforms);
}
SYMB(clIcdGetPlatformIDsKHR);
/*CL_API_ENTRY void * CL_API_CALL clGetExtensionFunctionAddressForPlatform(
cl_platform_id platform,
const char * func_name) CL_API_SUFFIX__VERSION_1_2 {
}*/
#if defined(__APPLE__) || defined(__MACOSX)
CL_API_ENTRY void * __attribute__((visibility("default"))) CL_API_CALL clGetExtensionFunctionAddress(
const char * func_name) CL_API_SUFFIX__VERSION_1_0 {
#else
CL_API_ENTRY void * CL_API_CALL INTclGetExtensionFunctionAddress(
const char * func_name) CL_API_SUFFIX__VERSION_1_0 {
#endif
debug_init();
debug_trace();
debug(D_LOG, "request address for %s", func_name);
if( func_name != NULL && strcmp("clIcdGetPlatformIDsKHR", func_name) == 0 )
return (void *)_GetPlatformIDs;
if (func_name != NULL && strcmp("extLIG", func_name) == 0) {
printf("65 : ");
}
return NULL;
}
SYMB(clGetExtensionFunctionAddress);
#ifndef ICD_SUFFIX
# define ICD_SUFFIX ""
#endif
#if defined(__APPLE__) || defined(__MACOSX)
CL_API_ENTRY cl_int __attribute__((visibility("default"))) CL_API_CALL 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_API_SUFFIX__VERSION_1_0 {
#else
CL_API_ENTRY cl_int CL_API_CALL INTclGetPlatformInfo(
cl_platform_id platform,
cl_platform_info param_name,
size_t param_value_size,
void * param_value,
size_t * param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 {
#endif
debug_init();
debug_trace();
debug(D_LOG, "request info for 0x%x", param_name);
if (param_name==0 && param_value_size==0
&& param_value==NULL && param_value_size_ret==NULL) {
printf("1 : ");
}
char cl_platform_profile[] = "FULL_PROFILE";
char cl_platform_version[] = "OpenCL 1.2";
char cl_platform_name[] = "DummyCL" ICD_SUFFIX;
char cl_platform_vendor[] = "ocl-icd ICD test" ICD_SUFFIX
#ifdef ICD_WITHOUT_EXTENSION
" (no ext)"
#endif
;
char cl_platform_extensions[] =
#ifdef ICD_WITHOUT_EXTENSION
"private_ext"
#else
"cl_khr_icd"
#endif
;
char cl_platform_icd_suffix_khr[] = "LIG";
size_t size_string;
char * string_p;
if( platform != NULL ) {
int found = 0;
int i;
for(i=0; i<num_master_platforms; i++) {
if( platform == &master_platforms[i] )
found = 1;
}
if(!found)
return CL_INVALID_PLATFORM;
}
switch ( param_name ) {
case CL_PLATFORM_PROFILE:
string_p = cl_platform_profile;
size_string = sizeof(cl_platform_profile);
break;
case CL_PLATFORM_VERSION:
string_p = cl_platform_version;
size_string = sizeof(cl_platform_version);
break;
case CL_PLATFORM_NAME:
string_p = cl_platform_name;
size_string = sizeof(cl_platform_name);
break;
case CL_PLATFORM_VENDOR:
string_p = cl_platform_vendor;
size_string = sizeof(cl_platform_vendor);
break;
case CL_PLATFORM_EXTENSIONS:
#ifdef ICD_WITHOUT_EXTENSION
if (getenv("EMULATE_INTEL_ICD")) {
exit(3);
}
#endif
string_p = cl_platform_extensions;
size_string = sizeof(cl_platform_extensions);
break;
case CL_PLATFORM_ICD_SUFFIX_KHR:
string_p = cl_platform_icd_suffix_khr;
size_string = sizeof(cl_platform_icd_suffix_khr);
break;
default:
return CL_INVALID_VALUE;
break;
}
if( param_value != NULL ) {
if( size_string > param_value_size )
return CL_INVALID_VALUE;
memcpy(param_value, string_p, size_string);
}
if( param_value_size_ret != NULL )
*param_value_size_ret = size_string;
return CL_SUCCESS;
}
SYMB(clGetPlatformInfo);
CL_API_ENTRY cl_int CL_API_CALL
INTclGetDeviceIDs(cl_platform_id pid /* platform */,
cl_device_type ctype /* device_type */,
cl_uint num /* num_entries */,
cl_device_id * devid /* devices */,
cl_uint * res /* num_devices */) CL_API_SUFFIX__VERSION_1_0
{
if (ctype==0 && num==0 && devid==NULL && res==NULL) {
printf("2 : ");
}
char* ENVNAME=NULL;
if (res == NULL) { return CL_SUCCESS; }
switch (ctype) {
case CL_DEVICE_TYPE_GPU:
ENVNAME="NB_GPU" ICD_SUFFIX;
break;
case CL_DEVICE_TYPE_CPU:
ENVNAME="NB_CPU" ICD_SUFFIX;
break;
case CL_DEVICE_TYPE_ALL:
ENVNAME="NB_ALL" ICD_SUFFIX;
break;
}
if (ENVNAME==NULL) {
*res=0;
} else {
char* strnb=getenv(ENVNAME);
if (strnb) {
*res=atoi(getenv(ENVNAME));
} else {
*res=0;
}
}
return CL_SUCCESS;
}
#pragma GCC visibility pop