@@ -137,27 +137,94 @@ function test_stencil()
137137 end
138138 end
139139
140- @testset " Invalid neighborhood distance" begin
141- A = ones(Blocks(1 , 1 ), Int, 2 , 2 )
142- B = zeros(Blocks(1 , 1 ), Int, 2 , 2 )
143- @test_throws_unwrap ArgumentError Dagger. spawn_datadeps() do
144- @stencil begin
145- B[idx] = sum(@neighbors(A[idx], 0 , Wrap()))
140+ @testset " Tuple neighborhood distance" begin
141+ # 1D case: distance (2,)
142+ @testset " 1D with distance (2,)" begin
143+ A = DArray([1 , 2 , 3 , 4 , 5 , 6 ], Blocks(2 ,))
144+ B = zeros(Blocks(2 ,), Int, 6 )
145+ Dagger. spawn_datadeps() do
146+ @stencil begin
147+ B[idx] = sum(@neighbors(A[idx], (2 ,), Wrap()))
148+ end
146149 end
150+ # For each element, neighbors at distance 2 in 1D: [-2, -1, 0, 1, 2]
151+ # B[1] neighbors: A[5], A[6], A[1], A[2], A[3] (wrapping) = 5+6+1+2+3 = 17
152+ # B[2] neighbors: A[6], A[1], A[2], A[3], A[4] = 6+1+2+3+4 = 16
153+ # B[3] neighbors: A[1], A[2], A[3], A[4], A[5] = 1+2+3+4+5 = 15
154+ # B[4] neighbors: A[2], A[3], A[4], A[5], A[6] = 2+3+4+5+6 = 20
155+ # B[5] neighbors: A[3], A[4], A[5], A[6], A[1] = 3+4+5+6+1 = 19
156+ # B[6] neighbors: A[4], A[5], A[6], A[1], A[2] = 4+5+6+1+2 = 18
157+ expected_B_1d = [17 , 16 , 15 , 20 , 19 , 18 ]
158+ @test collect(B) == expected_B_1d
147159 end
148- @test_throws_unwrap ArgumentError Dagger. spawn_datadeps() do
149- @stencil begin
150- B[idx] = sum(@neighbors(A[idx], - 1 , Wrap()))
160+
161+ # 2D case: distance (1, 2) - different per dimension
162+ @testset " 2D with distance (1, 2)" begin
163+ A = DArray(reshape(1 : 12 , 3 , 4 ), Blocks(1 , 2 ))
164+ B = zeros(Blocks(1 , 2 ), Int, 3 , 4 )
165+ Dagger. spawn_datadeps() do
166+ @stencil begin
167+ B[idx] = sum(@neighbors(A[idx], (1 , 2 ), Wrap()))
168+ end
151169 end
170+ # Distance (1, 2) means:
171+ # - dimension 1 (rows): offsets -1, 0, 1
172+ # - dimension 2 (cols): offsets -2, -1, 0, 1, 2
173+ # Total neighborhood size: 3 * 5 = 15 elements
174+ expected_B_2d = zeros(Int, 3 , 4 )
175+ for i in 1 : 3 , j in 1 : 4
176+ sum_val = 0
177+ for di in - 1 : 1 , dj in - 2 : 2
178+ row = mod1(i+ di, 3 )
179+ col = mod1(j+ dj, 4 )
180+ sum_val += A[row, col]
181+ end
182+ expected_B_2d[i, j] = sum_val
183+ end
184+ @test collect(B) == expected_B_2d
152185 end
153- @test_throws_unwrap ArgumentError Dagger. spawn_datadeps() do
154- @stencil begin
155- B[idx] = sum(@neighbors(A[idx], 1.5 , Wrap()))
186+
187+ # 3D case: distance (1, 2, 1) - different per dimension
188+ @testset " 3D with distance (1, 2, 1)" begin
189+ # Need chunk sizes >= 2*distance+1 for each dimension
190+ # distance (1, 2, 1) requires chunks >= (3, 5, 3)
191+ A = DArray(reshape(1 : 120 , 4 , 5 , 6 ), Blocks(4 , 5 , 3 ))
192+ B = zeros(Blocks(4 , 5 , 3 ), Int, 4 , 5 , 6 )
193+ Dagger. spawn_datadeps() do
194+ @stencil begin
195+ B[idx] = sum(@neighbors(A[idx], (1 , 2 , 1 ), Wrap()))
196+ end
156197 end
198+ # Distance (1, 2, 1) means:
199+ # - dimension 1: offsets -1, 0, 1 (3 elements)
200+ # - dimension 2: offsets -2, -1, 0, 1, 2 (5 elements)
201+ # - dimension 3: offsets -1, 0, 1 (3 elements)
202+ # Total neighborhood size: 3 * 5 * 3 = 45 elements
203+ expected_B_3d = zeros(Int, 4 , 5 , 6 )
204+ for i in 1 : 4 , j in 1 : 5 , k in 1 : 6
205+ sum_val = 0
206+ for di in - 1 : 1 , dj in - 2 : 2 , dk in - 1 : 1
207+ row = mod1(i+ di, 4 )
208+ col = mod1(j+ dj, 5 )
209+ depth = mod1(k+ dk, 6 )
210+ sum_val += A[row, col, depth]
211+ end
212+ expected_B_3d[i, j, k] = sum_val
213+ end
214+ @test collect(B) == expected_B_3d
157215 end
158- @test_throws_unwrap ArgumentError Dagger. spawn_datadeps() do
159- @stencil begin
160- B[idx] = sum(@neighbors(A[idx], 2 , Wrap()))
216+ end
217+
218+ @testset " Invalid neighborhood distance" begin
219+ A = ones(Blocks(1 , 1 ), Int, 2 , 2 )
220+ B = zeros(Blocks(1 , 1 ), Int, 2 , 2 )
221+ for value in [0 , - 1 , 1.5 , 2 ]
222+ for dist in [value, (value,)]
223+ @test_throws_unwrap ArgumentError Dagger. spawn_datadeps() do
224+ @stencil begin
225+ B[idx] = sum(@neighbors(A[idx], value, Wrap()))
226+ end
227+ end
161228 end
162229 end
163230 end
196263 end
197264 end
198265end
266+
0 commit comments