Example #1
0
    def test_API_return_okay(self):
        p = Program(
            Fork2, Forward, Drop,
            ElementInstance("Fork2", "dup"),
            ElementInstance("Forward", "fwd"),
            ElementInstance("Drop", "drop"),
            Connect("dup", "fwd", "out1"),
            Connect("fwd", "drop"),
            InternalTrigger("t"),
            APIFunction("func", ["int"], "int"),
            ResourceMap("func", "dup"),
            ResourceMap("t", "fwd"),
            ResourceMap("t", "drop"),
        )
        g = program_to_graph_pass(p)
        join_and_resource_annotation_pass(g, True, False)
        self.assertEqual(5, len(g.instances))
        self.assertEqual(1, len(g.states))
        roots = self.find_roots(g)
        self.assertEqual(set(['dup', 'fwd_buffer_read']), roots)
        self.assertEqual(2, len(self.find_subgraph(g, 'dup')))
        self.assertEqual(3, len(self.find_subgraph(g, 'fwd_buffer_read')))

        self.check_api_return(g, [("dup", "int")])
        self.check_api_return_from(g, [])
        self.check_api_return_final(g, ["dup"])
Example #2
0
    def test_pipeline(self):
        p = Program(
            APIFunction("producer", ["int"], None),
            InternalTrigger("consumer"),
            Forward,
            Element("Comsumer", [Port("in", ["int"])], [], r'''printf("%d\n", in());'''),
            ElementInstance("Forward", "Forwarder"),
            ElementInstance("Comsumer", "Comsumer"),
            Connect("Forwarder", "Comsumer"),
            ResourceMap("producer", "Forwarder"),
            ResourceMap("consumer", "Comsumer"),
        )

        g1 = program_to_graph_pass(p)
        g2 = program_to_graph_pass(p)
        join_and_resource_annotation_pass(g2, True, False)

        self.assertEqual(2, len(g1.instances))
        self.assertEqual(4, len(g2.instances))

        root1 = self.find_roots(g1)
        root2 = self.find_roots(g2)

        self.assertEqual(set(["Forwarder"]), root1)
        self.assertEqual(set(["Forwarder", "Comsumer_buffer_read"]), root2)
        self.assertEqual(set(["Forwarder", "Comsumer_buffer_in_write"]),
                         self.find_subgraph(g2, "Forwarder"))
        self.assertEqual(set(["Comsumer_buffer_read", "Comsumer"]),
                         self.find_subgraph(g2, "Comsumer_buffer_read"))
Example #3
0
 def test_init_end2end_another(self):
     p = Program(State("One", "int x;"), State("Multi", "One* core;"),
                 StateInstance("One", "one[4]"),
                 StateInstance("Multi", "all[4]", [AddressOf("one[4]")]))
     dp = desugar_spec_impl(p)
     g = program_to_graph_pass(dp)
     join_and_resource_annotation_pass(g, False, False)
     all = g.state_instances["all0"]
     self.assertEqual([AddressOf("one0")], all.init)
Example #4
0
 def test_init_end2end(self):
     p = Program(State("One", "int x;"), State("Multi", "One* cores[4];"),
                 StateInstance("One", "one[4]"),
                 StateInstance("Multi", "all", [AddressOf("one[4]")]))
     dp = desugar_spec_impl(p)
     g = program_to_graph_pass(dp)
     join_and_resource_annotation_pass(g, False, False)
     all = g.state_instances["all"]
     expect = [[AddressOf("one" + str(i)) for i in range(4)]]
     self.assertEqual(expect, all.init)
Example #5
0
 def test_API_not_always_return_but_okay(self):
     p = Program(
         Element("Filter", [Port("in", ["int"])], [Port("out", ["int"])],
                 r'''int x = in(); output switch { case (x>0): out(x); }'''),
         ElementInstance("Filter", "filter"),
         APIFunction("func", ["int"], "int", "-1"),
         ResourceMap("func", "filter"),
     )
     g = program_to_graph_pass(p)
     join_and_resource_annotation_pass(g, True, False)
