#include headers from memory, not from filesystem?


In a commercial environment, we have the requirement that no OpenCL source code can be stored in the file system as plain text. Is it still possible under this restriction to use #include statements in OpenCL code, i.e., can I somehow tell the OpenCL compiler that “#include <something.h>” means to fetch “something.h” from a location in memory instead of the file system? Although #include statements can sometimes be avoided by passing headers as additional source strings to “clCreateProgramWithSource”, there are still situations which should be left to the OpenCL preprocessor, such as code like this:

#include <use_some_feature.h>

where USE_SOME_FEATURE is dynamically specified as an OpenCL preprocessor option at application runtime. Do you have any ideas about this?

Thanks & kind regards,

You can preprocess your file (replacing include <…> with the real code) before sending it to opencl driver.

But even if not present on the file system, sending sources to opencl driver (via createprogramwithsource), is not very safe.
They can easily be watched, with a fake opencl driver.

This could be a solution if I could tell the preprocessor to only process “#include” statements and not to touch the “#ifdef USE_SOME_FEATURE” in the above example. Do you know if this is possible?

This is true, but if the environment changes (e.g., hardware upgrade, OpenCL or driver update), the source code has to be recompiled. According to the OpenCL documentation, the binaries may contain an intermediate representation, from which the actual binary code can be created, but this is not required, so I don’t see a generic solution here.

Thanks & kind regards,

I don’t see a generic solution here

That’s because there is no generic binary solution :expressionless:

When I said “preprocess your file”, I want to say process it yourself in your host program to replace the #include <…> line with the code in file “…”.
So everything is possible, it’s up to you.

Why about using the inverse of a traditional header gaurd to turn the header file on and off? The trick is making sure to pass the files in the correct order to clCreateProgramWithSource. Something like the following:





__kernel void Foo()

Then call clCreateProgramWithSource passing the source from mykernel.h first and then use_some_feature.h.

Though I sympathize with your frustration of shipping binary OpenCL code, so much so I was compelled to start a blog: http://iheartcode.blogspot.com/2010/04/ … n-gpu.html