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

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

一、记录耗时案例

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

cpp 复制代码
#include <iostream>
#include <vector>
#include <cuda_runtime.h>

 
__global__ void dummyKernel() {

    // Dummy kernel to ensure CUDA context is initialized
}

 

int main() {

    // CUDA device IDs
    int device1 = 0;
    int numIterations = 10; // Number of times to call cudaSetDevice

 
    // Create CUDA events
    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    // Vector to store elapsed times
    std::vector<float> elapsedTimes(numIterations);

 
    // Set initial device (optional, but ensures a known starting state)
    cudaSetDevice(device1);

 
    // Measure time for multiple cudaSetDevice calls
    for (int i = 0; i < numIterations; ++i) {
        // Record the start event
        cudaEventRecord(start, 0);
 
        // Set the device (this is the operation we are timing)
        cudaSetDevice(device1);

        // Record the stop event
        cudaEventRecord(stop, 0);

        // Measure the elapsed time between the start and stop events
        cudaEventElapsedTime(&elapsedTimes[i], start, stop);

        // Output results
        std::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 results
    std::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 ready
    dummyKernel<<<1, 1>>>();
    cudaDeviceSynchronize();
 

    // Clean up
    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    return 0;
}

二、编译和运行

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

bash 复制代码
nvcc -o test_cudaSetDevice_multiple test_cudaSetDevice_multiple.cu

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

bash 复制代码
./test_cudaSetDevice_multiple

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

相关推荐
2601_9623878216 小时前
Python CUDA 编程 - 2 - Numba 简介
python·numpy·cuda·jit编译器·numba
每日出拳老爷子4 天前
【AI】Ollama 更新后 skipping CUDA 掉回 CPU
gpu·nvidia·cuda·ollama·本地大模型
CODER03048 天前
ubuntu24.04:降内核+显卡驱动+cuda+cudnn+pytorch+anaconda+pycharm
人工智能·pytorch·深度学习·自然语言处理·cuda·ubuntu24.04·降内核
Zhu75814 天前
GPU驱动与CUDA安装指南
nvidia·cuda
喜壹LittlePrince15 天前
CUDA 入门第一课:从线程索引到向量加法与朴素矩阵乘法
cuda·hpc
xier_ran17 天前
【infra之路】GPU 执行与存储层次全景关系图
人工智能·深度学习·cuda
椒颜皮皮虾྅21 天前
【TensorRTtSharp v4.0】在 C# 中使用 TensorRT CSharp API v4.0 动态编译并运行 CUDA Kernel
android·开发语言·人工智能·深度学习·c#·cuda
Albart57521 天前
vLLM多卡部署终极踩坑:CUDA error worker进程异常退出 完整定位&生产根治方案
cuda·nccl·vllm·大模型部署·多卡推理·大模型踩坑
帅气的小峰24 天前
pybind11与nanobind可以共存吗?如何迁移?
c++·python·cuda·推理引擎
(initial)1 个月前
C-05. Kernel Fusion 代价边界:少写回 vs 寄存器压力与 occupancy
c语言·开发语言·cuda