0

Im a programming OpenCL via pyopenCL on a Ubuntu 16.04.3 64bit, on Nvidia's Tesla K10.G2.8GB.

So far, anything runs smoothly as long as I don't include header files into my OpenCL kernel. As soon, as I put #include <stdlib.h> on top of my header file, the compilation of my openCL kernels fails with different files missing, amongst them being

gnu/stubs-32.h
sys/cdefs.h

Searching for that problem, brings up answers like

Error "gnu/stubs-32.h: No such file or directory" while compiling Nachos source code

or

https://askubuntu.com/questions/470796/fatal-error-sys-cdefs-h-no-such-file-or-directory

baiscally suggesting to install libc6-dev-i386 or gcc-multilib and g++-multilib, supposing that the underlying problem is a 64bit/32bit problem. My question is, are my OpenCL binaries for the GPU compiled as 32bit binaries (how can I check?)?

If yes:

Are there other caveats, when I want to compile 32bit binaries on a 64bit OS?

Furthermore: Can I use 64bit floats, when my kernel is compiled in 32bit? (e.g., will #pragma OPENCL EXTENSION cl_khr_fp64 : enable still work?)

If no:

Do I have to manually locate / copy all the needed header files and include them by hand?

Also: Some of my co-workers even doubt, that including standard C headers into OpenCL kernels is possible due to missing linkers. Any light on that is also appreciated.

Dschoni
  • 3,714
  • 6
  • 45
  • 80
  • I am not sure, but I think OpenCL might just be the language https://www.khronos.org/files/opencl-1-2-quick-reference-card.pdf and related resources: https://www.khronos.org/opencl/resources . In my very limited knowledge of PyOpenCL, I have not written or seen `#include _any_library_` in a kernel. – benshope Sep 26 '17 at 08:20
  • A quick search for `include header openCL` here on SO turns up at least some people who included their own headers into OpenCL kernel code. I'm talking about kernel code written in OpenCL C (basically a subset of C) – Dschoni Sep 26 '17 at 08:24
  • Oh, yea I guess so. Just saw this one: https://stackoverflow.com/questions/14502925/include-headers-to-opencl-cl-file I'll let someone who knows more about PyOpenCL answer your question... – benshope Sep 26 '17 at 08:32

2 Answers2

3

Standard C library and other system headers cannot be included into OpenCL C code, basically because they are only compatible with the current system (a host), whereas an OpenCL C code could run on a different device with a different architecture (a GPU in your case).

As a replacement for standard C functions, OpenCL C defines a set of built-in functions, which are available without any #include: printf, large number of math functions, atomics, image-related functions, etc.

See the "OpenCL Specification: 6.12 Built-in Functions" for a complete list: https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf

That doesn't mean you can't create a header with OpenCL C code and #include it into an OpenCL C program. This works fine:

// foo.h
void foo() {
  printf("hello world!");
}

// kernel.cl
#include "foo.h"
__kernel void use_foo() {
  foo();
}
0

OpenCL explicitly provides for the use of libraries in the kernels. See cl_program clLinkProgram(...) in the OpenCL API documentation.

However, any library must be compiled for the processor on which it will be run. If you run your kernel on a multi-core CPU, in that case some of the OS libraries may be usable, but where you run your kernels on other processors (GPU, TPU, DSP, FPGA), then the library will need to be compiled in the machine code of that processor, (not just the bit-count).

Depending on the particular processor, there will be limits to the size of the device program, individual kernels, and the number and size of variables.

For most cases, this means that you would want relatively small libraries (compared to programming for a desktop CPU), and you would need the complete C source code of the libraries you wish to use in your kernel.

(For some more recent processors and OpenCL drivers, you may be able to use C++ libraries within certain constraints - read the documentation for the version you are using.)