-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathshard_levels.jl
297 lines (255 loc) · 10.5 KB
/
shard_levels.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
"""
ShardLevel{Lvl, [Val]}()
Each subfiber of a Shard level is stored in a thread-local tensor of type
`Lvl`, in a thread-local memory space.
Each sublevel is stored in a vector of type `Val` with `eltype(Val) = Lvl`.
```jldoctest
julia> tensor_tree(Tensor(Dense(Shard(Element(0.0))), [1, 2, 3]))
3-Tensor
└─ Dense [1:3]
├─ [1]: Shard ->
│ └─ 1.0
├─ [2]: Shard ->
│ └─ 2.0
└─ [3]: Shard ->
└─ 3.0
```
"""
struct ShardLevel{Device, Lvl, Ptr, Task, Val} <: AbstractLevel
device::Device
lvl::Lvl
ptr::Ptr
task::Task
val::Val
end
const Shard = ShardLevel
ShardLevel(device::Device, lvl::Lvl) where {Device, Lvl} =
ShardLevel{Device}(device, lvl, postype(lvl)[], postype(lvl)[], moveto(lvl, device)) #TODO scatterto?
ShardLevel{Device}(device, lvl::Lvl, ptr::Ptr, task::Task, val::Val) where {Device, Lvl, Ptr, Task, Val} =
ShardLevel{Device, Lvl, Ptr, Task, Val}(device, lvl, ptr, task, val)
Base.summary(::Shard{Device, Lvl, Ptr, Task, Val}) where {Device, Lvl, Ptr, Task, Val} = "Shard($(Lvl))"
similar_level(lvl::Shard{Device, Lvl, Ptr, Task, Val}, fill_value, eltype::Type, dims...) where {Device, Lvl, Ptr, Task, Val} =
ShardLevel(lvl.device, similar_level(lvl.lvl, fill_value, eltype, dims...))
postype(::Type{<:Shard{Device, Lvl, Ptr, Task, Val}}) where {Device, Lvl, Ptr, Task, Val} = postype(Lvl)
function moveto(lvl::ShardLevel, device)
lvl_2 = moveto(lvl.lvl, device)
ptr_2 = moveto(lvl.ptr, device)
task_2 = moveto(lvl.task, device)
return ShardLevel(lvl_2, ptr_2, task_2, val_2)
end
pattern!(lvl::ShardLevel) = ShardLevel(pattern!(lvl.lvl), lvl.ptr, lvl.task, map(pattern!, lvl.val))
set_fill_value!(lvl::ShardLevel, init) = ShardLevel(set_fill_value!(lvl.lvl, init), lvl.ptr, lvl.task, map(lvl_2 -> set_fill_value!(lvl_2, init), lvl.val))
Base.resize!(lvl::ShardLevel, dims...) = ShardLevel(resize!(lvl.lvl, dims...), lvl.ptr, lvl.task, map(lvl_2 -> resize!(lvl_2, dims...), lvl.val))
function Base.show(io::IO, lvl::ShardLevel{Device, Lvl, Ptr, Task, Val}) where {Device, Lvl, Ptr, Task, Val}
print(io, "Shard(")
if get(io, :compact, false)
print(io, "…")
else
show(io, lvl.lvl)
print(io, ", ")
show(io, lvl.ptr)
print(io, ", ")
show(io, lvl.task)
print(io, ", ")
show(io, lvl.val)
end
print(io, ")")
end
function labelled_show(io::IO, fbr::SubFiber{<:ShardLevel})
(lvl, pos) = (fbr.lvl, fbr.pos)
print(io, "shard($(lvl.task[pos])) -> ")
end
function labelled_children(fbr::SubFiber{<:ShardLevel})
lvl = fbr.lvl
pos = fbr.pos
pos > length(lvl.val) && return []
[LabelledTree(SubFiber(lvl.val[lvl.task[pos]], lvl.ptr[pos]))]
end
@inline level_ndims(::Type{<:ShardLevel{Device, Lvl, Ptr, Task, Val}}) where {Device, Lvl, Ptr, Task, Val} = level_ndims(Lvl)
@inline level_size(lvl::ShardLevel{Device, Lvl, Ptr, Task, Val}) where {Device, Lvl, Ptr, Task, Val} = level_size(lvl.lvl)
@inline level_axes(lvl::ShardLevel{Device, Lvl, Ptr, Task, Val}) where {Device, Lvl, Ptr, Task, Val} = level_axes(lvl.lvl)
@inline level_eltype(::Type{ShardLevel{Device, Lvl, Ptr, Task, Val}}) where {Device, Lvl, Ptr, Task, Val} = level_eltype(Lvl)
@inline level_fill_value(::Type{<:ShardLevel{Device, Lvl, Ptr, Task, Val}}) where {Device, Lvl, Ptr, Task, Val} = level_fill_value(Lvl)
function (fbr::SubFiber{<:ShardLevel})(idxs...)
q = fbr.pos
return SubFiber(fbr.lvl.val[q], 1)(idxs...)
end
countstored_level(lvl::ShardLevel, pos) = pos
mutable struct VirtualShardLevel <: AbstractVirtualLevel
device
lvl # stand-in for the sublevel for virtual resize, etc.
ex
ptr
task
val
Tv
Device
Lvl
Ptr
Task
Val
end
postype(lvl::VirtualShardLevel) = postype(lvl.lvl)
is_level_injective(ctx, lvl::VirtualShardLevel) = [is_level_injective(ctx, lvl.lvl)..., true]
function is_level_atomic(ctx, lvl::VirtualShardLevel)
(below, atomic) = is_level_atomic(ctx, lvl.lvl)
return ([below; [atomic]], atomic)
end
function is_level_concurrent(ctx, lvl::VirtualShardLevel)
(data, _) = is_level_concurrent(ctx, lvl.lvl)
return (data, true)
end
function lower(ctx::AbstractCompiler, lvl::VirtualShardLevel, ::DefaultStyle)
quote
$ShardLevel{$(lvl.Lvl), $(lvl.Ptr), $(lvl.Task), $(lvl.Val)}($(ctx(lvl.lvl)), $(lvl.val))
end
end
function virtualize(ctx, ex, ::Type{ShardLevel{Device, Lvl, Ptr, Task, Val}}, tag=:lvl) where {Device, Lvl, Ptr, Task, Val}
sym = freshen(ctx, tag)
ptr = freshen(ctx, tag, :_ptr)
task = freshen(ctx, tag, :_task)
val = freshen(ctx, tag, :_val)
push_preamble!(ctx, quote
$sym = $ex
$ptr = $ex.ptr
$task = $ex.task
$val = $ex.val
end)
device_2 = virtualize(ctx, :($ex.device), Device, sym)
lvl_2 = virtualize(ctx, :($ex.lvl), Lvl, sym)
VirtualShardLevel(device_2, lvl_2, sym, ptr, task, val, typeof(level_fill_value(Lvl)), Device, Lvl, Ptr, Task, Val)
end
Base.summary(lvl::VirtualShardLevel) = "Shard($(lvl.Lvl))"
virtual_level_resize!(ctx, lvl::VirtualShardLevel, dims...) = (lvl.lvl = virtual_level_resize!(ctx, lvl.lvl, dims...); lvl)
virtual_level_size(ctx, lvl::VirtualShardLevel) = virtual_level_size(ctx, lvl.lvl)
virtual_level_eltype(lvl::VirtualShardLevel) = virtual_level_eltype(lvl.lvl)
virtual_level_fill_value(lvl::VirtualShardLevel) = virtual_level_fill_value(lvl.lvl)
function virtual_moveto_level(ctx, lvl::VirtualShardLevel, arch)
val_2 = freshen(ctx, lvl.val)
push_preamble!(ctx, quote
$val_2 = $(lvl.val)
$(lvl.val) = $moveto($(lvl.val), $(ctx(arch)))
end)
push_epilogue!(ctx, quote
$(lvl.val) = $val_2
end)
virtual_moveto_level(ctx, lvl.lvl, arch)
end
function declare_level!(ctx, lvl::VirtualShardLevel, pos, init)
push_preamble!(ctx,
virtual_parallel_region(ctx, lvl.device) do ctx_2
lvl_2 = virtualize(ctx_2, :($(lvl.ex).val[$(ctx_2(get_task_num(get_task(ctx_2))))]), lvl.Lvl) #TODO should this virtualize the eltype of Val?
declare_level!(ctx_2, lvl_2, literal(1), init)
end
)
lvl
end
"""
assemble:
mapping is pos -> task, ptr. task says which task has it, ptr says which position in that task has it.
read:
read from pos to task, ptr. simple.
write:
allocate something for this task on that position, assemble on the task itself on demand. Complain if the task is wrong.
The outer level needs to be concurrent, like denselevel.
"""
function assemble_level!(ctx, lvl::VirtualShardLevel, pos_start, pos_stop)
pos_start = cache!(ctx, :pos_start, simplify(ctx, pos_start))
pos_stop = cache!(ctx, :pos_stop, simplify(ctx, pos_stop))
pos = freshen(ctx, :pos)
sym = freshen(ctx, :pointer_to_lvl)
push_preamble!(ctx, quote
Finch.resize_if_smaller!($(lvl.task), $(ctx(pos_stop)))
Finch.resize_if_smaller!($(lvl.ptr), $(ctx(pos_stop)))
Finch.fill_range!($(lvl.task), $(ctx(pos_start)), $(ctx(pos_stop)), 0)
end)
lvl
end
supports_reassembly(::VirtualShardLevel) = false
"""
these two are no-ops, we insteaed do these on instantiate
"""
function freeze_level!(ctx, lvl::VirtualShardLevel, pos)
return lvl
end
function thaw_level!(ctx::AbstractCompiler, lvl::VirtualShardLevel, pos)
return lvl
end
function instantiate(ctx, fbr::VirtualSubFiber{VirtualShardLevel}, mode)
if mode.kind === reader
(lvl, pos) = (fbr.lvl, fbr.pos)
tag = lvl.ex
isnulltest = freshen(ctx, tag, :_nulltest)
Vf = level_fill_value(lvl.Lvl)
sym = freshen(ctx, :pointer_to_lvl)
val = freshen(ctx, lvl.ex, :_val)
return Thunk(
body = (ctx) -> begin
lvl_2 = virtualize(ctx.code, :($(lvl.val)[$(ctx(pos))]), lvl.Lvl, sym)
instantiate(ctx, VirtualSubFiber(lvl_2, literal(1)), mode)
end,
)
else
(lvl, pos) = (fbr.lvl, fbr.pos)
tag = lvl.ex
sym = freshen(ctx, :pointer_to_lvl)
return Thunk(
body = (ctx) -> begin
lvl_2 = virtualize(ctx.code, :($(lvl.val)[$(ctx(pos))]), lvl.Lvl, sym)
lvl_2 = thaw_level!(ctx, lvl_2, literal(1))
push_preamble!(ctx, assemble_level!(ctx, lvl_2, literal(1), literal(1)))
res = instantiate(ctx, VirtualSubFiber(lvl_2, literal(1)), mode)
push_epilogue!(ctx,
contain(ctx) do ctx_2
lvl_2 = freeze_level!(ctx_2, lvl_2, literal(1))
:($(lvl.val)[$(ctx_2(pos))] = $(ctx_2(lvl_2)))
end
)
res
end
)
end
end
#we need some sort of localization step at the start of a parallel region whereby we can thaw the shart level
"""
assemble:
mapping is pos -> task, ptr. task says which task has it, ptr says which position in that task has it.
read:
read from pos to task, ptr. simple.
write:
allocate something for this task on that position, assemble on the task itself on demand. Complain if the task is wrong.
The outer level needs to be concurrent, like denselevel.
"""
function instantiate(ctx, fbr::VirtualHollowSubFiber{VirtualShardLevel}, mode)
@assert mode.kind === updater
(lvl, pos) = (fbr.lvl, fbr.pos)
tag = lvl.ex
sym = freshen(ctx, :pointer_to_lvl)
task = freshen(ctx, tag, :_task)
return Thunk(
preamble = quote
$task = $(lvl.task)[$(ctx(pos))]
if task == 0
$(lvl.task)[$(ctx(pos))] = $(gettasknum(ctx))
qos = local_qos_fill
if $(lvl.local_qos_fill) > $(lvl.local_qos_stop)
$local_qos_stop = max($local_qos_stop << 1, 1)
$(contain(ctx_2->assemble_level!(ctx_2, lvl.lvl, value(qos_fill, Tp), value(qos_stop, Tp)), ctx))
end
else
qos = $(lvl.ptr)[$(ctx(pos))]
qos_stop = $(lvl.local_qos_stop)
#only in safe mode, we check if task == $(gettasknum(ctx)) and if not error("Task mismatch in ShardLevel")
end
dirty = true
end,
body = (ctx) -> VirtualHollowSubFiber(lvl.lvl, value(qos), dirty),
epilogue = quote
#this task will always own this position forever, even if we don't write to it. Still, we try to be conservative of memory usage of the underlying level.
if dirty && $(lvl.ptr)[$(ctx(pos))] == 0
local_qos_fill += 1
$(lvl.ptr)[$(ctx(pos))] = $(lvl.local_qos_fill) += 1
end
end
)
end