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