def bench_file(filename): """ Perform comparative benchmark on matrices loaded from Matrix Market files. :param filename: a Matrix Market file """ bs = dict() for dtype in WANT_DTYPES: print("# loading matrix from file ", filename) lt = - rsb.rsb_time() a = rsb.rsb_matrix(filename,dtype=dtype) lt = lt + rsb.rsb_time() printf("# loaded a matrix with %.1e nnz in %.1e s (%.1e nnz/s)\n",a.nnz,lt,a.nnz/lt) printf("# loaded as type %s/%c (default is %s/%c)\n", a.dtype, DT2TC[a.dtype], rsb.rsb_dtype, DT2TC[rsb.rsb_dtype]) if not a._is_unsymmetric(): if WANT_SYMMETRIZE: print("# NOTE: loaded RSB matrix is NOT unsymmetric: expanding symmetry to cope with csr.") (I, J, V) = a.find() a = rsb.rsb_matrix((np.append(V,V), (np.append(I,J), np.append(J,I))), a.shape, dtype=dtype) else: print("# NOTE: loaded RSB matrix is NOT unsymmetric, but scipy will only perform unsymmetric SpMM") if a is not None: (I, J, V) = a.find() c = sp.sparse.csr_matrix((V, (I, J))) ( mtxname, _ ) = os.path.splitext(os.path.basename(filename)) ( mtxname, _ ) = os.path.splitext(mtxname) bd = bench_matrix(a, c, mtxname) dict_stat_merge(bs, derived_bench_stats(bd)) bsc = bs.copy() dict_sum_average_all(bsc) print_perf_record(bsc,beg="pyrsb:speedups:",fields=True) return bs
def bench(timeout, a, x, y): iterations = 0 dt = -rsb.rsb_time() while dt + rsb.rsb_time() < timeout: iterations = iterations + 1 y = a * x # See __mul__ # a.spmm(x,y) # This form avoids the copy of y. dt = dt + rsb.rsb_time() op_dt = dt / iterations return (op_dt, dt, iterations)
def bench_file(filename): """ Perform comparative benchmark on matrices loaded from Matrix Market files. :param filename: a Matrix Market file """ print("# loading from file ", filename) lt = - rsb.rsb_time() a = rsb.rsb_file_mtx_load(bytes(filename, encoding="utf-8")) lt = lt + rsb.rsb_time() printf("# loaded a matrix with %.1e nnz in %.1e s (%.1e nnz/s)\n",a.nnz(),lt,a.nnz()/lt) if not a._is_unsymmetric(): print("# NOTE: loaded RSB matrix is NOT unsymmetric, but scipy will only perform unsymmetric SpMM") if a is not None: (I, J, V) = a.find() c = sp.sparse.csr_matrix((V, (I, J))) bench_matrix(a, c)
def bench(timeout, a, x, y): """ Benchmark multiplication operation. :param timeout: benchmark time :param a: matrix :param x: right hand side vector :param y: result vector :return: a tuple with operation time, benchmark time, performed iterations """ iterations = 0 dt = -rsb.rsb_time() while dt + rsb.rsb_time() < timeout: iterations = iterations + 1 y += a * x # See __mul__ # a.spmm(x,y) # This form avoids the copy of y. dt = dt + rsb.rsb_time() op_dt = dt / iterations return (op_dt, dt, iterations)
def bench_random_files(): """ Perform comparative benchmark on randomly generated matrices. """ for nrA in WANT_NRA: ncA = nrA dnst = (math.sqrt(1.0 * nrA)) / nrA # print("# generating ",nrA,"x",ncA," with density ",dnst) printf("# generating %d x %d with with density %.1e\n", nrA, ncA, dnst) gt = -rsb.rsb_time() c = sp.sparse.rand(nrA, ncA, density=dnst, format=WANT_PSF, dtype=sp.double) gt = gt + rsb.rsb_time() (I, J, V) = sp.sparse.find(c) ct = -rsb.rsb_time() a = rsb.rsb_matrix((V, (I, J)), [nrA, ncA]) ct = ct + rsb.rsb_time() printf("# generated a matrix with %.1e nnz in %.1e s (%.1e nnz/s), converted to RSB in %.1e s\n",a.nnz(),gt,a.nnz()/gt,ct) bench_matrix(a, c)
def bench_random_matrices(): """ Perform comparative benchmark on randomly generated matrices. """ for dtype in WANT_DTYPES: for nrA in WANT_NRA: ncA = nrA dnst = (math.sqrt(1.0 * nrA)) / nrA # print("# generating ",nrA,"x",ncA," with density ",dnst) printf("# generating %d x %d with with density %.1e\n", nrA, ncA, dnst) gt = -rsb.rsb_time() c = sp.sparse.rand(nrA, ncA, density=dnst, format=WANT_PSF, dtype=rsb.rsb_dtype) gt = gt + rsb.rsb_time() (I, J, V) = sp.sparse.find(c) V = dtype(V) c = sp.sparse.csr_matrix((V, (I, J)), [nrA, ncA]) ct = -rsb.rsb_time() a = rsb.rsb_matrix((V, (I, J)), [nrA, ncA], dtype=dtype) ct = ct + rsb.rsb_time() printf("# generated a matrix with %.1e nnz in %.1e s (%.1e nnz/s), converted to RSB in %.1e s\n",a.nnz,gt,a.nnz/gt,ct) bd = bench_matrix(a, c, "random") derived_bench_stats(bd)
def bench(timeout, a, x, y): """ Benchmark multiplication operation. :param timeout: benchmark time :param a: matrix :param x: right hand side vector :param y: result vector :return: a tuple with min operation time, benchmark time, performed iterations """ iterations = 0 if timeout > 0.0: bench(0.0, a, x, y) # single op to warm-up caches op_dt = float('+inf') t0 = rsb.rsb_time() t1 = t0 if WANT_ZERO_ALLOC: if (isinstance(a,rsb.rsb_matrix)): while t1 - t0 < timeout or iterations == 0: iterations = iterations + 1 t2 = rsb.rsb_time() a._spmm(x,y) # This form avoids the copy of y t1 = rsb.rsb_time() op_dt = min(op_dt,t1-t2) else: while t1 - t0 < timeout or iterations == 0: iterations = iterations + 1 t2 = rsb.rsb_time() # y += a._mul_multivector(x) # inefficient sp.sparse._sparsetools.csr_matvecs(a.shape[0], a.shape[1], x.shape[1], a.indptr, a.indices, a.data, x.ravel(), y.ravel()) t1 = rsb.rsb_time() op_dt = min(op_dt,t1-t2) else: while t1 - t0 < timeout or iterations == 0: iterations = iterations + 1 t2 = rsb.rsb_time() y += a * x # Inefficient (result created repeatedly) see __mul__ t1 = rsb.rsb_time() op_dt = min(op_dt,t1-t2) bt = rsb.rsb_time() - t0 return (op_dt, bt, iterations)
def bench_matrix(a, c, mtxname): """ Perform comparative benchmark: rsb vs csr. :param a: rsb matrix :param c: csr matrix """ brdict = { 'mtxname': mtxname, 'at_time': 0.0, 'nsubm': a.nsubm(), 'bpnz': a._idx_bpnz() } #tmax = 2 #tmax = 0.1 #tmax = -3 tmax = 0 bd = dict() psf = WANT_PSF if WANT_RENDER: filename = sprintf("%s-%c.eps",mtxname,DT2TC[a.dtype]) a.render(filename) for nrhs in WANT_NRHS: bd[nrhs] = dict() if WANT_AUTOTUNE == 0: for nrhs in WANT_NRHS: for order in WANT_ORDER: (rsb_dt,psf_dt) = bench_both(a, c, psf, brdict, order, nrhs) bd[nrhs][order] = bench_record(a, psf, brdict, order, nrhs, rsb_dt, psf_dt) elif WANT_AUTOTUNE == 1: o = a.copy() if WANT_VERBOSE: print("Will autotune matrix for SpMV ", a) at_time = rsb.rsb_time() o.autotune(verbose=WANT_VERBOSE_TUNING,tmax=tmax) brdict['at_time'] = rsb.rsb_time() - at_time if WANT_RENDER: filename = sprintf("%s-%c-tuned.eps",mtxname,DT2TC[o.dtype]) o.render(filename) for nrhs in WANT_NRHS: for order in WANT_ORDER: (rsb_dt,psf_dt) = bench_both(a, c, psf, brdict, order, nrhs) (rsb_at_dt,psf_at_dt) = bench_both(o, c, psf, brdict, order, nrhs) bd[nrhs][order] = bench_record(o, psf, brdict, order, nrhs, rsb_dt, psf_dt, rsb_at_dt, psf_at_dt) del o elif WANT_AUTOTUNE == 2: for nrhs in WANT_NRHS: for order in WANT_ORDER: if WANT_VERBOSE: print("Will autotune one matrix instance for different specific SpMM ", a) (rsb_dt,psf_dt) = bench_both(a, c, psf, brdict, order, nrhs) at_time = rsb.rsb_time() a.autotune(verbose=WANT_VERBOSE_TUNING,nrhs=nrhs,order=order,tmax=tmax) brdict['at_time'] = rsb.rsb_time() - at_time if WANT_RENDER: filename = sprintf("%s-%c-tuned-%c-%d.eps",mtxname,DT2TC[a.dtype],order,nrhs) a.render(filename) (rsb_at_dt,psf_at_dt) = bench_both(a, c, psf, brdict, order, nrhs) bd[nrhs][order] = bench_record(a, psf, brdict, order, nrhs, rsb_dt, psf_dt, rsb_at_dt, psf_at_dt) elif WANT_AUTOTUNE >= 3: for nrhs in WANT_NRHS: for order in WANT_ORDER: (rsb_dt,psf_dt) = bench_both(a, c, psf, brdict, order, nrhs) o = a.copy() if WANT_VERBOSE: print("Will autotune copies of starting matrix for specific SpMM ", a) at_time = rsb.rsb_time() for i in range(2,+WANT_AUTOTUNE): o.autotune(verbose=WANT_VERBOSE_TUNING,nrhs=nrhs,order=order,tmax=tmax) brdict['at_time'] = rsb.rsb_time() - at_time if WANT_RENDER: filename = sprintf("%s-%c-tuned-%c-%d.eps",mtxname,DT2TC[o.dtype],order,nrhs) o.render(filename) (rsb_at_dt,psf_at_dt) = bench_both(o, c, psf, brdict, order, nrhs) bd[nrhs][order] = bench_record(o, psf, brdict, order, nrhs, rsb_dt, psf_dt, rsb_at_dt, psf_at_dt) del o del a del c return bd