コード例 #1
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception(
            'USAGE: python relative_quantities.py coeff_file m,k,n')

    full_stab_mat = 0
    if len(sys.argv) > 3:
        full_stab_mat = sys.argv[3]

    coeffs = read_coeffs(coeff_file)

    # Using the notation from our paper
    a_vec = np.array(
        [sum(np.abs([float(x) for x in row])) for row in transpose(coeffs[0])])
    b_vec = np.array(
        [sum(np.abs([float(x) for x in row])) for row in transpose(coeffs[1])])

    e_vec = [
        np.dot(np.abs([float(x) for x in row]), a_vec * b_vec)
        for row in coeffs[2]
    ]

    R = len(coeffs[0][0])

    print R, dims[0], dims[1], dims[2], int(max(e_vec))
コード例 #2
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception('USAGE: python stability_parameters.py coeff_file m,k,n')

    coeffs = read_coeffs(coeff_file)

    phi = compute_phi(coeffs)
    print 'phi   =', phi

    sigma = compute_sigma(coeffs, dims)
    print 'sigma =', sigma

    omega = 1.0 + float(phi) / sigma
    print 'omega =', omega

    # One recursive step
    print 'optimal lambda (1 recursive step)  = ', 2.0 ** -(53 / (float(phi + sigma)))
    print 'error (1 recursive step)           = ', 2.0 ** -(53 / omega)

    # Two recursive steps
    phi *= 2
    omega = 1.0 + float(phi) / sigma
    print 'optimal lambda (2 recursive steps) = ', 2.0 ** -(53 / (float(phi + sigma)))
    print 'error (2 recursive steps)          = ', 2.0 ** -(53 / omega)
コード例 #3
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception(
            'USAGE: python stability_parameters.py coeff_file m,k,n')

    coeffs = read_coeffs(coeff_file)

    phi = compute_phi(coeffs)
    print 'phi   =', phi

    sigma = compute_sigma(coeffs, dims)
    print 'sigma =', sigma

    omega = 1.0 + float(phi) / sigma
    print 'omega =', omega

    # One recursive step
    print 'optimal lambda (1 recursive step)  = ', 2.0**-(53 /
                                                          (float(phi + sigma)))
    print 'error (1 recursive step)           = ', 2.0**-(53 / omega)

    # Two recursive steps
    phi *= 2
    omega = 1.0 + float(phi) / sigma
    print 'optimal lambda (2 recursive steps) = ', 2.0**-(53 /
                                                          (float(phi + sigma)))
    print 'error (2 recursive steps)          = ', 2.0**-(53 / omega)
コード例 #4
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception('USAGE: python stability_vector.py coeff_file m,k,n')

    coeffs = read_coeffs(coeff_file)

    # Using the notation from our paper
    a_vec = np.array([
        sum(np.abs([zero_one(x) for x in row])) for row in transpose(coeffs[0])
    ])
    b_vec = np.array([
        sum(np.abs([zero_one(x) for x in row])) for row in transpose(coeffs[1])
    ])

    e_vec = [
        np.dot(np.abs([float(x) for x in row]), a_vec * b_vec)
        for row in coeffs[2]
    ]

    mn = max_norm(coeffs[0]) * max_norm(coeffs[1]) * max_norm(coeffs[2])
    emax = int(np.max(e_vec))

    print emax, mn, emax * mn
コード例 #5
0
ファイル: counts.py プロジェクト: liyancas/fast-matmul
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(",")])
    except:
        raise Exception("USAGE: python counts.py coeff_file m,k,n")

    coeffs = convert.read_coeffs(coeff_file)
    coeffs[2] = subexpr_elim.transpose(coeffs[2])

    counts = [base_counts(coeffs[i]) for i in range(3)]
    counts_X = [(0, 0, 0) for i in range(3, 6)]
    if len(coeffs) > 3:
        counts_X = [elim_counts(coeffs[i]) for i in range(3, 6)]
    xtotal = tuple([sum(cnts) for cnts in zip(*(counts_X))])
    total = tuple([sum(cnts) for cnts in zip(*(counts + counts_X))])

    print "      +   r   w"
    print "A    %3d %3d %3d" % counts[0]
    print "B    %3d %3d %3d" % counts[1]
    print "C    %3d %3d %3d" % counts[2]
    print "AX   %3d %3d %3d" % counts_X[0]
    print "BX   %3d %3d %3d" % counts_X[1]
    print "CX   %3d %3d %3d" % counts_X[2]
    print "xtot %3d %3d %3d" % xtotal
    print "tot  %3d %3d %3d" % total
