Skip to content

Commit

Permalink
Merge pull request #18 from JuliaParallel/other-workers
Browse files Browse the repository at this point in the history
Implement other_workers() and other_procs()
  • Loading branch information
JamesWrigley authored Dec 12, 2024
2 parents 09533a9 + 5141f58 commit 0cca4d3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/src/_changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ This documents notable changes in DistributedNext.jl. The format is based on
- A watcher mechanism has been added to detect when both the Distributed stdlib
and DistributedNext may be active and adding workers. This should help prevent
incompatibilities from both libraries being used simultaneously ([#10]).
- [`other_workers()`](@ref) and [`other_procs()`](@ref) were implemented and
exported ([#18]).

## [v1.0.0] - 2024-12-02

Expand Down
2 changes: 2 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ DistributedNext.nprocs
DistributedNext.nworkers
DistributedNext.procs()
DistributedNext.procs(::Integer)
DistributedNext.other_procs()
DistributedNext.workers
DistributedNext.other_workers
DistributedNext.rmprocs
DistributedNext.interrupt
DistributedNext.myid
Expand Down
2 changes: 2 additions & 0 deletions src/DistributedNext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ export
nworkers,
pmap,
procs,
other_procs,
remote,
remotecall,
remotecall_fetch,
remotecall_wait,
remote_do,
rmprocs,
workers,
other_workers,
WorkerPool,
RemoteChannel,
Future,
Expand Down
23 changes: 23 additions & 0 deletions src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,8 @@ end
Return a list of all process identifiers, including pid 1 (which is not included by [`workers()`](@ref)).
See also [`other_procs()`](@ref).
# Examples
```julia-repl
\$ julia -p 2
Expand All @@ -957,6 +959,14 @@ function procs()
end
end

"""
other_procs([pid::Integer])
Identical to [`procs()`](@ref) and [`procs(::Integer)`](@ref), except that the
current worker is filtered out.
"""
other_procs() = filter(!=(myid()), procs())

function id_in_procs(id) # faster version of `id in procs()`
if myid() == 1 || (PGRP.topology === :all_to_all && !isclusterlazy())
for x in PGRP.workers
Expand All @@ -979,6 +989,8 @@ end
Return a list of all process identifiers on the same physical node.
Specifically all workers bound to the same ip-address as `pid` are returned.
See also [`other_procs()`](@ref).
"""
function procs(pid::Integer)
if myid() == 1
Expand All @@ -994,11 +1006,15 @@ function procs(pid::Integer)
end
end

other_procs(pid::Integer) = filter(!=(myid()), procs(pid))

"""
workers()
Return a list of all worker process identifiers.
See also [`other_workers()`](@ref).
# Examples
```julia-repl
\$ julia -p 2
Expand All @@ -1018,6 +1034,13 @@ function workers()
end
end

"""
other_workers()
Identical to [`workers()`](@ref) except that the current worker is filtered out.
"""
other_workers() = filter(!=(myid()), workers())

function cluster_mgmt_from_master_check()
if myid() != 1
throw(ErrorException("Only process 1 can add and remove workers"))
Expand Down
9 changes: 8 additions & 1 deletion test/distributed_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ end
end

@testset "Distributed loading of packages" begin
addprocs_with_testenv(4)
@test isempty(other_workers())
@test isempty(other_procs())

pids = addprocs_with_testenv(4)

@test nprocs() == 5
@test other_workers() == workers()
@test other_procs() == pids
@test other_procs(1) == pids

global id_me = myid()
global id_other = filter(x -> x != id_me, procs())[rand(1:(nprocs()-1))]
Expand Down

0 comments on commit 0cca4d3

Please sign in to comment.