def test_zip(): seg11 = Segment([TaggedValue(1, "C")]) seg21 = Segment( [TaggedValue(1, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg31 = Segment( [TaggedValue(1, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) lt1 = LTree([seg11, seg21, seg31]) seg12 = Segment([TaggedValue(2, "C")]) seg22 = Segment( [TaggedValue(2, "N"), TaggedValue(2, "L"), TaggedValue(2, "L")]) seg32 = Segment( [TaggedValue(2, "N"), TaggedValue(2, "L"), TaggedValue(2, "L")]) lt2 = LTree([seg12, seg22, seg32]) res = lt1.zip(lt2) seg1_exp = Segment([TaggedValue((1, 2), "C")]) seg2_exp = Segment([ TaggedValue((1, 2), "N"), TaggedValue((1, 2), "L"), TaggedValue((1, 2), "L") ]) seg3_exp = Segment([ TaggedValue((1, 2), "N"), TaggedValue((1, 2), "L"), TaggedValue((1, 2), "L") ]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) assert res == exp
def test_dacc(): c = 0 seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg2, seg3]) res = lt.dacc(operator.add, operator.add, c, fun.idt, fun.idt, operator.add, operator.add) seg1_exp = Segment([TaggedValue(0, "C")]) seg2_exp = Segment([ TaggedValue(13, "N"), TaggedValue(13 + 31, "L"), TaggedValue(13 + 31, "L") ]) seg3_exp = Segment([ TaggedValue(13, "N"), TaggedValue(13 + 72, "L"), TaggedValue(13 + 72, "L") ]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) assert res == exp
def test_deserialization_2_2(): # btree5 exp = Node( 13, Node(3, Leaf(1), Leaf(1)), Node(9, Node(7, Node(3, Leaf(1), Leaf(1)), Node(3, Leaf(1), Leaf(1))), Leaf(1))) seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment([TaggedValue(3, "C")]) seg3 = Segment([TaggedValue(1, "L")]) seg4 = Segment([TaggedValue(1, "L")]) seg5 = Segment([TaggedValue(9, "C")]) seg6 = Segment([TaggedValue(7, "C")]) seg7 = Segment([TaggedValue(3, "C")]) seg8 = Segment([TaggedValue(1, "L")]) seg9 = Segment([TaggedValue(1, "L")]) seg10 = Segment([TaggedValue(3, "C")]) seg11 = Segment([TaggedValue(1, "L")]) seg12 = Segment([TaggedValue(1, "L")]) seg13 = Segment([TaggedValue(1, "L")]) lt = LTree([ seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8, seg9, seg10, seg11, seg12, seg13 ]) res = lt.deserialization() assert res == exp
def test_map_not_empty(): # pylint: disable=missing-docstring seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg2, seg3]) pt = PTree(lt) res = pt.map(lambda x: x + 1, lambda x: x - 1).to_seq() seg1_exp = Segment([TaggedValue(12, "C")]) seg2_exp = Segment( [TaggedValue(30, "N"), TaggedValue(48, "L"), TaggedValue(33, "L")]) seg3_exp = Segment( [TaggedValue(71, "N"), TaggedValue(93, "L"), TaggedValue(43, "L")]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) if PID == 0 else None assert res == exp
def test_serialization_2_2(): # btree5 bt = Node( 13, Node(3, Leaf(1), Leaf(1)), Node(9, Node(7, Node(3, Leaf(1), Leaf(1)), Node(3, Leaf(1), Leaf(1))), Leaf(1))) m = 2 res = LTree.init_from_bt(bt, m) seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment([TaggedValue(3, "C")]) seg3 = Segment([TaggedValue(1, "L")]) seg4 = Segment([TaggedValue(1, "L")]) seg5 = Segment([TaggedValue(9, "C")]) seg6 = Segment([TaggedValue(7, "C")]) seg7 = Segment([TaggedValue(3, "C")]) seg8 = Segment([TaggedValue(1, "L")]) seg9 = Segment([TaggedValue(1, "L")]) seg10 = Segment([TaggedValue(3, "C")]) seg11 = Segment([TaggedValue(1, "L")]) seg12 = Segment([TaggedValue(1, "L")]) seg13 = Segment([TaggedValue(1, "L")]) exp = LTree([ seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8, seg9, seg10, seg11, seg12, seg13 ]) assert res == exp
def test_dacc(): # pylint: disable=missing-docstring c = 0 seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg2, seg3]) res = PTree(lt).dacc(fun.add, fun.add, c, fun.idt, fun.idt, fun.add, fun.add).to_seq() seg1_exp = Segment([TaggedValue(0, "C")]) seg2_exp = Segment([ TaggedValue(13, "N"), TaggedValue(13 + 31, "L"), TaggedValue(13 + 31, "L") ]) seg3_exp = Segment([ TaggedValue(13, "N"), TaggedValue(13 + 72, "L"), TaggedValue(13 + 72, "L") ]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) if PID == 0 else None assert res == exp
def test_uacc(): seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg2, seg3]) res = lt.uacc(fun.add, fun.idt, fun.add, fun.add, fun.add) seg1_exp = Segment([TaggedValue(13 + 31 + 47 + 32 + 72 + 92 + 42, "C")]) seg2_exp = Segment([ TaggedValue(31 + 47 + 32, "N"), TaggedValue(47, "L"), TaggedValue(32, "L") ]) seg3_exp = Segment([ TaggedValue(72 + 92 + 42, "N"), TaggedValue(92, "L"), TaggedValue(42, "L") ]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) assert exp == res
def test_uacc(): # pylint: disable=missing-docstring seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg2, seg3]) pt = PTree(lt) res = pt.uacc(fun.add, fun.idt, fun.add, fun.add, fun.add).to_seq() seg1_exp = Segment([TaggedValue(13 + 31 + 47 + 32 + 72 + 92 + 42, "C")]) seg2_exp = Segment([ TaggedValue(31 + 47 + 32, "N"), TaggedValue(47, "L"), TaggedValue(32, "L") ]) seg3_exp = Segment([ TaggedValue(72 + 92 + 42, "N"), TaggedValue(92, "L"), TaggedValue(42, "L") ]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) if PID == 0 else None assert exp == res
def test_deserialization_2_3(): # btree5 exp = Node( 13, Node(3, Leaf(1), Leaf(1)), Node(9, Node(7, Node(3, Leaf(1), Leaf(1)), Node(3, Leaf(1), Leaf(1))), Leaf(1))) seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg3 = Segment( [TaggedValue(9, "N"), TaggedValue(7, "C"), TaggedValue(1, "L")]) seg4 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg5 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) lt = LTree([seg1, seg2, seg3, seg4, seg5]) res = lt.deserialization() assert res == exp
def test_serialization_2_5(): # btree5 bt = Node( 13, Node(3, Leaf(1), Leaf(1)), Node(9, Node(7, Node(3, Leaf(1), Leaf(1)), Node(3, Leaf(1), Leaf(1))), Leaf(1))) m = 5 res = LTree.init_from_bt(bt, m) seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg3 = Segment( [TaggedValue(9, "N"), TaggedValue(7, "C"), TaggedValue(1, "L")]) seg4 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg5 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) exp = LTree([seg1, seg2, seg3, seg4, seg5]) assert res == exp
def test_map2(): seg11 = Segment([TaggedValue(1, "C")]) seg21 = Segment( [TaggedValue(1, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg31 = Segment( [TaggedValue(1, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) lt1 = LTree([seg11, seg21, seg31]) seg12 = Segment([TaggedValue(2, "C")]) seg22 = Segment( [TaggedValue(2, "N"), TaggedValue(2, "L"), TaggedValue(2, "L")]) seg32 = Segment( [TaggedValue(2, "N"), TaggedValue(2, "L"), TaggedValue(2, "L")]) lt2 = LTree([seg12, seg22, seg32]) res = lt1.map2(operator.add, lt2) seg1_exp = Segment([TaggedValue(3, "C")]) seg2_exp = Segment( [TaggedValue(3, "N"), TaggedValue(3, "L"), TaggedValue(3, "L")]) seg3_exp = Segment( [TaggedValue(3, "N"), TaggedValue(3, "L"), TaggedValue(3, "L")]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) assert res == exp
def test_uacc_illformed(): seg1 = Segment([TaggedValue(13, "C")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg3]) with pytest.raises(IllFormedError): lt.uacc(fun.add, fun.idt, fun.add, fun.add, fun.add)
def test_reduce(): seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg2, seg3]) res = lt.reduce(fun.add, fun.idt, fun.add, fun.add, fun.add) exp = 13 + 31 + 47 + 32 + 72 + 92 + 42 assert res == exp
def test_deserialization_1_1(): # btree4 exp = Node(1, Node(2, Node(4, Leaf(6), Leaf(7)), Node(5, Leaf(8), Leaf(9))), Leaf(3)) seg1 = Segment([TaggedValue(1, "C")]) seg2 = Segment([TaggedValue(2, "C")]) seg3 = Segment([TaggedValue(4, "C")]) seg4 = Segment([TaggedValue(6, "L")]) seg5 = Segment([TaggedValue(7, "L")]) seg6 = Segment([TaggedValue(5, "C")]) seg7 = Segment([TaggedValue(8, "L")]) seg8 = Segment([TaggedValue(9, "L")]) seg9 = Segment([TaggedValue(3, "L")]) lt = LTree([seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8, seg9]) res = lt.deserialization() assert res == exp
def test_reduce(): # pylint: disable=missing-docstring seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg2, seg3]) pt = PTree(lt) res = pt.reduce(fun.add, fun.idt, fun.add, fun.add, fun.add) exp = lt.reduce(fun.add, fun.idt, fun.add, fun.add, fun.add) if PID == 0 else None assert res == exp
def test_serialization_1_1(): # btree4 bt = Node(1, Node(2, Node(4, Leaf(6), Leaf(7)), Node(5, Leaf(8), Leaf(9))), Leaf(3)) m = 1 res = LTree.init_from_bt(bt, m) seg1 = Segment([TaggedValue(1, "C")]) seg2 = Segment([TaggedValue(2, "C")]) seg3 = Segment([TaggedValue(4, "C")]) seg4 = Segment([TaggedValue(6, "L")]) seg5 = Segment([TaggedValue(7, "L")]) seg6 = Segment([TaggedValue(5, "C")]) seg7 = Segment([TaggedValue(8, "L")]) seg8 = Segment([TaggedValue(9, "L")]) seg9 = Segment([TaggedValue(3, "L")]) exp = LTree([seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8, seg9]) assert res == exp
def test_deserialization_1_4(): # btree4 seg1 = Segment([TaggedValue(1, "C")]) seg2 = Segment([TaggedValue(2, "C")]) seg3 = Segment( [TaggedValue(4, "N"), TaggedValue(6, "L"), TaggedValue(7, "L")]) seg4 = Segment( [TaggedValue(5, "N"), TaggedValue(8, "L"), TaggedValue(9, "L")]) seg5 = Segment([TaggedValue(3, "L")]) lt = LTree([seg1, seg2, seg3, seg4, seg5]) res = lt.deserialization() exp = Node(1, Node(2, Node(4, Leaf(6), Leaf(7)), Node(5, Leaf(8), Leaf(9))), Leaf(3)) assert res == exp
def illformed_ltree(): # pylint: disable=missing-docstring seg1 = Segment([TaggedValue(13, "C")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg3]) with pytest.raises(IllFormedError): PTree(lt)
def test_map2_not_same_size(): seg11 = Segment([TaggedValue(13, "C")]) seg21 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg31 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt1 = LTree([seg11, seg21, seg31]) seg12 = Segment([TaggedValue(13, "C")]) seg22 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) lt2 = LTree([seg12, seg22]) with pytest.raises(AssertionError): lt1.map2(operator.add, lt2)
def test_serialization_1_3(): # btree4 bt = Node(1, Node(2, Node(4, Leaf(6), Leaf(7)), Node(5, Leaf(8), Leaf(9))), Leaf(3)) m = 3 res = LTree.init_from_bt(bt, m) seg1 = Segment( [TaggedValue(1, "N"), TaggedValue(2, "C"), TaggedValue(3, "L")]) seg2 = Segment( [TaggedValue(4, "N"), TaggedValue(6, "L"), TaggedValue(7, "L")]) seg3 = Segment( [TaggedValue(5, "N"), TaggedValue(8, "L"), TaggedValue(9, "L")]) exp = LTree([seg1, seg2, seg3]) assert res == exp
def test_map2_not_same_size(): # pylint: disable=missing-docstring seg11 = Segment([TaggedValue(13, "C")]) seg21 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg31 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt1 = LTree([seg11, seg21, seg31]) seg22 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) lt2 = LTree([seg22]) pt1 = PTree(lt1) pt2 = PTree(lt2) with pytest.raises(AssertionError): pt1.zip(pt2)
def test_zip(): # pylint: disable=missing-docstring seg11 = Segment([TaggedValue(1, "C")]) seg21 = Segment( [TaggedValue(1, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg31 = Segment( [TaggedValue(1, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) lt1 = LTree([seg11, seg21, seg31]) seg12 = Segment([TaggedValue(2, "C")]) seg22 = Segment( [TaggedValue(2, "N"), TaggedValue(2, "L"), TaggedValue(2, "L")]) seg32 = Segment( [TaggedValue(2, "N"), TaggedValue(2, "L"), TaggedValue(2, "L")]) lt2 = LTree([seg12, seg22, seg32]) pt1 = PTree(lt1) pt2 = PTree(lt2) res = pt1.zip(pt2).to_seq() seg1_exp = Segment([TaggedValue((1, 2), "C")]) seg2_exp = Segment([ TaggedValue((1, 2), "N"), TaggedValue((1, 2), "L"), TaggedValue((1, 2), "L") ]) seg3_exp = Segment([ TaggedValue((1, 2), "N"), TaggedValue((1, 2), "L"), TaggedValue((1, 2), "L") ]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) if PID == 0 else None assert res == exp
def test_map_not_empty(): seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(31, "N"), TaggedValue(47, "L"), TaggedValue(32, "L")]) seg3 = Segment( [TaggedValue(72, "N"), TaggedValue(92, "L"), TaggedValue(42, "L")]) lt = LTree([seg1, seg2, seg3]) res = lt.map(fun.incr, fun.decr) seg1_exp = Segment([TaggedValue(12, "C")]) seg2_exp = Segment( [TaggedValue(30, "N"), TaggedValue(48, "L"), TaggedValue(33, "L")]) seg3_exp = Segment( [TaggedValue(71, "N"), TaggedValue(93, "L"), TaggedValue(43, "L")]) exp = LTree([seg1_exp, seg2_exp, seg3_exp]) assert res == exp
def to_seq(self): full_content = [] if PID == 0: full_index = self.get_full_index() res = LTree([None] * full_index.length()) full_content.extend(self.__content) for iproc in range(1, NPROCS): full_content.extend(COMM.recv(source=iproc, tag=TAG_TO_SEQ)['c']) for i in range(full_index.length()): (start, offset) = full_index[i] res[i] = full_content[start:start + offset] return res else: COMM.send({'c': self.__content}, dest=0, tag=TAG_TO_SEQ) return None
def _main(): parser = argparse.ArgumentParser() parser.add_argument( "-i", help="name of the file that contains the LTree to split") parser.add_argument( "-o", help="name of the files that will contained the PTree (split)") parser.add_argument("-n", help="how many files we want to get", type=int) args = parser.parse_args() filename_in = args.i filename_out = args.o numfiles = args.n if filename_in[-3:] != ".lt": filename_in = filename_in + ".lt" if filename_out[-3:] != ".pt": filename_out = filename_out + ".pt" linear_tree = LTree.init_from_file(filename_in) _create_pt_files(linear_tree, numfiles, filename_out)
def _test(bin_tree): print(bin_tree) par.at_root(lambda: print("-----")) m_bridge_param = 3 linear_tree = LTree.init_from_bt(bin_tree, m_bridge_param) parallel_tree = PTree(linear_tree) par.at_root(lambda: print(linear_tree)) print(parallel_tree) par.at_root(lambda: print("-----")) res = prefix(parallel_tree) par.at_root(lambda: print("prefix result:")) print(res) par.at_root(lambda: print("-----")) res = size(parallel_tree) par.at_root(lambda: print("size result:")) print(res) par.at_root(lambda: print("-----")) res = size_by_node(parallel_tree) par.at_root(lambda: print("size_by_node result:")) print(res) par.at_root(lambda: print("-----")) res = ancestors(parallel_tree) par.at_root(lambda: print("ancestors result:")) print(res)
def test_dacc_empty(): c = 0 lt = LTree() with pytest.raises(AssertionError): lt.dacc(operator.add, operator.add, c, fun.idt, fun.idt, operator.add, operator.add)
def test_map_empty(): lt = LTree() with pytest.raises(AssertionError): lt.map(fun.idt, fun.idt)
def test_reduce_empty(): lt = LTree() with pytest.raises(AssertionError): lt.reduce(fun.add, fun.idt, fun.add, fun.add, fun.add)
def test_uacc_empty(): lt = LTree() with pytest.raises(AssertionError): lt.uacc(fun.add, fun.idt, fun.add, fun.add, fun.add)