コード例 #6
0
ファイル: subexpr_elim.py プロジェクト: timminn/fast-matmul
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
        print 'Reading coefficients for %d x %d x %d matrix' % dims
    except:
        raise Exception('USAGE: python subexpr_elim.py coeff_file m,k,n')

    coeffs = convert.read_coeffs(coeff_file)
    print 'nnz = ', num_nonzero(coeffs)

    A_elim = eliminate(coeffs[0])
    B_elim = eliminate(coeffs[1])
    # Transpose the C coefficients
    C_coeffs = transpose(coeffs[2])
    C_elim = eliminate(C_coeffs)

    A_subs, num_subs_A = update_coeffs(coeffs[0], A_elim)
    B_subs, num_subs_B = update_coeffs(coeffs[1], B_elim)
    C_subs, num_subs_C = update_coeffs(C_coeffs, C_elim)
    C_coeffs = transpose(C_coeffs)

    total_elim = num_subs_A + num_subs_B + num_subs_C
    print 'Eliminating %d non-zeros' % total_elim

    new_nonzeros = int(coeff_file.split('-')[-1]) - total_elim
    new_name = '-'.join(coeff_file.split('-')[:-1]) + '-%d' % (new_nonzeros)

    print 'Writing information to ' + new_name
    with open(new_name, 'w') as out_file:
        out_file.write('# Eliminated version of %s\n' % coeff_file)

        def write_coeff_set(coeff_set):
            ''' Write the coefficient set for a single matrix (A, B, or C). '''
            def pretty_print(value):
                if float(int(float(value))) == float(value):
                    return str(int(float(value)))
                else:
                    return str(value)

            for entry in coeff_set:
                # Single (i, j) entry of a single matrix.
                out_file.write(' '.join([pretty_print(val)
                                         for val in entry]) + '\n')

            if len(coeff_set) == 0:
                out_file.write('\n')

        write_coeff_set(coeffs[0])
        out_file.write('#\n')
        write_coeff_set(coeffs[1])
        out_file.write('#\n')
        write_coeff_set(C_coeffs)
        out_file.write('# Substitution information\n')
        write_coeff_set(A_subs)
        out_file.write('#\n')
        write_coeff_set(B_subs)
        out_file.write('#\n')
        write_coeff_set(C_subs)
コード例 #7
0
ファイル: subexpr_elim.py プロジェクト: arbenson/fast-matmul
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
        print 'Reading coefficients for %d x %d x %d matrix' % dims
    except:
        raise Exception('USAGE: python subexpr_elim.py coeff_file m,k,n')

    coeffs = convert.read_coeffs(coeff_file)
    print 'nnz = ', num_nonzero(coeffs)

    A_elim = eliminate(coeffs[0])
    B_elim = eliminate(coeffs[1])
    # Transpose the C coefficients
    C_coeffs = transpose(coeffs[2])
    C_elim = eliminate(C_coeffs)

    A_subs, num_subs_A = update_coeffs(coeffs[0], A_elim)
    B_subs, num_subs_B = update_coeffs(coeffs[1], B_elim)
    C_subs, num_subs_C = update_coeffs(C_coeffs, C_elim)
    C_coeffs = transpose(C_coeffs)

    total_elim = num_subs_A + num_subs_B + num_subs_C
    print 'Eliminating %d non-zeros' % total_elim

    new_nonzeros = int(coeff_file.split('-')[-1]) - total_elim
    new_name = '-'.join(coeff_file.split('-')[:-1]) + '-%d' % (new_nonzeros)

    print 'Writing information to ' + new_name
    with open(new_name, 'w') as out_file:
        out_file.write('# Eliminated version of %s\n' % coeff_file)
        def write_coeff_set(coeff_set):
            ''' Write the coefficient set for a single matrix (A, B, or C). '''
            def pretty_print(value):
                if float(int(float(value))) == float(value):
                    return str(int(float(value)))
                else:
                    return str(value)

            for entry in coeff_set:
                # Single (i, j) entry of a single matrix.
                out_file.write(' '.join([pretty_print(val) for val in entry]) + '\n')

            if len(coeff_set) == 0:
                out_file.write('\n')
     
        write_coeff_set(coeffs[0])
        out_file.write('#\n')
        write_coeff_set(coeffs[1])
        out_file.write('#\n')
        write_coeff_set(C_coeffs)
        out_file.write('# Substitution information\n')
        write_coeff_set(A_subs)
        out_file.write('#\n')
        write_coeff_set(B_subs)
        out_file.write('#\n')
        write_coeff_set(C_subs)
