Skip to content

Commit

Permalink
Make tests consider size arrays (todo: maybe make arrays do not retur…
Browse files Browse the repository at this point in the history
…n size arrays) allocate size arrays only after symbols are allocated
  • Loading branch information
ThrudPrimrose committed Dec 6, 2024
1 parent 8c164a4 commit e76f39d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
33 changes: 17 additions & 16 deletions dace/codegen/targets/framecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,22 +908,6 @@ def generate_code(self,
global_symbols = copy.deepcopy(sdfg.symbols)
global_symbols.update({aname: arr.dtype for aname, arr in sdfg.arrays.items()})

# Allocate size arrays (always check as name and array changes affect size descriptor names)
size_arrays = sdfg.size_arrays()
callsite_stream.write(f'//Declare size arrays\n', sdfg)
for size_desc_name in size_arrays:
size_nodedesc = sdfg.arrays[size_desc_name]
assert ("__return" not in size_desc_name)
ctypedef = size_nodedesc.dtype.ctype
from dace.codegen.targets import cpp
array = [v for v in sdfg.arrays.values() if v.size_desc_name is not None and v.size_desc_name == size_desc_name]
assert (len(array) == 1)
array = array[0]
size_str = ",".join(["0" if cpp.sym2cpp(dim).startswith("__dace_defer") else cpp.sym2cpp(dim) for dim in array.shape])
alloc_str = f'{ctypedef} {size_desc_name}[{size_nodedesc.shape[0]}]{{{size_str}}};\n'
callsite_stream.write(alloc_str)
self.dispatcher.defined_vars.add(size_desc_name, disp.DefinedType.Pointer, ctypedef)

interstate_symbols = {}
for cfr in sdfg.all_control_flow_regions():
for e in cfr.dfs_edges(cfr.start_block):
Expand Down Expand Up @@ -975,6 +959,23 @@ def generate_code(self,

callsite_stream.write('\n', sdfg)

# After the symbols
# Allocate size arrays (always check as name and array changes affect size descriptor names)
size_arrays = sdfg.size_arrays()
callsite_stream.write(f'//Declare size arrays\n', sdfg)
for size_desc_name in size_arrays:
size_nodedesc = sdfg.arrays[size_desc_name]
assert ("__return" not in size_desc_name)
ctypedef = size_nodedesc.dtype.ctype
from dace.codegen.targets import cpp
array = [v for v in sdfg.arrays.values() if v.size_desc_name is not None and v.size_desc_name == size_desc_name]
assert (len(array) == 1)
array = array[0]
size_str = ",".join(["0" if cpp.sym2cpp(dim).startswith("__dace_defer") else cpp.sym2cpp(dim) for dim in array.shape])
alloc_str = f'{ctypedef} {size_desc_name}[{size_nodedesc.shape[0]}]{{{size_str}}};\n'
callsite_stream.write(alloc_str)
self.dispatcher.defined_vars.add(size_desc_name, disp.DefinedType.Pointer, ctypedef)

#######################################################################
# Generate actual program body

Expand Down
2 changes: 1 addition & 1 deletion tests/passes/array_elimination_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def tester(A: dace.float64[20], B: dace.float64[20]):
sdutil.inline_sdfgs(sdfg)
sdutil.fuse_states(sdfg)
Pipeline([ArrayElimination()]).apply_pass(sdfg, {})
assert len(sdfg.arrays) == 4
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 4


def test_merge_simple():
Expand Down
20 changes: 10 additions & 10 deletions tests/passes/lift_struct_views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def test_simple_tasklet_access():
state.add_edge(t1, 'o1', write, None, dace.Memlet('Z'))

assert len(state.nodes()) == 3
assert len(sdfg.arrays) == 2
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 2

res = LiftStructViews().apply_pass(sdfg, {})

assert len(res['A']) == 1
assert len(res['Z']) == 1
assert len(state.nodes()) == 5
assert len(sdfg.arrays) == 4
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 4
assert sdfg.is_valid()


Expand All @@ -55,14 +55,14 @@ def test_sliced_tasklet_access():
state.add_edge(t1, 'o1', write, None, dace.Memlet('Z'))

assert len(state.nodes()) == 3
assert len(sdfg.arrays) == 2
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 2

res = LiftStructViews().apply_pass(sdfg, {})

assert len(res['A']) == 1
assert len(res['Z']) == 1
assert len(state.nodes()) == 5
assert len(sdfg.arrays) == 4
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 4
assert sdfg.is_valid()


Expand Down Expand Up @@ -91,12 +91,12 @@ def test_sliced_multi_tasklet_access():
state.add_edge(t1, 'o1', write, None, dace.Memlet('Z'))

assert len(state.nodes()) == 3
assert len(sdfg.arrays) == 2
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 2

FixedPointPipeline([LiftStructViews()]).apply_pass(sdfg, {})

assert len(state.nodes()) == 9
assert len(sdfg.arrays) == 8
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 8
assert sdfg.is_valid()


Expand All @@ -121,12 +121,12 @@ def test_tasklet_access_to_cont_array():
state.add_edge(t1, 'o1', write, None, dace.Memlet('Z[0]'))

assert len(state.nodes()) == 3
assert len(sdfg.arrays) == 2
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 2

FixedPointPipeline([LiftStructViews()]).apply_pass(sdfg, {})

assert len(state.nodes()) == 7
assert len(sdfg.arrays) == 6
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 6
assert sdfg.is_valid()


Expand Down Expand Up @@ -157,12 +157,12 @@ def test_sliced_multi_tasklet_access_to_cont_array():
state.add_edge(t1, 'o1', write, None, dace.Memlet('Z[0]'))

assert len(state.nodes()) == 3
assert len(sdfg.arrays) == 2
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 2

FixedPointPipeline([LiftStructViews()]).apply_pass(sdfg, {})

assert len(state.nodes()) == 11
assert len(sdfg.arrays) == 10
assert len(sdfg.arrays) - len(sdfg.size_arrays()) == 10
assert sdfg.is_valid()


Expand Down
6 changes: 3 additions & 3 deletions tests/passes/scalar_fission_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_scalar_fission():
# Both interstate edges should be different now.
assert tmp1_edge.assignments != tmp2_edge.assignments
# There should now be 5 arrays in the SDFG, i.e. 2 more than before since two isolated scopes of tmp exist.
assert len(sdfg.arrays.keys()) == 5
assert len(sdfg.arrays.keys()) - len(sdfg.size_arrays()) == 5
# Assert all accesses per scope are identical.
assert all([n.data == list(tmp1_edge.assignments.values())[0] for n in [tmp1_write, loop1_read_tmp]])
assert all([n.data == list(tmp2_edge.assignments.values())[0] for n in [tmp2_write, loop2_read_tmp]])
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_branch_subscopes_nofission():

Pipeline([ScalarFission()]).apply_pass(sdfg, {})

assert set(sdfg.arrays.keys()) == {'A', 'B', 'C'}
assert set(sdfg.arrays.keys()).difference(sdfg.size_arrays()) == {'A', 'B', 'C'}

def test_branch_subscopes_fission():
sdfg = dace.SDFG('branch_subscope_fission')
Expand Down Expand Up @@ -279,7 +279,7 @@ def test_branch_subscopes_fission():

Pipeline([ScalarFission()]).apply_pass(sdfg, {})

assert set(sdfg.arrays.keys()) == {'A', 'B', 'C', 'B_0', 'B_1'}
assert set(sdfg.arrays.keys()).difference(set(sdfg.size_arrays())) == {'A', 'B', 'C', 'B_0', 'B_1'}

if __name__ == '__main__':
test_scalar_fission()
Expand Down

0 comments on commit e76f39d

Please sign in to comment.