1.helle_cuda学习

从一个简单的例子进入cuda的学习:

cpp 复制代码
#include <stdio.h>
#include <cuda.h>
#include <cuda_runtime.h>

__global__ void hello_cuda(){
    // 泛指当前线程在所有block范围内的全局id
    unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
    printf("block id = [ %d ], thread id = [ %d ] hello cuda\n", blockIdx.x, idx);
}

int main() {
    hello_cuda<<< 3, 4 >>>();
    cudaDeviceSynchronize();
    return 0;
}



// 输出
// block id = [ 2 ], thread id = [ 8 ] hello cuda
// block id = [ 2 ], thread id = [ 9 ] hello cuda
// block id = [ 2 ], thread id = [ 10 ] hello cuda
// block id = [ 2 ], thread id = [ 11 ] hello cuda
// block id = [ 1 ], thread id = [ 4 ] hello cuda
// block id = [ 1 ], thread id = [ 5 ] hello cuda
// block id = [ 1 ], thread id = [ 6 ] hello cuda
// block id = [ 1 ], thread id = [ 7 ] hello cuda
// block id = [ 0 ], thread id = [ 0 ] hello cuda
// block id = [ 0 ], thread id = [ 1 ] hello cuda
// block id = [ 0 ], thread id = [ 2 ] hello cuda
// block id = [ 0 ], thread id = [ 3 ] hello cuda

(1)idx -- 线程的id

(2)global 表示cuda kernel 函数的前缀,该函数被cpu调用启动,在gpu上执行

(3)blockDim.x和blockIdx.x 表示block内线程的数量以及每个block的id

(4)threadIdx.x表示block内的线程id

(5)<<< 3 , 4 >>> 在main函数中启动cuda kernel函数的标志,第一个参数表示block的数量,第二个参数表示每个block内的线程数量

(6)cudaDeviceSynchronize()表示该函数强制cpu等待gpu上的cuda kernel执行,即同步,这里也可以不写,只是cpu比gpu先执行完


host : cpu端部调用且在cpu端部执行的函数,正常的c++代码,无任何修饰符时默认是cpu端函数

device: GPU端调用且在GPU端执行的函数,可以理解为GPU端的函数封装,且编译器确定是否inline

noinline:强制编译器不inline

forceinline:强制编译器inline

eg:

host devce int run_on_cpu_or_gpu(){

return 1;

}


block中有很多个线程,


CUDA函数有哪些函数组成

1)__global__修饰的CUDA Kernel函数

2)main函数,基于<<<... , ... >>>启动cuda Kernel函数

3)可选::__device__和__host__修饰的函数

相关推荐
流浪0012 分钟前
Linux系统篇47——线程(十二) POSIX 信号量与环形队列,把判空判满提前到访问之前
linux·操作系统·线程·信号量·环形队列
xuxigifxfh4 分钟前
牛客:HJ170 01序列
java·开发语言·算法·华为机考
朝朝辞暮i15 分钟前
C++ 第 33 课:事件循环——spin() 到底在干什么
开发语言·c++·算法
m4Rk_16 分钟前
【论文阅读】Agent 记忆机制(96):M3-Agent——让 Agent 从视听经历中持续形成情景与语义长期记忆
论文阅读·人工智能·学习·开源·github
殷色玫瑰26 分钟前
AVL树:从平衡因子到四种旋转的完整实现
c语言·数据结构·c++·算法·visualstudio
咖丨喱27 分钟前
【MMC Core + hc16 Host(WiFi SDIO 如何落到控制器)】
linux
等我调个Bug36 分钟前
【Linux系统篇】权限
linux·服务器
jidaowansui1 小时前
B3643 图的存储———>数组模拟邻接矩阵
算法
y1su2 小时前
Leetcode 二分模板
java·数据结构·算法·leetcode·排序算法
~光~~2 小时前
【嵌入式linux学习】Cache 策略:Write-Through / Write-Back + Allocation
linux·学习·spring