2
# Created by: Robert Cimrman, 05.12.2005
4
"""Benchamrks for umfpack module"""
6
from optparse import OptionParser
10
import scipy.sparse as sp
11
import scipy.linalg as nla
17
defaultURL = 'http://www.cise.ufl.edu/research/sparse/HBformat/'
19
usage = """%%prog [options] <matrix file name> [<matrix file name>, ...]
21
<matrix file name> can be a local or distant (gzipped) file
26
supported formats are:
27
triplet .. [nRow, nCol, nItem] followed by 'nItem' * [ir, ic, value]
28
hb .. Harwell-Boeing format N/A
34
def read_triplet( fd ):
35
nRow, nCol = map( int, fd.readline().split() )
36
nItem = int( fd.readline() )
38
ij = nm.zeros( (nItem,2), nm.int32 )
39
val = nm.zeros( (nItem,), nm.float64 )
40
for ii, row in enumerate( fd.readlines() ):
42
ij[ii] = int( aux[0] ), int( aux[1] )
43
val[ii] = float( aux[2] )
45
mtx = sp.csc_matrix( (val, ij), dims = (nRow, nCol), nzmax = nItem )
51
def read_triplet2( fd ):
52
nRow, nCol = map( int, fd.readline().split() )
53
nItem = int( fd.readline() )
55
ij, val = io.read_array( fd,
56
columns = [(0,1), (2,)],
57
atype = (nm.int32, nm.float64),
60
mtx = sp.csc_matrix( (val, ij), dims = (nRow, nCol), nzmax = nItem )
65
formatMap = {'triplet' : read_triplet}
68
def readMatrix( matrixName, options ):
70
if options.default_url:
71
matrixName = defaultURL + matrixName
73
print 'url:', matrixName
75
if matrixName[:7] == 'http://':
76
fileName, status = urllib.urlretrieve( matrixName )
81
print 'file:', fileName
84
readMatrix = formatMap[options.format]
86
raise ValueError, 'unsupported format: %s' % options.format
88
print 'format:', options.format
91
if fileName[-3:] == '.gz':
92
fd = gzip.open( fileName )
96
mtx = readMatrix( fd )
107
parser = OptionParser( usage = usage )
108
parser.add_option( "-c", "--compare",
109
action = "store_true", dest = "compare",
111
help = "compare with default scipy.sparse solver [default: %default]" )
112
parser.add_option( "-p", "--plot",
113
action = "store_true", dest = "plot",
115
help = "plot time statistics [default: %default]" )
116
parser.add_option( "-d", "--default-url",
117
action = "store_true", dest = "default_url",
119
help = "use default url [default: %default]" )
120
parser.add_option( "-f", "--format", type = type( '' ),
121
dest = "format", default = 'triplet',
122
help = "matrix format [default: %default]" )
123
(options, args) = parser.parse_args()
125
if (len( args ) >= 1):
131
sizes, nnzs, times, errors = [], [], [], []
132
legends = ['umfpack', 'sparse.solve']
133
for ii, matrixName in enumerate( matrixNames ):
136
mtx = readMatrix( matrixName, options )
138
sizes.append( mtx.shape )
139
nnzs.append( mtx.nnz )
140
tts = nm.zeros( (2,), dtype = nm.double )
142
err = nm.zeros( (2,2), dtype = nm.double )
145
print 'size : %s (%d nnz)' % (mtx.shape, mtx.nnz)
147
sol0 = nm.ones( (mtx.shape[0],), dtype = nm.double )
150
umfpack = um.UmfpackContext()
153
sol = umfpack( um.UMFPACK_A, mtx, rhs, autoTranspose = True )
154
tts[0] = time.clock() - tt
155
print "umfpack : %.2f s" % tts[0]
157
error = mtx * sol - rhs
158
err[0,0] = nla.norm( error )
159
print '||Ax-b|| :', err[0,0]
162
err[0,1] = nla.norm( error )
163
print '||x - x_{exact}|| :', err[0,1]
167
sol = sp.solve( mtx, rhs )
168
tts[1] = time.clock() - tt
169
print "sparse.solve : %.2f s" % tts[1]
171
error = mtx * sol - rhs
172
err[1,0] = nla.norm( error )
173
print '||Ax-b|| :', err[1,0]
176
err[1,1] = nla.norm( error )
177
print '||x - x_{exact}|| :', err[1,1]
180
times = nm.array( times )
182
pylab.plot( times[:,0], 'b-o' )
184
pylab.plot( times[:,1], 'r-s' )
191
y2 = 0.5 * (ax[3] - ax[2])
192
xrng = range( len( nnzs ) )
194
yy = y2 + 0.4 * (ax[3] - ax[2])\
195
* nm.sin( ii * 2 * nm.pi / (len( xrng ) - 1) )
198
pylab.text( ii+0.02, yy,
199
'%s\n%.2e err_umf\n%.2e err_sp'
200
% (sizes[ii], nm.sum( errors[ii][0,:] ),
201
nm.sum( errors[ii][1,:] )) )
203
pylab.text( ii+0.02, yy,
205
% (sizes[ii], nm.sum( errors[ii][0,:] )) )
206
pylab.plot( [ii, ii], [ax[2], ax[3]], 'k:' )
208
pylab.xticks( xrng, ['%d' % (nnzs[ii] ) for ii in xrng] )
209
pylab.xlabel( 'nnz' )
210
pylab.ylabel( 'time [s]' )
211
pylab.legend( legends )
212
pylab.axis( [ax[0] - 0.05, ax[1] + 1, ax[2], ax[3]] )
215
if __name__ == '__main__':