0_Simple__asyncAPI
▶ CPU - GPU 异步操作
▶ 源代码
#include <stdio.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <helper_cuda.h>
#include <helper_functions.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;
} bool correct_output(int *data, const int n, const int x)
{
for (int i = ; i < n; i++)
{
if (data[i] != x)
{
printf("Error! data[%d] = %d, ref = %d\n", i, data[i], x);
return false;
}
}
return true;
} int main(int argc, char *argv[])
{
printf("Start.\n");
int devID = findCudaDevice(argc, (const char **)argv); // 通过命令行参数选择设备,可以为空
cudaDeviceProp deviceProps;
cudaGetDeviceProperties(&deviceProps, devID);
printf("CUDA device [%s]\n", deviceProps.name); const int n = * * ;
const int nbytes = n * sizeof(int);
const int value = ; int *a, *d_a;
cudaMallocHost((void **)&a, nbytes);
cudaMalloc((void **)&d_a, nbytes);
memset(a, , nbytes);
cudaMemset(d_a, , nbytes); cudaEvent_t start, stop; // GPU 端计时器
cudaEventCreate(&start);
cudaEventCreate(&stop); StopWatchInterface *timer = NULL; // CPU 端计时器
sdkCreateTimer(&timer);
sdkResetTimer(&timer); dim3 threads = dim3(, , );
dim3 blocks = dim3(n / threads.x, , ); sdkStartTimer(&timer); // 注意 GPU 计时器是夹在 CPU 计时器内的,但是 GPU 函数都是异步的
cudaEventRecord(start, );
cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, );
increment_kernel << <blocks, threads, , >> > (d_a, value);
cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, );
cudaEventRecord(stop, );
sdkStopTimer(&timer); unsigned long int counter = ; // 记录 GPU 运行完成以前 CPU 运行了多少次 while 的循环
while (cudaEventQuery(stop) == cudaErrorNotReady)
counter++; float gpu_time = 0.0f; // 此时保证 GPU 运行完成,才能记录时间
cudaEventElapsedTime(&gpu_time, start, stop); printf("time spent by GPU: %.2f\n", gpu_time);
printf("time spent by CPU: %.2f\n", sdkGetTimerValue(&timer));
printf("CPU executed %lu iterations while waiting for GPU to finish\n", counter);
printf("\n\tFinish: %s.", correct_output(a, n, value) ? "Pass" : "Fail"); cudaEventDestroy(start);
cudaEventDestroy(stop);
cudaFreeHost(a);
cudaFree(d_a);
getchar();
return ;
}
● 输出结果:
GPU Device : "GeForce GTX 1070" with compute capability 6.1 CUDA device [GeForce GTX ]
time spent by GPU: 11.50
time spent by CPU: 0.05
CPU executed iterations while waiting for GPU to finish Finish!
▶ 新姿势:
● 调用主函数时的第0个参数作为程序名字符串,可以用于输出。
int main(int argc, char *argv[])
...
printf("%s", argv[]);
● 在没有附加 flag 的情况下申请主机内存,注意使用cudaFreeHost释放
int *a, nbytes = n * sizeof(int);
cudaMallocHost((void **)&a, nbytes);
...
cudaFreeHost(a);
● 记录 CPU 调用 CUDA 所用的时间
StopWatchInterface *timer = NULL;
sdkCreateTimer(&timer);
sdkResetTimer(&timer);
sdkStartTimer(&timer); ...// 核函数调用 sdkStopTimer(&timer);
printf("%.2f ms", sdkGetTimerValue(&timer));
● 查看GPU队列状态的函数
extern __host__ cudaError_t CUDARTAPI cudaEventQuery(cudaEvent_t event);
■ stop为放置到流中的一个事件,cudaEventQuery(stop)返回该事件的状态,等于cudaSuccess(值等于0)表示已经发生;等于cudaErrorNotReady(值等于35)表示尚未发生。源代码中利用这段时间让CPU空转,记录了迭代次数。
while (cudaEventQuery(stop) == cudaErrorNotReady) counter++;
● stdlib.h 中关于返回成功和失败的宏
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
● 示例文件中的错误检查函数(定义在helper_cuda.h中),报告出错文件、行号、函数名,并且重启cudaDevice。
#define checkCudaErrors(val) check((val), #val, __FILE__, __LINE__) template< typename T >
void check(T result, char const *const func, const char *const file, int const line)
{
if (result)
{
fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n",
file, line, static_cast<unsigned int>(result), _cudaGetErrorEnum(result), func);
DEVICE_RESET// Make sure we call CUDA Device Reset before exiting
exit(EXIT_FAILURE);
}
} #define DEVICE_RESET cudaDeviceReset();
0_Simple__asyncAPI的更多相关文章
随机推荐
- mysql操作sql的小技巧
本篇集中整理一下执行sql的小技巧,这种方式不仅带来了操作上的便捷,也可以保证数据可以数据的安全性. 1:查询数据(保证查询性能) 首先想先解释一下 SELECT * 和 SELECT t.id , ...
- 【转】Mapreduce部署与第三方依赖包管理
Mapreduce部署是总会涉及到第三方包依赖问题,这些第三方包配置的方式不同,会对mapreduce的部署便捷性有一些影响,有时候还会导致脚本出错.本文介绍几种常用的配置方式: 1. HADOOP_ ...
- JavaWeb(一)Servlet中乱码解决与转发和重定向的区别
前言 前面其实已经把Servlet中所有的内容都介绍完了,这篇讲补充一点乱码和重定向与转发之间的区别! 一.request请求参数出现乱码问题 1.1.get请求 1)乱码示例 get请求的参数是在u ...
- UrlRewriter配置IIS支持伪静态
使用UrlRewriter时遇到了一些问题,在园子里的博问中找到了Astar的回答,防止以后找不到,就记录下来了. UrlRewriter.NET官方地址:http://urlrewriter.net ...
- ch3-模板语法({{}} v-html v-bind:id 表达式 指令 修饰符 过滤器)
1 模板语法 Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. 所有 Vue.js 的模板都是合法的 HTML ,所以能被遵循规范的浏览器 ...
- webpack 的使用2
实际项目中的配置 要加__dirname 不然会报错 注意path /dist 前不要加点 结果 将两个文件打包在一起 结果 传入对象 并且单独打包 name为key 加上本次打包的hash has ...
- C# 使用Parallel并行开发Parallel.For、Parallel.Foreach实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- 不错的JQuery屏幕居中提示信息封装,使用方便,可集成到项目
function showLoad(tipInfo, type, autohide) { var pic = ""; switch (type) { case 0: // load ...
- python 设计模式,“多”例模式
版本1:一个账号不能同时是司机乘客. #-*- coding:utf-8 -*- ''' Created on 2016年8月2日 @author: yangfanholiday ''' class ...
- (转)JVM类生命周期概述:加载时机与加载过程
原文地址: http://blog.csdn.net/justloveyou_/article/details/72466105 JVM类加载机制主要包括两个问题:类加载的时机与步骤 和 类加载的方式 ...