I am writing code that goes through an array and does a calculation on each element, with the results previous calculations being used for future ones.
I have a function that did this one index at a time, step!
, and it works fine, but it’s slow.
@views function step!(arr, U, M, dx, dt)
(Nx, Ny) = size(arr)
I1 = CartesianIndex(1, 1)
for i in 2:(Nx-1)
for j in 2:(Ny-1)
loc = CartesianIndex(j, i)
@inbounds calc!(arr[loc-I1:loc+I1], U, M, dx, dt)
end
end
end
I then wrote a function to calculate the indices that could be done all at the same time using SIMD, but were dependent on a previous calculation. However, this function doesn’t allocate to arr
and I am not sure why.
@views function step_loc!(arr, inds, U, M, dx, dt)
I1 = CartesianIndex(1, 1)
for it in inds
@simd for loc in it
@inbounds calc!(arr[loc-I1:loc+I1], U, M, dx, dt)
end
end
end
I’m wondering what the difference between these two functions is. calc!
hasn’t changed, but it assigns as expected in step!
,. but not in step_loc!
.
calc!
has the signature
@views function calc(arr, U, M, dx, dt)
and assigns specifically to arr[5] by
arr[5] = result
Thank you in advance for the help!