device function pointers

后端 未结 1 1456
温柔的废话
温柔的废话 2020-11-30 05:15

I need a device version of the following host code:

double (**func)(double x);

double func1(double x)
{
 return x+1.;
}

double func2(double x)
{
 return x+         


        
1条回答
  •  鱼传尺愫
    2020-11-30 05:58

    function pointers are allowed on Fermi. This is how you could do it:

    typedef double (*func)(double x);
    
    __device__ double func1(double x)
    {
    return x+1.0f;
    }
    
    __device__ double func2(double x)
    {
    return x+2.0f;
    }
    
    __device__ double func3(double x)
    {
    return x+3.0f;
    }
    
    __device__ func pfunc1 = func1;
    __device__ func pfunc2 = func2;
    __device__ func pfunc3 = func3;
    
    __global__ void test_kernel(func* f, int n)
    {
      double x;
    
      for(int i=0;i>>(d_f,N);
    
      cudaFree(d_f);
      free(h_f);
    
      return 0;
    }
    

    0 讨论(0)
提交回复
热议问题