c++ - 优化具有不规则内存访问的 CUDA 内核

标签 c++ c cuda gpgpu nvidia

我有以下 CUDA 内核,它似乎很难优化:

__global__ void DataLayoutTransformKernel(cuDoubleComplex* d_origx, cuDoubleComplex* d_origx_remap, int n, int filter_size, int ai )
{
    for(int idx = blockIdx.x * blockDim.x + threadIdx.x; idx < filter_size; idx+=blockDim.x * gridDim.x)
    {
        int index = (idx * ai) & (n-1);
        d_origx_remap[idx] = d_origx[index];
    }
}

//Parameters were defined before
int permute[loops] = {29165143,3831769,17603771,9301169,32350975, ...}
int n = 33554432;
int filter_size = 1783157;

for(int i=0; i<loops; i++)
{
    DataLayoutTransformKernel<<<dimGrid, dimBlock, 0, stream[i]>>>((cuDoubleComplex*) d_origx,(cuDoubleComplex*)d_origx_remap+i*filter_size, n, filter_size, permute[i]);

}

内核的目的是将d_origx[]的数据布局从不规则重新排序为规则(d_origx_remap)。内核以不同的访问步长 (ai) 启动多次。

这里的挑战是引用 d_origx[index] 数组时的不规则内存访问模式。我的想法是使用共享内存。但是对于这种情况,似乎很难使用共享内存来合并全局内存访问。

有没有人对如何优化这个内核有建议?

最佳答案

Trove 库是一个支持 AoS 的 CUDA/C++ 库,可能为随机 AoS 访问提供接近最佳的性能。从 GitHub 页面看来,对于 16 字节结构,trove 将比原始方法高出大约 2 倍。

https://github.com/BryanCatanzaro/trove

Random access performance using Trove compared to the naive direct access approach

关于c++ - 优化具有不规则内存访问的 CUDA 内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512257/

相关文章:

c++ - 如果发生错误则返回错误值,否则继续执行函数

c++ - 所有者绘制菜单的自定义背景

ios - 在 iOS 应用程序中运行 C 守护进程

cuda - 为什么编译器会给出错误?

c++ - CUDA 中的性能

C++ 膨胀 gzip 字符数组

c - C程序设计

c - 使用一系列管道过滤文本文件

ubuntu - 如何在 ubuntu 18.1 上安装 cuda-toolkit-10-0?

c++ - Microsoft SAL 如何防止差一错误