CUDA dim3网格绕过初始化

1 cuda initialization

我想dim3在我的CUDA程序中使用网格,但是在初始化时不知道参数(它们需要先计算).有一种简单的方法可以解决这个问题吗?

当然我可以在内核调用的括号中传递参数,例如,如果我的参数是m和n:kernel<<<m*n, thread_size>>>(...) 但是我需要重新计算索引.

Jac*_*ern 5

dim3在该文件中定义的整数结构类型vector_types.h

struct __device_builtin__ dim3
{
    unsigned int x, y, z;
#if defined(__cplusplus)
    __host__ __device__ dim3(unsigned int vx = 1, unsigned int vy = 1, unsigned int vz = 1) : x(vx), y(vy), z(vz) {}
    __host__ __device__ dim3(uint3 v) : x(v.x), y(v.y), z(v.z) {}
    __host__ __device__ operator uint3(void) { uint3 t; t.x = x; t.y = y; t.z = z; return t; }
#endif /* __cplusplus */
};
Run Code Online (Sandbox Code Playgroud)

您可以将dim3变量定义为

dim3 grid(256);            // defines a grid of 256 x 1 x 1 blocks
dim3 block(512,512);       // defines a block of 512 x 512 x 1 threads
Run Code Online (Sandbox Code Playgroud)

并用它作为

foo<<<grid,block>>>(...);
Run Code Online (Sandbox Code Playgroud)

如果您有兴趣,您将拥有

dim3 grid(m*n);
dim3 block(thread_size);
kernel<<<grid,block>>>(...)
Run Code Online (Sandbox Code Playgroud)

从定义dim3,它不需要明确初始化的领域gridblock.初始化期间未提供的任何字段初始化为1.

您可以更改分配的字段gridblock

grid.x = 512;
block.y = 64;
Run Code Online (Sandbox Code Playgroud)

  • 您可以随时设置一些虚拟值,然后使用grid.x = ... grid.y = ... grid.z = ...更改它们或者您可以在计算其初始值后声明变量? (4认同)