| | 1 | = template program = |
| | 2 | {{{ |
| | 3 | #include <stdio.h> |
| | 4 | #include <stdlib.h> |
| | 5 | #include <string.h> |
| | 6 | |
| | 7 | #ifdef __APPLE__ |
| | 8 | #include <OpenCL/opencl.h> |
| | 9 | #else |
| | 10 | #include <CL/cl.h> |
| | 11 | #endif |
| | 12 | |
| | 13 | #define MAX_SOURCE_SIZE (0x100000) |
| | 14 | |
| | 15 | int main() |
| | 16 | { |
| | 17 | cl_platform_id platform_id = NULL; |
| | 18 | cl_device_id device_id = NULL; |
| | 19 | cl_context context = NULL; |
| | 20 | cl_program program = NULL; |
| | 21 | cl_uint ret_num_devices; |
| | 22 | cl_uint ret_num_platforms; |
| | 23 | cl_int ret; |
| | 24 | |
| | 25 | FILE *fp; |
| | 26 | const char fileName[] = "temp.cl"; |
| | 27 | size_t source_size; |
| | 28 | char *source_str; |
| | 29 | |
| | 30 | ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms); |
| | 31 | ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices); |
| | 32 | |
| | 33 | context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret); |
| | 34 | |
| | 35 | fp = fopen(fileName, "r"); |
| | 36 | if (!fp) { |
| | 37 | fprintf(stderr, "Failed to load kernel.\n"); |
| | 38 | exit(1); |
| | 39 | } |
| | 40 | source_str = (char *)malloc(MAX_SOURCE_SIZE); |
| | 41 | source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp ); |
| | 42 | fclose( fp ); |
| | 43 | |
| | 44 | program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &ret); |
| | 45 | ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL); |
| | 46 | if(ret != CL_SUCCESS) { |
| | 47 | if(ret == CL_BUILD_PROGRAM_FAILURE) { |
| | 48 | cl_int logStatus; |
| | 49 | char * buildLog = NULL; |
| | 50 | size_t buildLogSize = 0; |
| | 51 | |
| | 52 | fprintf(stderr, "Error: %s\n", fileName); |
| | 53 | logStatus = clGetProgramBuildInfo (program, |
| | 54 | device_id, |
| | 55 | CL_PROGRAM_BUILD_LOG, |
| | 56 | buildLogSize, |
| | 57 | buildLog, |
| | 58 | &buildLogSize); |
| | 59 | |
| | 60 | buildLog = (char*)malloc(buildLogSize); |
| | 61 | memset(buildLog, 0, buildLogSize); |
| | 62 | |
| | 63 | logStatus = clGetProgramBuildInfo (program, |
| | 64 | device_id, |
| | 65 | CL_PROGRAM_BUILD_LOG, |
| | 66 | buildLogSize, |
| | 67 | buildLog, |
| | 68 | NULL); |
| | 69 | fprintf(stderr, "%s\n", buildLog); |
| | 70 | free(buildLog); |
| | 71 | } |
| | 72 | exit(-1); |
| | 73 | } |
| | 74 | |
| | 75 | free(source_str); |
| | 76 | |
| | 77 | clReleaseProgram(program); |
| | 78 | clReleaseContext(context); |
| | 79 | |
| | 80 | return 0; |
| | 81 | } |
| | 82 | }}} |