Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add navailable()/capacity(RemoteRef{Channel}) #12781

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,23 @@ end

eltype{T}(::Type{Channel{T}}) = T

function n_avail(c::Channel)
"""
The maximal number of elements the channel can store.
"""
capacity(c::Channel) = c.sz_max
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be length(Channel) perhaps? capacity is not currently defined anywhere in Base.

will also need to add docs and tests for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also n_avail() (so far used in the context of Channel only). To me length() is rather a synonym of n_avail(). capacity() could be potentially reused in other places to return what sizehint!() has set. But I would be glad to change it into anything core Julia developers decide.


"""
The number of elements currently in the channel.
"""
function navailable(c::Channel)
if c.put_pos >= c.take_pos
return c.put_pos - c.take_pos
else
return c.szp1 - c.take_pos + c.put_pos
end
end

show(io::IO, c::Channel) = print(io, "$(typeof(c))(sz_max:$(c.sz_max),sz_curr:$(n_avail(c)))")
show(io::IO, c::Channel) = print(io, "$(typeof(c))(capacity:$(capacity(c)), navailable:$(navailable(c)))")

start{T}(c::Channel{T}) = Ref{Nullable{T}}()
function done(c::Channel, state::Ref)
Expand Down
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ export

# multiprocessing
addprocs,
capacity,
ClusterManager,
fetch,
init_worker,
Expand All @@ -1217,6 +1218,7 @@ export
launch,
manage,
myid,
navailable,
nprocs,
nworkers,
pmap,
Expand Down
18 changes: 9 additions & 9 deletions base/multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,6 @@ function lookup_ref(pg, id, f)
rv
end

function isready(rr::RemoteRef, args...)
rid = rr2id(rr)
if rr.where == myid()
isready(lookup_ref(rid).c, args...)
else
remotecall_fetch(rr.where, id->isready(lookup_ref(rid).c, args...), rid)
end
end

del_client(id, client) = del_client(PGRP, id, client)
function del_client(pg, id, client)
rv = lookup_ref(id)
Expand Down Expand Up @@ -777,6 +768,15 @@ function call_on_owner(f, rr::RemoteRef, args...)
end
end

for f in (:isready, :navailable, :capacity)
f_ref = symbol(string(f, "_ref"))
@eval begin
($f)(rv::RemoteValue, args...) = ($f)(rv.c, args...)
($f_ref)(rid, args...) = ($f)(lookup_ref(rid).c, args...)
($f)(rr::RemoteRef, args...) = call_on_owner(($f_ref), rr, args...)
end
end

wait_ref(rid, args...) = (wait(lookup_ref(rid).c, args...); nothing)
wait(r::RemoteRef, args...) = (call_on_owner(wait_ref, r, args...); r)

Expand Down
16 changes: 16 additions & 0 deletions test/parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,33 @@ id_other = filter(x -> x != id_me, procs())[rand(1:(nprocs()-1))]

rr=RemoteRef()
@test typeof(rr) == RemoteRef{Channel{Any}}
@test !isready(rr)
@test navailable(rr) == 0
@test capacity(rr) > 0
a = rand(5,5)
put!(rr, a)
@test isready(rr)
@test navailable(rr) == 1
@test rr[2,3] == a[2,3]
@test rr[] == a
take!(rr)
@test !isready(rr)
@test navailable(rr) == 0

rr=RemoteRef(workers()[1])
@test typeof(rr) == RemoteRef{Channel{Any}}
@test !isready(rr)
@test navailable(rr) == 0
@test capacity(rr) > 0
a = rand(5,5)
put!(rr, a)
@test isready(rr)
@test navailable(rr) == 1
@test rr[1,5] == a[1,5]
@test rr[] == a
take!(rr)
@test !isready(rr)
@test navailable(rr) == 0

dims = (20,20,20)

Expand Down