我需要以下 主機代碼的設備版本:設備函數指針
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
@Yappie:這是錯誤的 - Fermi支持函數指針 – talonmies
CUDA SDK中有一個函數指針示例,您可以看到一個與您的問題非常相似的示例[在CUDA開發人員論壇的這篇文章中](http://forums.nvidia.com/index.php?showtopic=156792&view=findpost&p=1201985)。 – talonmies