JuliaPhysics / JuliaPhysics/Unitful.jl

cannot assign to ustriped AbstractArrays

Open
#644 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
675
Forks
124
Avg merge
3h 38m
Merged PRs (30d)
1

Description

it sure would be nice if Unitful played better with CuArrays. for example, currently, one cannot `ustrip` a `CuArray` and then assign to it:

```
julia> using Unitful, CUDA

julia> c = CuArray(zeros(3) * 1u"s")
3-element CuArray{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, 1, CUDA.Mem.DeviceBuffer}:
0.0 s
0.0 s
0.0 s

julia> ustrip(c)[1]=1 # innocuous warning about scalar indexing removed for succinctness
1

julia> c
3-element CuArray{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, 1, CUDA.Mem.DeviceBuffer}:
0.0 s
0.0 s
0.0 s
```

however, if i simply add a new method identical to [this one](https://github.com/PainterQubits/Unitful.jl/blob/9e329e8e7200b3e42e0a6676d2948c66d35061b4/src/utils.jl#L78) but with `CuArray`, then it works fine:

```
julia> import Unitful: ustrip

julia> @inline ustrip(A::CuArray{Q}) where {Q <: Quantity} = reinterpret(Unitful.numtype(Q), A)
ustrip (generic function with 16 methods)

julia> ustrip(c)[1]=1
1

julia> c
3-element CuArray{Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, 1, CUDA.Mem.DeviceBuffer}:
1.0 s
0.0 s
0.0 s
```

to be all inclusive, i think the right way to go here is just use `AbstractArray`. then it'll work too for MtlArray, ROCArray, etc.

_Originally posted by @bjarthur in https://github.com/PainterQubits/Unitful.jl/issues/630#issuecomment-1485550726_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with src/utils.jl at the linked ustrip method and inspect how its array dispatch is defined. Confirm the behavior with the CuArray example; done when ustrip permits assignment through the returned array while preserving the original quantity array, including other AbstractArray implementations mentioned in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.