【问题标题】:Cupy become slower when iterations increase当迭代次数增加时,Cupy 会变慢
【发布时间】: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 内存使用量保持不变。

顺便问一下,有没有什么办法可以避免这种减速?

【问题讨论】:

    标签: python gpu cupy


    【解决方案1】:

    这是 CUDA 内核队列的问题。

    请参阅以下内容:

    在您的代码中观察到的短暂执行是假的,因为当队列未填满时,cupy立即返回

    实际表现是最后一行。

    注意:这不是内存分配问题——正如我最初在最初的回答中所建议的那样——但我将原始答案记录在这里。


    原始(错误)答案

    可能是由于重新分配。

    当您import cupy 时,cupy 会分配“一些”GPU 内存。当cupy全部使用时,它必须分配更多的内存。这会增加执行时间。

    【讨论】:

    • 非常感谢! Cupy 是否提供任何清除内存的方法?
    • @Colin 请看我的帖子脚本。很抱歉让您感到困惑。
    • 谢谢!我的问题与问题 1294 类似。非常感谢您的帮助!
    猜你喜欢
    • 2017-03-25
    • 1970-01-01
    • 2020-04-21
    • 2011-10-11
    • 2020-08-27
    • 2011-08-16
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多