【发布时间】:2019-06-19 22:25:20
【问题描述】:
我正在学习使用 cupy。但是我发现了一个非常令人困惑的问题。似乎cupy在一个程序中表现良好一开始。当它运行一段时间时,Cupy 似乎要慢得多。代码如下:
import cupy as np
from line_profiler import LineProfiler
def test(ary):
for i in range(1000):
ary**6
def main():
rand=np.random.rand(1024,1024)
test(rand)
test(rand)
test(rand)
test(rand)
test(rand)
test(rand)
test(rand)
lp = LineProfiler()
lp_wrapper = lp(main)
lp_wrapper()
lp.print_stats()
这是时间表现:
Timer unit: 2.85103e-07 s
Total time: 16.3308 s
File: E:\Desktop\test.py
Function: main at line 8
Line # Hits Time Per Hit % Time Line Contents
==============================================================
8 def main():
9 1 1528817.0 1528817.0 2.7 rand=np.random.rand(1024,1024)
10 1 111014.0 111014.0 0.2 test(rand)
11 1 94528.0 94528.0 0.2 test(rand)
12 1 95636.0 95636.0 0.2 test(rand)
13 1 94892.0 94892.0 0.2 test(rand)
14 1 7728318.0 7728318.0 13.5 test(rand)
15 1 23872383.0 23872383.0 41.7 test(rand)
16 1 23754666.0 23754666.0 41.5 test(rand)
当cupy完成5000次通电后,变得很慢。
我在windows上跑了这段代码,cuda版本是10.0
希望得到答案。非常感谢!
感谢您的回答!我打印了 Cupy 的内存使用情况:
import cupy as np
def test(ary):
mempool = cupy.get_default_memory_pool()
pinned_mempool = cupy.get_default_pinned_memory_pool()
for i in range(1000):
ary**6
print("used bytes: %s"%mempool.used_bytes())
print("total bytes: %s\n"%mempool.total_bytes())
def main():
rand=np.random.rand(1024,1024)
test(rand)
test(rand)
test(rand)
test(rand)
test(rand)
test(rand)
test(rand)
这是输出:
used bytes: 8388608
total bytes: 16777216
used bytes: 8388608
total bytes: 16777216
used bytes: 8388608
total bytes: 16777216
used bytes: 8388608
total bytes: 16777216
used bytes: 8388608
total bytes: 16777216
used bytes: 8388608
total bytes: 16777216
used bytes: 8388608
total bytes: 16777216
似乎在迭代期间 GPU 内存使用量保持不变。
顺便问一下,有没有什么办法可以避免这种减速?
【问题讨论】: