def test_axpy(): print("==== Program start ====") N.set(24) print('Scalar-vector multiplication %d' % (N.get())) # Initialize arrays: Randomize A and X, zero Y A = dace.float64(np.random.rand()) X = np.random.rand(N.get()).astype(np.float64) Y = np.random.rand(N.get()).astype(np.float64) A_regression = np.float64() X_regression = np.ndarray([N.get()], dtype=np.float64) Y_regression = np.ndarray([N.get()], dtype=np.float64) A_regression = A X_regression[:] = X[:] Y_regression[:] = Y[:] sdfg = common.vectorize(axpy) sdfg(A=A, X=X, Y=Y, N=N) c_axpy = sp.linalg.blas.get_blas_funcs('axpy', arrays=(X_regression, Y_regression)) if dace.Config.get_bool('profiling'): dace.timethis('axpy', 'BLAS', (2 * N.get()), c_axpy, X_regression, Y_regression, N.get(), A_regression) else: c_axpy(X_regression, Y_regression, N.get(), A_regression) diff = np.linalg.norm(Y_regression - Y) / N.get() print("Difference:", diff) print("==== Program end ====") assert diff <= 1e-5
def test_spmv(): W.set(64) H.set(64) nnz.set(640) print('Sparse Matrix-Vector Multiplication %dx%d (%d non-zero elements)' % (W.get(), H.get(), nnz.get())) A_row = dace.ndarray([H + 1], dtype=dace.uint32) A_col = dace.ndarray([nnz], dtype=dace.uint32) A_val = dace.ndarray([nnz], dtype=dace.float32) x = dace.ndarray([W], dace.float32) b = dace.ndarray([H], dace.float32) # Assuming uniform sparsity distribution across rows nnz_per_row = nnz.get() // H.get() nnz_last_row = nnz_per_row + (nnz.get() % H.get()) if nnz_last_row > W.get(): print('Too many nonzeros per row') exit(1) # RANDOMIZE SPARSE MATRIX A_row[0] = dace.uint32(0) A_row[1:H.get()] = dace.uint32(nnz_per_row) A_row[-1] = dace.uint32(nnz_last_row) A_row = np.cumsum(A_row, dtype=np.uint32) # Fill column data for i in range(H.get() - 1): A_col[nnz_per_row*i:nnz_per_row*(i+1)] = \ np.sort(np.random.choice(W.get(), nnz_per_row, replace=False)) # Fill column data for last row A_col[nnz_per_row * (H.get() - 1):] = np.sort( np.random.choice(W.get(), nnz_last_row, replace=False)) A_val[:] = np.random.rand(nnz.get()).astype(dace.float32.type) ######################### x[:] = np.random.rand(W.get()).astype(dace.float32.type) b[:] = dace.float32(0) # Setup regression A_sparse = scipy.sparse.csr_matrix((A_val, A_col, A_row), shape=(H.get(), W.get())) sdfg = spmv.to_sdfg() vectorize(sdfg, 'j') sdfg(A_row=A_row, A_col=A_col, A_val=A_val, x=x, b=b, H=H, W=W, nnz=nnz) if dace.Config.get_bool('profiling'): dace.timethis('spmv', 'scipy', 0, A_sparse.dot, x) diff = np.linalg.norm(A_sparse.dot(x) - b) / float(H.get()) print("Difference:", diff) print("==== Program end ====") assert diff <= 1e-5
def test_filter(): N.set(64) ratio = np.float32(0.5) print('Predicate-Based Filter. size=%d, ratio=%f' % (N.get(), ratio)) A = np.random.rand(N.get()).astype(np.float32) B = np.zeros_like(A) outsize = dace.scalar(dace.uint32) outsize[0] = 0 sdfg = pbf.to_sdfg() vectorize(sdfg, 'i') sdfg(A=A, B=B, outsz=outsize, ratio=ratio, N=N) if dace.Config.get_bool('profiling'): dace.timethis('filter', 'numpy', 0, regression, A, ratio) filtered = regression(A, ratio) if len(filtered) != outsize[0]: print( "Difference in number of filtered items: %d (DaCe) vs. %d (numpy)" % (outsize[0], len(filtered))) totalitems = min(outsize[0], N.get()) print('DaCe:', B[:totalitems].view(type=np.ndarray)) print('Regression:', filtered.view(type=np.ndarray)) exit(1) # Sort the outputs filtered = np.sort(filtered) B[:outsize[0]] = np.sort(B[:outsize[0]]) if len(filtered) == 0: print("==== Program end ====") exit(0) diff = np.linalg.norm(filtered - B[:outsize[0]]) / float(outsize[0]) print("Difference:", diff) if diff > 1e-5: totalitems = min(outsize[0], N.get()) print('DaCe:', B[:totalitems].view(type=np.ndarray)) print('Regression:', filtered.view(type=np.ndarray)) print("==== Program end ====") assert diff <= 1e-5
######################### x[:] = np.random.rand(W.get()).astype(dtype.type) #b[:] = dtype(0) # Setup regression A_sparse = scipy.sparse.csr_matrix((A_val, A_col, A_row), shape=(H.get(), W.get())) spmv = make_sdfg(args["specialize"]) if args["specialize"]: spmv.specialize(dict(H=H, W=W, nnz=nnz)) spmv(A_row=A_row, A_col=A_col, A_val=A_val, x=x, b=b, H=H, W=W, nnz=nnz) if dace.Config.get_bool('profiling'): dace.timethis('spmv', 'scipy', 0, A_sparse.dot, x) diff = np.linalg.norm(A_sparse.dot(x) - b) / float(H.get()) print("Difference:", diff) if diff >= 1e-5: print("Validation failed.") print("Result:") print(b) print("Reference:") print(A_sparse.dot(x)) print("Type \"debug\" to enter debugger, " "or any other string to quit (timeout in 10 seconds)") read, _, _ = select.select([sys.stdin], [], [], 10) if len(read) > 0 and sys.stdin.readline().strip().lower() == "debug": print("Entering debugger...") pdb.set_trace()
print('Matrix addition %dx%d' % (M.get(), N.get())) # Initialize arrays: Randomize A and B, zero C A[:] = np.random.rand(M.get(), N.get()).astype(dace.float64.type) B[:] = np.random.rand(M.get(), N.get()).astype(dace.float64.type) C[:] = dace.float64(0) A_regression = np.ndarray([M.get(), N.get()], dtype=np.float64) B_regression = np.ndarray([M.get(), N.get()], dtype=np.float64) C_regression = np.ndarray([M.get(), N.get()], dtype=np.float64) A_regression[:] = A[:] B_regression[:] = B[:] C_regression[:] = C[:] if args["compile-only"]: dace.compile(mat_add, A, B, C) else: mat_add(A, B, C) if dace.Config.get_bool('profiling'): dace.timethis('mat_add', contender, dace.eval(2 * N * N * N), np.dot, A_regression, B_regression, C_regression) else: np.add(A_regression, B_regression, C_regression) diff = np.linalg.norm(C_regression - C) / float(dace.eval(M * N)) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
a << A[i, j] out >> hist(1, lambda x, y: x + y)[:] out[min(int(a * BINS), BINS - 1)] = 1 if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("W", type=int, nargs="?", default=32) parser.add_argument("H", type=int, nargs="?", default=32) args = vars(parser.parse_args()) W.set(args["W"]) H.set(args["H"]) print('Histogram %dx%d' % (W.get(), H.get())) A = np.random.rand(H.get(), W.get()).astype(dace.float32.type) hist = np.zeros([BINS], dtype=np.uint32) histogram(A, hist) if dace.Config.get_bool('profiling'): dace.timethis('histogram', 'numpy', (H.get() * W.get()), np.histogram, A, BINS) diff = np.linalg.norm(np.histogram(A, bins=BINS, range=(0.0, 1.0))[0][1:-1] - hist[1:-1]) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
b >> B[i, j] b = a if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("W", type=int, nargs="?", default=64) parser.add_argument("H", type=int, nargs="?", default=64) args = vars(parser.parse_args()) W.set(args["W"]) H.set(args["H"]) print('Transpose %dx%d' % (W.get(), H.get())) A = np.random.rand(H.get(), W.get()).astype(np.float32) B = np.zeros([W.get(), H.get()], dtype=np.float32) transpose(A, B) if dace.Config.get_bool('profiling'): dace.timethis('transpose', 'numpy', (H.get() * W.get()), np.transpose, A) diff = np.linalg.norm(np.transpose(A) - B) / (H.get() * W.get()) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
N.set(args["N"]) print('Scalar-vector multiplication %d' % (N.get())) # Initialize arrays: Randomize A and X, zero Y A = dace.float64(np.random.rand()) X = np.random.rand(N.get()).astype(np.float64) Y = np.random.rand(N.get()).astype(np.float64) A_regression = np.float64() X_regression = np.ndarray([N.get()], dtype=np.float64) Y_regression = np.ndarray([N.get()], dtype=np.float64) A_regression = A X_regression[:] = X[:] Y_regression[:] = Y[:] axpy(A, X, Y) c_axpy = sp.linalg.blas.get_blas_funcs('axpy', arrays=(X_regression, Y_regression)) if dace.Config.get_bool('profiling'): dace.timethis('axpy', 'BLAS', dace.eval(2 * N), c_axpy, X_regression, Y_regression, N.get(), A_regression) else: c_axpy(X_regression, Y_regression, N.get(), A_regression) diff = np.linalg.norm(Y_regression - Y) / float(dace.eval(N)) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("W", type=int, nargs="?", default=32) parser.add_argument("H", type=int, nargs="?", default=32) args = vars(parser.parse_args()) A = dace.ndarray([H, W], dtype=dace.uint8) hist = dace.ndarray([BINS], dtype=dace.uint32) W.set(args["W"]) H.set(args["H"]) print('Histogram (dec) %dx%d' % (W.get(), H.get())) A[:] = np.random.randint(0, 256, (H.get(), W.get())).astype(dace.uint8.type) hist[:] = dace.uint32(0) histogram(A, hist) if dace.Config.get_bool('profiling'): dace.timethis('histogram', 'numpy', dace.eval(H * W), np.histogram, A, BINS) diff = np.linalg.norm(np.histogram(A, bins=BINS)[0] - hist) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
histogram.draw_to_file() A = dace.ndarray([H, W], dtype=dtype) hist = dace.ndarray([num_bins], dtype=itype) A[:] = np.random.rand(H.get(), W.get()).astype(dace.float32.type) hist[:] = itype(0) if args["specialize"]: histogram(A=A, hist=hist) else: histogram(A=A, H=H, W=W, hist=hist) if dace.Config.get_bool('profiling'): dace.timethis('histogram', 'numpy', dace.eval(H * W), np.histogram, A, num_bins) ref = np.histogram(A, bins=num_bins.get(), range=(0.0, 1.0))[0] diff = np.linalg.norm(ref[1:-1] - hist[1:-1]) print("Difference:", diff) if diff > 1e-5: print("** Kernel") print(hist) print("** Reference") print(ref) print("Validation failed.") print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
def run_spmv(size_w, size_h, num_nonzero, specialize): cols.set(size_w) rows.set(size_h) nnz.set(num_nonzero) print("Sparse Matrix-Vector Multiplication {}x{} " "({} non-zero elements, {}specialized)".format( cols.get(), rows.get(), nnz.get(), "not " if not specialize else "")) A_row = dace.ndarray([rows + 1], dtype=itype) A_col = dace.ndarray([nnz], dtype=itype) A_val = dace.ndarray([nnz], dtype=dtype) x = dace.ndarray([cols], dtype) b = dace.ndarray([rows], dtype) # Assuming uniform sparsity distribution across rows nnz_per_row = nnz.get() // rows.get() nnz_last_row = nnz_per_row + (nnz.get() % rows.get()) if nnz_last_row > cols.get(): print("Too many nonzeros per row") exit(1) # RANDOMIZE SPARSE MATRIX A_row[0] = itype(0) A_row[1:rows.get()] = itype(nnz_per_row) A_row[-1] = itype(nnz_last_row) A_row = np.cumsum(A_row, dtype=itype.type) # Fill column data for i in range(rows.get() - 1): A_col[nnz_per_row*i:nnz_per_row*(i+1)] = \ np.sort(np.random.choice(cols.get(), nnz_per_row, replace=False)) # Fill column data for last row A_col[nnz_per_row * (rows.get() - 1):] = np.sort( np.random.choice(cols.get(), nnz_last_row, replace=False)) A_val[:] = np.random.rand(nnz.get()).astype(dtype.type) ######################### x[:] = np.random.rand(cols.get()).astype(dtype.type) #b[:] = dtype(0) # Setup regression A_sparse = scipy.sparse.csr_matrix((A_val, A_col, A_row), shape=(rows.get(), cols.get())) spmv = make_sdfg(specialize) if specialize: spmv.specialize(dict(rows=rows, cols=cols, nnz=nnz)) spmv(A_row=A_row, A_col=A_col, A_val=A_val, x=x, b=b, rows=rows, cols=cols, nnz=nnz) if dace.Config.get_bool("profiling"): dace.timethis("spmv", "scipy", 0, A_sparse.dot, x) diff = np.linalg.norm(A_sparse.dot(x) - b) / float(rows.get()) print("Difference:", diff) if diff >= 1e-5: print("Validation failed.") print("Result:") print(b) print("Reference:") print(A_sparse.dot(x)) print("Type \"debug\" to enter debugger, " "or any other string to quit (timeout in 10 seconds)") read, _, _ = select.select([sys.stdin], [], [], 10) if len(read) > 0 and sys.stdin.readline().strip().lower() == "debug": print("Entering debugger...") pdb.set_trace() else: print("Exiting...") print("==== Program end ====") if diff > 1e-5: raise RuntimeError("Validation failed.") return spmv
n = r ** k print('FFT on vector of length %d' % n) N.set(n) R.set(r) K.set(k) X = np.random.rand(n).astype(np.complex64) + 1j * np.random.rand(n).astype(np.complex64) Y_own = np.zeros_like(X, dtype=np.complex64) own_fft(X, Y_own, N=N, K=K, R=R) Y_np = fft(X) if dace.Config.get_bool('profiling'): dace.timethis('FFT', 'numpy_fft', (n**2), fft, X) print("\n### RESULTS ###") print("X:", np.array_str(X, precision=2, suppress_small=True)) print("Y_own: ", np.array_str(Y_own, precision=10, suppress_small=True)) print("Y_np: ", np.array_str(Y_np, precision=10, suppress_small=True)) eps = 1e-10 diff = np.linalg.norm(Y_own - Y_np) / n if diff < eps: print("Difference:", diff) else: print("\033[91mDifference:", diff, "\033[0m") print("==== Program end ====") exit(0 if diff <= 1e-3 else 1)
b = a if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("W", type=int, nargs="?", default=64) parser.add_argument("H", type=int, nargs="?", default=64) args = vars(parser.parse_args()) A = dace.ndarray([H, W], dtype=dace.float32) B = dace.ndarray([H, W], dtype=dace.float32) W.set(args["W"]) H.set(args["H"]) print('Transpose %dx%d' % (W.get(), H.get())) A[:] = np.random.rand(H.get(), W.get()).astype(dace.float32.type) B[:] = dace.float32(0) transpose(A, B) if dace.Config.get_bool('profiling'): dace.timethis('transpose', 'numpy', dace.eval(H * W), np.transpose, A) diff = np.linalg.norm(np.transpose(A) - B) / float(dace.eval(H * W)) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
gemm = gemm.to_sdfg() from dace.transformation.interstate import GPUTransformSDFG gemm.apply_transformations(GPUTransformSDFG, options={'sequential_innermaps': False}, validate=True, validate_all=False, strict=True) gemm.validate() for m in [k for k, v in gemm.node(0).scope_dict().items() if isinstance(k, dace.nodes.MapEntry) and v is None]: m.map.schedule = dace.ScheduleType.GPU_Persistent for m in [k for k, v in gemm.node(0).scope_dict().items() if isinstance(k, dace.nodes.MapEntry) and v is not None]: m.map.schedule = dace.ScheduleType.GPU_Device gemm(A, B, C) if dace.Config.get_bool('profiling'): dace.timethis('gemm', 'numpy', (2 * M.get() * K.get() * N.get()), np.dot, A, B, C_regression) else: np.dot(A, B, C_regression) diff = np.linalg.norm(C_regression - C) / (M.get() * N.get()) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
# Initialize arrays: Randomize A and X, zero Y A = dace.float64(np.random.rand()) X[:] = np.random.rand(N.get()).astype(dace.float64.type) Y[:] = np.random.rand(N.get()).astype(dace.float64.type) A_regression = np.float64() X_regression = np.ndarray([N.get()], dtype=np.float64) Y_regression = np.ndarray([N.get()], dtype=np.float64) A_regression = A X_regression[:] = X[:] Y_regression[:] = Y[:] if args["compile-only"]: dace.compile(axpy, A, X, Y) else: axpy(A, X, Y) c_axpy = sp.linalg.blas.get_blas_funcs('axpy', arrays=(X_regression, Y_regression)) if dace.Config.get_bool('profiling'): dace.timethis('axpy', contender, dace.eval(2 * N), c_axpy, X_regression, Y_regression, N.get(), A_regression) else: c_axpy(X_regression, Y_regression, N.get(), A_regression) diff = np.linalg.norm(Y_regression - Y) / float(dace.eval(N)) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
parser = argparse.ArgumentParser() parser.add_argument("M", type=int, nargs="?", default=24) parser.add_argument("K", type=int, nargs="?", default=24) parser.add_argument("N", type=int, nargs="?", default=24) args = vars(parser.parse_args()) M.set(args["M"]) K.set(args["K"]) N.set(args["N"]) print('Matrix multiplication %dx%dx%d' % (M.get(), K.get(), N.get())) # Initialize arrays: Randomize A and B, zero C A = np.random.rand(M.get(), K.get()).astype(np.float64) B = np.random.rand(K.get(), N.get()).astype(np.float64) C = np.zeros([M.get(), N.get()], dtype=np.float64) C_regression = np.zeros_like(C) gemm(A, B, C) if dace.Config.get_bool('profiling'): dace.timethis('gemm', 'numpy', dace.eval(2 * M * K * N), np.dot, A, B, C_regression) else: np.dot(A, B, C_regression) diff = np.linalg.norm(C_regression - C) / float(dace.eval(M * N)) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
print("Predicate-Based Filter. size={}, ratio={} ({}specialized)".format( N.get(), ratio, "" if args["specialize"] else "not ")) A[:] = np.random.rand(N.get()).astype(dace.float32.type) B[:] = dace.float32(0) sdfg = make_sdfg(args["specialize"]) sdfg.draw_to_file() if args["specialize"]: sdfg.specialize(dict(N=N)) sdfg(A=A, B=B, outsize=outsize, ratio=ratio) else: sdfg(A=A, B=B, outsize=outsize, ratio=ratio, N=N) if dace.Config.get_bool('profiling'): dace.timethis('filter', 'numpy', 0, regression, A, ratio) filtered = regression(A, ratio) if len(filtered) != outsize[0]: print( "Difference in number of filtered items: %d (DaCe) vs. %d (numpy)" % (outsize[0], len(filtered))) totalitems = min(outsize[0], N.get()) print('DaCe:', B[:totalitems].view(type=np.ndarray)) print('Regression:', filtered.view(type=np.ndarray)) exit(1) # Sort the outputs filtered = np.sort(filtered) B[:outsize[0]] = np.sort(B[:outsize[0]])
W.set(args["W"]) print("Histogram {}x{} ({}specialized)".format( H.get(), W.get(), "" if args["specialize"] else "not ")) A = dace.ndarray([H, W], dtype=dtype) hist = dace.ndarray([num_bins], dtype=dace.uint32) A[:] = np.random.rand(H.get(), W.get()).astype(dace.float32.type) hist[:] = dace.uint32(0) if args["specialize"]: histogram(A=A, hist=hist) else: histogram(A=A, H=H, W=W, hist=hist) if dace.Config.get_bool('profiling'): dace.timethis('histogram', 'numpy', (H.get() * W.get()), np.histogram, A, num_bins) diff = np.linalg.norm( np.histogram(A, bins=num_bins.get(), range=(0.0, 1.0))[0][1:-1] - hist[1:-1]) print("Difference:", diff) if diff > 1e-5: print("Validation failed.") print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)