Thanks mauro for the idea; it looks neat; though I personally like the optional parameters with ;
better, but that’s just a personal taste; I wrote a wrapper for that, and this part now seems to work fine.
Interestingly combining this solution here with one from my previous questions yields a further point I can’t get my mind to (still learning all this stuff though). When indexing an neighbor index, as I learned here, i adapted
abstract myAbsT
immutable myT <: myAbsT
v::Float64
end
function myFun{T <: myAbsT,N}(
f::Array{T,N}, α::Array{Float64,1},MaxIterations,myMask::BitArray{N}=falses(f))::Array{T,N}
iter::Int64 = 1
x::Array{T,N} = f
R = CartesianRange(size(f))
while ( iter < MaxIterations )
iter = iter+1
for d = 1:ndims(f)
unitOffset::CartesianIndex{N} = CartesianIndex{N}(ntuple(i::Integer->i::Integer==d ? 1: 0, N))
for i in R
i2 = i+unitOffset
if (i2 in R) && (~myMask[i])
x[i] = T(x[i2].v+x[i].v)
end
end
end
end
return x
end
preF = ones(2^9,2^9)
@time myFun(myT.(preF), [0.2],200)
@code_warntype myFun(myT.(preF), [0.2],200)
@time myFun(myT.(preF), [0.2],200,falses(preF))
@code_warntype myFun(myT.(preF), [0.2],200,falses(preF))
While the first call/warntype works completely fine (maybe because the mask even gets eliminated in optimization), for the second call (well it’s the same values) the d
iterate gets Core.Box
as a type. Still for this example (not for my larger run on the productive code) both are qually fast and the second oneeven uses only 1.6k allocations while the first requires 39k. Can anyone explain why?
I know I’m asking a lot of questions, for now I’m still trying to understand such details of Julia (coming from Matlab, Mathematica, and such).