当前位置: 首页 > news >正文

CUDA 计时功能,记录GPU程序/函数耗时,cudaEventCreate,cudaEventRecord,cudaEventElapsedTime

为了测试GPU函数的耗时,可以使用 CUDA 提供的计时功能:cudaEventCreate, cudaEventRecord, 和 cudaEventElapsedTime。这些函数可以帮助你测量某个 CUDA 操作(如设置设备)所花费的时间。

一、记录耗时案例

以下是一个示例程序,它测量调用 cudaSetDevice 所花费的时间:

#include <iostream>
#include <vector>
#include <cuda_runtime.h>__global__ void dummyKernel() {// Dummy kernel to ensure CUDA context is initialized
}int main() {// CUDA device IDsint device1 = 0;int numIterations = 10; // Number of times to call cudaSetDevice// Create CUDA eventscudaEvent_t start, stop;cudaEventCreate(&start);cudaEventCreate(&stop);// Vector to store elapsed timesstd::vector<float> elapsedTimes(numIterations);// Set initial device (optional, but ensures a known starting state)cudaSetDevice(device1);// Measure time for multiple cudaSetDevice callsfor (int i = 0; i < numIterations; ++i) {// Record the start eventcudaEventRecord(start, 0);// Set the device (this is the operation we are timing)cudaSetDevice(device1);// Record the stop eventcudaEventRecord(stop, 0);// Measure the elapsed time between the start and stop eventscudaEventElapsedTime(&elapsedTimes[i], start, stop);// Output resultsstd::cout << "Number of iterations: i " << i << std::endl;std::cout << " time to set device " << device1 << ": " << elapsedTimes[i] << " ms" << std::endl;}// Calculate statistics (e.g., average time)float totalTime = 0.0f;for (float time : elapsedTimes) {totalTime += time;}float averageTime = totalTime / numIterations;// Output resultsstd::cout << "Number of iterations: " << numIterations << std::endl;std::cout << "Average time to set device " << device1 << ": " << averageTime << " ms" << std::endl;// Optionally, run a dummy kernel to ensure CUDA is initialized and readydummyKernel<<<1, 1>>>();cudaDeviceSynchronize();// Clean upcudaEventDestroy(start);cudaEventDestroy(stop);return 0;
}

二、编译和运行

         2.1 编译: 使用 nvcc 编译这个 CUDA 程序。(上面程序文件铭为test_cudaSetDevice_multiple.cu)

nvcc -o test_cudaSetDevice_multiple test_cudaSetDevice_multiple.cu

        2.2 运行: ,然后运行生成的可执行文件。

./test_cudaSetDevice_multiple

哈哈哈,就得到运行结果啦! 

http://www.lryc.cn/news/501101.html

相关文章:

  • PDF 文件如何转为 CAD 图纸?PDF2CAD 使用教程
  • 【YashanDB知识库】php查询超过256长度字符串,数据被截断的问题
  • 暴雨AI加速计算服务器新品X8840上市
  • 在多个分布式机器间设置和使用 NFS(Network File System)共享目录的步骤如下:
  • 机器学习中的 Transformer 简介(第 1 部分)
  • D3实现站点路线图demo分享
  • 非文件形式的内存动态函数库调用接口
  • liunx docker 部署 nacos seata sentinel
  • 解决没法docker pull问题
  • 面试小札:闪电五连鞭_2
  • Milvus向量数据库06-RAG检索增强
  • 信创国产化时代:打造安全高效的信创网站解决方案
  • python编程Day13-异常介绍捕获异常抛出异常
  • 【JAVA高级篇教学】第二篇:使用 Redisson 实现高效限流机制
  • 力扣-图论-8【算法学习day.58】
  • Spring 中的验证、数据绑定和类型转换
  • Github----提交人不是自己
  • 常用工具软件
  • Oracle报错ORA-01653: 表xx无法通过 8192在表空间中扩展
  • 【C语言】库函数常见的陷阱与缺陷(3):内存分配函数
  • Vue前端实现预览并打印PDF文档
  • CSS学习记录07
  • 喆塔科技携手国家级创新中心,共建高性能集成电路数智化未来
  • 基于单片机的汽车雨刷器装置
  • 013-SpringBoot 定义优雅的全局异常处理方式
  • nginx 网页正常访问 F5 404
  • Idea Spring Initializr没有 Java 8选项解决办法
  • 【Leetcode Top 100】104. 二叉树的最大深度
  • C#实现一个HttpClient集成通义千问-开发前准备
  • 使用ssh免密登录实现自动化部署rsync+nfs+lsync(脚本)