In most of the NVidia and AMD examples, the kernel code is kept in a separate .cl file and streamed in. In Apple’s HelloCL example, the kernel code is stored as a string in the file.
The NVidia examples use a function – oclLoadProgStream. The AMD examples use a non-portable stream. Here’s how to do it without using any vendor-specific functions.
#include <fstream>
…
std::ifstream in;
std::string KernelSource, line;
…
in.open(“hello.cl”);
if (in.is_open())
while (getline(in, line))
KernelSource += line + “\n”;
else
{
printf(“Error: hello.cl does not exist!\n”);
return EXIT_FAILURE;
}
in.close();
Note that the end of line markers must be included, otherwise the number of characters in the string must be supplied.
Download the source: hello.cpp, hello.cl
leave a reply