Advertisement

记录 | CUDA编程中用constexpr替代__host__&__device__

阅读量:

例如,在使用 __host____device__ 的情形中,具体表现如下:

复制代码
    #include <cstdio>
    #include <cuda_runtime.h>
    
    __host__ __device__ void say_hello(){
    printf("Hello, world!\n");
    }
    
    __global__ void kernel(){
    say_hello();
    }
    
    int main(){
    kernel<<<1, 1>>>();
    cudaDeviceSynchronize();
    say_hello();
    return 0;
    }
    }
    
    

此时可采用 constexpr 作为 __host__ __device 的替代方案,替换后的代码示例如下:

复制代码
    #include <cstdio>
    #include <cuda_runtime.h>

全部评论 (0)

还没有任何评论哟~