示例#1
0
MAXIT = 800
SSOR_STEPS = 2

L = poisson2d_vec_sym_blk(N)
S = L.to_sss()

b = np.ones(N*N, 'd')

print 'Solving 2D-Laplace equation using PCG and SSOR preconditioner with variable omega'
print
print 'omega    nit    time       resid' 
print '--------------------------------' 

for omega in [0.1*(i+1) for i in range(20)]:

    K_ssor = precon.ssor(S, omega, SSOR_STEPS)
    t1 = time.clock()

    x = np.empty(N*N, 'd')
    info, iter, relres = pcg(S, b, x, TOL, MAXIT, K_ssor)

    elapsed_time = time.clock() - t1

    r = np.zeros(N*N, 'd')
    S.matvec(x, r)
    r = b - r
    res_nrm2 = np.linalg.norm(r)

    if info == 0:
        iter_str = str(iter)
    else:
示例#2
0
@Deprecated('Use pysparse.itsolvers.Gmres instead.')
def gmres(*args, **kwargs):
    return  krylov.gmres(*args, **kwargs)

if __name__ == '__main__':
    import numpy
    from pysparse.precon import precon
    from pysparse.tools import poisson

    n = 100
    n2 = n*n
    A = PysparseMatrix(matrix=poisson.poisson2d_sym(n))
    b = numpy.ones(n2); b /= numpy.linalg.norm(b)
    x = numpy.empty(n2)
    K = precon.ssor(A.matrix.to_sss())
    fmt = '%8s  %7.1e  %2d  %4d'

    def resid(A, b, x):
        r = b - A*x
        return numpy.linalg.norm(r)

    for Solver in [Pcg, Minres, Cgs, Qmrs, Gmres, Bicgstab]:
        solver = Solver(A)
        solver.solve(b, x, 1.0e-6, 3*n)
        print fmt % (solver.name, resid(A, b, x), solver.nofCalled,
                     solver.totalIterations)
        solver.solve(b, x, 1.0e-6, 3*n, K)
        print fmt % (solver.name, resid(A, b, x), solver.nofCalled,
                     solver.totalIterations)
示例#3
0
x = np.empty(n * n, "d")
info, iter, relres = gmres(L, b, x, 1e-12, 200)
print "info=%d, iter=%d, relres=%e" % (info, iter, relres)

print "Solve time using LL matrix: %8.2f s" % (time.clock() - t1)

print "norm(x) = %g" % np.linalg.norm(x)

r = np.empty(n * n, "d")
A.matvec(x, r)
r = b - r
print "norm(b - A*x) = %g" % np.linalg.norm(r)

# -----------------------------------------------------------------------------

K_ssor = precon.ssor(S, 1.0)
t1 = time.clock()

x = np.empty(n * n, "d")
info, iter, relres = gmres(S, b, x, 1e-12, 500, K_ssor, 20)
print "info=%d, iter=%d, relres=%e" % (info, iter, relres)

print "Solve time using SSS matrix and SSOR preconditioner: %8.2f s" % (time.clock() - t1)

print "norm(x) = %g" % np.linalg.norm(x)

r = np.empty(n * n, "d")
S.matvec(x, r)
r = b - r
print "norm(b - A*x) = %g" % np.linalg.norm(r)
示例#4
0
 def _applyToMatrix(self, A):
     """
     Returns (preconditioning matrix, resulting matrix)
     """
     A = A.to_sss()
     return precon.ssor(A), A
示例#5
0
x = np.empty(n*n, 'd')
info, iter, relres = gmres(L, b, x, 1e-12, 200)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)

print 'Solve time using LL matrix: %8.2f s' % (time.clock() - t1)

print 'norm(x) = %g' % np.linalg.norm(x)

r = np.empty(n*n, 'd')
A.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % np.linalg.norm(r)

# -----------------------------------------------------------------------------

K_ssor = precon.ssor(S, 1.0)
t1 = time.clock()

x = np.empty(n*n, 'd')
info, iter, relres = gmres(S, b, x, 1e-12, 500, K_ssor, 20)
print 'info=%d, iter=%d, relres=%e' % (info, iter, relres)

print 'Solve time using SSS matrix and SSOR preconditioner: %8.2f s' % (time.clock() - t1)

print 'norm(x) = %g' % np.linalg.norm(x)

r = np.empty(n*n, 'd')
S.matvec(x, r)
r = b - r
print 'norm(b - A*x) = %g' % np.linalg.norm(r)
示例#6
0
            self.dinv[i] = 1.0 / A[i, i]

    def precon(self, x, y):
        np.multiply(x, self.dinv, y)


