博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CUDA入门(六) 异步并行执行解析
阅读量:6954 次
发布时间:2019-06-27

本文共 2928 字,大约阅读时间需要 9 分钟。

为了更好地压榨GPU和CPU,很多时候都使用异步并行的方法让主机端与设备端并行执行,即控制在设备没有完成任务请求前就被返回给主机端。

异步执行的意义在于:首先,处于同一数据流内的计算与数据拷贝都是依次进行的,但一个流内的计算可以和另一个流的数据传输同时进行,因此通过异步执行就能够使GPU中的执行单元与存储器单元同时工作,更好地压榨GPU的性能;其次,当GPU在进行计算或者数据传输时就返回给主机线程,主机不必等待GPU运行完毕就可以进行进行一些计算,更好地压榨了CPU的性能。
之前,单单使用GPU的同步函数,会使设备在完成请求任务前,不会返回主机线程,主机线程将进入让步(yield)、阻滞(block)或者自旋(spin)。
下面一个SDK简单的异步并行执行的程序:

#include "cuda_runtime.h"#include "device_launch_parameters.h"#include 
#include
#include
#include"helper_timer.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 correct_output(int *data, const int n, const int x){ for (int i = 0; i < n; i++) if (data[i] != x) return 0;}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; int *a = 0; cudaMallocHost((void**)&a, nbytes); memset(a, 0, nbytes); 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 startGPU, stopGPU; cudaEventCreate(&startGPU); cudaEventCreate(&stopGPU); StopWatchInterface *timer = NULL; sdkCreateTimer(&timer); sdkResetTimer(&timer); cudaThreadSynchronize(); float gpu_time = 0.0f; sdkStartTimer(&timer); cudaEventRecord(startGPU, 0); cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, 0); increment_kernel <<
> >(d_a, value); cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, 0); cudaEventRecord(stopGPU, 0); sdkStopTimer(&timer); unsigned long int counter = 0; while (cudaEventQuery(stopGPU) == cudaErrorNotReady) { counter++; } cudaEventElapsedTime(&gpu_time, startGPU, stopGPU); printf("time spent executing by the GPU:%.2f\n", gpu_time); printf("time spent by CPU in CUDA calls:%.8f\n", sdkGetTimerValue(&timer)); printf("GPU execute %d iteration while waiting forGPU to finish\n", counter); printf("-------------------------------------------------------------------------\n"); if (correct_output(a, n, value)) printf("TEST PASSED\n"); else printf("Test FAILED\n"); cudaEventDestroy(startGPU); cudaEventDestroy(stopGPU); cudaFreeHost(a); cudaFree(d_a); getchar(); cudaThreadExit(); return 0;}

执行结果:

这里写图片描述
代码中
GPU上执行的是对每一个数据进行加26的处理,在CPU上执行加1迭代,
执行结果依次是GPU执行的时间,异步时CPU调用GPU所用的时间,以及在GPU上进行计算时CPU进行迭代的次数。

其中:

void *memset(void *s, int ch, size_t n);
函数解释:将s中前n个字节替换为ch并返回s;

memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。

helper_timer.h在C:\ProgramData\NVIDIA Corporation\CUDA Samples\v7.5\common\inc下是在CUDA取消了cutil.h后包含计时的一个文件

cudaThreadSynchronize()函数是是CPU与GPU同步

你可能感兴趣的文章
2017"百度之星"程序设计大赛 - 复赛1003&&HDU 6146 Pokémon GO【数学,递推,dp】
查看>>
[LeetCode] Valid Parenthesis String 验证括号字符串
查看>>
各大SRC中的CSRF技巧
查看>>
Docker for Windows 使用入门
查看>>
【Django】Web应用开发经由
查看>>
SpringBoot(九)-- SpringBoot JDBC
查看>>
Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
查看>>
基于Centos搭建nginx+uwsgi运行django环境
查看>>
context switch
查看>>
Oracle awr报告生成操作步骤
查看>>
【DB2】DB2使用IMPORT命令导入含有自增长列的表报错处理
查看>>
微服务之springCloud-docker-comsumer(三)
查看>>
dhcpcd守护进程分析【转】
查看>>
Linux - 理不清的权限chmod与chown区别
查看>>
TCP协议疑难杂症全景解析
查看>>
redis 1
查看>>
Python安装pycurl失败,及解决办法
查看>>
cocos2d的常用动作及效果总结之四:Special Actions
查看>>
[ lucene扩展 ] MoreLikeThis 相似检索
查看>>
如果返回结构体类型变量(named return value optimisation,NRVO)
查看>>