コード例 #8
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception('USAGE: python stability_vector.py coeff_file m,k,n')

    full_stab_mat = 0
    if len(sys.argv) > 3:
        full_stab_mat = sys.argv[3]

    coeffs = read_coeffs(coeff_file)

    # Using the notation from our paper
    a_vec = np.array(
        [sum(np.abs([float(x) for x in row])) for row in transpose(coeffs[0])])
    b_vec = np.array(
        [sum(np.abs([float(x) for x in row])) for row in transpose(coeffs[1])])

    e_vec = [
        np.dot(np.abs([float(x) for x in row]), a_vec * b_vec)
        for row in coeffs[2]
    ]

    alpha_vec = np.array([num_nonzero(row) for row in transpose(coeffs[0])])
    beta_vec = np.array([num_nonzero(row) for row in transpose(coeffs[1])])
    ab_vec = alpha_vec + beta_vec
    gamma_vec = [num_nonzero(row) for row in coeffs[2]]

    W_ind = np.array([[float(val) for val in row] for row in coeffs[2]])
    W_ind[np.nonzero(W_ind)] = 1
    q_vec = [gamma_vec[k] + np.max(ab_vec * W_ind[k,:]) \
                 for k in range(W_ind.shape[0])]

    # Number of additions
    nnz = sum([val for val in ab_vec]) + sum([val for val in gamma_vec])

    # print as q_vec emax
    rank = len(coeffs[0][0])
    mkn = dims[0] * dims[1] * dims[2]
    print mkn, rank, nnz, int(np.max(q_vec)), int(np.max(e_vec))

    if full_stab_mat:
        # Print in the same style as D'Alberto presents in his 2014 paper.
        print 'e vector:'
        for i in range(dims[0]):
            out_format = '\t' + ' '.join(['%4d' for _ in range(dims[2])])
            vals = e_vec[(i * dims[2]):(i * dims[2] + dims[2])]
            print out_format % tuple(vals)
        print 'q vector:'
        for i in range(dims[0]):
            out_format = '\t' + ' '.join(['%4d' for _ in range(dims[2])])
            vals = q_vec[(i * dims[2]):(i * dims[2] + dims[2])]
            print out_format % tuple(vals)
