cuda:thread->block->stream

 程序结构

1.核函数

核函数的定义和c语言方式类似,使用__global__什么核函数,线程的数目通过<<<...,nums>>>来传递。

// Kernel definition
__global__ void VecAdd(float* A, float* B, float* C)
{
    int i = threadIdx.x;
    C[i] = A[i] + B[i];
}
int main()
{
    ...
    // Kernel invocation with N threads
    VecAdd<<<1, N>>>(A, B, C);
    ...
}

 2.线程的结构

线程是一个三维向量(x,y,z),在使用的过程中,可以使用(x),(x,y),(x,y,z)

以下,是一个使用二维(x,y)的核函数

// Kernel definition
__global__ void MatAdd(float A[N][N], float B[N][N],
                       float C[N][N])
{
    int i = threadIdx.x;
    int j = threadIdx.y;
    C[i][j] = A[i][j] + B[i][j];
}
int main()
{
    ...
    // Kernel invocation with one block of N * N * 1 threads
    int numBlocks = 1;
    dim3 threadsPerBlock(N, N);
    MatAdd<<<numBlocks, threadsPerBlock>>>(A, B, C);
    ...
}

 Grid of Thread Blocks.

threadindex(x,y) = threadid(x+y*Dx);

原文地址:https://www.cnblogs.com/linyuanzhou/p/5507671.html