def test_svd(python_method_impl): with CodeBuilder(name="primary") as cb: cb("n", 3) cb("nodes", "`<builtin>array`(n)") cb("vdm", "`<builtin>array`(n*n)") cb("identity", "`<builtin>array`(n*n)") cb("nodes[i]", "i/n", loops=[("i", 0, "n")]) cb("vdm[j*n + i]", "nodes[i]**j", loops=[("i", 0, "n"), ("j", 0, "n")]) cb("vdm_u, vdm_sigma, vdm_vt", "`<builtin>svd`(vdm, n)") cb("vdm_usigma", "`<builtin>array`(n*n)") cb("vdm_v", "`<builtin>array`(n*n)") cb("vdm_usigma[i + j*n]", "vdm_u[i + j*n] * vdm_sigma[j]", loops=[("i", 0, "n"), ("j", 0, "n")]) cb("vdm_v[i + j*n]", "vdm_vt[j + i*n]", loops=[("i", 0, "n"), ("j", 0, "n")]) cb("vdm_2", "`<builtin>matmul`(vdm_usigma, vdm_vt, n, n)") cb("diff", "vdm-vdm_2") cb((), "`<builtin>print`(diff)") cb.yield_state("diff", "result", 0, "final") from utils import execute_and_return_single_result code = create_DAGCode_with_steady_phase(cb.statements) result = execute_and_return_single_result(python_method_impl, code) assert la.norm(result) < 1e-10
def test_switch_phases(python_method_impl): from dagrt.language import CodeBuilder, ExecutionPhase with CodeBuilder(name="state_1") as builder_1: builder_1(var("<state>x"), 1) builder_1.switch_phase("state_2") with CodeBuilder(name="state_2") as builder_2: builder_2.yield_state(var("<state>x"), "x", 0, "final") code = DAGCode(phases={ "state_1": ExecutionPhase(name="state_1", next_phase="state_1", statements=builder_1.statements), "state_2": ExecutionPhase(name="state_2", next_phase="state_2", statements=builder_2.statements) }, initial_phase="state_1") from utils import execute_and_return_single_result result = execute_and_return_single_result(python_method_impl, code, initial_context={"x": 0}, max_steps=2) assert result == 1
def test_arrays_and_linalg(python_method_impl): with CodeBuilder(name="primary") as cb: cb("n", "4") cb("nodes", "`<builtin>array`(n)") cb("vdm", "`<builtin>array`(n*n)") cb("identity", "`<builtin>array`(n*n)") cb("nodes[i]", "i/n", loops=[("i", 0, "n")]) cb("identity[i]", "0", loops=[("i", 0, "n*n")]) cb("identity[i*n + i]", "1", loops=[("i", 0, "n")]) cb("vdm[j*n + i]", "nodes[i]**j", loops=[("i", 0, "n"), ("j", 0, "n")]) cb("vdm_inverse", "`<builtin>linear_solve`(vdm, identity, n, n)") cb("myarray", "`<builtin>matmul`(vdm, vdm_inverse, n, n)") cb((), "`<builtin>print`(myarray)") cb.yield_state("myarray", "result", 0, "final") from utils import execute_and_return_single_result code = create_DAGCode_with_steady_phase(cb.statements) result = execute_and_return_single_result(python_method_impl, code) result = result.reshape(4, 4, order="F") assert la.norm(result - np.eye(4)) < 1e-10
def test_CodeBuilder_assign(python_method_impl): with CodeBuilder("phase") as builder: builder(var("x"), 1) builder.yield_state(var("x"), "x", 0, "final") code = create_DAGCode_with_steady_phase(builder.statements) result = execute_and_return_single_result(python_method_impl, code) assert result == 1
def test_arrays_and_looping(python_method_impl): with CodeBuilder(name="primary") as cb: cb("myarray", "`<builtin>array`(20)") cb("myarray[i]", "i", loops=[("i", 0, 20)]) cb.yield_state("myarray[15]", "result", 0, "final") from utils import execute_and_return_single_result code = create_DAGCode_with_steady_phase(cb.statements) result = execute_and_return_single_result(python_method_impl, code) assert result == 15
def test_CodeBuilder_condition_with_else_not_taken(python_method_impl): with CodeBuilder("phase") as builder: builder(var("x"), 1) with builder.if_(var("x"), "==", 1): builder(var("x"), 2) with builder.else_(): builder(var("x"), 3) builder.yield_state(var("x"), "x", 0, "final") code = create_DAGCode_with_steady_phase(builder.statements) result = execute_and_return_single_result(python_method_impl, code) assert result == 2
def test_isnan(python_method_impl, value): cbuild = RawCodeBuilder() cbuild.add_and_get_ids( Assign(id="assign_1", assignee="x", assignee_subscript=(), expression=var("<builtin>isnan")(value)), YieldState(id="return", time=0, time_id="final", expression=var("x"), component_id="<state>", depends_on=["assign_1"])) cbuild.commit() code = create_DAGCode_with_steady_phase(cbuild.statements) result = execute_and_return_single_result(python_method_impl, code) assert result == np.isnan(value)
def test_CodeBuilder_restart_step(python_method_impl): with CodeBuilder("init") as builder_init: builder_init("<p>x", "0") with CodeBuilder("state1") as builder1: builder1("<p>x", "<p>x + 1") with builder1.if_("<p>x == 1"): builder1.restart_step() with CodeBuilder("state2") as builder2: builder2.yield_state(var("<p>x"), "x", 0, "final") phases = [ builder_init.as_execution_phase(next_phase="state1"), builder1.as_execution_phase(next_phase="state2"), builder2.as_execution_phase(next_phase="state2") ] code = DAGCode.from_phases_list(phases, "init") result = execute_and_return_single_result(python_method_impl, code, max_steps=4) assert result == 2
def test_norm(python_method_impl, order, norm_suffix, test_vector): def true_norm(x): if np.isscalar(x): return abs(x) return np.linalg.norm(x, ord=order) cbuild = RawCodeBuilder() cbuild.add_and_get_ids( Assign(id="assign_1", assignee="x", assignee_subscript=(), expression=test_vector), Assign(id="assign_2", assignee="n", assignee_subscript=(), expression=( var("<builtin>norm_%s" % norm_suffix)(var("x"))), depends_on=["assign_1"]), YieldState(id="return", time=0, time_id="final", expression=var("n"), component_id="<state>", depends_on=["assign_2"])) cbuild.commit() code = create_DAGCode_with_steady_phase(cbuild.statements) result = execute_and_return_single_result(python_method_impl, code) assert np.allclose(result, true_norm(test_vector))
def test_IfThenElse(python_method_impl): from utils import execute_and_return_single_result code, expected_result = get_IfThenElse_test_code_and_expected_result() result = execute_and_return_single_result(python_method_impl, code) assert result == expected_result