-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
processor.jl
282 lines (241 loc) · 9.27 KB
/
processor.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
export OSProc, Context, addprocs!, rmprocs!
"""
Processor
An abstract type representing a processing device and associated memory, where
data can be stored and operated on. Subtypes should be immutable, and
instances should compare equal if they represent the same logical processing
device/memory. Subtype instances should be serializable between different
nodes. Subtype instances may contain a pointer to a "parent" `Processor` to
make it easy to transfer data to/from other types of `Processor` at runtime.
"""
abstract type Processor end
const PROCESSOR_CALLBACKS = []
"""
execute!(proc::Processor, f, args...) -> Any
Executes the function `f` with arguments `args` on processor `proc`. This
function can be overloaded by `Processor` subtypes to allow executing function
calls differently than normal Julia.
"""
execute!
"""
iscompatible(proc::Processor, opts, f, Targs...) -> Bool
Indicates whether `proc` can execute `f` over `Targs` given `opts`. `Processor`
subtypes should overload this function to return `true` if and only if it is
essentially guaranteed that `f(::Targs...)` is supported. Additionally,
`iscompatible_func` and `iscompatible_arg` can be overriden to determine
compatibility of `f` and `Targs` individually. The default implementation
returns `false`.
"""
iscompatible(proc::Processor, opts, f, Targs...) =
iscompatible_func(proc, opts, f) &&
all(x->iscompatible_arg(proc, opts, x), Targs)
iscompatible_func(proc::Processor, opts, f) = false
iscompatible_arg(proc::Processor, opts, x) = false
"""
default_enabled(proc::Processor) -> Bool
Returns whether processor `proc` is enabled by default (opt-out). `Processor` subtypes can override this function to make themselves opt-in (default returns `false`).
"""
default_enabled(proc::Processor) = false
"""
get_processors(proc::Processor) -> Vector{T} where T<:Processor
Returns the full list of processors contained in `proc`, if any. `Processor`
subtypes should overload this function if they can contain sub-processors. The
default method will return a `Vector` containing `proc` itself.
"""
get_processors(proc::Processor) = Processor[proc]
"""
get_parent(proc::Processor) -> Processor
Returns the parent processor for `proc`. The ultimate parent processor is an
`OSProc`. `Processor` subtypes should overload this to return their most
direct parent.
"""
get_parent
"""
move(from_proc::Processor, to_proc::Processor, x)
Moves and/or converts `x` such that it's available and suitable for usage on
the `to_proc` processor. This function can be overloaded by `Processor`
subtypes to transport arguments and convert them to an appropriate form before
being used for exection. Subtypes of `Processor` wishing to implement efficient
data movement should provide implementations where `x::Chunk`.
"""
move(from_proc::Processor, to_proc::Processor, x) = x
"""
capacity(proc::Processor=OSProc()) -> Int
Returns the total processing capacity of `proc`.
"""
capacity(proc=OSProc()) = length(get_processors(proc))
"""
OSProc <: Processor
Julia CPU (OS) process, identified by Distributed pid. Executes thunks when
threads or other "children" processors are not available, and/or the user has
not opted to use those processors.
"""
struct OSProc <: Processor
pid::Int
attrs::Dict{Symbol,Any}
children::Vector{Processor}
queue::Vector{Processor}
end
const OSPROC_CACHE = Dict{Int,OSProc}()
OSProc(pid::Int=myid()) = get!(OSPROC_CACHE, pid) do
remotecall_fetch(get_osproc, pid, pid)
end
function get_osproc(pid::Int)
proc = OSProc(pid, Dict{Symbol,Any}(), Processor[], Processor[])
for cb in PROCESSOR_CALLBACKS
try
child = Base.invokelatest(cb, proc)
child !== nothing && push!(proc.children, child)
catch err
@error "Error in processor callback" exception=(err,catch_backtrace())
end
end
proc
end
function add_callback!(func)
push!(Dagger.PROCESSOR_CALLBACKS, func)
empty!(OSPROC_CACHE)
end
Base.:(==)(proc1::OSProc, proc2::OSProc) = proc1.pid == proc2.pid
iscompatible(proc::OSProc, opts, f, args...) =
any(child->iscompatible(child, opts, f, args...), proc.children)
iscompatible_func(proc::OSProc, opts, f) =
any(child->iscompatible_func(child, opts, f), proc.children)
iscompatible_arg(proc::OSProc, opts, args...) =
any(child->
all(arg->iscompatible_arg(child, opts, arg), args),
proc.children)
get_processors(proc::OSProc) =
vcat((get_processors(child) for child in proc.children)...,)
function choose_processor(options, f, Targs)
osproc = OSProc()
if isempty(osproc.queue)
for child in osproc.children
grandchildren = get_processors(child)
append!(osproc.queue, grandchildren)
end
end
@assert !isempty(osproc.queue)
for i in 1:length(osproc.queue)
proc = popfirst!(osproc.queue)
push!(osproc.queue, proc)
if !iscompatible(proc, options, f, Targs...)
continue
end
if default_enabled(proc) && isempty(options.proctypes)
return proc
elseif any(p->proc isa p, options.proctypes)
return proc
end
end
throw(ProcessorSelectionException(options.proctypes, osproc.queue, f, Targs))
end
struct ProcessorSelectionException <: Exception
proctypes::Vector{Type}
procsavail::Vector{Processor}
f
args
end
function Base.show(io::IO, pex::ProcessorSelectionException)
println(io, "(Worker $(myid())) Exhausted all available processor types!")
println(io, " Proctypes: $(pex.proctypes)")
println(io, " Procs Available: $(pex.procsavail)")
println(io, " Function: $(pex.f)")
print(io, " Arguments: $(pex.args)")
end
execute!(proc::OSProc, f, args...) = f(args...)
default_enabled(proc::OSProc) = true
"""
ThreadProc <: Processor
Julia CPU (OS) thread, identified by Julia thread ID.
"""
struct ThreadProc <: Processor
owner::Int
tid::Int
end
iscompatible(proc::ThreadProc, opts, f, args...) = true
iscompatible_func(proc::ThreadProc, opts, f) = true
iscompatible_arg(proc::ThreadProc, opts, x) = true
@static if VERSION >= v"1.3.0-DEV.573"
execute!(proc::ThreadProc, f, args...) = fetch(Threads.@spawn begin
task_local_storage(:processor, proc)
f(args...)
end)
else
# TODO: Use Threads.@threads?
execute!(proc::ThreadProc, f, args...) = fetch(@async begin
task_local_storage(:processor, proc)
f(args...)
end)
end
get_parent(proc::ThreadProc) = OSProc(proc.owner)
default_enabled(proc::ThreadProc) = true
# TODO: ThreadGroupProc?
"A context represents a set of processors to use for an operation."
mutable struct Context
procs::Vector{Processor}
proc_lock::ReentrantLock
log_sink::Any
log_file::Union{String,Nothing}
profile::Bool
options
end
"""
Context(;nthreads=Threads.nthreads()) -> Context
Context(xs::Vector{OSProc}) -> Context
Context(xs::Vector{Int}) -> Context
Create a Context, by default adding each available worker once from
every available thread. Use the `nthreads` keyword to use a different
number of threads.
It is also possible to create a Context from a vector of [`OSProc`](@ref),
or equivalently the underlying process ids can also be passed directly
as a `Vector{Int}`.
Special fields include:
- 'log_sink': A log sink object to use, if any.
- `log_file::Union{String,Nothing}`: Path to logfile. If specified, at
scheduler termination logs will be collected, combined with input thunks, and
written out in DOT format to this location.
- `profile::Bool`: Whether or not to perform profiling with Profile stdlib.
"""
Context(procs::Vector{P}=Processor[OSProc(w) for w in workers()];
proc_lock=ReentrantLock(), log_sink=NoOpLog(), log_file=nothing,
profile=false, options=nothing) where {P<:Processor} =
Context(procs, proc_lock, log_sink, log_file, profile, options)
Context(xs::Vector{Int}) = Context(map(OSProc, xs))
procs(ctx::Context) = lock(ctx) do
copy(ctx.procs)
end
"""
write_event(ctx::Context, event::Event)
Write a log event
"""
function write_event(ctx::Context, event::Event)
write_event(ctx.log_sink, event)
end
"""
lock(f, ctx::Context)
Acquire `ctx.proc_lock`, execute `f` with the lock held, and release the lock when `f` returns.
"""
Base.lock(f, ctx::Context) = lock(f, ctx.proc_lock)
"""
addprocs!(ctx::Context, xs)
Add new workers `xs` to `ctx`.
Workers will typically be assigned new tasks in the next scheduling iteration if scheduling is ongoing.
Workers can be either `Processor`s or the underlying process IDs as `Integer`s.
"""
addprocs!(ctx::Context, xs::AbstractVector{<:Integer}) = addprocs!(ctx, map(OSProc, xs))
addprocs!(ctx::Context, xs::AbstractVector{<:Processor}) = lock(ctx) do
append!(ctx.procs, xs)
end
"""
rmprocs!(ctx::Context, xs)
Remove the specified workers `xs` from `ctx`.
Workers will typically finish all their assigned tasks if scheduling is ongoing but will not be assigned new tasks after removal.
Workers can be either `Processor`s or the underlying process IDs as `Integer`s.
"""
rmprocs!(ctx::Context, xs::AbstractVector{<:Integer}) = rmprocs!(ctx, map(OSProc, xs))
rmprocs!(ctx::Context, xs::AbstractVector{<:Processor}) = lock(ctx) do
filter!(p -> p ∉ xs, ctx.procs)
end
"Gets the current processor executing the current thunk."
thunk_processor() = task_local_storage(:processor)::Processor