Quote from official document:
It is not allowed to take the address of a
__device__
function in host code
I haven't been surprised for a long time that entire CUDA is made up of ridiculous restrictions. What if I told you that paranoid nvidia lies as usually and actually you can get addresses of kernel functions in your host code?
But first lets check what workarounds we can employ to have functions pointers. I don't know for what pedagogical purpose this code intentionally was written so poorly and does not free the allocated memory - and now millions of brainless artificial idiots will copy-paste it forever, so I made patched version. You can realize that attempt to read from early gathered with cudaMemcpyFromSymbol addresses will results error 1 (invalid argument)
cuda-gdb
/*0080*/ MOV R18, 32@lo(_Z9calc_hashPKcPi) ;
/*0090*/ MOV R19, 32@hi(_Z9calc_hashPKcPi) ;
MOV R18, 0
MOV R19, 0
MOV R4, 0
MOV R5, 0
/*00b0*/ MOV R4, 32@lo($str) ;
/*00c0*/ MOV R5, 32@hi($str) ;
nvdisasm lies
[9] .rel.text._Z10dirty_hackPPcS0_ type 9 flags 0
7 relocs:
[7] 20 sym 5 cf1 R_CUDA_ABS32_LO_32
[7] 40 sym 5 cf1 R_CUDA_ABS32_HI_32
[7] 80 sym 15 _Z9calc_hashPKcPi R_CUDA_FUNC_DESC32_LO_32
[7] 90 sym 15 _Z9calc_hashPKcPi R_CUDA_FUNC_DESC32_HI_32
[7] B0 sym 6 $str R_CUDA_ABS32_LO_32
[7] C0 sym 6 $str R_CUDA_ABS32_HI_32
[7] 120 sym 16 vprintf R_CUDA_ABS47_34
Can you spot the difference? Yep, format string $str has relocs of R_CUDA_ABS_XXX, while loading address of function - R_CUDA_FUNC_DESC32_XXX. Official nvdisasm just can't show you it real solution
[9] .rel.text._Z10dirty_hackPPcS0_ type 9 flags 0
7 relocs:
[7] 20 sym 5 cf1 R_CUDA_ABS32_LO_32
[7] 40 sym 5 cf1 R_CUDA_ABS32_HI_32
[7] 80 sym 15 _Z9calc_hashPKcPi R_CUDA_ABS32_LO_32
[7] 90 sym 15 _Z9calc_hashPKcPi R_CUDA_ABS32_HI_32
[7] B0 sym 6 $str R_CUDA_ABS32_LO_32
[7] C0 sym 6 $str R_CUDA_ABS32_HI_32
[7] 120 sym 16 vprintf R_CUDA_ABS47_34
f_addr: 0x7f8235a00100 (nil)
cf1 (nil) what 0x7f8235a00100 value (nil) calc_hash 0x7f8237e32d00
f_addr2: 0x7f8235a00100 0x7f8237e32d00
Комментариев нет:
Отправить комментарий