开发者

Can I embed C++ Classes in OpenCL Kernels?

开发者 https://www.devze.com 2023-02-10 21:06 出处:网络
is there a possibility to use self-defined C++ - classes in an OpenCL kernel? It should work like this:

is there a possibility to use self-defined C++ - classes in an OpenCL kernel? It should work like this:

#include "element.cpp"
__kernel void do_something(__global element* input, __global elem开发者_运维问答ent* output);
{
    int index = get_global_id(0);
    output[index] = input[index].update(index);
}

This is interesting, because you can specify the work that must be done in element::update(int no) afterwards.

I did not get it to work. This is what the OpenCL-Compiler tells me:

unknown type name 'class'

In CUDA this works. Are there any other ideas, if the approach with objects in the OpenCL kernel does not work?

Thanks for your hints in advance!


I believe OpenCL follows C99 language specification and not C++. The specifications for C++ version of OpenCL is going on. I believe AMD APP has implemented the C++ version of OpenCL. Coming back to your question, I think it's best to have a struct as the interface between C++ and C. The C++ version should be a wrapper around the C implementation, IF you direly need to do so.

EDIT: I couldn't place this in comments, hence putting it here. AMD's C++ libraries around OpenCL include a static C++ library and Bolt.


No. The OpenCL language extends C99, and therefore does not have support for C++ keywords and features, e.g. "class".

If your code to apply is both C++ and OpenCL i.e. in the common subset of both, you could have something similar to

element update(element in) { ... ; return result; }

and invoke that in either OpenCL or C++, as desired e.g.

output[index] = update(input[index]);

provided that element is a struct, and not with fields of non-C types.

In general, the inputs and outputs to OpenCL must be simple structs or arrays, not classes.


No, others as they tell you OpenCL is based in C99 therefore you can use structs like

typedef struct{
   float mini;
   int pos;
}A;
0

精彩评论

暂无评论...
验证码 换一张
取 消