【发布时间】:2012-02-18 11:59:47
【问题描述】:
我需要以下设备版本 主机代码:
double (**func)(double x);
double func1(double x)
{
return x+1.;
}
double func2(double x)
{
return x+2.;
}
double func3(double x)
{
return x+3.;
}
void test(void)
{
double x;
for(int i=0;i<3;++i){
x=func[i](2.0);
printf("%g\n",x);
}
}
int main(void)
{
func=(double (**)(double))malloc(10*sizeof(double (*)(double)));
test();
return 0;
}
其中 func1、func2、func3 必须是 __device__ 功能 和“测试” 必须是(适当修改的)__global__ 内核。
我有一个 NVIDIA GeForce GTS 450(计算能力 2.1) 先感谢您 米歇尔
================================================ =========
一个可行的解决方案
#define REAL double
typedef REAL (*func)(REAL x);
__host__ __device__ REAL func1(REAL x)
{
return x+1.0f;
}
__host__ __device__ REAL func2(REAL x)
{
return x+2.0f;
}
__host__ __device__ REAL func3(REAL x)
{
return x+3.0f;
}
__device__ func func_list_d[3];
func func_list_h[3];
__global__ void assign_kernel(void)
{
func_list_d[0]=func1;
func_list_d[1]=func2;
func_list_d[2]=func3;
}
void assign(void)
{
func_list_h[0]=func1;
func_list_h[1]=func2;
func_list_h[2]=func3;
}
__global__ void test_kernel(void)
{
REAL x;
for(int i=0;i<3;++i){
x=func_list_d[i](2.0);
printf("%g\n",x);
}
}
void test(void)
{
REAL x;
printf("=============\n");
for(int i=0;i<3;++i){
x=func_list_h[i](2.0);
printf("%g\n",x);
}
}
int main(void)
{
assign_kernel<<<1,1>>>();
test_kernel<<<1,1>>>();
cudaThreadSynchronize();
assign();
test();
return 0;
}
【问题讨论】:
-
设备代码不支持函数指针。
-
@Yappie:这是错误的——Fermi 支持函数指针
-
CUDA SDK 中有一个函数指针示例,您可以看到一个与您的问题in this post on the CUDA developer forums 非常相似的示例。
标签: cuda