|
| 1 | +using TaskLocalValues |
| 2 | + |
| 3 | +### getindex |
| 4 | + |
| 5 | +struct GetIndex{T,N} <: ArrayOp{T,N} |
| 6 | + input::ArrayOp |
| 7 | + idx::Tuple |
| 8 | +end |
| 9 | + |
| 10 | +GetIndex(input::ArrayOp, idx::Tuple) = |
| 11 | + GetIndex{eltype(input), ndims(input)}(input, idx) |
| 12 | + |
| 13 | +function stage(ctx::Context, gidx::GetIndex) |
| 14 | + inp = stage(ctx, gidx.input) |
| 15 | + |
| 16 | + dmn = domain(inp) |
| 17 | + idxs = [if isa(gidx.idx[i], Colon) |
| 18 | + indexes(dmn)[i] |
| 19 | + else |
| 20 | + gidx.idx[i] |
| 21 | + end for i in 1:length(gidx.idx)] |
| 22 | + |
| 23 | + # Figure out output dimension |
| 24 | + view(inp, ArrayDomain(idxs)) |
| 25 | +end |
| 26 | + |
| 27 | +function size(x::GetIndex) |
| 28 | + map(a -> a[2] isa Colon ? |
| 29 | + size(x.input, a[1]) : length(a[2]), |
| 30 | + enumerate(x.idx)) |> Tuple |
| 31 | +end |
| 32 | + |
| 33 | +Base.getindex(c::ArrayOp, idx::ArrayDomain) = |
| 34 | + _to_darray(GetIndex(c, indexes(idx))) |
| 35 | +Base.getindex(c::ArrayOp, idx...) = |
| 36 | + _to_darray(GetIndex(c, idx)) |
| 37 | + |
| 38 | +const GETINDEX_CACHE = TaskLocalValue{Dict{Tuple,Any}}(()->Dict{Tuple,Any}()) |
| 39 | +const GETINDEX_CACHE_SIZE = ScopedValue{Int}(0) |
| 40 | +with_index_caching(f, size::Integer=1) = with(f, GETINDEX_CACHE_SIZE=>size) |
| 41 | +function Base.getindex(A::DArray{T,N}, idx::NTuple{N,Int}) where {T,N} |
| 42 | + # Scalar indexing check |
| 43 | + assert_allowscalar() |
| 44 | + |
| 45 | + # Boundscheck |
| 46 | + checkbounds(A, idx...) |
| 47 | + |
| 48 | + # Find the associated partition and offset within it |
| 49 | + part_idx, offset_idx = partition_for(A, idx) |
| 50 | + |
| 51 | + # If the partition is cached, use that for lookup |
| 52 | + cache = GETINDEX_CACHE[] |
| 53 | + cache_size = GETINDEX_CACHE_SIZE[] |
| 54 | + if cache_size > 0 && haskey(cache, part_idx) |
| 55 | + return cache[part_idx][offset_idx...] |
| 56 | + end |
| 57 | + |
| 58 | + # Uncached, fetch the partition |
| 59 | + part = fetch(A.chunks[part_idx...]) |
| 60 | + |
| 61 | + # Insert the partition into the cache |
| 62 | + if cache_size > 0 |
| 63 | + if length(cache) >= cache_size |
| 64 | + # Evict a random entry |
| 65 | + key = rand(keys(cache)) |
| 66 | + delete!(cache, key) |
| 67 | + end |
| 68 | + cache[part_idx] = part |
| 69 | + end |
| 70 | + |
| 71 | + # Return the value |
| 72 | + return part[offset_idx...] |
| 73 | +end |
| 74 | +function partition_for(A::DArray, idx::NTuple{N,Int}) where N |
| 75 | + part_idx = zeros(Int, N) |
| 76 | + offset_idx = zeros(Int, N) |
| 77 | + for dim in 1:N |
| 78 | + part_idx_slice = @view part_idx[1:(dim-1)] |
| 79 | + trailing_idx_slice = ntuple(i->Colon(), N-dim) |
| 80 | + sds = @view A.subdomains[part_idx_slice..., :, trailing_idx_slice...] |
| 81 | + for (sd_idx, sd) in enumerate(sds) |
| 82 | + sd_range = (sd.indexes::NTuple{N,UnitRange{Int}})[dim] |
| 83 | + if sd_range.start <= idx[dim] <= sd_range.stop |
| 84 | + part_idx[dim] = sd_idx |
| 85 | + offset_idx[dim] = idx[dim] - sd_range.start + 1 |
| 86 | + break |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + return (part_idx...,), (offset_idx...,) |
| 91 | +end |
| 92 | +Base.getindex(A::DArray, idx::Integer...) = |
| 93 | + getindex(A, idx) |
| 94 | +Base.getindex(A::DArray, idx::Integer) = |
| 95 | + getindex(A, Base._ind2sub(A, idx)) |
| 96 | +Base.getindex(A::DArray, idx::CartesianIndex) = |
| 97 | + getindex(A, Tuple(idx)) |
| 98 | + |
| 99 | +### setindex! |
| 100 | + |
| 101 | +function Base.setindex!(A::DArray{T,N}, value, idx::NTuple{N,Int}) where {T,N} |
| 102 | + # Scalar indexing check |
| 103 | + assert_allowscalar() |
| 104 | + |
| 105 | + # Boundscheck |
| 106 | + checkbounds(A, idx...) |
| 107 | + |
| 108 | + # Find the associated partition and offset within it |
| 109 | + part_idx, offset_idx = partition_for(A, idx) |
| 110 | + |
| 111 | + # If the partition is cached, evict it |
| 112 | + cache = GETINDEX_CACHE[] |
| 113 | + if haskey(cache, part_idx) |
| 114 | + delete!(cache, part_idx) |
| 115 | + end |
| 116 | + |
| 117 | + # Set the value |
| 118 | + part = A.chunks[part_idx...] |
| 119 | + space = memory_space(part) |
| 120 | + scope = Dagger.scope(worker=root_worker_id(space)) |
| 121 | + return fetch(Dagger.@spawn scope=scope setindex!(part, value, offset_idx...)) |
| 122 | +end |
| 123 | +Base.setindex!(A::DArray, value, idx::Integer...) = |
| 124 | + setindex!(A, value, idx) |
| 125 | +Base.setindex!(A::DArray, value, idx::Integer) = |
| 126 | + setindex!(A, value, Base._ind2sub(A, idx)) |
| 127 | +Base.setindex!(A::DArray, value, idx::CartesianIndex) = |
| 128 | + setindex!(A, value, Tuple(idx)) |
| 129 | + |
| 130 | +### Allow/disallow scalar indexing |
| 131 | + |
| 132 | +const ALLOWSCALAR_TASK = TaskLocalValue{Bool}(()->true) |
| 133 | +const ALLOWSCALAR_SCOPE = ScopedValue{Bool}(false) |
| 134 | +isallowscalar() = ALLOWSCALAR_TASK[] || ALLOWSCALAR_SCOPE[] |
| 135 | +function assert_allowscalar() |
| 136 | + if !isallowscalar() |
| 137 | + throw(ArgumentError("Scalar indexing is disallowed\nSee `allowscalar` and `allowscalar!` for ways to disable this check, if necessary")) |
| 138 | + end |
| 139 | +end |
| 140 | +"Allow/disallow scalar indexing for the current task." |
| 141 | +function allowscalar!(allow::Bool=true) |
| 142 | + ALLOWSCALAR_TASK[] = allow |
| 143 | +end |
| 144 | +"Allow/disallow scalar indexing for the duration of executing `f`." |
| 145 | +function allowscalar(f, allow::Bool=true) |
| 146 | + old = ALLOWSCALAR_TASK[] |
| 147 | + allowscalar!(allow) |
| 148 | + try |
| 149 | + return with(f, ALLOWSCALAR_SCOPE=>allow) |
| 150 | + finally |
| 151 | + allowscalar!(old) |
| 152 | + end |
| 153 | +end |
0 commit comments