码迷,mamicode.com
首页 > 其他好文 > 详细

cuda:thread->block->stream

时间:2016-05-19 10:16:40      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

 程序结构

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);
    ...
}

 

cuda:thread->block->stream

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!