コード例 #9
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception('USAGE: python stability_vector.py coeff_file m,k,n')

    full_stab_mat = 0
    if len(sys.argv) > 3:
        full_stab_mat = sys.argv[3]

    coeffs = read_coeffs(coeff_file)

    # Using the notation from our paper
    a_vec = np.array([sum(np.abs([float(x) for x in row])) for row in transpose(coeffs[0])])
    b_vec = np.array([sum(np.abs([float(x) for x in row])) for row in transpose(coeffs[1])])

    e_vec = [np.dot(np.abs([float(x) for x in row]), a_vec * b_vec) for row in coeffs[2]]

    alpha_vec = np.array([num_nonzero(row) for row in transpose(coeffs[0])])
    beta_vec  = np.array([num_nonzero(row) for row in transpose(coeffs[1])])
    ab_vec = alpha_vec + beta_vec
    gamma_vec = [num_nonzero(row) for row in coeffs[2]]

    W_ind = np.array([[float(val) for val in row] for row in coeffs[2]])
    W_ind[np.nonzero(W_ind)] = 1
    q_vec = [gamma_vec[k] + np.max(ab_vec * W_ind[k,:]) \
                 for k in range(W_ind.shape[0])]

    # Number of additions
    nnz = sum([val for val in ab_vec]) + sum([val for val in gamma_vec])

    # print as q_vec emax
    rank = len(coeffs[0][0])
    mkn = dims[0] * dims[1] * dims[2]
    print mkn, rank, nnz, int(np.max(q_vec)), int(np.max(e_vec))

    if full_stab_mat:
        # Print in the same style as D'Alberto presents in his 2014 paper.
        print 'e vector:'
        for i in range(dims[0]):
            out_format = '\t' + ' '.join(['%4d' for _ in range(dims[2])])
            vals = e_vec[(i * dims[2]):(i * dims[2] + dims[2])]
            print out_format % tuple(vals)
        print 'q vector:'
        for i in range(dims[0]):
            out_format = '\t' + ' '.join(['%4d' for _ in range(dims[2])])
            vals = q_vec[(i * dims[2]):(i * dims[2] + dims[2])]
            print out_format % tuple(vals)
コード例 #10
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception('USAGE: python stability_vector.py coeff_file m,k,n')

    coeffs = read_coeffs(coeff_file)

    # Using the notation from our paper
    a_vec = np.array([sum(np.abs([zero_one(x) for x in row])) for row in transpose(coeffs[0])])
    b_vec = np.array([sum(np.abs([zero_one(x) for x in row])) for row in transpose(coeffs[1])])

    e_vec = [np.dot(np.abs([float(x) for x in row]), a_vec * b_vec) for row in coeffs[2]]

    mn = max_norm(coeffs[0]) * max_norm(coeffs[1]) * max_norm(coeffs[2])
    emax = int(np.max(e_vec))

    print emax, mn, emax * mn
コード例 #11
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception('USAGE: python relative_quantities.py coeff_file m,k,n')

    full_stab_mat = 0
    if len(sys.argv) > 3:
        full_stab_mat = sys.argv[3]

    coeffs = read_coeffs(coeff_file)

    # Using the notation from our paper
    a_vec = np.array([sum(np.abs([float(x) for x in row])) for row in transpose(coeffs[0])])
    b_vec = np.array([sum(np.abs([float(x) for x in row])) for row in transpose(coeffs[1])])

    e_vec = [np.dot(np.abs([float(x) for x in row]), a_vec * b_vec) for row in coeffs[2]]

    R = len(coeffs[0][0])

    print R, dims[0], dims[1], dims[2], int(max(e_vec))
コード例 #12
0
def main():
    try:
        coeff_file = sys.argv[1]
        dims = tuple([int(d) for d in sys.argv[2].split(',')])
    except:
        raise Exception('USAGE: python stability_parameters.py coeff_file m,k,n')

    coeffs = read_coeffs(coeff_file)

    phi = compute_phi(coeffs)
    print('phi   =', phi)

    sigma = compute_sigma(coeffs, dims)
    print('sigma =', sigma)

    omega = 1.0 + float(phi) / sigma
    print('omega =', omega)

    # set bits of precision
    d = 24 # float
    prec = 'single'
    #d = 53 # double
    #prec = 'double'
    
    # One recursive step
    print('optimal lambda (1 recursive step)  = ', 2.0 ** -(d / (float(phi + sigma))))
    print('error (1 recursive step)           = ', 2.0 ** -(d / omega))

    # Two recursive steps
    phi *= 2
    omega = 1.0 + float(phi) / sigma
    print('optimal lambda (2 recursive steps) = ', 2.0 ** -(d / (float(phi + sigma))))
    print('error (2 recursive steps)          = ', 2.0 ** -(d / omega))
    
    # Note precision 
    print('\t\t....................... assuming ', prec, ' precision')