Cleanup debug, refactor libdummycl
[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 <stdio.h>
31 #include <string.h>
32 #define NUM_PLATFORMS 1
33
34 #define SYMB(f) \
35 typeof(INT##f) f __attribute__ ((alias ("INT" #f), visibility("default")))
36
37 #pragma GCC visibility push(hidden)
38
39 int debug_ocl_icd_mask;
40
41 cl_uint const num_master_platforms = NUM_PLATFORMS;
42 struct _cl_platform_id master_platforms[NUM_PLATFORMS] = { {&master_dispatch} };
43
44 static cl_int _GetPlatformIDs(  
45              cl_uint num_entries, 
46              cl_platform_id *platforms,
47              cl_uint *num_platforms) {
48   debug_trace();
49   if( platforms == NULL && num_platforms == NULL )
50     return CL_INVALID_VALUE;
51   if( num_entries == 0 && platforms != NULL )
52     return CL_INVALID_VALUE;
53   if( num_master_platforms == 0)
54     return CL_PLATFORM_NOT_FOUND_KHR;
55   if( num_platforms != NULL ){
56     debug(D_LOG, "  asked num_platforms");
57     *num_platforms = num_master_platforms; }
58   if( platforms != NULL ) {
59     debug(D_LOG, "  asked %i platforms", num_entries);
60     cl_uint i;
61     for( i=0; i<(num_master_platforms<num_entries?num_master_platforms:num_entries); i++)
62       platforms[i] = &master_platforms[i];
63   }
64   return CL_SUCCESS;
65 }
66
67 CL_API_ENTRY cl_int CL_API_CALL INTclGetPlatformIDs(  
68              cl_uint num_entries, 
69              cl_platform_id *platforms,
70              cl_uint *num_platforms) {
71   debug_init();
72   debug_trace();
73   return _GetPlatformIDs(num_entries, platforms, num_platforms);
74 }
75 SYMB(clGetPlatformIDs);
76
77 CL_API_ENTRY cl_int CL_API_CALL INTclIcdGetPlatformIDsKHR(  
78              cl_uint num_entries, 
79              cl_platform_id *platforms,
80              cl_uint *num_platforms) {
81   debug_init();
82   debug_trace();
83   return _GetPlatformIDs(num_entries, platforms, num_platforms);
84 }
85 SYMB(clIcdGetPlatformIDsKHR);
86
87 /*CL_API_ENTRY void * CL_API_CALL clGetExtensionFunctionAddressForPlatform(
88              cl_platform_id platform,
89              const char *   func_name) CL_API_SUFFIX__VERSION_1_2 {
90 }*/
91
92 CL_API_ENTRY void * CL_API_CALL INTclGetExtensionFunctionAddress(
93              const char *   func_name) CL_API_SUFFIX__VERSION_1_0 {
94   debug_init();
95   debug_trace();
96   debug(D_LOG, "request address for %s", func_name);
97   if( func_name != NULL &&  strcmp("clIcdGetPlatformIDsKHR", func_name) == 0 )
98     return (void *)_GetPlatformIDs;
99   return NULL;
100 }
101 SYMB(clGetExtensionFunctionAddress);
102
103 CL_API_ENTRY cl_int CL_API_CALL INTclGetPlatformInfo(
104              cl_platform_id   platform, 
105              cl_platform_info param_name,
106              size_t           param_value_size, 
107              void *           param_value,
108              size_t *         param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 {
109   debug_init();
110   debug_trace();
111   debug(D_LOG, "request info for 0x%x", param_name);
112
113   char cl_platform_profile[] = "FULL_PROFILE";
114   char cl_platform_version[] = "OpenCL 1.2";
115   char cl_platform_name[] = "DummyCL";
116   char cl_platform_vendor[] = "LIG";
117   char cl_platform_extensions[] = "cl_khr_icd";
118   char cl_platform_icd_suffix_khr[] = "LIG";
119   size_t size_string;
120   char * string_p;
121   if( platform != NULL ) {
122     int found = 0;
123     int i;
124     for(i=0; i<num_master_platforms; i++) {
125       if( platform == &master_platforms[i] )
126         found = 1;
127     }
128     if(!found)
129       return CL_INVALID_PLATFORM;
130   }
131   switch ( param_name ) {
132     case CL_PLATFORM_PROFILE:
133       string_p = cl_platform_profile;
134       size_string = sizeof(cl_platform_profile);
135       break;
136     case CL_PLATFORM_VERSION:
137       string_p = cl_platform_version;
138       size_string = sizeof(cl_platform_version);
139       break;
140     case CL_PLATFORM_NAME:
141       string_p = cl_platform_name;
142       size_string = sizeof(cl_platform_name);
143       break;
144     case CL_PLATFORM_VENDOR:
145       string_p = cl_platform_vendor;
146       size_string = sizeof(cl_platform_vendor);
147       break;
148     case CL_PLATFORM_EXTENSIONS:
149       string_p = cl_platform_extensions;
150       size_string = sizeof(cl_platform_extensions);
151       break;
152     case CL_PLATFORM_ICD_SUFFIX_KHR:
153       string_p = cl_platform_icd_suffix_khr;
154       size_string = sizeof(cl_platform_icd_suffix_khr);
155       break;
156     default:
157       return CL_INVALID_VALUE;
158       break;
159   }
160   if( param_value != NULL ) {
161     if( size_string > param_value_size )
162       return CL_INVALID_VALUE;
163     memcpy(param_value, string_p, size_string);
164   }
165   if( param_value_size_ret != NULL )
166     *param_value_size_ret = size_string;
167   return CL_SUCCESS;
168 }
169 SYMB(clGetPlatformInfo);
170
171 #pragma GCC visibility pop
172