def resid(A, b, x):
    r = x.copy()
    A.matvec(x, r)
    r = b - r
    return math.sqrt(np.dot(r, r))


K_diag = diag_prec(A)
K_jac = precon.jacobi(A, 1.0, 1)
K_ssor = precon.ssor(A, 1.0, 1)
# K_ilu = precon.ilutp(L)

n = L.shape[0]
b = np.arange(n).astype(np.Float)
x = np.zeros(n, 'd')
info, iter, relres = pcg(A, b, x, 1e-6, 1000)
print 'pcg, K_none: ', info, iter, relres, resid(A, b, x)
x = np.zeros(n, 'd')
info, iter, relres = pcg(A, b, x, 1e-6, 1000, K_diag)
print 'pcg, K_diag: ', info, iter, relres, resid(A, b, x)
x = np.zeros(n, 'd')
info, iter, relres = pcg(A, b, x, 1e-6, 1000, K_jac)
print 'pcg, K_jac: ', info, iter, relres, resid(A, b, x)
x = np.zeros(n, 'd')
info, iter, relres = pcg(A, b, x, 1e-6, 1000, K_ssor)
示例#7
0
            nof += 1
            if nof >= nofPrimes:
                break
        i = i+2
    return primes

n = 20000
print 'Generating first %d primes...' % n
primes = get_primes(n)

print 'Assembling coefficient matrix...'
A = spmatrix.ll_mat_sym(n, n*8)
d = 1
while d < n:
    for i in range(d, n):
        A[i,i-d] = 1.0
    d *= 2
for i in range(n):
    A[i,i] = 1.0 * primes[i]

A = A.to_sss()
K = precon.ssor(A)

print 'Solving linear system...'
b = np.zeros(n); b[0] = 1.0
x = np.empty(n)
info, iter, relres = minres(A, b, x, 1e-16, n, K)

print info, iter, relres
print '%.16e' % x[0]
示例#8
0
def gmres(*args, **kwargs):
    return krylov.gmres(*args, **kwargs)


if __name__ == '__main__':
    import numpy
    from pysparse.precon import precon
    from pysparse.tools import poisson

    n = 100
    n2 = n * n
    A = PysparseMatrix(matrix=poisson.poisson2d_sym(n))
    b = numpy.ones(n2)
    b /= numpy.linalg.norm(b)
    x = numpy.empty(n2)
    K = precon.ssor(A.matrix.to_sss())
    fmt = '%8s  %7.1e  %2d  %4d'

    def resid(A, b, x):
        r = b - A * x
        return numpy.linalg.norm(r)

    for Solver in [Pcg, Minres, Cgs, Qmrs, Gmres, Bicgstab]:
        solver = Solver(A)
        solver.solve(b, x, 1.0e-6, 3 * n)
        print fmt % (solver.name, resid(
            A, b, x), solver.nofCalled, solver.totalIterations)
        solver.solve(b, x, 1.0e-6, 3 * n, K)
        print fmt % (solver.name, resid(
            A, b, x), solver.nofCalled, solver.totalIterations)
示例#9
0
        n = self.shape[0]
        self.dinv = np.zeros(n, 'd')
        for i in xrange(n):
            self.dinv[i] = 1.0 / A[i,i]
    def precon(self, x, y):
        np.multiply(x, self.dinv, y)

def resid(A, b, x):
    r = x.copy()
    A.matvec(x, r)
    r = b - r
    return math.sqrt(np.dot(r, r))

K_diag = diag_prec(A)
K_jac = precon.jacobi(A, 1.0, 1)
K_ssor = precon.ssor(A, 1.0, 1)
# K_ilu = precon.ilutp(L)

n = L.shape[0];
b = np.arange(n).astype(np.Float)
x = np.zeros(n, 'd')
info, iter, relres = pcg(A, b, x, 1e-6, 1000)
print 'pcg, K_none: ', info, iter, relres, resid(A, b, x)
x = np.zeros(n, 'd')
info, iter, relres = pcg(A, b, x, 1e-6, 1000, K_diag)
print 'pcg, K_diag: ', info, iter, relres, resid(A, b, x)
x = np.zeros(n, 'd')
info, iter, relres = pcg(A, b, x, 1e-6, 1000, K_jac)
print 'pcg, K_jac: ', info, iter, relres, resid(A, b, x)
x = np.zeros(n, 'd')
info, iter, relres = pcg(A, b, x, 1e-6, 1000, K_ssor)
示例#10
0
                break
        i = i + 2
    return primes


n = 20000
print 'Generating first %d primes...' % n
primes = get_primes(n)

