Pointers 将指向设备常量内存的指针传递给内核,而不是直接使用

Pointers 将指向设备常量内存的指针传递给内核,而不是直接使用,pointers,memory,cuda,constants,Pointers,Memory,Cuda,Constants,我在Ubuntu12.10和GCC4.6上使用CUDA 5.0和GTX670,我编写了一个名为Grid的类: Grid类正在其他两个类中使用: 点云库达 粒子系统 现在我想在pointcloudcuda和particlesystem的内核中使用Grid的(非静态)方法,尽管它们将使用不同的网格(不同的网格对象具有不同的值)。因此,对于我使用Grid的所有类,我有两个选择: 1) 我就是这么做的 Grid hostGrid(...); cudaMalloc(gridOnDeviceGlob

我在Ubuntu12.10和GCC4.6上使用CUDA 5.0和GTX670,我编写了一个名为Grid的类:

Grid类正在其他两个类中使用:

  • 点云库达
  • 粒子系统
现在我想在pointcloudcuda和particlesystem的内核中使用Grid的(非静态)方法,尽管它们将使用不同的网格(不同的网格对象具有不同的值)。因此,对于我使用Grid的所有类,我有两个选择:

1) 我就是这么做的

Grid hostGrid(...);
cudaMalloc(gridOnDeviceGlobal, sizeof(Grid))
cudaMemcpy(gridOnDeviceGlobal, &hostGrid, sizeof(Grid), cudaMemcpyHostToDevice)
cloudKernel<<< numBlocks, numThreads >>>(someDate, gridOnDeviceGlobal);
输入pointcloudcuda.cu,以及两个函数

void copyParametersToGpu(Grid *hostGrid)
{
    cudaMemcpyToSymbolAsync(myGridForPointCloudCuda, hostGrid, sizeof(Grid))
}

void getDevicePointerOfGridForPointCloudCuda(Grid** ptr)
{
    cudaGetSymbolAddress((void**)ptr, myGridForPointCloudCuda);
}
现在,在pointcloudcuda.cpp中,我可以

Grid hostGrid(...);
copyParametersToGpu(&hostGrid);
Grid* gridOnDeviceConstant;
getDevicePointerOfGridForPointCloudCuda(&gridOnDeviceConstant);
cloudKernel<<< numBlocks, numThreads >>>(someDate, gridOnDeviceConstant);
Grid主机网格(…);
copyParametersToGpu(&hostGrid);
Grid*GridOnDeviconstant;
getDevicePointerOfGridForPointCloudCuda(&GridOnDeviceInstant);
cloudKernel>(someDate,gridOnDeviceConstant);
在我看来,2)的优点是能够更快地访问内核中的常量内存。不过,在其他地方,我读到这不起作用,因为编译CUDA内核的编译器在编译时不知道传递的网格指针是指向全局内存还是常量内存,因此必须使用较慢的内存获取指令

在Geforce GTX 670上,2)会比1)快吗

有没有更好的方法来做我想做的事?我只需要将不同的网格实例传递给内核。在我开始使用多个网格实例之前,常量变量是一个舒适且快速的选择


谢谢

如果您有多个网格实例,而不仅仅是在恒定内存中分配一个网格数组,请将网格实例复制到数组中,并在调用内核时将索引传递到网格数组中,而不是指向特定网格实例的指针。在内核内部,使用索引访问特定的网格实例

Grid hostGrid(...);
copyParametersToGpu(&hostGrid);
Grid* gridOnDeviceConstant;
getDevicePointerOfGridForPointCloudCuda(&gridOnDeviceConstant);
cloudKernel<<< numBlocks, numThreads >>>(someDate, gridOnDeviceConstant);