Example #6
0
 def test_shared_state(self):
     p = Program(
         State("Shared", "int sum;", "100"),
         Element("Sum", [Port("in", ["int"])], [], r'''this.sum += in(); printf("%d\n", this.sum);''',
                 [("Shared", "this")]),
         StateInstance("Shared", "s"),
         ElementInstance("Sum", "sum1", ["s"]),
         ElementInstance("Sum", "sum2", ["s"])
     )
     g = program_to_graph_pass(p)
     join_and_resource_annotation_pass(g, True, False)
     self.assertEqual(2, len(g.instances))
     roots = self.find_roots(g)
     self.assertEqual(set(['sum1', 'sum2']), roots)
     self.assertEqual(set(['s']), set(g.state_instances.keys()))
Example #7
0
 def test_API_not_always_return(self):
     p = Program(
         Element("Filter", [Port("in", ["int"])], [Port("out", ["int"])],
                 r'''int x = in(); output switch { case (x>0): out(x); }'''),
         ElementInstance("Filter", "filter"),
         APIFunction("func", ["int"], "int"),
         ResourceMap("func", "filter"),
     )
     try:
         g = program_to_graph_pass(p)
         join_and_resource_annotation_pass(g, True, False)
     except Exception as e:
         self.assertNotEqual(e.message.find("doesn't always return, and the default return value is not provided."), -1)
     else:
         self.fail('Exception is not raised.')
Example #8
0
 def test_error_both_internal_external(self):
     p = Program(
         Forward,
         ElementInstance("Forward", "f1"),
         APIFunction("api", ["int"], "int"),
         InternalTrigger("t"),
         ResourceMap("api", "f1"),
         ResourceMap("t", "f1"),
     )
     try:
         g = program_to_graph_pass(p)
         join_and_resource_annotation_pass(g, True, False)
     except Exception as e:
         self.assertNotEqual(e.message.find("Element instance 'f1' cannot be mapped to both 'api' and 't'."), -1)
     else:
         self.fail('Exception is not raised.')
Example #9
0
 def test_API_return_error(self):
     p = Program(
         Forward,
         ElementInstance("Forward", "f1"),
         ElementInstance("Forward", "f2"),
         Connect("f1", "f2"),
         APIFunction("func", ["int"], None),
         ResourceMap("func", "f1"),
         ResourceMap("func", "f2"),
     )
     try:
         g = program_to_graph_pass(p)
         join_and_resource_annotation_pass(g, True, False)
     except Exception as e:
         self.assertNotEqual(e.message.find("API 'func' should have no return value"), -1)
     else:
         self.fail('Exception is not raised.')
Example #10
0
 def test_API_basic_no_output(self):
     p = Program(
         Element("Inc", [Port("in", ["int"])], [Port("out", ["int"])], r'''int x = in() + 1; output { out(x); }'''),
         Element("Print", [Port("in", ["int"])], [], r'''printf("%d\n", in());'''),
         APIFunction("add_and_print", ["int"], None),
         ElementInstance("Inc", "inc1"),
         ElementInstance("Print", "print"),
         Connect("inc1", "print"),
         ResourceMap("add_and_print", "inc1"),
         ResourceMap("add_and_print", "print"),
     )
     g = program_to_graph_pass(p)
     join_and_resource_annotation_pass(g, True, False)
     self.assertEqual(2, len(g.instances))
     self.assertEqual(0, len(g.states))
     roots = self.find_roots(g)
     self.assertEqual(set(['inc1']), roots)
     self.check_api_return(g, [])
     self.check_api_return(g, [])
     self.check_api_return_final(g, [])
Example #11
0
    def test_API_no_return_okay(self):
        p = Program(
            Forward,
            ElementInstance("Forward", "f1"),
            ElementInstance("Forward", "f2"),
            Connect("f1", "f2"),
            APIFunction("func", ["int"], None),
            ResourceMap("func", "f1"),
        )
        g = program_to_graph_pass(p)
        join_and_resource_annotation_pass(g, True, False)
        self.assertEqual(4, len(g.instances))
        self.assertEqual(1, len(g.states))
        roots = self.find_roots(g)
        self.assertEqual(set(['f1', 'f2_buffer_read']), roots)
        self.assertEqual(2, len(self.find_subgraph(g, 'f1')))
        self.assertEqual(2, len(self.find_subgraph(g, 'f2_buffer_read')))

        self.check_api_return(g, [])
        self.check_api_return(g, [])
        self.check_api_return_final(g, [])
