Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type instability in CFtransformdata! #28

Closed
tiemvanderdeure opened this issue Jan 16, 2025 · 1 comment
Closed

Type instability in CFtransformdata! #28

tiemvanderdeure opened this issue Jan 16, 2025 · 1 comment

Comments

@tiemvanderdeure
Copy link
Contributor

Just following up on JuliaGeo/NCDatasets.jl#273 and moving it over here since it's actually a CommonDataModel problem.

CFtransformdata is not type stable if the eltype of data and out are not the same.

using CommonDataModel
(fv, scale_factor, add_offset, time_origin, time_factor, maskingvalue) = ((1f0,), nothing, nothing, nothing, nothing, missing)
A = rand(1000,1000)
CommonDataModel.CFtransformdata(A, fv, scale_factor, add_offset, time_origin, time_factor, maskingvalue, Union{Float64, Missing});
@time CommonDataModel.CFtransformdata(A, fv, scale_factor, add_offset, time_origin, time_factor, maskingvalue, Union{Float64, Missing});

shows 0.082064 seconds (2.00 M allocations: 39.093 MiB, 18.78% gc time).

For some reason this doesn't always show up on actual file reads, but if there is a time dimension it does:

using Rasters, NCDatasets, Dates
ras = Raster(rand(X(1:100), Y(1:100), Ti(DateTime(2000):Year(1):DateTime(2000)))) |> replace_missing

fname = tempname() * ".nc"
write(fname, ras)
Raster(fname); @time Raster(fname);
write(fname, ras[Ti = 1], force = true)
Raster(fname); @time Raster(fname);

Returns

  0.004901 seconds (21.19 k allocations: 635.992 KiB)
  0.004241 seconds (1.03 k allocations: 298.234 KiB)

Note that the time dimension here has length 1 so this really should be the same.

This is all fixed after dropping the @inline in

@inline function CFtransformdata!(out,data::AbstractArray{T,N},fv,scale_factor,add_offset,time_origin,time_factor,maskingvalue) where {T,N}

I'll make a PR with that change, but maybe there are other/better fixes? I don't understand at all why the time dimension makes a difference and neither @code_warntype nor Cthulhu flag this.

@Alexander-Barth
Copy link
Member

Thanks again for the PR. I am closing the issue. Feel free to reopen if the issue persists :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants