Parallel Assembly Of A Sparse Matrix In Python
I'm trying to use mpi4py to assemble a very large sparse matrix in parallel. Each rank produces a sparse sub matrix (in scipy's dok format) that needs to be put in place in the ver
Solution 1:
For what is worth, using three element lists work pretty well as suggested by hpaulj. Here's a working example:
from mpi4py import MPI
from numpy import *
import scipy.sparse as ss
from timeit import default_timer as timer
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
tic = timer()
# each rank generates a sparse matrix with N entries on the diagonal
N = 100000
block = ss.eye(N, format = 'coo')
# extract indices and values
out = [ block.data, block.row , block.col]
out[1] = out[1] + rank * N
out[2] = out[2] + rank * N
# gather all the arrays representing the submatrices
full_list = comm.gather(out,root=0)
if rank == 0:
dat = concatenate([x[0] for x in full_list])
row = concatenate([x[1] for x in full_list])
col = concatenate([x[2] for x in full_list])
final_result = ss.csr_matrix( ( dat, (row, col) ) )
toc = timer()
print 'Matrix assembled in', toc-tic, 'seconds'
The assembly is definitely much faster using coo
matrices rather than dok
.
Post a Comment for "Parallel Assembly Of A Sparse Matrix In Python"