CUDA
🔗 CuPy
CuPyはNumPyまたはSciPyをCUDA上で動かすライブラリ。
$ conda install -c conda-forge cupy
速度比較
import time
import numpy as np
import cupy as cp
# CPU
start_time = time.time()
a = np.random.rand(5000, 5000)
b = np.random.rand(5000, 5000)
result = np.dot(a, b)
end_time = time.time()
print(f"NumPy Time: {end_time - start_time} seconds")
# GPU
start_time = time.time()
a = cp.random.rand(5000, 5000)
b = cp.random.rand(5000, 5000)
result = cp.dot(a, b)
end_time = time.time()
print(f"CuPy Time: {end_time - start_time} seconds")1回目
NumPy Time: 0.7657599449157715 seconds
CuPy Time: 0.2356104850769043 seconds
2回目
NumPy Time: 0.7455253601074219 seconds
CuPy Time: 0.23205804824829102 seconds
3回目
NumPy Time: 0.7388787269592285 seconds
CuPy Time: 0.22411775588989258 seconds
平均
NumPy Time: 0.750054677327474 seconds ①
CuPy Time: 0.230595429738363 seconds ②
① / ② 3.25
3.25倍速い。