Skip to content

Commit 54293a0

Browse files
authoredApr 11, 2024··
Merge pull request #500 from JuliaParallel/jps/num_processors
Add num_processors helper
2 parents 541c3bd + 989d60e commit 54293a0

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed
 

‎src/utils/scopes.jl

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Returns the set of all processors (across all Distributed workers) that are
77
compatible with the given scope.
88
"""
9-
function compatible_processors(scope::AbstractScope, ctx::Context=Sch.eager_context())
9+
function compatible_processors(scope::AbstractScope=get_options(:scope, DefaultScope()), ctx::Context=Sch.eager_context())
1010
compat_procs = Set{Processor}()
1111
for gproc in procs(ctx)
1212
# Fast-path in case entire process is incompatible
@@ -22,3 +22,20 @@ function compatible_processors(scope::AbstractScope, ctx::Context=Sch.eager_cont
2222
end
2323
return compat_procs
2424
end
25+
26+
"""
27+
num_processors(scope::AbstractScope=DefaultScope(), all::Bool=false) -> Int
28+
29+
Returns the number of processors available to Dagger by default, or if
30+
specified, according to `scope`. If `all=true`, instead returns the number of
31+
processors known to Dagger, whether or not they've been disabled by the user.
32+
Most users will want to use `num_processors()`.
33+
"""
34+
function num_processors(scope::AbstractScope=get_options(:scope, DefaultScope());
35+
all::Bool=false)
36+
if all
37+
return length(all_processors())
38+
else
39+
return length(compatible_processors(scope))
40+
end
41+
end

‎test/processors.jl

+1
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,6 @@ end
100100
w_procs = Dagger.get_processors(OSProc(w))
101101
@test all(proc->proc in all_procs, w_procs)
102102
end
103+
@test Dagger.num_processors(;all=true) == length(all_procs)
103104
end
104105
end

‎test/scopes.jl

+7
Original file line numberDiff line numberDiff line change
@@ -241,25 +241,32 @@
241241
@testset "compatible_processors" begin
242242
scope = Dagger.scope(workers=[])
243243
comp_procs = Dagger.compatible_processors(scope)
244+
@test Dagger.num_processors(scope) == length(comp_procs)
244245
@test !any(proc->proc in comp_procs, Dagger.get_processors(OSProc(wid1)))
245246
@test !any(proc->proc in comp_procs, Dagger.get_processors(OSProc(wid2)))
246247

247248
scope = Dagger.scope(worker=wid1)
248249
comp_procs = Dagger.compatible_processors(scope)
250+
@test Dagger.num_processors(scope) == length(comp_procs)
249251
@test all(proc->proc in comp_procs, Dagger.get_processors(OSProc(wid1)))
250252
@test !any(proc->proc in comp_procs, Dagger.get_processors(OSProc(wid2)))
251253

252254
scope = Dagger.scope(worker=wid1, thread=2)
253255
comp_procs = Dagger.compatible_processors(scope)
256+
@test Dagger.num_processors(scope) == length(comp_procs)
254257
@test length(comp_procs) == 1
255258
@test !all(proc->proc in comp_procs, Dagger.get_processors(OSProc(wid1)))
256259
@test !all(proc->proc in comp_procs, Dagger.get_processors(OSProc(wid2)))
257260
@test Dagger.ThreadProc(wid1, 2) in comp_procs
258261

259262
scope = Dagger.scope(workers=[wid1, wid2])
260263
comp_procs = Dagger.compatible_processors(scope)
264+
@test Dagger.num_processors(scope) == length(comp_procs)
261265
@test all(proc->proc in comp_procs, Dagger.get_processors(OSProc(wid1)))
262266
@test all(proc->proc in comp_procs, Dagger.get_processors(OSProc(wid2)))
267+
268+
comp_procs = Dagger.compatible_processors()
269+
@test Dagger.num_processors() == length(comp_procs)
263270
end
264271

265272
rmprocs([wid1, wid2])

0 commit comments

Comments
 (0)
Please sign in to comment.