b = a @dp.map(_[0:W]) def compute_tmp_again(i): a << tmp[i] b >> tmp[i] b = a + a @dp.map(_[0:W]) def compute_output(i): a << tmp[i] b >> B[i] b = a + a if __name__ == '__main__': W.set(3) A = dp.ndarray([W]) B = dp.ndarray([W]) A[:] = np.mgrid[0:W.get()] B[:] = dp.float32(0.0) raw_prog(A, B) diff = np.linalg.norm(4 * A - B) / W.get() print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
W = dp.symbol('W') @dp.program def indirection(A, x, B): @dp.map(_[0:W]) def ind(i): bla << A[x[i]] out >> B[i] out = bla if __name__ == '__main__': W.set(5) A = dp.ndarray([W * W]) B = dp.ndarray([W]) x = dp.ndarray([W], dtype=dp.uint32) A[:] = np.arange(10, 10 + W.get() * W.get()) B[:] = dp.float32(0) x[:] = np.random.randint(0, W.get() * W.get(), W.get()) indirection(A, x, B, W=W) print(x) print(B) B_ref = np.array([A[x[i]] for i in range(W.get())], dtype=B.dtype) diff = np.linalg.norm(B - B_ref) exit(0 if diff == 0 else 1)
dace.reduce(lambda a, b: a + b, tmp, sum) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("W", type=int, nargs="?", default=128) parser.add_argument("H", type=int, nargs="?", default=128) args = vars(parser.parse_args()) W.set(args["W"]) H.set(args["H"]) print('Map-Reduce Test %dx%d' % (W.get(), H.get())) A = dace.ndarray([1, H, 1, W, 1], dtype=dace.float32) B = dace.ndarray([H, W], dtype=dace.float32) res = dace.ndarray([1], dtype=dace.float32) A[:] = np.random.rand(1, H.get(), 1, W.get(), 1).astype(dace.float32.type) B[:] = dace.float32(0) res[:] = dace.float32(0) mapreduce_test_3(A, B, res) diff = np.linalg.norm(5 * A.reshape((H.get(), W.get())) - B) / (H.get() * W.get()) diff_res = abs((np.sum(B) - res[0])).view(type=np.ndarray) print("Difference:", diff, diff_res) print("==== Program end ====") exit(0 if diff <= 1e-5 and diff_res <= 1 else 1)
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())) spmv(A_row, A_col, A_val, x, b) 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 ====") exit(0 if diff <= 1e-5 else 1)
@dace.program def prog(A, B): no = dace.define_local([number], dace.float32) number = dace.define_local([W], dace.float32) f(A, number) @dace.map(_[0:W]) def bla2(i): inp << number[i] out >> B[i] out = 2 * inp if __name__ == '__main__': W.set(3) A = dace.ndarray([W]) B = dace.ndarray([W]) A[:] = np.mgrid[0:W.get()] B[:] = dace.float32(0.0) prog(A, B) diff = np.linalg.norm(4 * A - B) / W.get() print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)
import numpy as np import dace as dp from dace.sdfg import SDFG from dace.memlet import Memlet # Constructs an SDFG with two consecutive tasklets if __name__ == '__main__': print('Multidimensional offset and stride test') # Externals (parameters, symbols) N = dp.symbol('N') N.set(20) input = dp.ndarray([N, N], dp.float32) output = dp.ndarray([4, 3], dp.float32) input[:] = (np.random.rand(N.get(), N.get()) * 5).astype(dp.float32.type) output[:] = dp.float32(0) # Construct SDFG mysdfg = SDFG('offset_stride') state = mysdfg.add_state() A_ = state.add_array('A', [6, 6], dp.float32, offset=[2, 3], strides=[N, N]) B_ = state.add_array('B', [3, 2], dp.float32, offset=[-1, -1], strides=[4, 3]) map_entry, map_exit = state.add_map('mymap', [('i', '1:4'), ('j', '1:3')]) tasklet = state.add_tasklet('mytasklet', {'a'}, {'b'}, 'b = a')
def reset_tmp(y, x): out >> tmp[y, x] out = dace.float32(0.0)
dace.reduce(lambda a, b: a + b, A, red2) dace.reduce(lambda a, b: a + b, B[2:H - 2, 5, :], red1[0]) dace.reduce(lambda a, b: a + b, B[3:H - 3, 5:7, :], red1[1:], axis=(2, 0)) dace.reduce(lambda a, b: a - b, B[2:H - 2, 5, :], red1[0]) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("W", type=int, nargs="?", default=20) parser.add_argument("H", type=int, nargs="?", default=20) args = vars(parser.parse_args()) A = dace.ndarray([W, H], dtype=dace.float32) B = dace.ndarray([H, W, H], dtype=dace.float32) red1 = dace.ndarray([3], dtype=dace.float32) red2 = dace.ndarray([1], dtype=dace.float32) W.set(args["W"]) H.set(args["H"]) print('Conflict Resolution Test %dx%d' % (W.get(), H.get())) A[:] = np.random.rand(H.get(), W.get()).astype(dace.float32.type) B[:] = np.random.rand(H.get(), W.get(), H.get()).astype(dace.float32.type) red1[:] = dace.float32(0) red2[:] = dace.float32(0) confres_test.compile(A, B, red1, red2) print("==== Program end ====")
def dot(A: dace.float32[N], B: dace.float32[N], out: dace.float32[1]): @dace.map def product(i: _[0:N]): a << A[i] b << B[i] o >> out(1, lambda x, y: x + y) o = a * b if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("N", type=int, nargs="?", default=64) args = vars(parser.parse_args()) N.set(args["N"]) A = dace.ndarray([N], dtype=dace.float32) B = dace.ndarray([N], dtype=dace.float32) out_AB = dace.scalar(dace.float32) print('Dot product %d' % (N.get())) A[:] = np.random.rand(N.get()).astype(dace.float32.type) B[:] = np.random.rand(N.get()).astype(dace.float32.type) out_AB[0] = dace.float32(0) dot(A, B, out_AB) diff_ab = np.linalg.norm(np.dot(A, B) - out_AB) / float(N.get()) print("Difference (A*B):", diff_ab) exit(0 if (diff_ab <= 1e-5) else 1)
kernel[0, 2] * input[0, 2] + kernel[1, 0] * input[1, 0] + kernel[1, 1] * input[1, 1] + kernel[1, 2] * input[1, 2] + kernel[2, 0] * input[2, 0] + kernel[2, 1] * input[2, 1] + kernel[2, 2] * input[2, 2]) if __name__ == "__main__": print("==== Program start ====") print('Conv2D %dx%d' % (N.get(), N.get())) A = dace.ndarray([N, N], dtype=dace.float32) B = dace.ndarray([N, N], dtype=dace.float32) # Initialize arrays: Randomize A, zero B A[:] = dace.float32(0) B[:] = dace.float32(0) A[1:N.get() - 1, 1:N.get() - 1] = np.random.rand(dace.eval(N - 2), dace.eval(N - 2)).astype( dace.float32.type) regression = np.ndarray([N.get() - 2, N.get() - 2], dtype=np.float32) regression[:] = A[1:N.get() - 1, 1:N.get() - 1] #print(A.view(type=np.ndarray)) ############################################# # Run DaCe program sdfg = stencil3x3.to_sdfg() sdfg.add_constants({'kernel': KERNEL}) sdfg(A=A, B=B, N=N)
N = dace.symbol('N') @dace.program(dace.float32[N], dace.float32[N]) def cudahello(V, Vout): # Transient variable @dace.map(_[0:N]) def multiplication(i): in_V << V[i] out >> Vout[i] out = in_V * 2.0 if __name__ == "__main__": N.set(52) print('Vector double CUDA %d' % (N.get())) V = dace.ndarray([N], dace.float32) Vout = dace.ndarray([N], dace.float32) V[:] = np.random.rand(N.get()).astype(dace.float32.type) Vout[:] = dace.float32(0) cudahello(V, Vout) diff = np.linalg.norm(2 * V - Vout) / N.get() print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 1)