この例は興味深いかもしれません:
$ cat t237.cu
#include <stdio.h>
__device__ int f1(){ printf("dev f1\n"); return 0;}
__device__ int f2(){ printf("dev f2\n"); return 0;}
__device__ int f3(){ printf("dev f3\n"); return 0;}
__device__ int *fptrf1 = (int *)f1;
__device__ int *fptrf2 = (int *)f2;
__device__ int *fptrf3 = (int *)f3;
__global__ void mykernel(int (*fptr)()){
fptr();
printf("executed\n");
}
int main(){
int *hf1, *hf2, *hf3;
cudaMemcpyFromSymbol(&hf1, fptrf1, sizeof(int *));
cudaMemcpyFromSymbol(&hf2, fptrf2, sizeof(int *));
cudaMemcpyFromSymbol(&hf3, fptrf3, sizeof(int *));
mykernel<<<1,1>>>((int (*)())hf1);
cudaDeviceSynchronize();
mykernel<<<1,1>>>((int (*)())hf2);
cudaDeviceSynchronize();
mykernel<<<1,1>>>((int (*)())hf3);
cudaDeviceSynchronize();
return 0;
}
$ nvcc -arch=sm_20 -O3 -o t237 t237.cu
$ ./t237
dev f1
executed
dev f2
executed
dev f3
executed
[bob@cluster1 misc]$
これはおおむねジャレッドが提案していたことに沿っていると思います。彼が述べたように、これはホスト コードでは不可能です。
&SomeDeviceFunctionTemplate<int>
SomeDeviceFunctionTemplate
関数を参照すると仮定し__device__
ます。