Skip to content

Conversation

@FredericWantiez
Copy link
Member

In AdvancedPS we often need to mutate the input of the the TapedFunction to handle random streams. It would be great if we could do something like:

function (model::Model) (rng::Random.AbstractRNG)
   ...
end

rng1 = Random.MersenneTwister()
task = Litbtask.TapedTask(model, rng1)

Libtask.consume(task)

new_rng = Random.MersenneTwister()
task2 = Libtask.copy(task, new_rng)

Libtask.consume(task) # Generates random numbers from rng1
Libtask.consume(task2) # Generates random numbers from new_rng

but for this to work we need to update the args... and bindings appropriately when we copy the running task.

This is just a draft, I'm not sure about all the implications of this change, especially with #138

Copy link
Member

@KDr2 KDr2 left a comment

Choose a reason for hiding this comment

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

I think storing the args in TapedTask will be more intuitive and it can keep the API unchanged.

diff --git a/src/tapedtask.jl b/src/tapedtask.jl
index 71656a3..8b10506 100644
--- a/src/tapedtask.jl
+++ b/src/tapedtask.jl
@@ -6,6 +6,7 @@ end
 struct TapedTask{F}
     task::Task
     tf::TapedFunction{F}
+    args
     produce_ch::Channel{Any}
     consume_ch::Channel{Int}
     produced_val::Vector{Any}
@@ -13,10 +14,11 @@ struct TapedTask{F}
     function TapedTask(
         t::Task,
         tf::TapedFunction{F},
+        args,
         produce_ch::Channel{Any},
         consume_ch::Channel{Int}
     ) where {F}
-        new{F}(t, tf, produce_ch, consume_ch, Any[])
+        new{F}(t, tf, args, produce_ch, consume_ch, Any[])
     end
 end

@@ -161,7 +163,7 @@ Base.IteratorEltype(::Type{<:TapedTask}) = Base.EltypeUnknown()

 function Base.copy(t::TapedTask)
     tf = copy(t.tf)
-    new_t = TapedTask(tf)
+    args = do_tape_copy_on(t.args...) # !!!
+    new_t = TapedTask(tf, args...) 
     storage = t.task.storage::IdDict{Any,Any}
     new_t.task.storage = copy(storage)
     new_t.task.storage[:tapedtask] = new_t

@KDr2 KDr2 linked an issue May 8, 2022 that may be closed by this pull request
Copy link
Member

@yebai yebai left a comment

Choose a reason for hiding this comment

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

Many thanks, @KDr2 and @FredericWantiez- it looks good and I'm happy to merge once CI passes.

@yebai yebai merged commit 87262d6 into master May 10, 2022
@yebai yebai deleted the update_args branch May 10, 2022 20:44
@FredericWantiez FredericWantiez mentioned this pull request May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with copy and new TapedTask

4 participants