exanauts / ExaPF.jl

A Power Flow Solver for GPUs in Julia
MIT License
58 stars 5 forks source link

Get rid of uninitialized Arrays #249

Closed michel2323 closed 2 years ago

michel2323 commented 2 years ago

Removed undef everywhere. Uninitialized variables can lead to bugs in the derivatives that are very hard to debug. I would be in favor or not using undef as a matter of style.

codecov-commenter commented 2 years ago

Codecov Report

Merging #249 (bc65d1b) into master (be0d4e7) will decrease coverage by 0.04%. The diff coverage is 80.95%.

@@            Coverage Diff             @@
##           master     #249      +/-   ##
==========================================
- Coverage   75.62%   75.57%   -0.05%     
==========================================
  Files          28       28              
  Lines        3462     3456       -6     
==========================================
- Hits         2618     2612       -6     
  Misses        844      844              
Impacted Files Coverage Δ
src/PowerSystem/parsers/parse_con.jl 0.00% <0.00%> (ø)
src/PowerSystem/parsers/parse_rop.jl 0.00% <0.00%> (ø)
src/LinearSolvers/preconditioners.jl 81.92% <100.00%> (-0.21%) :arrow_down:
src/Polar/functions.jl 95.96% <100.00%> (ø)
src/Polar/newton.jl 86.79% <100.00%> (ø)
src/Polar/stacks.jl 100.00% <100.00%> (ø)
src/PowerSystem/parsers/parse_psse.jl 100.00% <100.00%> (ø)
src/PowerSystem/parsers/parse_raw.jl 95.50% <100.00%> (ø)

:mega: Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

michel2323 commented 2 years ago

@frapac With

using CUDA
using BenchmarkTools
AT = CuArray{Float64}

function exalloc(VD::Type, dims...)
    arr = VD(undef, dims...)
    fill!(arr, zero(eltype(VD)))
    return arr
end

@btime cux1 = exalloc(AT, (10,))
@btime cux2 = CuArray(zeros(eltype(AT),10))

I get:

  5.161 μs (29 allocations: 1.23 KiB)
  6.669 μs (5 allocations: 304 bytes)

I think the GC and CUDA.jl are smarter than that, no? Maybe, we don't need the fill!.

This leads to the same number of allocations though:

@inline function exalloc(VD::Type, dims...)
    arr = VD(zeros(eltype(VD),dims...))
    return arr
end

However, I admit, I'm not a fan of adding another layer just for that, even if it adds duplicates throughout the code.

michel2323 commented 2 years ago

Revisit some other time I'd say.