Dear Nvidia developers,
I’m trying to get the kernel binary file from a simple OpenCL code, using offline compilation. I have a first code that loads a c++ file where kernel is written and compile it with:
hprogram = clCreateProgramWithSource(hContext, 1, (const char **)&source_str,
(const size_t *)&source_size, &ret);
ret = clBuildProgram(hprogram, 1, &devices[0], NULL, NULL, NULL);
cl_uint program_num_devices;
clGetProgramInfo(hprogram, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint),&program_num_devices, NULL);
size_t binaries_sizes[program_num_devices];
clGetProgramInfo( hprogram, CL_PROGRAM_BINARY_SIZES,program_num_devices*sizeof(size_t),binaries_sizes, NULL);
char **binaries = new char*[program_num_devices];
for (size_t i = 0; i < program_num_devices; i++)
binaries[i] = new char[binaries_sizes[i]+1];
clGetProgramInfo(hprogram, CL_PROGRAM_BINARIES, program_num_devices*sizeof(size_t), binaries, NULL);
ofstream out_binary_file;
for (size_t i = 0; i < program_num_devices; i++)
{
binaries[i][binaries_sizes[i]] = '
hprogram = clCreateProgramWithSource(hContext, 1, (const char **)&source_str,
(const size_t *)&source_size, &ret);
ret = clBuildProgram(hprogram, 1, &devices[0], NULL, NULL, NULL);
cl_uint program_num_devices;
clGetProgramInfo(hprogram, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint),&program_num_devices, NULL);
size_t binaries_sizes[program_num_devices];
clGetProgramInfo( hprogram, CL_PROGRAM_BINARY_SIZES,program_num_devices*sizeof(size_t),binaries_sizes, NULL);
char *binaries = new char[program_num_devices];
for (size_t i = 0; i < program_num_devices; i++)
binaries[i] = new char[binaries_sizes[i]+1];
clGetProgramInfo(hprogram, CL_PROGRAM_BINARIES, program_num_devices*sizeof(size_t), binaries, NULL);
ofstream out_binary_file;
for (size_t i = 0; i < program_num_devices; i++)
{
binaries[i][binaries_sizes[i]] = '\0';
std::cout << "Program " << i << ":" << std::endl;
std::cout << binaries[i];
}
out_binary_file.open (“kernel.bin”);
for (size_t i = 0; i < binaries_sizes[0]; i++)
out_binary_file << binaries[0][i];
out_binary_file.close();
';
std::cout << "Program " << i << ":" << std::endl;
std::cout << binaries[i];
}
out_binary_file.open ("kernel.bin");
for (size_t i = 0; i < binaries_sizes[0]; i++)
out_binary_file << binaries[0][i];
out_binary_file.close();
Another main loads that kernel and launch it:
hProgram = clCreateProgramWithBinary(hContext, 1, &devices[0], (const size_t *)&binary_size,
(const unsigned char **)&binary_buf, &binary_status, &ret);
cl_kernel hKernel;
hKernel=clCreateKernel(hProgram,"vectorAdd", 0);
.....
Unfortunately, It doesn’t work :( The kernel written from first source code has ASCII format, it has not an executable format. But I don’t get any erro message, the compilation and load of kernel appears work well.
Someone could you help me?
Thanks in forward.