source: CIVL/examples/translation/opencl/setup/openCL.cvl@ aef8b11

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since aef8b11 was 2259218, checked in by Jacob Trieu <fuufusuu@…>, 12 years ago

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1110 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 7.8 KB
Line 
1
2#include "cl.cvl"
3#include<string.h>
4#include<stdlib.h>
5#include <stdio.h>
6
7$input int DEVICE_ID;
8$input int DEVICE_ID_BOUND;
9
10$assume 0 < DEVICE_ID && DEVICE_ID < DEVICE_ID_BOUND;
11
12
13/*
14 Gives back an int for the error code, fills devices with ids of devices
15 See cl.cvl for files
16 platform - The platform, can be NULL
17 device_type - Has the device type
18 num_entries - Is the max number of devices that can be filled
19 devices - A pointer that will contain the IDs
20 num_devices - Says how many devices were added, can be NULL
21*/
22int clGetDeviceIDs(cl_platform_id platform,
23 cl_device_type device_type,
24 cl_uint num_entries,
25 cl_device_id *devices,
26 cl_uint *num_devices)
27 {
28 //$assume device_type == CL_DEVICE_TYPE_DEFAULT;
29 int i = 0;
30 //cl_platform_id int value can be null
31
32 //num_entries number of devices that can be added
33 /*
34 if(num_entries == 0 && devices != 0)
35 {
36 return CL_INVALID_VALUE;
37 }
38
39 if(num_entries == 0 && devices == 0)
40 {
41 return CL_INVALID_VALUE;
42 }
43
44 //CIVL cannot pass this if block if not commented out
45 /*
46 if(device_type & (CL_DEVICE_TYPE_DEFAULT | CL_DEVICE_TYPE_CPU |
47 CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR |
48 CL_DEVICE_TYPE_CUSTOM | CL_DEVICE_TYPE_ALL))
49 {
50 */
51 while (i < num_entries)
52 {
53 if(devices)
54 {
55 devices[0] = (cl_device_id)(&DEVICE_ID);
56 //how to say "use anything"?
57 i++;
58 }
59 else
60 {
61 break;
62 }
63
64 }
65 if (num_devices)
66 {
67 *num_devices = i;
68 }
69 //}
70/*
71 else
72 {
73 return CL_DEVICE_NOT_FOUND;
74 }
75*/
76 //*devices list of devices found, use
77 //cl_device_id device_id;
78 //&device_id
79
80 return CL_SUCCESS;
81
82}
83
84/*
85 Creates the cl_context that will be used in later types to be made
86 properties - Context property names
87 num_devices - Number of devices
88 devices - Pointer to the devices from clGetDeviceIDs
89 pfn_notify - Callback function to report errors. Can be NULL
90 user_data - Part of pfn_notify. Can be NULL
91 errcode_ret - Returns proper error code.
92*/
93
94cl_context clCreateContext(const cl_context_properties *properties,
95 cl_uint num_devices,
96 const cl_device_id * devices,
97 void (CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *),
98 void * user_data,
99 cl_int * errcode_ret)
100{
101 cl_int default_errcode_ret;
102 cl_context ctx;
103
104 if(!errcode_ret)
105 {
106 errcode_ret = &default_errcode_ret;
107 }
108
109 if (!devices || !num_devices || (!pfn_notify && user_data))
110 {
111 *errcode_ret = CL_INVALID_VALUE;
112 return 0;
113 }
114
115 *errcode_ret = CL_SUCCESS;
116
117 ctx = (cl_context)malloc(sizeof(_cl_context));
118
119
120 ctx->properties = properties;
121 ctx->num_devices = num_devices;
122 ctx->devices = devices;
123 ctx->pfn_notify = pfn_notify;
124 ctx->user_data = user_data;
125 ctx->errcode_ret = errcode_ret;
126
127
128 if(*errcode_ret != CL_SUCCESS)
129 {
130 //delete ctx;
131 return 0;
132 }
133
134 return(cl_context) ctx;
135
136}
137
138/* Creates a cl_command_queue
139 context - The context created from clCreateContext
140 device - The pointer to IDs from clGetDeviceIDs
141 properties - Bitfield for properties, see cl.cvl
142 errcode_ret - Returns proper error code.
143*/
144cl_command_queue clCreateCommandQueue(cl_context context,
145 cl_device_id device,
146 cl_command_queue_properties properties,
147 cl_int* errcode_ret)
148{
149 cl_int default_errcode_ret;
150 cl_command_queue cmd;
151
152 // No errcode_ret ?
153 if (!errcode_ret)
154 {
155 errcode_ret = &default_errcode_ret;
156 }
157
158 cmd = (cl_command_queue)malloc(sizeof(_cl_command_queue));
159
160 cmd->context = context;
161 cmd->device = device;
162 cmd->properties = properties;
163 cmd->errcode_ret = errcode_ret;
164
165 return cmd;
166}
167
168/*Makes a program from context. Not quite like the original implementation
169 context - From clCreateContext
170 count - number of source codes for strings
171 strings - Array of chars that makes up code
172 lengths - string length, if NULL nulls the strings
173 errcode_ret - Returns proper error code
174*/
175cl_program clCreateProgramWithSource(cl_context context,
176 cl_uint count,
177 const char** strings,
178 const size_t * lengths,
179 cl_int * errcode_ret)
180{
181 cl_int default_errcode_ret;
182 cl_program program;
183
184 program = (cl_program)malloc(sizeof(_cl_program));
185
186 if(!errcode_ret)
187 {
188 errcode_ret = &default_errcode_ret;
189 }
190
191 *errcode_ret = CL_SUCCESS;
192 //*errcode_ret = program->loadSources(count, strings, lengths);
193
194 //corrects lengths
195
196 for (cl_uint i = 0; i < count; i++)
197 {
198 size_t len = 0;
199 const char *data = strings[i];
200
201 if (lengths && lengths[i])
202 {
203 len = lengths[i];
204 }
205 else
206 {
207 len = strlen(data);
208 }
209
210
211 //remove trailing \0's, or null characters at EOF
212 //remember that '' is char, "" is string
213 while (len > 0 && (data[len-1] == '\0'))
214 {
215 len--;
216 }
217
218 }
219
220 program->context = context;
221 program->count = count;
222 program->strings = strings;
223 program->lengths = lengths;
224 $assert (*errcode_ret == CL_SUCCESS);
225 //the original for loop can make *errcode_ret not CL_SUCCESS, this is a placeholder
226
227 return (cl_program) program;
228}
229
230/*Makes a program from context. Not quite like the original implementation
231 Still looks almost like clCreateProgramWithSource, change
232 context - From clCreateContext
233 num_devices - Number of devices in device_list
234 device_list - Pointer to list of devices associated with a program
235 lengths - Array of size in bytes the program binaries are
236 binaries - Array of pointers to program binaries
237 binary_status - Returns whether each device was loaded, an array of num_devices
238 errcode_ret - Returns proper error code
239*/
240
241cl_program clCreateProgramWithBinaries(cl_context context,
242 cl_uint num_devices,
243 const cl_device_id *device_list,
244 const size_t *lengths,
245 const unsigned char **binaries,
246 cl_int *binary_status,
247 cl_int *errcode_ret)
248{
249 cl_int default_errcode_ret;
250 cl_program program;
251 cl_uint context_num_devices = 0;
252 cl_device_id *context_devices;
253
254 program = (cl_program)malloc(sizeof(_cl_program));
255
256 if(!errcode_ret)
257 {
258 errcode_ret = &default_errcode_ret;
259 }
260
261 *errcode_ret = CL_SUCCESS;
262 //*errcode_ret = program->loadBinaries(count, strings, lengths);
263
264
265 for (cl_uint i = 0; i < num_devices; i++)
266 {
267 bool found = false;
268
269 if (!lengths[i] || !binaries[i])
270 {
271 if(binary_status)
272 {
273 binary_status[i] = CL_INVALID_VALUE;
274 break;
275 }
276
277 *errcode_ret = CL_INVALID_VALUE;
278 }
279
280 for (cl_uint j = 0; j < context_num_devices; j++)
281 {
282 if(device_list[i] == context_devices[j])
283 {
284 found = true;
285 return 0;
286 }
287 }
288
289 if(!found)
290 {
291 *errcode_ret = CL_INVALID_DEVICE;
292 break;
293 }
294 }
295
296 program->context = context;
297 program->lengths = lengths;
298 $assert (*errcode_ret == CL_SUCCESS);
299
300 return (cl_program) program;
301}
302
303
304/* "Builds" a program executable from the source/binary
305 program - The program, from clCreateProgramWithBinary or clCreateProgramWithContext
306 num_devices - Number of devices in device_list, can be 0
307 device_list - List of devices, can be NULL
308 options - Pointer describing build options, can be NULL
309 pfn_notify - Function pointer to notification routine, can be NULL and won't return until build is completed
310 user_data - argument for pfn_notify, can be NULL
311*/
312/*
313void clBuildProgram(cl_program program,
314 cl_uint num_devices,
315 const cl_device_id *device_list,
316 const char *options,
317 void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data),
318 void *user_data)
319{
320
321 if (num_devices)
322 {
323 cl_uint context_num_devices = 0;
324 cl_device_id *context_devices;
325 cl_context *context = (cl_context *) program->context;
326 cl_int result;
327
328 context_devices = (cl_device_id *)
329 }
330}
331*/
Note: See TracBrowser for help on using the repository browser.