print 'Assembling coefficient matrix...'
A = spmatrix.ll_mat_sym(n, n * 8)
d = 1
while d < n:
    for i in range(d, n):
        A[i, i - d] = 1.0
    d *= 2
for i in range(n):
    A[i, i] = 1.0 * primes[i]

A = A.to_sss()
K = precon.ssor(A)

print 'Solving linear system...'
b = np.zeros(n)
b[0] = 1.0
x = np.empty(n)
info, iter, relres = minres(A, b, x, 1e-16, n, K)

print info, iter, relres
print '%.16e' % x[0]
示例#11
0
def test_pcg(ProblemList, tol=1.0e-6):

    if len(ProblemList) == 0:
        usage()
        sys.exit(1)
    
    header1 = '%10s  %6s  %6s  ' % ('Name', 'n', 'nnz')
    header2 = '%6s  %8s  %8s  %4s  %6s  %6s\n' % ('iter','relres','error','info','form M','solve')
    dheader1 = '%10s  %6d  %6d  '
    dheader2 = '%6d  %8.1e  %8.1e  %4d  %6.2f  %6.2f\n' 
    lhead1 = len(header1)
    lhead2 = len(header2)
    lhead = lhead1 + lhead2
    sys.stderr.write('-' * lhead + '\n')
    sys.stderr.write(header1)
    sys.stderr.write(header2)
    sys.stderr.write('-' * lhead + '\n')

    # Record timings for each preconditioner
    timings = { 'None' : [],
                'Diagonal' : [],
                'SSOR' : []
              }

    for problem in ProblemList:
    
        A = spmatrix.ll_mat_from_mtx(problem)
        (m, n) = A.shape
        if m != n: break
    
        prob = os.path.basename(problem)
        if prob[-4:] == '.mtx': prob = prob[:-4]

        # Right-hand side is Ae
        e = np.ones(n, 'd')
        b = np.empty(n, 'd')
        A.matvec(e, b)

        sys.stdout.write(dheader1 % (prob, n, A.nnz))

        # No preconditioner
        x = np.zeros(n, 'd')
        t = cputime()
        info, iter, relres = pcg(A, b, x, tol, 2*n)
        t_noprec = cputime() - t
        err = np.linalg.norm(x-e, ord=np.Inf)
        sys.stdout.write(dheader2 % (iter, relres, err, info, 0.0, t_noprec))
        timings['None'].append(t_noprec)

        # Diagonal preconditioner
        x = np.zeros(n, 'd')
        t = cputime()
        M = precon.jacobi(A, 1.0, 1)
        t_getM_diag = cputime() - t
        t = cputime()
        info, iter, relres = pcg(A, b, x, tol, 2*n, M)
        t_diag = cputime() - t
        err = np.linalg.norm(x-e, ord=np.Inf)
        sys.stdout.write(lhead1 * ' ')
        sys.stdout.write(dheader2 % (iter,relres,err,info,t_getM_diag,t_diag))
        timings['Diagonal'].append(t_diag)

        # SSOR preconditioner
        # It appears that steps=1 and omega=1.0 are nearly optimal in all cases
        x = np.zeros(n, 'd')
        t = cputime()
        M = precon.ssor(A.to_sss(), 1.0, 1)
        t_getM_ssor = cputime() - t
        t = cputime()
        info, iter, relres = pcg(A, b, x, tol, 2*n, M)
        t_ssor = cputime() - t
        err = np.linalg.norm(x-e, ord=np.Inf)
        sys.stdout.write(lhead1 * ' ')
        sys.stdout.write(dheader2 % (iter,relres,err,info,t_getM_ssor,t_ssor))
        timings['SSOR'].append(t_ssor)
        sys.stderr.write('-' * lhead + '\n')

    return timings
示例#12
0
MAXIT = 800
SSOR_STEPS = 2

L = poisson2d_vec_sym_blk(N)
S = L.to_sss()

b = np.ones(N * N, 'd')

print 'Solving 2D-Laplace equation using PCG and SSOR preconditioner with variable omega'
print
print 'omega    nit    time       resid'
print '--------------------------------'

for omega in [0.1 * (i + 1) for i in range(20)]:

    K_ssor = precon.ssor(S, omega, SSOR_STEPS)
    t1 = time.clock()

    x = np.empty(N * N, 'd')
    info, iter, relres = pcg(S, b, x, TOL, MAXIT, K_ssor)

    elapsed_time = time.clock() - t1

    r = np.zeros(N * N, 'd')
    S.matvec(x, r)
    r = b - r
    res_nrm2 = np.linalg.norm(r)

    if info == 0:
        iter_str = str(iter)
    else: