For clarity, I've omitted quite a lot of error checking here (for example after compilation) so you'll need to add that in yourself!
Write a Binary
Firstly, lets open a normal kernel source file (in the variable srcFile) and load it into memory. Then build that as per usual. We then have to calculate the binary size and use clGetProgramInfo to write the binary into a variable. Finally we emit that to a file.FILE *fIn = fopen(srcFile, "r"); // Error check the fIn here // get the size fseek(fIn, 0L, SEEK_END); size_t sz = ftell(fIn); rewind(fIn); char *file = (char*)malloc(sizeof(char)*sz+1); fread(file, sizeof(char), sz, fIn); const char* cfile = (const char*)file; *m_cpProgram = clCreateProgramWithSource(*m_ctx, 1, &cfile, &sz, &ciErrNum); ciErrNum = clBuildProgram(*m_cpProgram, 1, (const cl_device_id*)m_cldDevices, compilerFlags, NULL, NULL); // Calculate how big the binary is ciErrNum = clGetProgramInfo(*m_cpProgram, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &kernel_length, NULL); unsigned char* bin; bin = (char*)malloc(sizeof(char)*kernel_length); ciErrNum = clGetProgramInfo(*m_cpProgram, CL_PROGRAM_BINARIES, kernel_length, &bin, NULL); // Print the binary out to the output file fp = fopen(strcat(srcFile,".bin"), "wb"); fwrite(bin, 1, kernel_length, fp); fclose(fp);
Read a Binary
The following extract can be used to build a program from a binary file, emitted or modified from a previous OpenCL run...// Based on the example given in the opencl programming guide FILE *fp = fopen("custom_file.ptx", "rb"); if (fp == NULL) return -1; fseek(fp, 0, SEEK_END); int kernel_length = ftell(fp); rewind(fp); unsigned char *binary = (unsigned char*)malloc(sizeof(unsigned char)*kernel_length+10); fclose(fp); cl_int clStat; *m_cpProgram = clCreateProgramWithBinary(*m_ctx, 1, (const cl_device_id*)m_cldDevices, &kernel_length, (const unsigned char**)&binary, &clStat, &ciErrNum); // Put an error check for ciErrNum here ciErrNum = clBuildProgram(*m_cpProgram, 1, (const cl_device_id*)m_cldDevices, NULL, NULL, NULL);
If you have an OpenCL program where the performance of the total program is important (rather than the kernel itself) then the use of precompiled kernels provides a small but noticeable performance advantage since fewer stages of compilation have to be performed at runtime.
No comments:
Post a Comment
Leave a comment!