Speeding up a function

Thanks a ton. I only have a problem with

@views @inbounds function foo2_t!(num,market_ids)
    Threads.@threads for id ∈ market_ids
        s = sum(num[:,id])
        num[:,id] ./= s+1
    end
end

This introduces deviations. Do you know why?

i.e., compare this

function foo1_t!(num,market_ids)
    @inbounds for id ∈ market_ids
        @views s = sum(num[:,id])
        @views num[:,id] ./= s+1
    end
end
@views @inbounds function foo2_t!(num,market_ids)
    Threads.@threads for id ∈ market_ids
        s = sum(num[:,id])
        num[:,id] ./= s+1
    end
end
mat_1_t = similar(randvar_nu_t)
mat_2_t = similar(randvar_nu_t)
bar_t!(mat_1_t,randvar_nu_t,delta)
bar_t!(mat_2_t,randvar_nu_t,delta)
mat_1_t == mat_2_t
foo1_t!(mat_1_t, market_ids)
foo2_t!(mat_2_t, market_ids)
mat_1_t == mat_2_t

You can see, that the two results are not the same at the end, but they are after the bar_t!() function.

Does not matter too much, you helped me a ton! I just thought it is weird that somehow Threads.@threads changes the result.