-
Notifications
You must be signed in to change notification settings - Fork 98
/
LazyArrays.jl
404 lines (337 loc) · 10.4 KB
/
LazyArrays.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
lazy_map(f,a::AbstractArray...) -> AbstractArray
Applies the `Map` (or `Function`) `f` to the entries of the arrays in `a`
(see the definition of [`Map`](@ref)).
The resulting array `r` is such that `r[i]` equals to `evaluate(f,ai...)` where `ai`
is the tuple containing the `i`-th entry of the arrays in `a` (see function
[`evaluate`](@ref) for more details).
In other words, the resulting array is numerically equivalent to:
map( (x...)->evaluate(f,x...), a...)
# Examples
Using a function as mapping
```jldoctest
using Gridap.Arrays
a = collect(0:5)
b = collect(10:15)
c = lazy_map(+,a,b)
println(c)
# output
[10, 12, 14, 16, 18, 20]
```
Using a user-defined mapping
```jldoctest
using Gridap.Arrays
import Gridap.Arrays: evaluate!
a = collect(0:5)
b = collect(10:15)
struct MySum <: Map end
evaluate!(cache,::MySum,x,y) = x + y
k = MySum()
c = lazy_map(k,a,b)
println(c)
# output
[10, 12, 14, 16, 18, 20]
```
"""
function lazy_map(k,f::AbstractArray...)
fi = map(testitem,f)
T = return_type(k, fi...)
lazy_map(k,T,f...)
end
#lazy_map(::typeof(evaluate),k::AbstractArray,f::AbstractArray...) = LazyArray(k,f...)
# This is the function to be overload to specialize on the Map f
"""
lazy_map(f,::Type{T},a::AbstractArray...) where T
Like [`lazy_map(f,a::AbstractArray...)`](@ref), but the user provides the element type
of the resulting array in order to circumvent type inference.
"""
function lazy_map(k,T::Type,f::AbstractArray...)
s = _common_size(f...)
lazy_map(evaluate,T,Fill(k, s), f...)
end
# This is the function to be overload to specialize on the array types
function lazy_map(::typeof(evaluate),T::Type,k::AbstractArray,f::AbstractArray...)
s = _common_size(k,f...)
N = length(s)
LazyArray(T,Val(N),k,f...)
end
"""
Subtype of `AbstractArray` which is the result of `lazy_map`. It represents the
result of lazy_mapping a `Map` to a set of arrays that
contain the mapping arguments. This struct makes use of the cache provided
by the mapping in order to compute its indices (thus allowing to prevent
allocation). The array is lazy, i.e., the values are only computed on
demand. It extends the `AbstractArray` API with two methods:
`array_cache(a::AbstractArray)`
`getindex!(cache,a::AbstractArray,i...)`
"""
struct LazyArray{G,T,N,F} <: AbstractArray{T,N}
maps::G
args::F
function LazyArray(::Type{T}, g::AbstractArray, f::AbstractArray...) where T
G = typeof(g)
F = typeof(f)
N = ndims(g)
new{G,T,N,F}(g, f)
end
function LazyArray(::Type{T},::Val{N}, g::AbstractArray, f::AbstractArray...) where {T,N}
@check ndims(g) == N || N == 1
G = typeof(g)
F = typeof(f)
new{G,T,N,F}(g, f)
end
end
#function LazyArray(g::AbstractArray{S}, f::AbstractArray...) where S
# isconcretetype(S) ? gi = testitem(g) : @notimplemented
# fi = map(testitem,f)
# T = return_type(gi, fi...)
# LazyArray(T, g, f...)
#end
IndexStyle(::Type{<:LazyArray}) = IndexCartesian()
IndexStyle(::Type{<:LazyArray{G,T,1} where {G,T}}) = IndexLinear()
uses_hash(::Type{<:LazyArray}) = Val{true}()
function same_branch(a,b)
a === b
end
function same_branch(a::Fill,b::Fill)
typeof(a) != typeof(b) && return false
size(a) != size(b) && return false
a.value == b.value
end
function all_same_branch(a::Tuple,b::Tuple)
for i in 1:length(a)
if same_branch(a[i],b[i]) == false
return false
end
end
true
end
function same_branch(a::LazyArray,b::LazyArray)
typeof(a) != typeof(b) && return false
length(a.args) != length(b.args) && return false
same_branch(a.maps,b.maps) && all_same_branch(a.args,b.args)
end
function _get_cache(dict,a)
id = objectid(a)
if haskey(dict,id)
o,c = dict[id]
return c
end
for item in dict
if same_branch(a,second(item)[1])
return second(item)[2]
end
end
return nothing
end
function array_cache(dict::Dict,a::LazyArray)
cache = _get_cache(dict,a)
if cache === nothing
_cache = _array_cache!(dict,a)
dict[objectid(a)] = (a,_cache)
else
_cache = cache
end
_cache
end
mutable struct IndexItemPair{T,V}
index::T
item::V
end
function _array_cache!(dict::Dict,a::LazyArray)
@boundscheck begin
@notimplementedif ! all(map(isconcretetype, map(eltype, a.args)))
if ! (eltype(a.maps) <: Function)
@notimplementedif ! isconcretetype(eltype(a.maps))
end
end
gi = testitem(a.maps)
fi = map(testitem,a.args)
cg = array_cache(dict,a.maps)
cf = map(fi->array_cache(dict,fi),a.args)
cgi = return_cache(gi, fi...)
index = -1
#item = evaluate!(cgi,gi,testargs(gi,fi...)...)
item = return_value(gi,fi...)
(cg, cgi, cf), IndexItemPair(index, item)
end
function getindex!(cache, a::LazyArray, i::Integer)
_cache, index_and_item = cache
index = LinearIndices(a)[i]
if index_and_item.index != index
cg, cgi, cf = _cache
gi = getindex!(cg, a.maps, i)
index_and_item.item = _getindex_and_call!(cgi,gi,cf,a.args,i)
index_and_item.index = index
end
index_and_item.item
end
function getindex!(cache, a::LazyArray{G,T,N}, i::Vararg{Integer,N}) where {G,T,N}
_cache, index_and_item = cache
index = LinearIndices(a)[i...]
if index_and_item.index != index
cg, cgi, cf = _cache
gi = getindex!(cg, a.maps, i...)
index_and_item.item = _getindex_and_call!(cgi,gi,cf,a.args,i...)
index_and_item.index = index
end
index_and_item.item
end
function _getindex_and_call!(cgi,gi,cf,args,i...)
fi = map((cj,fj) -> getindex!(cj,fj,i...),cf,args)
evaluate!(cgi, gi, fi...)
end
function Base.getindex(a::LazyArray, i::Integer)
gi = a.maps[i]
fi = map(fj -> fj[i],a.args)
vi = evaluate(gi, fi...)
vi
end
function Base.getindex(a::LazyArray{G,T,N}, i::Vararg{Integer,N}) where {G,T,N}
gi = a.maps[i...]
fi = map(fj -> fj[i...],a.args)
vi = evaluate(gi, fi...)
vi
end
Base.size(a::LazyArray) = size(a.maps)
Base.size(a::LazyArray{G,T,1} where {G,T}) = (length(a.maps),)
function Base.sum(a::LazyArray)
cache = array_cache(a)
_sum_lazy_array(cache,a)
end
function _sum_lazy_array(cache,a)
r = zero(testitem(a))
for i in eachindex(a)
ai = getindex!(cache,a,i)
r += ai
end
r
end
function testitem(a::LazyArray{A,T} where A) where T
if length(a) > 0
first(a)
else
gi = testitem(a.maps)
fi = map(testitem,a.args)
return_value(gi,fi...)
end::T
end
# Particular implementations for Fill
function lazy_map(::typeof(evaluate),f::Fill, a::Fill...)
ai = map(ai->ai.value,a)
r = evaluate(f.value, ai...)
s = _common_size(f, a...)
Fill(r, s)
end
function lazy_map(::typeof(evaluate),::Type{T}, f::Fill, a::Fill...) where T
ai = map(ai->ai.value,a)
r = evaluate(f.value, ai...)
s = _common_size(f, a...)
Fill(r, s)
end
function _common_size(a::AbstractArray...)
a1, = a
#@check all(map(ai->length(a1) == length(ai),a)) "Array sizes $(map(size,a)) are not compatible."
if all( map(ai->size(a1) == size(ai),a) )
size(a1)
else
(length(a1),)
end
end
# Needed only for testing purposes
struct ArrayWithCounter{T,N,A} <: AbstractArray{T,N}
array::A
counter::Array{Int,N}
function ArrayWithCounter(a::AbstractArray{T,N}) where {T,N}
c = zeros(Int,size(a))
new{T,N,typeof(a)}(a,c)
end
end
Base.size(a::ArrayWithCounter) = size(a.array)
function Base.getindex(a::ArrayWithCounter,i::Integer)
a.counter[i] += 1
a.array[i]
end
function Base.getindex(a::ArrayWithCounter{T,N},i::Vararg{Integer,N}) where {T,N}
a.counter[i...] += 1
a.array[i...]
end
Base.IndexStyle(::Type{<:ArrayWithCounter{T,N,A}}) where {T,A,N} = IndexStyle(A)
function resetcounter!(a::ArrayWithCounter)
fill!(a.counter,zero(eltype(a.counter)))
end
# These extra methods are introduced to circumvent an unwanted run-time dispatch with julia 0.15 and 0.16
# see https://discourse.julialang.org/t/performance-depends-dramatically-on-compilation-order/58425
# Hopefully, they can be removed in the future
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any},i...)
f1 = getindex!(cf[1],args[1],i...)
evaluate!(cgi,gi,f1)
end
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any,Any},i...)
f1 = getindex!(cf[1],args[1],i...)
f2 = getindex!(cf[2],args[2],i...)
evaluate!(cgi,gi,f1,f2)
end
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any,Any,Any},i...)
f1 = getindex!(cf[1],args[1],i...)
f2 = getindex!(cf[2],args[2],i...)
f3 = getindex!(cf[3],args[3],i...)
evaluate!(cgi,gi,f1,f2,f3)
end
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any,Any,Any,Any},i...)
f1 = getindex!(cf[1],args[1],i...)
f2 = getindex!(cf[2],args[2],i...)
f3 = getindex!(cf[3],args[3],i...)
f4 = getindex!(cf[4],args[4],i...)
evaluate!(cgi,gi,f1,f2,f3,f4)
end
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any,Any,Any,Any,Any},i...)
f1 = getindex!(cf[1],args[1],i...)
f2 = getindex!(cf[2],args[2],i...)
f3 = getindex!(cf[3],args[3],i...)
f4 = getindex!(cf[4],args[4],i...)
f5 = getindex!(cf[5],args[5],i...)
evaluate!(cgi,gi,f1,f2,f3,f4,f5)
end
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any,Any,Any,Any,Any,Any},i...)
f1 = getindex!(cf[1],args[1],i...)
f2 = getindex!(cf[2],args[2],i...)
f3 = getindex!(cf[3],args[3],i...)
f4 = getindex!(cf[4],args[4],i...)
f5 = getindex!(cf[5],args[5],i...)
f6 = getindex!(cf[6],args[6],i...)
evaluate!(cgi,gi,f1,f2,f3,f4,f5,f6)
end
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any,Any,Any,Any,Any,Any,Any},i...)
f1 = getindex!(cf[1],args[1],i...)
f2 = getindex!(cf[2],args[2],i...)
f3 = getindex!(cf[3],args[3],i...)
f4 = getindex!(cf[4],args[4],i...)
f5 = getindex!(cf[5],args[5],i...)
f6 = getindex!(cf[6],args[6],i...)
f7 = getindex!(cf[7],args[7],i...)
evaluate!(cgi,gi,f1,f2,f3,f4,f5,f6,f7)
end
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any,Any,Any,Any,Any,Any,Any,Any},i...)
f1 = getindex!(cf[1],args[1],i...)
f2 = getindex!(cf[2],args[2],i...)
f3 = getindex!(cf[3],args[3],i...)
f4 = getindex!(cf[4],args[4],i...)
f5 = getindex!(cf[5],args[5],i...)
f6 = getindex!(cf[6],args[6],i...)
f7 = getindex!(cf[7],args[7],i...)
f8 = getindex!(cf[8],args[8],i...)
evaluate!(cgi,gi,f1,f2,f3,f4,f5,f6,f7,f8)
end
function _getindex_and_call!(cgi,gi,cf,args::Tuple{Any,Any,Any,Any,Any,Any,Any,Any,Any},i...)
f1 = getindex!(cf[1],args[1],i...)
f2 = getindex!(cf[2],args[2],i...)
f3 = getindex!(cf[3],args[3],i...)
f4 = getindex!(cf[4],args[4],i...)
f5 = getindex!(cf[5],args[5],i...)
f6 = getindex!(cf[6],args[6],i...)
f7 = getindex!(cf[7],args[7],i...)
f8 = getindex!(cf[8],args[8],i...)
f9 = getindex!(cf[9],args[9],i...)
evaluate!(cgi,gi,f1,f2,f3,f4,f5,f6,f7,f8,f9)
end