args = vars(parser.parse_args()) A = dace.ndarray([N], dtype=dace.float32) B = dace.ndarray([N], dtype=dace.float32) out_AB = dace.scalar(dace.float64) out_AA = dace.scalar(dace.float64) N.set(args["N"]) 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.float64(0) out_AA[0] = dace.float64(0) cdot = dace.compile(dot, A, B, out_AB) cdot(A, B, out_AB) # To allow reloading the SDFG code file with the same name del cdot cdot_self = dace.compile(dot, A, A, out_AA) cdot_self(A, A, out_AA) diff_ab = np.linalg.norm(np.dot(A, B) - out_AB) / float(N.get()) diff_aa = np.linalg.norm(np.dot(A, A) - out_AA) / float(N.get()) print("Difference (A*B):", diff_ab) print("Difference (A*A):", diff_aa) exit(0 if (diff_ab <= 1e-5 and diff_aa <= 1e-5) else 1)
state.add_edge(A, None, Atrans, None, Memlet.simple(A, fullrange)) _, me, mx = state.add_mapped_tasklet( 'compute', dict(i=irange, j=jrange), dict(a=Memlet.simple(Atrans, 'i-1,j')), 'b = math.exp(a)', dict(b=Memlet.simple(B, 'i,j'))) state.add_edge(Atrans, None, me, None, Memlet.simple(Atrans, fullrange)) state.add_edge(mx, None, B, None, Memlet.simple(B, fullrange)) ########################################################################## code_nonspec = spec_sdfg.generate_code() if 'Dynamic' not in code_nonspec[0].code: print('ERROR: Constants were needlessly specialized') exit(1) spec_sdfg.specialize(dict(N=N, M=M)) code_spec = spec_sdfg.generate_code() if 'Dynamic' in code_spec[0].code: print('ERROR: Constants were not properly specialized') exit(2) func = dp.compile(spec_sdfg) func(A=input, B=output, N=N, M=M) diff = np.linalg.norm( np.exp(input[1:(N.get() - 1), 0:M.get()]) - output[1:-1, :]) / N.get() print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 3)
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)
if __name__ == "__main__": print('Reloadable DaCe program test') array_one = np.random.rand(10).astype(np.float64) array_two = np.random.rand(20).astype(np.float64) output_one = np.zeros(10, dtype=np.float64) output_two = np.zeros(20, dtype=np.float64) prog_one = program_generator(10, 2.0) prog_two = program_generator(20, 4.0) # This should create two libraries for the two SDFGs, as they compile over # the same folder func1 = dace.compile(prog_one) try: func2 = dace.compile(prog_two) except CompilationError: # On some systems (e.g., Windows), the file will be locked, so # compilation will fail print('Compilation failed due to locked file. Skipping test.') exit(0) func1(input=array_one, output=output_one) func2(input=array_two, output=output_two) diff1 = np.linalg.norm(2.0 * array_one - output_one) / 10.0 diff2 = np.linalg.norm(4.0 * array_two - output_two) / 20.0 print("Differences:", diff1, diff2) exit(0 if (diff1 < 1e-5 and diff2 < 1e-5) else 1)
t >> tol(1, lambda x, y: x + y) b = a t = a * a elif tol <= 5: @dace.map(_[0:W]) def something(i): a << A[0, i] b >> B[0, i] b = a elif tol <= 6: @dace.map(_[0:W]) def something(i): a << A[0, i] b >> B[0, i] b = a else: for i in range(W): @dace.map(_[0:W]) def something(j): a << A[0, j] b >> B[0, j] b = a if __name__ == '__main__': dace.compile(control_flow_test, dace.float32[W, H], dace.float32[H, W], dace.float32)
#!/usr/bin/env python import dace as dp W = dp.symbol('W') H = dp.symbol('H') @dp.external_function def transpose(input, output): @dp.map(_[0:H, 0:W]) def compute(i, j): a << input[j, i] b >> output[i, j] b = a @dp.external_function def bla(input, output): dp.call(transpose, input, output) @dp.program def myprogram(A, B): dp.call(bla, A, B) if __name__ == '__main__': dp.compile(myprogram, dp.float32[W, H], dp.float32[H, W])
# Initialize arrays: Randomize A and B, zero C A[:] = np.random.rand(M.get(), N.get()).astype(dace.float64.type) B[:] = np.random.rand(N.get(), K.get()).astype(dace.float64.type) C[:] = dace.float64(0) A_regression = np.ndarray([M.get(), N.get()], dtype=np.float64) B_regression = np.ndarray([N.get(), K.get()], dtype=np.float64) C_regression = np.ndarray([M.get(), K.get()], dtype=np.float64) A_regression[:] = A[:] B_regression[:] = B[:] C_regression[:] = C[:] if args["sdfg"] is not None: sdfg = dace.SDFG.from_file(args["sdfg"]) gemmfunc = dace.compile(sdfg) else: gemmfunc = dace.compile(gemm, A, B, C) if not args["compile-only"]: gemmfunc(A, B, C) if dace.Config.get_bool('profiling'): dace.timethis('gemm', contender, dace.eval(2 * N * N * N), np.dot, A_regression, B_regression, C_regression) else: np.dot(A_regression, B_regression, C_regression) #print(C.view(type=np.ndarray)) diff = np.linalg.norm(C_regression - C) / float(dace.eval(M * K))
@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()) A = dace.ndarray([N], dtype=dace.float32) out_AA = dace.scalar(dace.float64) N.set(args["N"]) print('Dot product %d' % (N.get())) A[:] = np.random.rand(N.get()).astype(dace.float32.type) out_AA[0] = dace.float64(0) cdot_self = dace.compile(dot, A, A, out_AA) cdot_self(A, A, out_AA) diff_aa = np.linalg.norm(np.dot(A, A) - out_AA) / float(N.get()) print("Difference:", diff_aa) exit(0 if (diff_aa <= 1e-5) else 1)
else: @dace.map(_[0:W]) def something(j): a << A[0, j] b >> B[0, j] b = a for i in range(3): @dace.map(_[0:W]) def something(j): a << A[1, j] b >> B[1, j] b = a while tol[0] < 4: @dace.map(_[0:W]) def something(i): a << A[0, i] b >> B[0, i] t >> tol(1, lambda x, y: x + y) b = a t = a * a if __name__ == '__main__': dace.compile(myprogram, dace.float32[W, H], dace.float32[H, W], dace.float32[1])
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(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 ====")
dict(i=irange, j=jrange), dict(a=Memlet.simple(Atrans, 'i-1,j')), 'b = math.exp(a)', dict(b=Memlet.simple(B, 'i,j'))) state.add_edge(Atrans, None, me, None, Memlet.simple(Atrans, fullrange)) state.add_edge(mx, None, B, None, Memlet.simple(B, fullrange)) ########################################################################## code_nonspec = spec_sdfg.generate_code(specialize=False) if 'Dynamic' not in code_nonspec[0].code: print('ERROR: Constants were needlessly specialized') exit(1) code_spec = spec_sdfg.generate_code(specialize=True) if 'Dynamic' in code_spec[0].code: print('ERROR: Constants were not properly specialized') exit(2) spec_sdfg.draw_to_file() func = dp.compile(spec_sdfg, specialize=True) func(A=input, B=output, N=N, M=M) diff = np.linalg.norm( np.exp(input[1:dp.eval(N - 1), 0:dp.eval(M)]) - output[1:-1, :]) / dp.eval(N) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 3)
Memlet.from_array(globalstream.data, globalstream.desc(sdfg))) state.add_nedge(globalstream, globalarr, Memlet.from_array(globalarr.data, globalarr.desc(sdfg))) sdfg.fill_scope_connectors() sdfg.draw_to_file() if __name__ == '__main__': print('Thread-local stream test') N.set(20) output = np.ndarray([N.get()], dtype=np.float32) output[:] = dp.float32(0) code_nonspec = sdfg.generate_code() if 'Threadlocal' not in code_nonspec[0].code: print('ERROR: Thread-local stream was not created') exit(1) func = dp.compile(sdfg) func(ga=output, N=N) output = np.sort(output) diff = np.linalg.norm(output - np.arange(0, N.get(), dtype=np.float32)) print("Difference:", diff) print("==== Program end ====") exit(0 if diff <= 1e-5 else 2)