liz*_*iza 2 memory-management cuda
是否可以将内存添加到全局内存中先前分配的数组?
我需要做的是:
//cudamalloc memory for d_A
int n=0;int N=100;
do
{
Kernel<<< , >>> (d_A,n++);
//add N memory to d_A
while(n!=5)}
Run Code Online (Sandbox Code Playgroud)
做另一个cudamalloc删除以前分配的数组的值?在我的情况下,应该保留以前分配的数组的值...
首先,cudaMalloc的行为类似于malloc,而不是realloc.这意味着cudaMalloc将在新位置分配全新的设备内存.cuda API中没有realloc函数.
其次,作为一种解决方法,您可以再次使用cudaMalloc来分配更多内存.在分配新地址之前,请记住使用cudaFree释放设备指针d_a.以下代码在功能上是您想要的.
int n=0;int N=100;
//set the initial memory size
size = <something>;
do
{
//allocate just enough memory
cudaMalloc((void**) &d_A, size);
Kernel<<< ... >>> (d_A,n++);
//free memory allocated for d_A
cudaFree(d_A);
//increase the memory size
size+=N;
while(n!=5)}
Run Code Online (Sandbox Code Playgroud)
第三,cudaMalloc可能是一项昂贵的操作,我希望上面的代码会很慢.我想你应该考虑为什么要增长阵列.d_A对于最大的用例,你能为内存分配一次内存吗?如果您知道以后需要1,000个字节,则可能没有理由只分配100个字节!
//calculate the max memory requirement
MAX_SIZE = <something>;
//allocate only once
cudaMalloc((void**) &d_A, MAX_SIZE);
//use for loops when they are appropriate
for(n=0; n<5; n++)
{
Kernel<<< ... >>> (d_A,n);
}
Run Code Online (Sandbox Code Playgroud)