omlins / ParallelStencil.jl

Package for writing high-level code for parallel high-performance stencil computations that can be deployed on both GPUs and CPUs
BSD 3-Clause "New" or "Revised" License
316 stars 32 forks source link

Type unstable Data.Number #133

Closed albert-de-montserrat closed 9 months ago

albert-de-montserrat commented 9 months ago

Was running some tests and just realised that Data.Number appears to be type unstable. E.g. :

julia> 
       function foo(nx, ny)
           A_eff = (8*2)/1e9*nx*ny*sizeof(Data.Number)
       end
foo (generic function with 2 methods)

julia> @btime foo($20,$20)
  66.888 ns (2 allocations: 32 bytes)
5.12e-5

with


julia> @code_warntype foo(20, 20)
MethodInstance for foo(::Int64, ::Int64)
  from foo(nx, ny) @ Main ~/Desktop/Boris/MagmaThermoKinematics.jl/HydroMech2D_arrays.jl:289
Arguments
  #self#::Core.Const(foo)
  nx::Int64
  ny::Int64
Locals
  A_eff::Any
Body::Any
1 ─ %1 = (8 * 2)::Core.Const(16)
│   %2 = (%1 / 1.0e9)::Core.Const(1.6e-8)
│   %3 = Main.Data.Number::Any
│   %4 = Main.sizeof(%3)::Any
│   %5 = (%2 * nx * ny * %4)::Any
│        (A_eff = %5)
└──      return %5
omlins commented 9 months ago

@albert-de-montserrat Thanks for catching this! Making the type aliases in the Data module constant solves this:

julia> using ParallelStencil
[ Info: Precompiling ParallelStencil [94395366-693c-11ea-3b26-d9b7aac5d958]

julia> @init_parallel_stencil(package=Threads, numbertype=Float64)

julia> function foo(nx, ny)
                  A_eff = (8*2)/1e9*nx*ny*sizeof(Data.Number)
              end
foo (generic function with 1 method)

julia> @code_warntype foo(20, 20)
MethodInstance for foo(::Int64, ::Int64)
  from foo(nx, ny) @ Main REPL[4]:1
Arguments
  #self#::Core.Const(foo)
  nx::Int64
  ny::Int64
Locals
  A_eff::Float64
Body::Float64
1 ─ %1 = (8 * 2)::Core.Const(16)
│   %2 = (%1 / 1.0e9)::Core.Const(1.6e-8)
│   %3 = Main.Data.Number::Core.Const(Float64)
│   %4 = Main.sizeof(%3)::Core.Const(8)
│   %5 = (%2 * nx * ny * %4)::Float64
│        (A_eff = %5)
└──      return %5

PR coming up...

albert-de-montserrat commented 9 months ago

Great thanks!