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

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

相关推荐
Starry-sky(jing)2 天前
onnxruntime 安装后 GPU 没加速?3 条命令确认它跑在 CPU 还是 GPU
python·gpu·cuda·onnxruntime·推理加速
论文复现现场8 天前
LAMMPS 用 RTX 5090 能跑多大?32GB 显存、CUDA 编译与规模验证
cuda·分子动力学·lammps·rtx5090
EatFan8 天前
从 CUDA 到昇腾:一张对照表拆解 DeepSeek 最新开源组件(TileLang / DeepGEMM / DeepEP / TileKernels)
开源·cuda·deepseek·deepgemm·tilelang·华为昇腾
论文复现现场10 天前
RTX 3090 24GB 适合科研吗?单卡论文复现、PyTorch 显存检查与 OOM 排错
人工智能·pytorch·python·深度学习·cuda·rtx3090
论文复现现场13 天前
RTX 5090 32GB 适合科研计算吗?单卡训练、推理、论文复现与 OOM 判断方法
pytorch·深度学习·cuda·rtx5090
论文复现现场13 天前
论文复现看到“RTX 3090 or higher”:显存、CUDA、batch size 与 OOM 怎么判断?
开发语言·pytorch·batch·cuda·rtx3090
_nibel13 天前
第二章:Details 重点数据细读 ⭐教你读懂 Nsight Compute 报告 系列合集⭐
cuda·ncu·nsight compute
论文复现现场22 天前
MiniCPM-o 4.5 需要多少显存?RTX 3090 24GB 的 BF16、AWQ、GGUF 部署边界与验收方法
cuda·多模态大模型·显存优化·rtx3090·minicpm-o
qq_1998868723 天前
第8板块·第2节:统一内存的高级特性与性能调优
c++·人工智能·gpu算力·cuda
qq_1998868723 天前
第8板块·第3节:设备间拷贝与点对点(P2P)传输
c++·人工智能·gpu算力·cuda