Closed jarroyoe closed 2 years ago
Hi @jarroyoe ,
JLD2 does not support writing to the same file from multiple threads at the same time.
If each of your threads does a lot of independent work and only write results sometimes, you could use a lock
as shown below. (Now all other threads will wait for each other when writing a file)
Otherwise you could have threads write to separate files (and combine later)
julia> Threads.nthreads()
8
julia> filelock = ReentrantLock()
ReentrantLock(nothing, Base.GenericCondition{Base.Threads.SpinLock}(Base.InvasiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), 0)
julia> using JLD2
julia> filename = "test_threaded.jld2"
"test_threaded.jld2"
julia> Threads.@threads for n=1:1000
# do expensive computation
lock(filelock) do
jldopen(filename, "a") do f
f["$n"] = n
end
end
end
julia> f = jldopen(filename)
JLDFile /home/isensee/test_threaded.jld2 (read-only)
ββπ’ 126
ββπ’ 876
ββπ’ 251
ββπ’ 877
ββπ’ 252
ββπ’ 626
ββπ’ 253
ββπ’ 127
ββπ’ 627
ββ β― (991 more entries)
Thank you! Is there any significant reduction in performance of using locks, say the threads stop when one of them is writing, or the waiting time only occurs when two threads reach the writing at the same time?
El jue., 24 de marzo de 2022 1:23 a. m., JonasIsensee < @.***> escribiΓ³:
Hi @jarroyoe https://github.com/jarroyoe , JLD2 does not support writing to the same file from multiple threads at the same time. If each of your threads does a lot of independent work and only write results sometimes, you could use a lock as shown below. (Now all other threads will wait for each other when writing a file) Otherwise you could have threads write to separate files (and combine later)
julia> Threads.nthreads() 8
julia> filelock = ReentrantLock() ReentrantLock(nothing, Base.GenericCondition{Base.Threads.SpinLock}(Base.InvasiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), 0)
julia> using JLD2
julia> filename = "test_threaded.jld2" "test_threaded.jld2"
julia> @.*** for n=1:1000
do expensive computation
lock(filelock) do jldopen(filename, "a") do f f["$n"] = n end end end
β Reply to this email directly, view it on GitHub https://github.com/JuliaIO/JLD2.jl/issues/391#issuecomment-1077358689, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALD7U5J5L2WAY6WE7CMGMB3VBQRAJANCNFSM5RPFJJOQ . You are receiving this because you were mentioned.Message ID: @.***>
Yeah, in principle the waiting should only occur when two threads attempt to write at the same time. If writing happens rarely that may be best. However, this depends a lot on the application and I suggest to try and benchmark it yourself. :)
I have a script that writes an Vector{Any} into a .jld2 file inside a for loop to get partial results of the runs. I'm trying to implement parallel processing using Threads, and while most of the time it works well, in my best run so far I got the following error:
From what I understand, it seems that two threads tried to overwrite the file at the same time, which caused an error. Is there any way to prevent the threads from doing this?