I’ve recently written code that references a static array of pointers to device functions, as shown in the extract below
template<int DIM>
__device__ void helperKernel( int x )
{
int buffer[DIM]; // this is real reason for using this 'pattern'
// ...
}
typedef void (*HelperKernel)( int );
__device__ static HelperKernel helper[] = {
0 ,
&helperKernel<1> ,
&helperKernel<2> ,
&helperKernel<3>
};
__global__ void demo( int x , int dim )
{
if( dim > 0 && dim < 4 )
{
(helper[dim])( x );
}
}
The code runs (or appears to run) successfully but I’m not certain if its valid CUDA code. Does CUDA permit static arrays of pointers to device functions? If so, out of interest, when would this static array be initialized?