【问题标题】:Big difference in execution time for first and subsequent run of cupy functions首次和后续运行 cupy 函数的执行时间差异很大
【发布时间】:2020-06-20 11:47:13
【问题描述】:

当我在 cupy 数组上运行 cupy 函数时,第一次调用函数所用的时间比第二次运行要长得多,即使我第二次在不同的数组上运行它也是如此。

这是为什么?

import cupy as cp
cp.__version__
# 7.5.0

A = cp.random.random((1024, 1024))
B = cp.random.random((1024, 1024))

from time import time
def test(func, *args):
    t = time()
    func(*args)
    print("{}".format(round(time() - t, 4)))
    
test(cp.fft.fft2, A)
test(cp.fft.fft2, B)
# 0.129
# 0.001
test(cp.matmul, A, A.T)
test(cp.matmul, B, B.T)
# 0.171
# 0.0
test(cp.linalg.inv, A)
test(cp.linalg.inv, B)
# 0.259
# 0.002

【问题讨论】:

    标签: cupy


    【解决方案1】:

    当您第一次在 Python 进程中使用函数时,CuPy 会在后台即时编译内核,这需要一些时间。

    来自CuPy documentation

    CuPy 使用动态内核合成:当需要内核调用时, 它编译为给定的形状和数据类型优化的内核代码 参数,将其发送到 GPU 设备,然后执行内核。这 编译后的代码缓存到 $(HOME)/.cupy/kernel_cache 目录(这个 通过设置 CUPY_CACHE_DIR 可以覆盖缓存路径 环境变量)。它可能会使第一个内核的速度变慢 调用,尽管这种减速将在第二次执行时解决。 CuPy 还将发送到 GPU 设备的内核代码缓存在 进程,这减少了进一步调用的内核传输时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 2013-09-30
      相关资源
      最近更新 更多