我想將兩個巨大的矩陣相乘,大小超過 100,000 行和列。我在具有多個 GPU 的服務器上運行任務,假設有 8 個 RTX 3090 GPU,它們的記憶體大小為 24GB,顯然矩陣無法放入其中,因此我不能直接使用 cupy.array。這是我的想法:
- 使用 numpy.array 在主記憶體中存盤兩個矩陣
- 將它們切成塊,可能是 4 塊或 9 塊
- 將塊發送到 GPU,計算它
- 將結果塊檢索到主記憶體,重新組裝它們
以下是我的問題:
- python中有沒有可以自動實作我的想法的庫?
- 我想并行使用GPU,我認為瓶頸是主存和GPU記憶體之間的資料傳輸,即numpy.array -> cupy.array。我可以使用多處理庫并行移動資料嗎?PCIe總線怎么樣?
筆記:
- 假設矩陣不是稀疏的。
[[a1,b1], * [[a2,b2], = [[a1a2 b1c2, a1b2 b1d2],
[c1,d1]] [c2,d2]] [c1a2 d1c2, c1b2 d1d2]]
import cupy as cp
import numpy as np
N = 27000
P = 27000
# init two matrices
source1 = np.random.random((N * 2, P * 2))
source2 = np.random.random((N * 2, P * 2))
# cut them in blocks
a1 = source1[:N, :P]
b1 = source1[:N, P:]
c1 = source1[N:, :P]
d1 = source1[N:, P:]
a2 = source2[:N, :P]
b2 = source2[:N, P:]
c2 = source2[N:, :P]
d2 = source2[N:, P:]
# move a1 and a2 to one gpu
m1 = cp.array(a1)
m2 = cp.array(a2)
r1 = m1 * m2
# free memory so that m3 and m4 can fit in gpu's ram
del m1
del m2
# move b1 and c2 to one gpu
m3 = cp.array(b1)
m4 = cp.array(c2)
r2 = m3 * m4
del m3
del m4
r1 = r2
uj5u.com熱心網友回復:
Python 有一個特殊的庫:https://documen.tician.de/pycuda/
簡單例子:
import pycuda.autoinit
import pycuda.driver as drv
import numpy
from pycuda.compiler import SourceModule
mod = SourceModule("""
__global__ void multiply_them(float *dest, float *a, float *b)
{
const int i = threadIdx.x;
dest[i] = a[i] * b[i];
}
""")
multiply_them = mod.get_function("multiply_them")
a = numpy.random.randn(400).astype(numpy.float32)
b = numpy.random.randn(400).astype(numpy.float32)
dest = numpy.zeros_like(a)
multiply_them(
drv.Out(dest), drv.In(a), drv.In(b),
block=(400,1,1), grid=(1,1))
print dest-a*b
uj5u.com熱心網友回復:
查看“cuBLAS 多 GPU 擴展”:https : //developer.nvidia.com/cublas
您必須申請搶先體驗計劃。現有的 Python 庫可能不會利用此擴展,但您可以在更新 CUDA 庫后啟用它。獲得訪問權限后,您必須閱讀檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/398142.html
