Skip to content

Commit

Permalink
Update stencil2coo kernels for shifts > 1 (#442)
Browse files Browse the repository at this point in the history
Fix the indexing in all kernels in `stencil2coo_kernels.py` so that the
method `toarray` works for `StencilMatrix` objects in 1D/2D/3D with
`shift` larger than 1.

The unit tests for `toarray` in `test_stencil_matrix.py` have been
parametrized to run with `shift` values of 1 and 2.

---------

Co-authored-by: Yaman Güçlü <yaman.guclu@gmail.com>
  • Loading branch information
maxlin-ipp and yguclu authored Sep 27, 2024
1 parent 7306f23 commit 4ac671e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
24 changes: 12 additions & 12 deletions psydac/linalg/kernels/stencil2coo_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def stencil2coo_1d_C(A:'T[:,:]', data:'T[:]', rows:'int64[:]', cols:'int64[:]',
for j1 in range(ncl1):
value = A[i1+pp1,j1]
if abs(value) == 0.0:continue
J = ((I//cm1)*dm1+j1-dp1)%nc1
J = ((I*dm1//cm1)+j1-dp1)%nc1
rows[nnz] = I
cols[nnz] = J
data[nnz] = value
Expand All @@ -38,7 +38,7 @@ def stencil2coo_1d_F(A:'T[:,:]', data:'T[:]', rows:'int64[:]', cols:'int64[:]',
for j1 in range(ncl1):
value = A[i1+pp1,j1]
if abs(value) == 0.0:continue
J = ((I//cm1)*dm1+j1-dp1)%nc1
J = ((I*dm1//cm1)+j1-dp1)%nc1
rows[nnz] = I
cols[nnz] = J
data[nnz] = value
Expand Down Expand Up @@ -66,8 +66,8 @@ def stencil2coo_2d_C(A:'T[:,:,:,:]', data:'T[:]', rows:'int64[:]', cols:'int64[:
for j2 in range(ncl2):
value = A[i1+pp1,i2+pp2,j1,j2]
if abs(value) == 0.0:continue
jj1 = ((ii1//cm1)*dm1+j1-dp1)%nc1
jj2 = ((ii2//cm2)*dm2+j2-dp2)%nc2
jj1 = ((ii1*dm1//cm1)+j1-dp1)%nc1
jj2 = ((ii2*dm2//cm2)+j2-dp2)%nc2

J = jj1*nc2 + jj2

Expand Down Expand Up @@ -97,8 +97,8 @@ def stencil2coo_2d_F(A:'T[:,:,:,:]', data:'T[:]', rows:'int64[:]', cols:'int64[:
for j2 in range(ncl2):
value = A[i1+pp1,i2+pp2,j1,j2]
if abs(value) == 0.0:continue
jj1 = ((ii1//cm1)*dm1+j1-dp1)%nc1
jj2 = ((ii2//cm2)*dm2+j2-dp2)%nc2
jj1 = ((ii1*dm1//cm1)+j1-dp1)%nc1
jj2 = ((ii2*dm2//cm2)+j2-dp2)%nc2

J = jj2*nc1 + jj1

Expand Down Expand Up @@ -132,9 +132,9 @@ def stencil2coo_3d_C(A:'T[:,:,:,:,:,:]', data:'T[:]', rows:'int64[:]', cols:'int
for j3 in range(ncl3):
value = A[i1+pp1,i2+pp2,i3+pp3,j1,j2,j3]
if abs(value) == 0.0:continue
jj1 = ((ii1//cm1)*dm1+j1-dp1)%nc1
jj2 = ((ii2//cm2)*dm2+j2-dp2)%nc2
jj3 = ((ii3//cm3)*dm3+j3-dp3)%nc3
jj1 = ((ii1*dm1//cm1)+j1-dp1)%nc1
jj2 = ((ii2*dm2//cm2)+j2-dp2)%nc2
jj3 = ((ii3*dm3//cm3)+j3-dp3)%nc3

J = jj1*nc2*nc3 + jj2*nc3 + jj3

Expand Down Expand Up @@ -170,9 +170,9 @@ def stencil2coo_3d_F(A:'T[:,:,:,:,:,:]', data:'T[:]', rows:'int64[:]', cols:'int
for j3 in range(ncl3):
value = A[i1+pp1,i2+pp2,i3+pp3,j1,j2,j3]
if abs(value) == 0.0:continue
jj1 = ((ii1//cm1)*dm1+j1-dp1)%nc1
jj2 = ((ii2//cm2)*dm2+j2-dp2)%nc2
jj3 = ((ii3//cm3)*dm3+j3-dp3)%nc3
jj1 = ((ii1*dm1//cm1)+j1-dp1)%nc1
jj2 = ((ii2*dm2//cm2)+j2-dp2)%nc2
jj3 = ((ii3*dm3//cm3)+j3-dp3)%nc3

J = jj3*nc1*nc2 + jj2*nc1 + jj1

Expand Down
8 changes: 4 additions & 4 deletions psydac/linalg/tests/test_stencil_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def test_stencil_matrix_2d_serial_spurious_entries( dtype, p1, p2, s1, s2, P1, P
@pytest.mark.parametrize('dtype', [float])
@pytest.mark.parametrize('n1', [7])
@pytest.mark.parametrize('p1', [1,2])
@pytest.mark.parametrize('s1', [1])
@pytest.mark.parametrize('s1', [1, 2])
@pytest.mark.parametrize('P1', [True, False])
def test_stencil_matrix_1d_serial_toarray( dtype, n1, p1, s1, P1):
# Select non-zero values based on diagonal index
Expand Down Expand Up @@ -489,8 +489,8 @@ def test_stencil_matrix_1d_serial_toarray( dtype, n1, p1, s1, P1):
@pytest.mark.parametrize('n2', [8, 7])
@pytest.mark.parametrize('p1', [1, 2])
@pytest.mark.parametrize('p2', [1, 3])
@pytest.mark.parametrize('s1', [1])
@pytest.mark.parametrize('s2', [1])
@pytest.mark.parametrize('s1', [1, 2])
@pytest.mark.parametrize('s2', [1, 2])
@pytest.mark.parametrize('P1', [True, False])
@pytest.mark.parametrize('P2', [True, False])
def test_stencil_matrix_2d_serial_toarray( dtype, n1, n2, p1, p2, s1, s2, P1, P2):
Expand Down Expand Up @@ -2191,7 +2191,7 @@ def test_stencil_matrix_2d_serial_backend_switch(dtype, n1, n2, p1, p2, s1, s2,
@pytest.mark.parametrize('dtype', [float, complex])
@pytest.mark.parametrize('n1', [20, 67])
@pytest.mark.parametrize('p1', [1, 2, 3])
@pytest.mark.parametrize('sh1', [1])
@pytest.mark.parametrize('sh1', [1, 2])
@pytest.mark.parametrize('P1', [True, False])
@pytest.mark.parallel
def test_stencil_matrix_1d_parallel_toarray(dtype, n1, p1, sh1, P1):
Expand Down

0 comments on commit 4ac671e

Please sign in to comment.