c++ - CUDA内存分配性能

标签 c++ cuda

我正在 CUDA 上使用图像过滤器。图像处理比在 CPU 上快得多。但是问题是图片的分配真的很慢。

这就是我分配内存和设置图像的方式。

hr = cudaMalloc(&m_device.originalImage,    size);                                                                          
hr = cudaMalloc(&m_device.modifiedImage,    size);                                                                          
hr = cudaMalloc(&m_device.tempImage,    size);                                                                  
hr = cudaMemset( m_device.modifiedImage, 0, size);                                                                          
hr = cudaMemcpy( m_device.originalImage, host.originalImage, size, cudaMemcpyHostToDevice); 

这是执行程序的结果。

C:\cpu_gpu_filters(GPU)\x64\Release>cpu_gpu_filters test-case.txt
C:\Users\Max\Desktop\test_set\cheshire_cat_1280x720.jpg
Init time: 519 ms
Time spent: 2.35542 ms
C:\Users\Max\Desktop\test_set\cheshire_cat_1366x768.jpg
Init time: 31 ms
Time spent: 2.68595 ms
C:\Users\Max\Desktop\test_set\cheshire_cat_1600x900.jpg
Init time: 44 ms
Time spent: 3.54835 ms
C:\Users\Max\Desktop\test_set\cheshire_cat_1920x1080.jpg
Init time: 61 ms
Time spent: 4.98131 ms
C:\Users\Max\Desktop\test_set\cheshire_cat_2560x1440.jpg
Init time: 107 ms
Time spent: 9.0727 ms
C:\Users\Max\Desktop\test_set\cheshire_cat_3840x2160.jpg
Init time: 355 ms
Time spent: 20.1453 ms
C:\Users\Max\Desktop\test_set\cheshire_cat_5120x2880.jpg
Init time: 449 ms
Time spent: 35.815 ms
C:\Users\Max\Desktop\test_set\cheshire_cat_7680x4320.jpg
Init time: 908 ms
Time spent: 75.4647 ms

UPD 带有时间测量的代码:

start = high_resolution_clock::now();
Initialize();
stop = high_resolution_clock::now();
long long ms = duration_cast<milliseconds>(stop - start).count();
long long us = duration_cast<microseconds>(stop - start).count();
cout << "Init time: " << ms << " ms" << endl;


start = high_resolution_clock::now();
GpuTimer gpuTimer;
gpuTimer.Start();
RunGaussianBlurKernel(
    m_device.modifiedImage,
    m_device.tempImage,
    m_device.originalImage, 
    m_device.filter,
    m_filter.width,
    m_host.originalImage.rows, 
    m_host.originalImage.cols
    );
gpuTimer.Stop();

第一个图像是最小的,但初始化需要 519 毫秒。也许,这是因为需要加载驱动程序或其他东西。然后,当图像尺寸增加时,初始化时间也会增加。实际上,这看起来合乎逻辑,但我仍然不确定初始化过程应该那么慢。难道我做错了什么?

最佳答案

在您的单元代码中,您有一个 cudaMemset,其执行时间取决于大小。还有 cudaMemcpy,其执行时间大致由内存拷贝大小(以字节为单位)除以 PCI-Express 的带宽得出。这部分很可能是导致 init 时间增加的原因。通过 NSIGHT 运行它将为您提供更精确的执行时间数据。但是,如果没有 MCVE,则很难确定。

关于c++ - CUDA内存分配性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37264010/

相关文章:

c++ - 将数组分配给多维数组,反之亦然

c++ - 初始化 POD 数组中的第一个元素,保留其余元素未初始化

c++ - 使用双向链表实现稀疏矩阵中的多项式

optimization - CUDA:while 循环索引正确性

python - Tensorflow 导入错误

c++ - 如何使用 Qt oauth 创建登录页面?

c++ - Unresolved external 噩梦

c++ - Cuda:访问冲突写入位置 0x0000000000000000

c++ - 如何使用 CUDA 将 std::vector<std::string> 复制到 GPU 设备

cuda 5.0支持的c++版本