Cupy释放统一内存 [英] Cupy freeing unified memory

查看:195
本文介绍了Cupy释放统一内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在释放cupy中分配的内存时遇到问题.由于内存限制,我想使用统一内存.当我创建一个将分配给统一内存的变量并想要释放它时,它被标记为已释放,并且该池现在为空,可以再次使用,但是当我查看资源监视器时,内存仍然无法释放.当我创建另一个变量时,它也会添加到内存中(我认为可能会像文档中提到的那样重用标记为已占用的内存,但事实并非如此.)

I have a problem with freeing allocated memory in cupy. Due to memory constraints, I want to use unified memory. When I create a variable that will be allocated to the unified memory and want to free it, it is labelled as being freed and that the pool is now empty, to be used again, but when I take a look at a resource monitor, the memory is still not freed. When I create another variable it also adds to memory (I thought that perhaps the memory labelled as taken would be reused as is mentioned in the documentation but that is not the case.)

这是一个小程序,可以通过增加睡眠来对其进行测试,以便能够查看资源监视器中的内存变化.

Here is a little program to test this with added sleep to be able to see the memory change in a resource monitor.

import cupy as cp
import time

def pool_stats(mempool):
    print('used:',mempool.used_bytes(),'bytes')
    print('total:',mempool.total_bytes(),'bytes\n')

pool = cp.cuda.MemoryPool(cp.cuda.memory.malloc_managed) # get unified pool
cp.cuda.set_allocator(pool.malloc) # set unified pool as default allocator

print('create first variable')
val1 = cp.zeros((50*1024,10*1024))
pool_stats(pool)
time.sleep(3)


print('delete first variable')
del val1
pool_stats(pool)
time.sleep(3)

print('free cupy memory')
pool.free_all_blocks()
pool_stats(pool)
time.sleep(3)

print('create second variable')
val2 = cp.zeros((50*1024,10*1024))
pool_stats(pool)
time.sleep(3)

print('delete second variable')
del val2
pool_stats(pool)
time.sleep(3)

print('free cupy memory')
pool.free_all_blocks()
pool_stats(pool)
time.sleep(3)

这是程序的输出:

create first variable
used: 4194304000 bytes
total: 4194304000 bytes

delete first variable
used: 0 bytes
total: 4194304000 bytes

free cupy memory
used: 0 bytes
total: 0 bytes

create second variable
used: 4194304000 bytes
total: 4194304000 bytes

delete second variable
used: 0 bytes
total: 4194304000 bytes

free cupy memory
used: 0 bytes
total: 0 bytes

所以输出是我期望的.但这没有反映在资源监视器( nvtop htop )上.

So the output is what I'd expect. But this is not reflected by memory usage on resource monitors (nvtop and htop).

这是我运行此程序的结果.如 nvtop 所示.另外,当gpu内存空间不足时,它会使用系统内存(因为应该使用统一内存),这也可以在 htop 中看到(我想说的是认为是硬件监视器的问题,因为可以在2个不同的监视器中看到它)

Here is what I got running this program. As seen with nvtop. Also, when the gpu memory runs out of space, it uses systems memory (as it is supposed to with unified memory) and this is also seen in htop(I'm trying to say I don't think its the hardware monitor's problem since it is seen across 2 different monitors)

统一内存的行为应类似于默认内存.

Unified memory should behave like default memory.

默认内存图是从几乎相同的程序中获取的,但没有统一的内存.我在两者之间也获得了相同的控制台输出.

The default memory graph is taken from practically the same program but without unified memory. I get the same console output across both too.

我还尝试释放固定的内存.

I also tried freeing pinned memory.

我在做错什么吗?可能是个错误吗?这也许是内存泄漏吗?

Is there something I'm doing wrong? Might this be a bug? is this perhaps a memory leak?

我还提到了,但找不到任何内容

I also referred to this but couldn't find anything.

推荐答案

从现在的主持人删除的答案来看,这似乎是CuPy中的错误.开发人员在github网站上提出了门票补丁(截至2020-9-22).

From a now moderator deleted answer, this appears to be a bug in CuPy. A developer has raised a ticket on the github site, and a patch has been proposed (as at 2020-9-22).

短期内,您将不得不接受此问题,并等到以后的版本中出现修复程序为止.如果这很关键,请考虑从github提取当前的开发分支并构建您自己的包.没有可以解决此问题的用户代码解决方法.

In the short term, you will have to accept this as a problem and wait until a fix appears in a future release. If this is critical, then consider pulling the current development branch from github and building your own package. There is no user code workaround which can solve this.

这篇关于Cupy释放统一内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