我可以减少 CUDA 中数组的大小吗
Can I reduce size of array in CUDA
我已经使用 cudaMalloc3D 为 3d 数组分配了内存 - 在执行第一个内核后,我确定我不需要它的一部分。
例如在伪代码中:
A = [100,100,100]
kernel()// data of intrest is just in subrange of A
B = [10:20, 20:100, 50:80]// part that I need other entries I would like to have removed
... // new allocations
kernelb()...
我想释放的剩余内存(或立即用于我现在需要分配的其他数组)
我知道我可以释放数组并重新分配 - 但它似乎不是最佳选择。
P.S.
顺便问一下,有没有办法像 cudaMalloc3D 一样使用 cudaMallocAsync - 我的意思是 cudaMalloc3D 可以方便地使用 3d 数组并处理填充。
当前的 CUDA API 没有 realloc 功能。
您似乎已经知道 cudaMalloc smaller array -> cudaMemcpy to smaller array -> cudaFree large array
的常见解决方法
如果您确实需要 realloc
,您可以使用 GPU 虚拟内存管理编写自己的分配器。 https://developer.nvidia.com/blog/introducing-low-level-gpu-virtual-memory-management/
我已经使用 cudaMalloc3D 为 3d 数组分配了内存 - 在执行第一个内核后,我确定我不需要它的一部分。 例如在伪代码中:
A = [100,100,100]
kernel()// data of intrest is just in subrange of A
B = [10:20, 20:100, 50:80]// part that I need other entries I would like to have removed
... // new allocations
kernelb()...
我想释放的剩余内存(或立即用于我现在需要分配的其他数组)
我知道我可以释放数组并重新分配 - 但它似乎不是最佳选择。 P.S.
顺便问一下,有没有办法像 cudaMalloc3D 一样使用 cudaMallocAsync - 我的意思是 cudaMalloc3D 可以方便地使用 3d 数组并处理填充。
当前的 CUDA API 没有 realloc 功能。
您似乎已经知道 cudaMalloc smaller array -> cudaMemcpy to smaller array -> cudaFree large array
如果您确实需要 realloc
,您可以使用 GPU 虚拟内存管理编写自己的分配器。 https://developer.nvidia.com/blog/introducing-low-level-gpu-virtual-memory-management/