In my program, I have a loop where I need a buffer array. The required size of the buffer changes only a few times during the run. Also, I am using multithreading. So, the buffer should be thread-safe.
The following script is what I came up with. A const array _buffer
is declared. Function _get_buffer
returns the buffer, resizing the array if needed. Is there any problem with this method?
Also, is there a better way, or a package for pre-allocating and changing buffer sizes?
using Base.Threads
# Preallocated buffer array
const _buffer = [Array{Float64, 2}(undef, 0, 0)]
Threads.resize_nthreads!(_buffer)
"""Get preallocated buffer in a thread-safe way.
Resize buffer if the size is different from the needed size"""
function _get_buffer(buffer::Vector{Array{T, N}}, size_needed::NTuple{N, Int}) where {T, N}
tid = threadid()
if size(buffer[tid]) != size_needed
buffer[tid] = zeros(T, size_needed)
end
buffer[tid]
end
# Usage
Nlist = vcat(repeat([40], 10), repeat([60], 10)) # Size of the buffer array for each iteration
@threads for N in Nlist
arr = _get_buffer(_buffer, (N, N))
# Do something with arr..
arr .= 1.0
println(sum(arr))
end