Beispiel #1
0
 def _applyToMatrix(self, A):
     """
     Returns (preconditioning matrix, resulting matrix)
     """
     return precon.jacobi(A), A.to_csr()
Beispiel #2
0
        self.shape = A.shape
        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')
Beispiel #3
0
        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')
Beispiel #4
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