I was finding that when I run the model from the Julia REPL that rarely I get the correct results and other most of the times the results did not make sense (e.g. NaN in the computed Array).
I found that the problem was caused by initializing an empty array such as:
WaterBalance =Array{Float64}(undef, 50000)
I needed to perform a simple loop.
âEvaporation= Array{Float64}(undef, 50000)
function GLOBAL()
for iT=2:N_iT
âŚ
âEvaporation[iT] = âEvaporation[iT-1] + discret.ÎZ[N_iZevapo] * Qevaporation[iT] * ÎT[iT]
end
I required that
âEvaporation [1] = 0.0
For reasons which I do not understand the solution I found was to initialize the Array by using zeros:
WaterBalance = zeros(Float64, 50000)
So my understanding is that there is a bug in Array.
While that is theoretically possible, it is much more likely that there is a bug in your code â you fail to overwrite some elements, and an Array{T}(undef, ...) can have random memory contents.
Isolating an MWE would be helpful, try to simplify your problem so that you can post it here.
Exactly. Your routine starts with iT = 2 (from iT=2:N_iT) and then access the first element in âEvaporation[iT-1].
As @Tamas_Papp and @greg_plowman pointed out, undef will not initialise anything in your array, so you will get a random piece of memory.
In fact, you can see below that the probability to get a zero with an undef-Array with length 100000 on my machine is around 6% (YMMV and especially there are some memory management things going on which will make this not-so-predictable), so I guess that roughly reflects your observations:
I was finding that when I run the model from the Julia REPL that rarely I get the correct results and other most of the times the results did not make sense ( e.g. NaN in the computed Array ).
julia> sum(iszero.(Array{Float64}(undef, 100000))). # in a fresh REPL session
6325
Note that executing this again indicates some kind of caching:
If you create an array with undef âelementsâ, you basically instruct your computer to allocate that amount of memory without caring whatâs inside it. If you use zeros however, you tell that you want to fill them with zeros after the allocation (you initialise it).
Initialisation to a specific value takes an extra amount of time and there are cases where this might be performance relevant.
It depends on your algorithm whether you explicitly need initial values or not. If you use that array to dump values in it and you are sure that by design you will never (read) access any elements of them which are uninitialised â which will lead to unexpected values/behaviour â you can go with undef and squeeze a bit more performance out of the computer. This however is clearly not true for you routine above.