menu

silverghost

世界可以想象,世界可以创造

Avatar

CUDA 类型定义 and 补充

1 Function Type Qualifiers 函数类型

1.1 __device__

__device__ 规定的函数,在device上执行。只能在device上调用。

1.2 __global__

__global__ 定义了一个kernel函数:在device上运行。只能在host上调用

1.3 __host__

__host__定义的函数:在host上运行,只能在host上调用。

没有定义__host__,__device__或者__global__的函数等同于__host__函数,系统都会把函数编译成host函数。

另外,__host__定义可以和__device__定义一起使用,编译器会把这个函数编译为host和device通用的函数。

1.4 Restrictions (强调,限制)

__device__函数通常是inline,所以如果需要不inline 就需要加上uninline

__device__和__global__函数都不支持递归调用。

__device__和__global__函数都不能定义static变量在函数内部。

__device__和__global__函数不能使用可变参数。

__device__函数没有函数地址,也没有指向它的函数指针,但是__global__函数有。

__global__定义和__host__定义不能一起使用。

__global__函数必须是void返回类型。

任何调用__global__的函数都必须指明运行配置。(Section 4.2.3)也就是线程kernel的调用方法。

__global__函数是异步调用的。在运行结束前就会返回。

__global__函数的参数通常是通过shared memory调用到Device中,最多是265bytes。

2. Variable Type Qualifiers
2.1 __device__
device上声明的变量,只能在在device上使用和调用.

2.2 __constant__
可以和__device__连用,处于constant memory space.可以被同一个grid的所有threads 从host runtime libary(e.g. cudaGetSymbolAddress(), cudaGetSymbolSize(), cudaMemcpyToSymbol() and cudaMemcpyFromSymbol)访问.
lifetime as application(grid bsw. kernel function)

2.3 __shared__
可以和__device__连用,处于shared memory space一个thread Block.只可以被同一个block的所有threads访问. lifetime same as block

2.4 Restrictions (强调,限制)