How to use C++ templates in OpenCL kernels?

前端 未结 6 1707
猫巷女王i
猫巷女王i 2020-12-24 08:53

I\'m a novice in OpenCL.

I have an algorithm which uses templates. It worked well with OpenMP parallelization but now the amount of data has grown and the only way t

6条回答
  •  有刺的猬
    2020-12-24 09:30

    I have written an experimental C++ to OpenCL C source transformation tool. The tool compiles C++ source (even some STL) into LLVM byte-code, and uses a modified version of the LLVM 'C' back-end to disassemble the byte-code into OpenCL 'C'.

    Please see http://dimitri-christodoulou.blogspot.com/2013/12/writing-opencl-kernels-in-c.html

    For example, this code using C++11's std::enable_if can be converted into OpenCL 'C' and then executed on the GPU:

    #include 
    
    template
    T foo(T t, typename std::enable_if::value >::type* = 0)
    {
        return 1;
    }
    
    template
    T foo(T t, typename std::enable_if::value >::type* = 0)
    {
        return 0;
    }
    
    extern "C" void _Kernel_enable_if_int_argument(int* arg0, int* out)
    {
        out[0] = foo(arg0[0]);
    }
    

提交回复
热议问题