Example #12
0
    def test_spec_impl(self):
        p = Program(
            Forward, ElementInstance("Forward", "f"),
            ElementInstance("Forward", "g"),
            Spec([
                APIFunction("run", ["int"], "int"),
                ResourceMap("run", "f"),
                ResourceMap("run", "g"),
                Connect("f", "g")
            ]),
            Impl([
                Connect("f", "g"),
                APIFunction("put", ["int"], None),
                ResourceMap("put", "f"),
                APIFunction("get", [], "int"),
                ResourceMap("get", "g"),
            ]))
        dp = desugar_spec_impl(p, "spec")
        g = program_to_graph_pass(dp)
        join_and_resource_annotation_pass(g, True, False)
        self.assertEqual(2, len(g.instances))
        self.assertEqual(set(['f']), self.find_roots(g))

        dp = desugar_spec_impl(p, "impl")
        g = program_to_graph_pass(dp)
        join_and_resource_annotation_pass(g, True, False)
        self.assertEqual(4, len(g.instances))
        self.assertEqual(set(['f', 'g_buffer_read']), self.find_roots(g))

        dp = desugar_spec_impl(p, "compare")
        g = program_to_graph_pass(dp)
        join_and_resource_annotation_pass(g, True, False)
        self.assertEqual(6, len(g.instances))
        self.assertEqual(set(['_spec_f', '_impl_f', '_impl_g_buffer_read']),
                         self.find_roots(g))
Example #13
0
 def test_trigger(self):
     p = Program(
         Forward,
         Drop,
         ElementInstance("Forward", "f[4]"),
         ElementInstance("Drop", "d[4]"),
         Connect("f[i]", "d[i]"),
         APIFunction("run[4]", ["int"], None),
         ResourceMap("run[i]", "f[i]"),
         InternalTrigger("t[4]"),
         ResourceMap("t[i]", "d[i]"),
     )
     p = desugar_spec_impl(p)
     g = program_to_graph_pass(p)
     join_and_resource_annotation_pass(g, True, False)
     self.assertEqual(16, len(g.instances))
     roots = self.find_roots(g)
     self.assertEqual(
         set([
             'f0', 'f1', 'f2', 'f3', 'd0_buffer_read', 'd1_buffer_read',
             'd2_buffer_read', 'd3_buffer_read'
         ]), roots)
Example #14
0
 def test_nonconflict_input_thread(self):
     p = Program(
         Forward, Drop,
         ElementInstance("Forward", "f1"),
         ElementInstance("Forward", "f2"),
         ElementInstance("Drop", "drop"),
         Connect("f1", "drop"),
         Connect("f2", "drop"),
         InternalTrigger("t1"),
         ResourceMap("t1", "drop"),
         InternalTrigger("t2"),
         ResourceMap("t2", "f1"),
         ResourceMap("t2", "f2"),
     )
     try:
         g = program_to_graph_pass(p)
         join_and_resource_annotation_pass(g, True, False)
     except Exception as e:
         self.assertNotEqual(e.message.find("Resource 't2' has more than one starting element instance"),
                             -1, 'Expect undefined exception.')
     else:
         self.fail('Exception is not raised.')
Example #15
0
 def test_api_para_para(self):
     p = Program(
         Forward,
         ElementInstance("Forward", "f[4]"),
         ElementInstance("Forward", "g[4]"),
         Connect("f[i]", "g[i]"),
         APIFunction("identity[4]", ["int"], "int"),
         ResourceMap("identity[i]", "f[i]"),
         ResourceMap("identity[i]", "g[i]"),
     )
     p = desugar_spec_impl(p)
     g = program_to_graph_pass(p)
     join_and_resource_annotation_pass(g, True, False)
     self.assertEqual(8, len(g.instances))
     roots = self.find_roots(g)
     self.assertEqual(set(['f0', 'f1', 'f2', 'f3']), roots)
     self.assertEqual(set(['f0', 'g0']),
                      self.find_subgraph(g, 'f0', set([])))
     self.assertEqual(set(['f1', 'g1']),
                      self.find_subgraph(g, 'f1', set([])))
     self.assertEqual(set(['f2', 'g2']),
                      self.find_subgraph(g, 'f2', set([])))
     self.assertEqual(set(['f3', 'g3']),
                      self.find_subgraph(g, 'f3', set([])))