Search code examples
cincludeopencl32bit-64bitpyopencl

Include Headers OpenCL (32bit vs 64bit)


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.


Solution

  • 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();
    }