Reputation: 309
I'm trying to read an OpenCL kernel from a file "kernel.cl", but the kernel I read in ends up having unknown symbols at the end of the program once I have read it. The number of unknown symbols are the same as number of lines in the kernel file.
The code I am using to get the kernel:
FILE *fp;
char *source_str;
size_t source_size, program_size;
fp = fopen("kernel.cl", "r");
if (!fp) {
printf("Failed to load kernel\n");
return 1;
}
fseek(fp, 0, SEEK_END);
program_size = ftell(fp);
rewind(fp);
source_str = (char*)malloc(program_size + 1);
source_str[program_size] = '\0';
fread(source_str, sizeof(char), program_size, fp);
fclose(fp);
This code works on another project, so I don't know what's wrong. Also it seems to work if all the code in the kernel is on one line.
Any help would be appreciated, thanks! :)
Upvotes: 2
Views: 4993
Reputation: 8036
If using C++ is an option, take a look at the program::create_with_source_file()
method provided by the Boost.Compute library. It simplifies the process of opening a file, reading the contents, and creating the OpenCL program object with the source code.
For example, you could simply do:
boost::compute::program my_program =
boost::compute::program::create_with_source_file("kernel.cl");
Upvotes: 1
Reputation: 9925
The MSDN page for fopen()
mentions that when files are opened with "r"
as the mode string, some translations will happen with regards to line-endings. This means that the size of the file you query may not match the amount of data read by fread()
. This explains why the number of invalid characters was equal to the number of lines in the file (and why it worked with all the code on one line).
The solution is to open the file with the "rb"
flag:
fp = fopen("kernel.cl", "rb");
Upvotes: 2