15

I have worked with OpenCL on a couple of projects, but have always written the kernel as one (sometimes rather large) function. Now I am working on a more complex project and would like to share functions across several kernels.

But the examples I can find all show the kernel as a single file (very few even call secondary functions). It seems like it should be possible to use multiple files - clCreateProgramWithSource() accepts multiple strings (and combines them, I assume) - although pyopencl's Program() takes only a single source.

So I would like to hear from anyone with experience doing this:

  • Are there any problems associated with multiple source files?
  • Is the best workaround for pyopencl to simply concatenate files?
  • Is there any way to compile a library of functions (instead of passing in the library source with each kernel, even if not all are used)?
  • If it's necessary to pass in the library source every time, are unused functions discarded (no overhead)?
  • Any other best practices/suggestions?

Thanks.

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
  • 2
    You can create one concatenation file having #include "part1.cl" #include "part2.cl". This works for me (NVidia compiler), although probably everything is compiled each time the app runs. I think that having a library of precompiled functions is a bit sci-fi, as the function code is inlined into each kernel (that's why you can't write recursive functions). – Radim Vansa Oct 01 '11 at 20:44

2 Answers2

6

I don't think OpenCL has a concept of multiple source files in a program - a program is one compilation unit. You can, however, use #include and pull in headers or other .cl files at compile time.

You can have multiple kernels in an OpenCL program - so, after one compilation, you can invoke any of the set of kernels compiled.

Any code not used - functions, or anything statically known to be unreachable - can be assumed to be eliminated during compilation, at some minor cost to compile time.

grrussel
  • 7,209
  • 8
  • 51
  • 71
  • thanks. this is what i am doing. but, frustratingly, pyopencl doesn't take into account the #include contents when caching kernels. so changing the "library" files doesn't affect compiled kernels without manually deleting the cache. anyway, i'll wait to see if someone has a better idea before marking you as best (and only!). cheers. – andrew cooke Oct 06 '11 at 12:05
  • 1
    If that's true, then the syntax of clCreateProgramWithSource is quite awkward, isn't it? It explicitly requires multiple strings and the length of each of those strings. I'm not sure it actually works (I got here trying to get it working) but it sounds weird that it can't work given the syntax... – user1111929 Feb 15 '12 at 19:44
5

In OpenCL 1.2 you link different object files together.

Yoav
  • 5,962
  • 5
  • 39
  • 61