洞察掌握android电视app开发中的安全与合规策略,提升企业运营效率
1136
2022-11-23
cuda编程入门(2)—— 浅谈核函数
cuda编程入门(1)
执行空间修饰符有三个 __global__, __device__, __host__。
Function Execution Space Specifiers
参考官网文档 Function Execution Space Specifiers
global
主要有以下几点:
1.Executed on the device, Callable from the host. 2.must specify its execution configuration 3.asynchronous, meaning it returns before the device has completed its execution.
device 主要有以下几点:
1.Executed on the device, 2.Callable from the device only.
// 两个向量加法kernel,grid和block均为一维__device__ float* add_2(float* x, float * y, float* z, int n) { // 获取该线程的全局索引 int index = threadIdx.x + blockIdx.x * blockDim.x; // 步长(线程总数) int stride = blockDim.x * gridDim.x; for (int i = index; i < n; i += stride) { z[i] = x[i] + y[i]; } return z;}__global__ void add(float* x, float * y, float* z, int n){ add_2(x, y, z, n);}#include
host 主要有以下几点:
1.Executed on the host, 2.Callable from the host only.
Execution Configuration
execution-configuration
Any call to a global function must specify the execution configuration for that call. The execution configuration defines the dimension of the grid and blocks that will be used to execute the function on the device, as well as the associated stream (see CUDA Runtime for a description of streams).
执行配置主要四个参数
<<< Dg, Db, Ns, S >>>
即 grid的维度信息, block的维度信息,共享内存以及cuda_stream。详细信息参考官网信息。 grid, block设置好了之后,可以根据内置变量 gridDim,blockDim,blockIdx,threadIdx,来计算线程号。
共享内存 Ns 以及 cuda_stream S 在后续博客中介绍。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~