| 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 | if(!lengths)
|
|---|
| 195 | {
|
|---|
| 196 | strings == NULL;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | *errcode_ret = CL_SUCCESS;
|
|---|
| 200 | //*errcode_ret = program->loadSources(count, strings, lengths);
|
|---|
| 201 |
|
|---|
| 202 | //corrects lengths
|
|---|
| 203 |
|
|---|
| 204 | for (cl_uint i = 0; i < count; i++)
|
|---|
| 205 | {
|
|---|
| 206 | size_t len = 0;
|
|---|
| 207 | const char *data = strings[i];
|
|---|
| 208 |
|
|---|
| 209 | if (lengths && lengths[i])
|
|---|
| 210 | {
|
|---|
| 211 | len = lengths[i];
|
|---|
| 212 | }
|
|---|
| 213 | else
|
|---|
| 214 | {
|
|---|
| 215 | len = strlen(data);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | //remove trailing \0's, or null characters at EOF
|
|---|
| 220 | //remember that '' is char, "" is string
|
|---|
| 221 | while (len > 0 && (data[len-1] == '\0'))
|
|---|
| 222 | {
|
|---|
| 223 | len--;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | program->context = context;
|
|---|
| 229 | program->count = count;
|
|---|
| 230 | program->strings = strings;
|
|---|
| 231 | program->lengths = lengths;
|
|---|
| 232 | $assert (*errcode_ret == CL_SUCCESS);
|
|---|
| 233 | //the original for loop can make *errcode_ret not CL_SUCCESS, this is a placeholder
|
|---|
| 234 |
|
|---|
| 235 | return (cl_program) program;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /*Makes a program from context. Not quite like the original implementation
|
|---|
| 239 | Still looks almost like clCreateProgramWithSource, change
|
|---|
| 240 | context - From clCreateContext
|
|---|
| 241 | num_devices - Number of devices in device_list
|
|---|
| 242 | device_list - Pointer to list of devices associated with a program
|
|---|
| 243 | lengths - Array of size in bytes the program binaries are
|
|---|
| 244 | binaries - Array of pointers to program binaries
|
|---|
| 245 | binary_status - Returns whether each device was loaded, an array of num_devices
|
|---|
| 246 | errcode_ret - Returns proper error code
|
|---|
| 247 | */
|
|---|
| 248 |
|
|---|
| 249 | cl_program clCreateProgramWithBinaries(cl_context context,
|
|---|
| 250 | cl_uint num_devices,
|
|---|
| 251 | const cl_device_id *device_list,
|
|---|
| 252 | const size_t *lengths,
|
|---|
| 253 | const unsigned char **binaries,
|
|---|
| 254 | cl_int *binary_status,
|
|---|
| 255 | cl_int *errcode_ret)
|
|---|
| 256 | {
|
|---|
| 257 | cl_int default_errcode_ret;
|
|---|
| 258 | cl_program program;
|
|---|
| 259 | cl_uint context_num_devices = 0;
|
|---|
| 260 | cl_device_id *context_devices;
|
|---|
| 261 |
|
|---|
| 262 | program = (cl_program)malloc(sizeof(_cl_program));
|
|---|
| 263 |
|
|---|
| 264 | if(!errcode_ret)
|
|---|
| 265 | {
|
|---|
| 266 | errcode_ret = &default_errcode_ret;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | *errcode_ret = CL_SUCCESS;
|
|---|
| 270 | //*errcode_ret = program->loadBinaries(count, strings, lengths);
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 | for (cl_uint i = 0; i < num_devices; i++)
|
|---|
| 274 | {
|
|---|
| 275 | bool found = false;
|
|---|
| 276 |
|
|---|
| 277 | if (!lengths[i] || !binaries[i])
|
|---|
| 278 | {
|
|---|
| 279 | if(binary_status)
|
|---|
| 280 | {
|
|---|
| 281 | binary_status[i] = CL_INVALID_VALUE;
|
|---|
| 282 | break;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | *errcode_ret = CL_INVALID_VALUE;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | for (cl_uint j = 0; j < context_num_devices; j++)
|
|---|
| 289 | {
|
|---|
| 290 | if(device_list[i] == context_devices[j])
|
|---|
| 291 | {
|
|---|
| 292 | found = true;
|
|---|
| 293 | return 0;
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | if(!found)
|
|---|
| 298 | {
|
|---|
| 299 | *errcode_ret = CL_INVALID_DEVICE;
|
|---|
| 300 | break;
|
|---|
| 301 | }
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | program->context = context;
|
|---|
| 305 | program->lengths = lengths;
|
|---|
| 306 | $assert (*errcode_ret == CL_SUCCESS);
|
|---|
| 307 |
|
|---|
| 308 | return (cl_program) program;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 | /* "Builds" a program executable from the source/binary
|
|---|
| 313 | program - The program, from clCreateProgramWithBinary or clCreateProgramWithContext
|
|---|
| 314 | num_devices - Number of devices in device_list, can be 0
|
|---|
| 315 | device_list - List of devices, can be NULL
|
|---|
| 316 | options - Pointer describing build options, can be NULL
|
|---|
| 317 | pfn_notify - Function pointer to notification routine, can be NULL and won't return until build is completed
|
|---|
| 318 | user_data - argument for pfn_notify, can be NULL
|
|---|
| 319 | */
|
|---|
| 320 |
|
|---|
| 321 | cl_int clBuildProgram(cl_program program,
|
|---|
| 322 | cl_uint num_devices,
|
|---|
| 323 | const cl_device_id *device_list,
|
|---|
| 324 | const char *options,
|
|---|
| 325 | void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data),
|
|---|
| 326 | void *user_data)
|
|---|
| 327 | {
|
|---|
| 328 |
|
|---|
| 329 | if (num_devices)
|
|---|
| 330 | {
|
|---|
| 331 | cl_uint context_num_devices = 0;
|
|---|
| 332 | cl_device_id *context_devices;
|
|---|
| 333 | cl_context *context = (cl_context *) program->context;
|
|---|
| 334 | cl_int result;
|
|---|
| 335 |
|
|---|
| 336 | context_devices = (cl_device_id *) malloc(context_num_devices * sizeof(cl_device_id));
|
|---|
| 337 |
|
|---|
| 338 | if(result != CL_SUCCESS)
|
|---|
| 339 | {
|
|---|
| 340 | return result;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | for (int i = 0; i < num_devices; i++)
|
|---|
| 344 | {
|
|---|
| 345 | bool found = false;
|
|---|
| 346 |
|
|---|
| 347 | for (int j = 0; j < context_num_devices; j++)
|
|---|
| 348 | {
|
|---|
| 349 | if (device_list[i] == context_devices[j])
|
|---|
| 350 | {
|
|---|
| 351 | found = true;
|
|---|
| 352 | break;
|
|---|
| 353 | }
|
|---|
| 354 | }
|
|---|
| 355 | if (!found)
|
|---|
| 356 | {
|
|---|
| 357 | return CL_INVALID_DEVICE;
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 | }
|
|---|
| 363 | return CL_SUCCESS;
|
|---|
| 364 |
|
|---|
| 365 | }
|
|---|
| 366 | /* Creates a kernel with the name of the method specified, and the program
|
|---|
| 367 | program - program from clCreateProgramWithX
|
|---|
| 368 | kernel_name - name of the kernel, should be the same as the method or else
|
|---|
| 369 | errcode_ret - Returns proper error code
|
|---|
| 370 | */
|
|---|
| 371 | cl_kernel clCreateKernel(cl_program program,
|
|---|
| 372 | const char * kernel_name,
|
|---|
| 373 | cl_int * errcode_ret)
|
|---|
| 374 | {
|
|---|
| 375 | cl_int dummy_errcode;
|
|---|
| 376 |
|
|---|
| 377 | if (!errcode_ret)
|
|---|
| 378 | {
|
|---|
| 379 | errcode_ret = &dummy_errcode;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | cl_kernel kernel = (cl_kernel)malloc(sizeof(_cl_kernel));
|
|---|
| 383 |
|
|---|
| 384 | kernel->kernel_name = kernel_name;
|
|---|
| 385 | kernel->errcode_ret = errcode_ret;
|
|---|
| 386 |
|
|---|
| 387 | return (cl_kernel)kernel;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | /*Creates the buffer object
|
|---|
| 391 | context - context made from clCreateContext
|
|---|
| 392 | flags - see cl.cvl and find the #define, determines what can be done with a buffer
|
|---|
| 393 | size - size of the buffer, using sizeof recommended
|
|---|
| 394 | host_ptr - pointer to buffer data, must be >= size of bytes, can be null
|
|---|
| 395 | errcode_ret - Returns proper error code, can be null
|
|---|
| 396 | */
|
|---|
| 397 | //consider for example T* buffer = (T*) malloc(sizeof(T));
|
|---|
| 398 | cl_mem clCreateBuffer(cl_context context,
|
|---|
| 399 | cl_mem_flags flags,
|
|---|
| 400 | size_t size,
|
|---|
| 401 | void * host_ptr,
|
|---|
| 402 | cl_int * errcode_ret)
|
|---|
| 403 | {
|
|---|
| 404 |
|
|---|
| 405 | cl_mem buf = (cl_mem)malloc(sizeof(_cl_mem));
|
|---|
| 406 |
|
|---|
| 407 | cl_int dummy_errcode;
|
|---|
| 408 |
|
|---|
| 409 | if (!errcode_ret)
|
|---|
| 410 | {
|
|---|
| 411 | errcode_ret = &dummy_errcode;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | *errcode_ret = CL_SUCCESS;
|
|---|
| 415 |
|
|---|
| 416 | if (size == 0)
|
|---|
| 417 | {
|
|---|
| 418 | *errcode_ret = CL_INVALID_BUFFER_SIZE;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | buf->context = context;
|
|---|
| 422 | //buf->size = (size_t*)malloc(size); If you use this make it a pointer again in cl.cvl
|
|---|
| 423 | buf->size = size;
|
|---|
| 424 | //size_t * sizes = (size_t*)malloc(size);
|
|---|
| 425 | buf->host_ptr = host_ptr;
|
|---|
| 426 | buf->flags = flags;
|
|---|
| 427 | buf->errcode_ret = errcode_ret;
|
|---|
| 428 |
|
|---|
| 429 | //Have to put in init, which creates a buf for each device
|
|---|
| 430 | //http://people.freedesktop.org/~steckdenis/clover/memobject_8cpp_source.html#l00105
|
|---|
| 431 |
|
|---|
| 432 | //number of devices is from context->num_devices, note that verify may cause it to print several times
|
|---|
| 433 | printf("num_devices is %d\n", context->num_devices);
|
|---|
| 434 | cl_device_id * devices = (cl_device_id *) malloc(context->num_devices * sizeof(cl_device_id));
|
|---|
| 435 | /*
|
|---|
| 436 | for (int i = 0; i< context->num_devices; i++)
|
|---|
| 437 | {
|
|---|
| 438 | devices[i] = device
|
|---|
| 439 | }
|
|---|
| 440 | */
|
|---|
| 441 |
|
|---|
| 442 | return (cl_mem)buf;
|
|---|
| 443 |
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|