cuda:thread->block->stream

本文介绍了CUDA编程的基本概念,包括核函数的定义与调用方法,以及如何利用线程结构进行矩阵和向量运算。

 程序结构

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值