I'm using numpy.memmap to compute large covarince matrix.
The question is why is multiplication in second case(when I store A.T on disk) is slower?
def matrix_append_row(fm,tp,mat):
#check if number of columns is the same
rows= fm.shape[0] + mat.shape[0]
new_fm = np.memmap(fm.filename, mode='r+', dtype= tp, shape=(rows,fm.shape[1]))
new_fm[fm.shape[0]:rows,:]= mat[:]
return new_fm
def generate_and_store_data(cols,batch,iter,tp):
#create memmap file and append generated data in cycle
#can't create empty array - need to store first entry by copy not by append
fm= np.memmap('A.npy', dtype=tp, mode='w+', shape=(batch,cols))
for i in xrange(iter):
data= np.random.rand(batch,cols)*100 # [0-1]*100
# print i
# t0= time.time()
if i==0:
fm[:]= data[:]
else:
fm= matrix_append_row(fm,tp,data)
# print (time.time()-t0)
# fm.flush()
# print fm.shape
return fm
A= generate_and_store_data(1000,5000,10,'float32')
#cov= A.T*A
# A memmaped file
print A.shape
M= A.shape[0]
N= A.shape[1]
#create cov mat
cov= np.memmap('cov.npy', dtype='float32', mode='w+', shape=(N,N))
#A.T don't copy data just view?
t0= time.time()
np.dot(A.T,A,out= cov)
print (time.time()-t0)
print A.T.shape
cov_= np.memmap('cov_.npy', dtype='float32', mode='w+', shape=(N,N))
At= np.memmap('At.npy', dtype='float32', mode='w+', shape=(N,M))
t0= time.time()
At= A.T # only reference
print (time.time()-t0)
t0= time.time()
At[:]= A.T # copy same as At[:]= (A.T)[:] ?
print (time.time()-t0)
t0= time.time()
At[:]= (A.T)[:]
print (time.time()-t0)
t0= time.time()
np.dot(At,A,out= cov_)
print (time.time()-t0)
output is
(50000, 1000)
2.84399986267
(1000, 50000)
0.0
2.17100000381
2.07899999619
2.73399996758
Update:
also tried blockwise matrix multiplication, but it is very slow (maybe I need to adjust block_size)
cov2= np.memmap('cov2.npy', dtype='float32', mode='w+', shape=(N,N))
block_size=100
t0= time.time()
for i_start in range(0, At.shape[0], block_size):
for j_start in range(0, A.shape[1], block_size):
for k_start in range(0, At.shape[1], block_size):
cov2[i_start:i_start+block_size, j_start:j_start + block_size] +=np.dot(At[i_start:i_start + block_size, k_start:k_start + block_size],A[k_start:k_start + block_size, j_start:j_start + block_size])
print (time.time()-t0)