|
1 | | -import Dagger: @stencil, Wrap, Pad |
| 1 | +import Dagger: @stencil, Wrap, Pad, Reflect |
2 | 2 |
|
3 | 3 | function test_stencil() |
4 | 4 | @testset "Simple assignment" begin |
@@ -67,6 +67,99 @@ function test_stencil() |
67 | 67 | @test collect(B) == expected_B_pad |
68 | 68 | end |
69 | 69 |
|
| 70 | + @testset "Reflect boundary (symmetric)" begin |
| 71 | + # Test symmetric reflection (edge element IS included/repeated) |
| 72 | + # For A = [1, 2, 3, 4] with Reflect(true): |
| 73 | + # idx=0 → 1, idx=-1 → 2 (reflection includes edge) |
| 74 | + # idx=5 → 4, idx=6 → 3 (reflection includes edge) |
| 75 | + A = DArray([1, 2, 3, 4], Blocks(2)) |
| 76 | + B = zeros(Blocks(2), Int, 4) |
| 77 | + Dagger.spawn_datadeps() do |
| 78 | + @stencil begin |
| 79 | + B[idx] = sum(@neighbors(A[idx], 1, Reflect(true))) |
| 80 | + end |
| 81 | + end |
| 82 | + # B[1]: neighbors at indices 0, 1, 2 -> reflected 0 becomes 1, so [1, 1, 2] = 4 |
| 83 | + # B[2]: neighbors at indices 1, 2, 3 -> [1, 2, 3] = 6 |
| 84 | + # B[3]: neighbors at indices 2, 3, 4 -> [2, 3, 4] = 9 |
| 85 | + # B[4]: neighbors at indices 3, 4, 5 -> reflected 5 becomes 4, so [3, 4, 4] = 11 |
| 86 | + expected_B_symm = [4, 6, 9, 11] |
| 87 | + @test collect(B) == expected_B_symm |
| 88 | + end |
| 89 | + |
| 90 | + @testset "Reflect boundary (mirror)" begin |
| 91 | + # Test mirror reflection (edge element NOT included/repeated) |
| 92 | + # For A = [1, 2, 3, 4] with Reflect(false): |
| 93 | + # idx=0 → 2, idx=-1 → 3 (reflection skips edge) |
| 94 | + # idx=5 → 3, idx=6 → 2 (reflection skips edge) |
| 95 | + A = DArray([1, 2, 3, 4], Blocks(2)) |
| 96 | + B = zeros(Blocks(2), Int, 4) |
| 97 | + Dagger.spawn_datadeps() do |
| 98 | + @stencil begin |
| 99 | + B[idx] = sum(@neighbors(A[idx], 1, Reflect(false))) |
| 100 | + end |
| 101 | + end |
| 102 | + # B[1]: neighbors at indices 0, 1, 2 -> reflected 0 becomes 2, so [2, 1, 2] = 5 |
| 103 | + # B[2]: neighbors at indices 1, 2, 3 -> [1, 2, 3] = 6 |
| 104 | + # B[3]: neighbors at indices 2, 3, 4 -> [2, 3, 4] = 9 |
| 105 | + # B[4]: neighbors at indices 3, 4, 5 -> reflected 5 becomes 3, so [3, 4, 3] = 10 |
| 106 | + expected_B_mirror = [5, 6, 9, 10] |
| 107 | + @test collect(B) == expected_B_mirror |
| 108 | + end |
| 109 | + |
| 110 | + @testset "Reflect boundary 2D (symmetric)" begin |
| 111 | + # Test 2D symmetric reflection with a gradient pattern |
| 112 | + A = DArray(reshape(1:16, 4, 4), Blocks(2, 2)) |
| 113 | + B = zeros(Blocks(2, 2), Int, 4, 4) |
| 114 | + Dagger.spawn_datadeps() do |
| 115 | + @stencil begin |
| 116 | + B[idx] = sum(@neighbors(A[idx], 1, Reflect(true))) |
| 117 | + end |
| 118 | + end |
| 119 | + # Symmetric: idx < 1 → 1 - idx, idx > size → 2*size + 1 - idx |
| 120 | + A_collected = collect(A) |
| 121 | + expected_B_symm = zeros(Int, 4, 4) |
| 122 | + for i in 1:4, j in 1:4 |
| 123 | + sum_val = 0 |
| 124 | + for di in -1:1, dj in -1:1 |
| 125 | + ni, nj = i + di, j + dj |
| 126 | + # Apply symmetric reflection logic |
| 127 | + # For symmetric: idx < 1 → 1 - idx, idx > size → 2*size + 1 - idx |
| 128 | + ni = ni < 1 ? 1 - ni : (ni > 4 ? 2*4 + 1 - ni : ni) |
| 129 | + nj = nj < 1 ? 1 - nj : (nj > 4 ? 2*4 + 1 - nj : nj) |
| 130 | + sum_val += A_collected[ni, nj] |
| 131 | + end |
| 132 | + expected_B_symm[i, j] = sum_val |
| 133 | + end |
| 134 | + @test collect(B) == expected_B_symm |
| 135 | + end |
| 136 | + |
| 137 | + @testset "Reflect boundary 2D (mirror)" begin |
| 138 | + # Test 2D mirror reflection with a gradient pattern |
| 139 | + A = DArray(reshape(1:16, 4, 4), Blocks(2, 2)) |
| 140 | + B = zeros(Blocks(2, 2), Int, 4, 4) |
| 141 | + Dagger.spawn_datadeps() do |
| 142 | + @stencil begin |
| 143 | + B[idx] = sum(@neighbors(A[idx], 1, Reflect(false))) |
| 144 | + end |
| 145 | + end |
| 146 | + # Mirror: idx < 1 → 2 - idx, idx > size → 2*size - idx |
| 147 | + A_collected = collect(A) |
| 148 | + expected_B_mirror = zeros(Int, 4, 4) |
| 149 | + for i in 1:4, j in 1:4 |
| 150 | + sum_val = 0 |
| 151 | + for di in -1:1, dj in -1:1 |
| 152 | + ni, nj = i + di, j + dj |
| 153 | + # Apply mirror reflection logic |
| 154 | + ni = ni < 1 ? 2 - ni : (ni > 4 ? 2*4 - ni : ni) |
| 155 | + nj = nj < 1 ? 2 - nj : (nj > 4 ? 2*4 - nj : nj) |
| 156 | + sum_val += A_collected[ni, nj] |
| 157 | + end |
| 158 | + expected_B_mirror[i, j] = sum_val |
| 159 | + end |
| 160 | + @test collect(B) == expected_B_mirror |
| 161 | + end |
| 162 | + |
70 | 163 | @testset "Multiple expressions" begin |
71 | 164 | A = zeros(Blocks(2, 2), Int, 4, 4) |
72 | 165 | B = zeros(Blocks(2, 2), Int, 4, 4) |
|
0 commit comments