Static-const-ify ICD loader info strings
[ocl-icd] / libdummy_icd.c
1 /**
2 Copyright (c) 2012, Brice Videau <brice.videau@imag.fr>
3 Copyright (c) 2012, Vincent Danjean <Vincent.Danjean@ens-lyon.org>
4 All rights reserved.
5       
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8     
9 1. Redistributions of source code must retain the above copyright notice, this
10    list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright notice,
12    this list of conditions and the following disclaimer in the documentation
13    and/or other materials provided with the distribution.
14         
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "ocl_icd_debug.h"
28 #include "libdummy_icd.h"
29 #include "libdummy_icd_gen.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #define NUM_PLATFORMS 1
34
35 #define SYMB(f) \
36 typeof(INT##f) f __attribute__ ((alias ("INT" #f), visibility("default")))
37
38 #pragma GCC visibility push(hidden)
39
40 int debug_ocl_icd_mask;
41
42 cl_uint const num_master_platforms = NUM_PLATFORMS;
43 struct _cl_platform_id master_platforms[NUM_PLATFORMS] = { {&master_dispatch} };
44
45 static cl_int _GetPlatformIDs(  
46              cl_uint num_entries, 
47              cl_platform_id *platforms,
48              cl_uint *num_platforms) {
49   debug_trace();
50   if( platforms == NULL && num_platforms == NULL )
51     return CL_INVALID_VALUE;
52   if( num_entries == 0 && platforms != NULL )
53     return CL_INVALID_VALUE;
54   if( num_master_platforms == 0)
55     return CL_PLATFORM_NOT_FOUND_KHR;
56   if( num_platforms != NULL ){
57     debug(D_LOG, "  asked num_platforms");
58     *num_platforms = num_master_platforms; }
59   if( platforms != NULL ) {
60     debug(D_LOG, "  asked %i platforms", num_entries);
61     cl_uint i;
62     for( i=0; i<(num_master_platforms<num_entries?num_master_platforms:num_entries); i++)
63       platforms[i] = &master_platforms[i];
64   }
65   return CL_SUCCESS;
66 }
67
68 CL_API_ENTRY cl_int CL_API_CALL INTclGetPlatformIDs(  
69              cl_uint num_entries, 
70              cl_platform_id *platforms,
71              cl_uint *num_platforms) {
72   debug_init();
73   debug_trace();
74   return _GetPlatformIDs(num_entries, platforms, num_platforms);
75 }
76 SYMB(clGetPlatformIDs);
77
78 CL_API_ENTRY cl_int CL_API_CALL INTclIcdGetPlatformIDsKHR(  
79              cl_uint num_entries, 
80              cl_platform_id *platforms,
81              cl_uint *num_platforms) {
82   debug_init();
83   debug_trace();
84   return _GetPlatformIDs(num_entries, platforms, num_platforms);
85 }
86 SYMB(clIcdGetPlatformIDsKHR);
87
88 /*CL_API_ENTRY void * CL_API_CALL clGetExtensionFunctionAddressForPlatform(
89              cl_platform_id platform,
90              const char *   func_name) CL_API_SUFFIX__VERSION_1_2 {
91 }*/
92
93 CL_API_ENTRY void * CL_API_CALL INTclGetExtensionFunctionAddress(
94              const char *   func_name) CL_API_SUFFIX__VERSION_1_0 {
95   debug_init();
96   debug_trace();
97   debug(D_LOG, "request address for %s", func_name);
98   if( func_name != NULL &&  strcmp("clIcdGetPlatformIDsKHR", func_name) == 0 )
99     return (void *)_GetPlatformIDs;
100   if (func_name != NULL && strcmp("extLIG", func_name) == 0) {
101     printf("65  : ");
102   }
103   return NULL;
104 }
105 SYMB(clGetExtensionFunctionAddress);
106
107 #ifndef ICD_SUFFIX
108 #  define ICD_SUFFIX ""
109 #endif
110
111 CL_API_ENTRY cl_int CL_API_CALL INTclGetPlatformInfo(
112              cl_platform_id   platform, 
113              cl_platform_info param_name,
114              size_t           param_value_size, 
115              void *           param_value,
116              size_t *         param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 {
117   debug_init();
118   debug_trace();
119   debug(D_LOG, "request info for 0x%x", param_name);
120
121   if (param_name==0 && param_value_size==0 
122       && param_value==NULL && param_value_size_ret==NULL) {
123     printf("1  : ");
124   }
125   char cl_platform_profile[] = "FULL_PROFILE";
126   char cl_platform_version[] = "OpenCL 1.2";
127   char cl_platform_name[] = "DummyCL" ICD_SUFFIX;
128   char cl_platform_vendor[] = "ocl-icd ICD test" ICD_SUFFIX
129 #ifdef ICD_WITHOUT_EXTENSION
130           " (no ext)"
131 #endif
132           ;
133   char cl_platform_extensions[] =
134 #ifdef ICD_WITHOUT_EXTENSION
135           "private_ext"
136 #else
137           "cl_khr_icd"
138 #endif
139           ;
140   char cl_platform_icd_suffix_khr[] = "LIG";
141   size_t size_string;
142   char * string_p;
143   if( platform != NULL ) {
144     int found = 0;
145     int i;
146     for(i=0; i<num_master_platforms; i++) {
147       if( platform == &master_platforms[i] )
148         found = 1;
149     }
150     if(!found)
151       return CL_INVALID_PLATFORM;
152   }
153   switch ( param_name ) {
154     case CL_PLATFORM_PROFILE:
155       string_p = cl_platform_profile;
156       size_string = sizeof(cl_platform_profile);
157       break;
158     case CL_PLATFORM_VERSION:
159       string_p = cl_platform_version;
160       size_string = sizeof(cl_platform_version);
161       break;
162     case CL_PLATFORM_NAME:
163       string_p = cl_platform_name;
164       size_string = sizeof(cl_platform_name);
165       break;
166     case CL_PLATFORM_VENDOR:
167       string_p = cl_platform_vendor;
168       size_string = sizeof(cl_platform_vendor);
169       break;
170     case CL_PLATFORM_EXTENSIONS:
171 #ifdef ICD_WITHOUT_EXTENSION
172       if (getenv("EMULATE_INTEL_ICD")) {
173         exit(3);
174       }
175 #endif
176       string_p = cl_platform_extensions;
177       size_string = sizeof(cl_platform_extensions);
178       break;
179     case CL_PLATFORM_ICD_SUFFIX_KHR:
180       string_p = cl_platform_icd_suffix_khr;
181       size_string = sizeof(cl_platform_icd_suffix_khr);
182       break;
183     default:
184       return CL_INVALID_VALUE;
185       break;
186   }
187   if( param_value != NULL ) {
188     if( size_string > param_value_size )
189       return CL_INVALID_VALUE;
190     memcpy(param_value, string_p, size_string);
191   }
192   if( param_value_size_ret != NULL )
193     *param_value_size_ret = size_string;
194   return CL_SUCCESS;
195 }
196 SYMB(clGetPlatformInfo);
197
198 CL_API_ENTRY cl_int CL_API_CALL
199 INTclGetDeviceIDs(cl_platform_id   pid /* platform */,
200                   cl_device_type   ctype /* device_type */,
201                   cl_uint          num /* num_entries */,
202                   cl_device_id *   devid /* devices */,
203                   cl_uint *        res /* num_devices */) CL_API_SUFFIX__VERSION_1_0
204 {
205         if (ctype==0 && num==0 && devid==NULL && res==NULL) {
206                 printf("2  : ");
207         }
208         char* ENVNAME=NULL;
209         if (res == NULL) { return CL_SUCCESS; }
210         switch (ctype) {
211         case CL_DEVICE_TYPE_GPU:
212                 ENVNAME="NB_GPU" ICD_SUFFIX;
213                 break;
214         case CL_DEVICE_TYPE_CPU:
215                 ENVNAME="NB_CPU" ICD_SUFFIX;
216                 break;
217         case CL_DEVICE_TYPE_ALL:
218                 ENVNAME="NB_ALL" ICD_SUFFIX;
219                 break;
220         }
221         if (ENVNAME==NULL) {
222                 *res=0;
223         } else {
224                 char* strnb=getenv(ENVNAME);
225                 if (strnb) {
226                         *res=atoi(getenv(ENVNAME));
227                 } else {
228                         *res=0;
229                 }
230         }
231         return CL_SUCCESS;
232 }
233 #pragma GCC visibility pop
234