Cuda实战-06 官方样例asyncAPI

说明

这个是官方样例 asyncAPI.cu,内容有所删改,只是为了方便初学者理解,相对较为综合,但收获也挺大。里面的注释都是我根据自己的理解添加的

代码


 // includes, system
 #include <stdio.h>

 // includes CUDA Runtime
 #include <cuda_runtime.h>
 
 __global__ void increment_kernel(int* g_data, int inc_value) {
     int idx = blockIdx.x * blockDim.x + threadIdx.x;
     g_data[idx] = g_data[idx] + inc_value;
 }
 
 int main(int argc, char* argv[]) {
 
     //显示第一张显卡名称
     cudaDeviceProp deviceProps;
     cudaGetDeviceProperties(&deviceProps, 0);
     printf("CUDA device [%s]\n", deviceProps.name);
 
     int n = 16 * 1024 * 1024;
     int nbytes = n * sizeof(int);
     int value = 26;
 
     //分配CPU内存
     int* a = 0;
     cudaMallocHost((void**)&a, nbytes);
     memset(a, 0, nbytes);
 
     //分配GPU内存
     int* d_a = 0;
     cudaMalloc((void**)&d_a, nbytes);
     cudaMemset(d_a, 255, nbytes);
 
     //设置核函数启动参数
     dim3 threads = dim3(512, 1);
     dim3 blocks = dim3(n / threads.x, 1);
 
     //创建计时任务
     cudaEvent_t start, stop;
     cudaEventCreate(&start);
     cudaEventCreate(&stop);
 
     //强制同步
     cudaDeviceSynchronize();
     //开始计时
     cudaEventRecord(start, 0);
     //异步传输数据
     cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, 0);
     //执行核函数
     increment_kernel << <blocks, threads, 0, 0 >> > (d_a, 100);
     //异步传输数据
     cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, 0);
     //停止计时
     cudaEventRecord(stop, 0);
     //获取时间
     float gpu_time = 0.0f;
     cudaEventSynchronize(stop);
     cudaEventElapsedTime(&gpu_time, start, stop);
 
     //打印时间
     printf("time spent executing by the GPU: %.4f\n", gpu_time);
 
     //释放资源
     cudaEventDestroy(start);
     cudaEventDestroy(stop);
     cudaFreeHost(a);
     cudaFree(d_a);
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦星辰.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值