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

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

相关推荐
不教书的塞涅卡2 天前
SSH远程接入PyTorch-CUDA-v2.9镜像,随时随地训练大模型
pytorch·ssh·cuda
安全二次方security²4 天前
CUDA C++编程指南(7.19&20)——C++语言扩展之Warp投票函数和Warp匹配函数
c++·人工智能·nvidia·cuda·投票函数·匹配函数·vote
安全二次方security²5 天前
CUDA C++编程指南(7.15&16)——C++语言扩展之内存空间谓词和转化函数
c++·人工智能·nvidia·cuda·内存空间谓词函数·内存空间转化函数·address space
安全二次方security²5 天前
CUDA C++编程指南(7.5&6)——C++语言扩展之内存栅栏函数和同步函数
c++·人工智能·nvidia·cuda·内存栅栏函数·同步函数·syncthreads
安全二次方security²6 天前
CUDA C++编程指南(7.2)——C++语言扩展之变量内存空间指定符
c++·人工智能·nvidia·cuda·内存空间指定符·__shared__·__device__
安全二次方security²6 天前
CUDA C++编程指南(7.1)——C++语言扩展之函数执行空间指定符
c++·人工智能·nvidia·cuda·cuda编程·global·函数执行空间指定符
八位数花园8 天前
PyTorch-CUDA镜像支持Knowledge Graph Embedding吗?
pytorch·cuda·知识图谱嵌入
KIDGINBROOK9 天前
DeepSeek DeepEP学习(五)Hybrid-EP dispatch
cuda·deepseek·deepep
被制作时长两年半的个人练习生10 天前
【FlashAttention】 FA2与FA1算法区别辨析
attention·cuda
程序员老周66610 天前
10.一文学会GPU与cuda原理,并从其原理来理解FlashAttention
人工智能·深度学习·语言模型·大模型·transformer·gpu算力·cuda