My intended program flow would look like the following if it were possible:
typedef struct structure_t
{
[...]
/* device function pointer. */
__device__ float (*function_pointer)(float, float, float[]);
[...]
} structure;
[...]
/* function to be assigned. */
__device__ float
my_function (float a, float b, float c[])
{
/* do some stuff on the device. */
[...]
}
void
some_structure_initialization_function (structure *st)
{
/* assign. */
st->function_pointer = my_function;
[...]
}
This is not possible, and ends in a familiar error during compilation regarding the placement of __device__ in the structure.
error: attribute "device" does not apply here
There are some examples of similar types of problems here on stackoverflow, but they all involve the use of static pointers outside the structure. Examples are device function pointers as struct members and device function pointers. I’ve taken a similar approach with success previously in other codes where it’s easy for me to use static device pointers and define them outside of any structures. Currently though this is a problem. It’s written as an API of sorts and the user may define one or two or dozens of structures which need to include a device function pointer. So, defining static device pointers outside of the structure is a major problem.
I’m fairly certain the answer exists within the posts I have linked above, through use symbol copies, but I’ve not been able to put them to successful use.
What you are trying to do is possible, but you have made a few mistakes in the way you are declaring and defining the structures that will hold and use the function pointer.
This is only because you are attempting to assign a memory space to a structure or class data member, which is illegal in CUDA. The memory space of the all class or structure data members are implicitly set when you define or instantiate a class. So something only slighlty different (and more concrete):
is perfectly valid. The function pointer
fpinfunctoris implicitly a__device__function when an instance offunctoris instantiated in device code. If it were instantiated in host code, the function pointer would implicitly be a host function. In the kernel, a device function pointer passed as argument is used to instantiate afunctorinstance. All perfectly legal.I believe I am correct in saying that there is no direct way to get the address of a
__device__function in host code, so you still require some static declarations and symbol manipulation. This might be different in CUDA 5, but I have not tested it to see. If we flesh out the device code above with a couple of__device__functions and some supporting host code:you get a fully compilable and runnable example. Here two
__device__functions and a static function table provide a mechanism for the host code to retrieve__device__function pointers at runtime. The kernel is called once with each__device__function and the results displayed, along with the exact same functor and functions instantiated and called from host code (and thus running on the host) for comparison:If I have understood your question correctly, the above example should give you pretty much all the design patterns you need to implement your ideas in device code.