使用float64 数据,结果大小约为 7 Go,因此不适合大量 PC RAM。但是你只有 30720² # 1e9 乘法要做,这需要几秒钟。
避免内存问题的一种方法是将结果切成合理的块,大小
n=3
div=10240
a=rand(n*div,1)
b=rand(1,n*div)
import pickle
def calculate(i,j):
u=dot(a[i*div:(i+1)*div,:],b[:,j*div:(j+1)*div])
return u
def save(i,j,u):
with open('data'+str(i)+str(j)+'.pk','wb') as f :
pickle.dump(u,f)
def timecount(f,args):
t0=time.time()
res=f(*args)
return res,time.time()-t0
def multidot():
tcalc,tsave=0,0
for i in range(n):
for j in range(n):
print (i,j)
u,dt=timecount(calculate,(i,j))
tcalc+=dt
_,dt=timecount(save,(i,j,u))
tsave+=dt
print('dot time',tcalc)
print('save time',tsave)
然后运行:
In [64]: multidot()
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
dot time 4.697121858596802
save time 29.11250686645508
所以dot 没有问题,只是内存问题。
要读回你的数据,逐块读取,就像这样:
with open('data00.pk','rb') as f : u=pickle.load(f)
运行后别忘了del data*.pk,它需要 6Go 在你的磁盘上;)