Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix internal subscript access if already existed #1228

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions dace/frontend/python/newast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3020,23 +3020,25 @@ def _add_read_access(self,
arr_type: data.Data = None):
if name in self.sdfg.arrays:
return (name, None)
elif (name, rng, 'w') in self.accesses:
return self.accesses[(name, rng, 'w')]
elif (name, rng, 'r') in self.accesses:
return self.accesses[(name, rng, 'r')]
elif name in self.variables:
return (self.variables[name], None)

if (name, rng, 'w') in self.accesses:
new_name, new_rng = self.accesses[(name, rng, 'w')]
elif (name, rng, 'r') in self.accesses:
new_name, new_rng = self.accesses[(name, rng, 'r')]
elif name in self.scope_vars:
new_name, new_rng = self._add_access(name, rng, 'r', target, new_name, arr_type)
full_rng = subsets.Range.from_array(self.sdfg.arrays[new_name])
if (_subset_has_indirection(rng, self) or _subset_is_local_symbol_dependent(rng, self)):
new_name, new_rng = self.make_slice(new_name, rng)
elif full_rng != new_rng:
new_name, new_rng = self.make_slice(new_name, new_rng)
return (new_name, new_rng)
else:
raise NotImplementedError

full_rng = subsets.Range.from_array(self.sdfg.arrays[new_name])
if (_subset_has_indirection(rng, self) or _subset_is_local_symbol_dependent(rng, self)):
new_name, new_rng = self.make_slice(new_name, rng)
elif full_rng != new_rng:
new_name, new_rng = self.make_slice(new_name, new_rng)
return (new_name, new_rng)

def _add_write_access(self,
name: str,
rng: subsets.Range,
Expand Down
31 changes: 30 additions & 1 deletion tests/numpy/subarray_in_nested_call_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2019-2021 ETH Zurich and the DaCe authors. All rights reserved.
# Copyright 2019-2023 ETH Zurich and the DaCe authors. All rights reserved.
import math
import numpy as np
import dace

Expand Down Expand Up @@ -45,6 +46,34 @@ def test_inout_connector():
assert np.allclose(a, ref)


def test_indirect_symbolic_access():

@dace.program
def tester(a: dace.float64[20], b: dace.float64[3], c: dace.float64[19]):
for i in dace.map[0:10]:
xtmp: dace.float64 = 0

local_offset_i = (i + 1) % 2

if local_offset_i < 3 % 2:
for kx in dace.unroll(range(math.ceil(3 / 2))):
ind_i = (i + 1 - kx * 2) // 2
kind_i = local_offset_i + kx * 2
if ind_i >= 0 and ind_i < 20:
xtmp += a[ind_i] * b[kind_i]

c[i] = xtmp

a = np.random.rand(20)
b = np.random.rand(15)
c = np.random.rand(10)
refc = np.copy(c)
tester.f(a, b, refc)
tester(a, b, c)
assert np.allclose(c, refc)


if __name__ == '__main__':
test()
test_inout_connector()
test_indirect_symbolic_access()
19 changes: 19 additions & 0 deletions tests/python_frontend/indirections_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ def test_indirection_scalar_nsdfg():
assert (np.allclose(res, A[x]))


@dc.program
def indirection_scalar2_nsdfg(A: dc.float64[10], x: dc.int32[10]):
B = np.empty_like(A)
for i in dc.map[0:A.shape[0]]:
a = x[i]
B[i] = A[a]
B[i] = A[a]
return B


def test_indirection_scalar2_nsdfg():
A = np.random.randn(10).astype(np.float64)
x = np.random.randint(0, 10, size=(10, ), dtype=np.int32)
res = indirection_scalar2_nsdfg(A, x)
assert (np.allclose(res, A[x]))


@dc.program
def indirection_scalar_assign_nsdfg(A: dc.float64[10], x: dc.int32[10]):
B = np.empty_like(A)
Expand Down Expand Up @@ -169,6 +186,7 @@ def test_indirection_scalar_range():


def test_indirection_scalar_range_nsdfg():

@dc.program
def indirection_scalar_range_nsdfg(A: dc.float64[10], x: dc.int32[11]):
B = np.empty_like(A)
Expand Down Expand Up @@ -374,6 +392,7 @@ def test_spmv():
test_indirection_scalar_assign()
test_indirection_scalar_augassign()
test_indirection_scalar_nsdfg()
test_indirection_scalar2_nsdfg()
test_indirection_scalar_assign_nsdfg()
test_indirection_scalar_augassign_nsdfg()
test_indirection_scalar_multi()
Expand Down