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__修饰的函数

相关推荐
Ln5x9qZC22 分钟前
尾递归与Continuation
算法
一路向北he2 分钟前
esp32库依赖
c语言·c++·算法
老四啊laosi2 分钟前
[双指针] 6. 查找总价为目标值的两个商品
算法·力扣·总价为目标值得两商品
Hello World . .5 分钟前
Linux:Linux命令行音视频播放器
linux·音视频
weixin_443478516 分钟前
Flutter第三方常用组件包学习之状态管理
javascript·学习·flutter
Engineer邓祥浩10 分钟前
JVM学习笔记(5) 第二部分 自动内存管理 第4章 虚拟机性能监控、故障处理工具
jvm·笔记·学习
YYYing.14 分钟前
【Linux/C++网络篇(二) 】TCP并发服务器演进史:从多进程到Epoll的进化指南
linux·服务器·网络·c++·tcp/ip
人间寥寥情难诉19 分钟前
LRU算法本地实现
java·算法·spring
moonsea020321 分钟前
2026.4.2
开发语言·c++·算法
SPC的存折21 分钟前
10、Ansible 生产级故障排查与运维最佳实践
linux·运维·ansible