cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

shawnccx
Journeyman III

include headers into opencl kernels

Hi all,

I need to include some .h files into .cl OpenCL kernels but I haven't found a way to solve it.

My problem is described as follows:

1.

(1) a kernel in example.cl file:

__kernel void example_kernel(length m, length n){

    ....

}

(2) type length is defined in test_header.h (in the same directory of all the other files)

typedef int length;

(3) According to OpenCL Spec, adding -I when building program

ciErrNum = clBuildProgram(cpProgram, 0, NULL, "-I ./", NULL, NULL);

But this doesn't work, the program got crashed at this line, the log of building program showed:

"/tmp/OCLkQF3NK.cl", line 2: error: overloaded function "length" is not a type

          name

  __kernel void example_kernel(length m, length n)

                             ^

"/tmp/OCLkQF3NK.cl", line 2: error: overloaded function "length" is not a type

          name

  __kernel void example_kernel(length m, length n)

                                       ^

2 errors detected in the compilation of "/tmp/OCLkQF3NK.cl".

Internal error: clc compiler invocation failed.

2. I tried again to include test_header.h in .cl file (I know this is forbidden in OpenCL Spec)

The program still got crashed when building, and the log became:

"./test_header.h", line 2: error: "length" has already been declared in the

          current scope

  typedef int length;

              ^

1 error detected in the compilation of "/tmp/OCLmy1VZ1.cl".

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Previously I just redefined them as Macros in the kernels to avoid including headers. But now, too many functions from the headers are needed, so I need to find a way to import the headers for kernel.

Is anyone seeing what could be the problem? I really need help with this.

thanks

Chongxiao

0 Likes
5 Replies
himanshu_gautam
Grandmaster

I was able to use a separate header file to be included in a kernel using the "-I ./" option. PFA the file i used to verify it.

I had tested it on a linux machine with internal driver(7xxx card) and a Win7 machine with 13.1 driver(6xxx card).

If this does not help you, please post a small repro case. Also mention details about your setup.

0 Likes

this is great, and for user defined header files it does certainly work. thank you

how about if i want to include something like <list> in my kernel?

it does not seem to like that.

0 Likes

Re: using <list>

the OpenCL kernel language is based on C99 with some limitations and additions.  You cannot refer to C++ std::list from within kernels.  You can use a subset of C++ in your kernels http://developer.amd.com/wordpress/media/2012/10/CPP_kernel_language.pdf , but my experience shows best to stick with the C language for your kernels. 

0 Likes
LeeHowes
Staff

It's certainly possible as noted below. However, I really advise against it. Loading .cl files from disc is bad enough from a portability standpoint, but then having to get the include paths right is just messy. I've always build a stringifier into my build system that embeds the .cl files into strings and then into compile-time headers. Unless you have complicated work in the headers themselves that you need a real preprocessor to handle, you may be better off having the script quickly parse out #include and pull those headers into the string too and embed the lot.

Yes, Stringification is a good idea. We had also earlier used a pre-processor to convert all CL kernel Files into strings which were finally included in a compilation unit.

But all these should go, when SPIR comes....(Standard Portable Intermediate Representation - OpenCL 2.0 (future) feature)

One should be able to ship their application with an SPIR binary.

0 Likes