From 80f56e2ab22b0d3583370b7e8df40d2034ea1c04 Mon Sep 17 00:00:00 2001 From: nHackel Date: Thu, 12 Dec 2024 10:47:27 +0100 Subject: [PATCH] Restructure MiscAlgorithms, "remove" multi threading for now --- src/MiscAlgorithms/MiscAlgorithms.jl | 2 +- ...gorithms.jl => MultiThreadedAlgorithms.jl} | 44 ------------------- src/MiscAlgorithms/ThreadPinnedAlgorithm.jl | 43 ++++++++++++++++++ 3 files changed, 44 insertions(+), 45 deletions(-) rename src/MiscAlgorithms/{RuntimeAlgorithms.jl => MultiThreadedAlgorithms.jl} (68%) create mode 100644 src/MiscAlgorithms/ThreadPinnedAlgorithm.jl diff --git a/src/MiscAlgorithms/MiscAlgorithms.jl b/src/MiscAlgorithms/MiscAlgorithms.jl index c18c448..e92aa75 100644 --- a/src/MiscAlgorithms/MiscAlgorithms.jl +++ b/src/MiscAlgorithms/MiscAlgorithms.jl @@ -1 +1 @@ -include("RuntimeAlgorithms.jl") \ No newline at end of file +include("ThreadPinnedAlgorithm.jl") \ No newline at end of file diff --git a/src/MiscAlgorithms/RuntimeAlgorithms.jl b/src/MiscAlgorithms/MultiThreadedAlgorithms.jl similarity index 68% rename from src/MiscAlgorithms/RuntimeAlgorithms.jl rename to src/MiscAlgorithms/MultiThreadedAlgorithms.jl index 1257753..7d78449 100644 --- a/src/MiscAlgorithms/RuntimeAlgorithms.jl +++ b/src/MiscAlgorithms/MultiThreadedAlgorithms.jl @@ -1,47 +1,3 @@ -export ThreadPinnedAlgorithm, ThreadPinnedAlgorithmParameter -Base.@kwdef struct ThreadPinnedAlgorithmParameter <: AbstractImageReconstructionParameters - threadID::Int64 - algo::AbstractImageReconstructionAlgorithm -end - -mutable struct ThreadPinnedAlgorithm <: AbstractImageReconstructionAlgorithm - params::ThreadPinnedAlgorithmParameter - recoTask::Union{Nothing,Task} - taskLock::ReentrantLock - inputChannel::Channel{Any} - outputChannel::Channel{Any} -end - -ThreadPinnedAlgorithm(params::ThreadPinnedAlgorithmParameter) = ThreadPinnedAlgorithm(params, nothing, ReentrantLock(), Channel{Any}(Inf), Channel{Any}(Inf)) - -take!(algo::ThreadPinnedAlgorithm) = take!(algo.outputChannel) -function put!(algo::ThreadPinnedAlgorithm, u) - put!(algo.inputChannel, u) - lock(algo.taskLock) - try - if isnothing(algo.recoTask) || istaskdone(algo.recoTask) - algo.recoTask = @tspawnat algo.params.threadID pinnedRecoTask(algo) - end - finally - unlock(algo.taskLock) - end -end -function pinnedRecoTask(algo::ThreadPinnedAlgorithm) - while isready(algo.inputChannel) - result = nothing - try - put!(algo.params.algo, take!(algo.inputChannel)) - result = take!(algo.params.algo) - catch e - result = e - end - put!(algo.outputChannel, result) - end -end -# TODO general async task, has to preserve order (cant just spawn task for each put) -# TODO Timeout task with timeout options for put and take -# TODO maybe can be cancelled? - export AbstractMultiThreadedProcessing abstract type AbstractMultiThreadedProcessing <: AbstractImageReconstructionAlgorithm end diff --git a/src/MiscAlgorithms/ThreadPinnedAlgorithm.jl b/src/MiscAlgorithms/ThreadPinnedAlgorithm.jl new file mode 100644 index 0000000..cd18526 --- /dev/null +++ b/src/MiscAlgorithms/ThreadPinnedAlgorithm.jl @@ -0,0 +1,43 @@ +export ThreadPinnedAlgorithm, ThreadPinnedAlgorithmParameter +Base.@kwdef struct ThreadPinnedAlgorithmParameter <: AbstractImageReconstructionParameters + threadID::Int64 + algo::AbstractImageReconstructionAlgorithm +end + +mutable struct ThreadPinnedAlgorithm <: AbstractImageReconstructionAlgorithm + params::ThreadPinnedAlgorithmParameter + recoTask::Union{Nothing,Task} + taskLock::ReentrantLock + inputChannel::Channel{Any} + outputChannel::Channel{Any} +end + +ThreadPinnedAlgorithm(params::ThreadPinnedAlgorithmParameter) = ThreadPinnedAlgorithm(params, nothing, ReentrantLock(), Channel{Any}(Inf), Channel{Any}(Inf)) + +take!(algo::ThreadPinnedAlgorithm) = take!(algo.outputChannel) +function put!(algo::ThreadPinnedAlgorithm, u) + put!(algo.inputChannel, u) + lock(algo.taskLock) + try + if isnothing(algo.recoTask) || istaskdone(algo.recoTask) + algo.recoTask = @tspawnat algo.params.threadID pinnedRecoTask(algo) + end + finally + unlock(algo.taskLock) + end +end +function pinnedRecoTask(algo::ThreadPinnedAlgorithm) + while isready(algo.inputChannel) + result = nothing + try + put!(algo.params.algo, take!(algo.inputChannel)) + result = take!(algo.params.algo) + catch e + result = e + end + put!(algo.outputChannel, result) + end +end +# TODO general async task, has to preserve order (cant just spawn task for each put) +# TODO Timeout task with timeout options for put and take +# TODO maybe can be cancelled? \ No newline at end of file