|
| 1 | +using Mmap |
| 2 | + |
| 3 | +""" |
| 4 | +A buffer that drops all elements put into it. Only to be used as the output |
| 5 | +buffer for a task - will throw if attached as an input. |
| 6 | +""" |
| 7 | +struct DropBuffer{T} end |
| 8 | +DropBuffer{T}(_) where T = DropBuffer{T}() |
| 9 | +Base.isempty(::DropBuffer) = true |
| 10 | +isfull(::DropBuffer) = false |
| 11 | +Base.put!(::DropBuffer, _) = nothing |
| 12 | +Base.take!(::DropBuffer) = error("Cannot `take!` from a DropBuffer") |
| 13 | + |
| 14 | +"A process-local buffer backed by a `Channel{T}`." |
| 15 | +struct ChannelBuffer{T} |
| 16 | + channel::Channel{T} |
| 17 | + len::Int |
| 18 | + count::Threads.Atomic{Int} |
| 19 | + ChannelBuffer{T}(len::Int=1024) where T = |
| 20 | + new{T}(Channel{T}(len), len, Threads.Atomic{Int}(0)) |
| 21 | +end |
| 22 | +Base.isempty(cb::ChannelBuffer) = isempty(cb.channel) |
| 23 | +isfull(cb::ChannelBuffer) = cb.count[] == cb.len |
| 24 | +function Base.put!(cb::ChannelBuffer{T}, x) where T |
| 25 | + put!(cb.channel, convert(T, x)) |
| 26 | + Threads.atomic_add!(cb.count, 1) |
| 27 | +end |
| 28 | +function Base.take!(cb::ChannelBuffer) |
| 29 | + take!(cb.channel) |
| 30 | + Threads.atomic_sub!(cb.count, 1) |
| 31 | +end |
| 32 | + |
| 33 | +"A cross-worker buffer backed by a `RemoteChannel{T}`." |
| 34 | +struct RemoteChannelBuffer{T} |
| 35 | + channel::RemoteChannel{Channel{T}} |
| 36 | + len::Int |
| 37 | + count::Threads.Atomic{Int} |
| 38 | + RemoteChannelBuffer{T}(len::Int=1024) where T = |
| 39 | + new{T}(RemoteChannel(()->Channel{T}(len)), len, Threads.Atomic{Int}(0)) |
| 40 | +end |
| 41 | +Base.isempty(cb::RemoteChannelBuffer) = isempty(cb.channel) |
| 42 | +isfull(cb::RemoteChannelBuffer) = cb.count[] == cb.len |
| 43 | +function Base.put!(cb::RemoteChannelBuffer{T}, x) where T |
| 44 | + put!(cb.channel, convert(T, x)) |
| 45 | + Threads.atomic_add!(cb.count, 1) |
| 46 | +end |
| 47 | +function Base.take!(cb::RemoteChannelBuffer) |
| 48 | + take!(cb.channel) |
| 49 | + Threads.atomic_sub!(cb.count, 1) |
| 50 | +end |
| 51 | + |
| 52 | +"A process-local ring buffer." |
| 53 | +mutable struct ProcessRingBuffer{T} |
| 54 | + read_idx::Int |
| 55 | + write_idx::Int |
| 56 | + @atomic count::Int |
| 57 | + buffer::Vector{T} |
| 58 | + function ProcessRingBuffer{T}(len::Int=1024) where T |
| 59 | + buffer = Vector{T}(undef, len) |
| 60 | + return new{T}(1, 1, 0, buffer) |
| 61 | + end |
| 62 | +end |
| 63 | +Base.isempty(rb::ProcessRingBuffer) = (@atomic rb.count) == 0 |
| 64 | +isfull(rb::ProcessRingBuffer) = (@atomic rb.count) == length(rb.buffer) |
| 65 | +function Base.put!(rb::ProcessRingBuffer{T}, x) where T |
| 66 | + len = length(rb.buffer) |
| 67 | + while (@atomic rb.count) == len |
| 68 | + yield() |
| 69 | + end |
| 70 | + to_write_idx = mod1(rb.write_idx, len) |
| 71 | + rb.buffer[to_write_idx] = convert(T, x) |
| 72 | + rb.write_idx += 1 |
| 73 | + @atomic rb.count += 1 |
| 74 | +end |
| 75 | +function Base.take!(rb::ProcessRingBuffer) |
| 76 | + while (@atomic rb.count) == 0 |
| 77 | + yield() |
| 78 | + end |
| 79 | + to_read_idx = rb.read_idx |
| 80 | + rb.read_idx += 1 |
| 81 | + @atomic rb.count -= 1 |
| 82 | + to_read_idx = mod1(to_read_idx, length(rb.buffer)) |
| 83 | + return rb.buffer[to_read_idx] |
| 84 | +end |
| 85 | + |
| 86 | +#= TODO |
| 87 | +"A server-local ring buffer backed by shared-memory." |
| 88 | +mutable struct ServerRingBuffer{T} |
| 89 | + read_idx::Int |
| 90 | + write_idx::Int |
| 91 | + @atomic count::Int |
| 92 | + buffer::Vector{T} |
| 93 | + function ServerRingBuffer{T}(len::Int=1024) where T |
| 94 | + buffer = Vector{T}(undef, len) |
| 95 | + return new{T}(1, 1, 0, buffer) |
| 96 | + end |
| 97 | +end |
| 98 | +Base.isempty(rb::ServerRingBuffer) = (@atomic rb.count) == 0 |
| 99 | +function Base.put!(rb::ServerRingBuffer{T}, x) where T |
| 100 | + len = length(rb.buffer) |
| 101 | + while (@atomic rb.count) == len |
| 102 | + yield() |
| 103 | + end |
| 104 | + to_write_idx = mod1(rb.write_idx, len) |
| 105 | + rb.buffer[to_write_idx] = convert(T, x) |
| 106 | + rb.write_idx += 1 |
| 107 | + @atomic rb.count += 1 |
| 108 | +end |
| 109 | +function Base.take!(rb::ServerRingBuffer) |
| 110 | + while (@atomic rb.count) == 0 |
| 111 | + yield() |
| 112 | + end |
| 113 | + to_read_idx = rb.read_idx |
| 114 | + rb.read_idx += 1 |
| 115 | + @atomic rb.count -= 1 |
| 116 | + to_read_idx = mod1(to_read_idx, length(rb.buffer)) |
| 117 | + return rb.buffer[to_read_idx] |
| 118 | +end |
| 119 | +=# |
| 120 | + |
| 121 | +#= |
| 122 | +"A TCP-based ring buffer." |
| 123 | +mutable struct TCPRingBuffer{T} |
| 124 | + read_idx::Int |
| 125 | + write_idx::Int |
| 126 | + @atomic count::Int |
| 127 | + buffer::Vector{T} |
| 128 | + function TCPRingBuffer{T}(len::Int=1024) where T |
| 129 | + buffer = Vector{T}(undef, len) |
| 130 | + return new{T}(1, 1, 0, buffer) |
| 131 | + end |
| 132 | +end |
| 133 | +Base.isempty(rb::TCPRingBuffer) = (@atomic rb.count) == 0 |
| 134 | +function Base.put!(rb::TCPRingBuffer{T}, x) where T |
| 135 | + len = length(rb.buffer) |
| 136 | + while (@atomic rb.count) == len |
| 137 | + yield() |
| 138 | + end |
| 139 | + to_write_idx = mod1(rb.write_idx, len) |
| 140 | + rb.buffer[to_write_idx] = convert(T, x) |
| 141 | + rb.write_idx += 1 |
| 142 | + @atomic rb.count += 1 |
| 143 | +end |
| 144 | +function Base.take!(rb::TCPRingBuffer) |
| 145 | + while (@atomic rb.count) == 0 |
| 146 | + yield() |
| 147 | + end |
| 148 | + to_read_idx = rb.read_idx |
| 149 | + rb.read_idx += 1 |
| 150 | + @atomic rb.count -= 1 |
| 151 | + to_read_idx = mod1(to_read_idx, length(rb.buffer)) |
| 152 | + return rb.buffer[to_read_idx] |
| 153 | +end |
| 154 | +=# |
| 155 | + |
| 156 | +#= |
| 157 | +""" |
| 158 | +A flexible puller which switches to the most efficient buffer type based |
| 159 | +on the sender and receiver locations. |
| 160 | +""" |
| 161 | +mutable struct UniBuffer{T} |
| 162 | + buffer::Union{ProcessRingBuffer{T}, Nothing} |
| 163 | +end |
| 164 | +function initialize_stream_buffer!(::Type{UniBuffer{T}}, T, send_proc, recv_proc, buffer_amount) where T |
| 165 | + if buffer_amount == 0 |
| 166 | + error("Return NullBuffer") |
| 167 | + end |
| 168 | + send_osproc = get_parent(send_proc) |
| 169 | + recv_osproc = get_parent(recv_proc) |
| 170 | + if send_osproc.pid == recv_osproc.pid |
| 171 | + inner = RingBuffer{T}(buffer_amount) |
| 172 | + elseif system_uuid(send_osproc.pid) == system_uuid(recv_osproc.pid) |
| 173 | + inner = ProcessBuffer{T}(buffer_amount) |
| 174 | + else |
| 175 | + inner = RemoteBuffer{T}(buffer_amount) |
| 176 | + end |
| 177 | + return UniBuffer{T}(buffer_amount) |
| 178 | +end |
| 179 | +
|
| 180 | +struct LocalPuller{T,B} |
| 181 | + buffer::B{T} |
| 182 | + id::UInt |
| 183 | + function LocalPuller{T,B}(id::UInt, buffer_amount::Integer) where {T,B} |
| 184 | + buffer = initialize_stream_buffer!(B, T, buffer_amount) |
| 185 | + return new{T,B}(buffer, id) |
| 186 | + end |
| 187 | +end |
| 188 | +function Base.take!(pull::LocalPuller{T,B}) where {T,B} |
| 189 | + if pull.buffer === nothing |
| 190 | + pull.buffer = |
| 191 | + error("Return NullBuffer") |
| 192 | + end |
| 193 | + value = take!(pull.buffer) |
| 194 | +end |
| 195 | +function initialize_input_stream!(stream::Stream{T,B}, id::UInt, send_proc::Processor, recv_proc::Processor, buffer_amount::Integer) where {T,B} |
| 196 | + local_buffer = remotecall_fetch(stream.ref.handle.owner, stream.ref.handle, id) do ref, id |
| 197 | + local_buffer, remote_buffer = initialize_stream_buffer!(B, T, send_proc, recv_proc, buffer_amount) |
| 198 | + ref.buffers[id] = remote_buffer |
| 199 | + return local_buffer |
| 200 | + end |
| 201 | + stream.buffer = local_buffer |
| 202 | + return stream |
| 203 | +end |
| 204 | +=# |
0 commit comments