Skip to content

Mention tape_copy in doc or expose a deepcopy flag ?  #139

Closed
@FredericWantiez

Description

@FredericWantiez

From what I understand Libtask does not deepcopy struct by default when used in TapedTask. For example this shares the initial model:

mutable struct Model
    t::TArray
    n::Int
    Model(n::Int) = new(TArray(Float64,n), n)
end

function (model::Model)()
    model.t[1] = 1
    Libtask.produce(model.t[1])
    for i in 2:model.n
        r = rand(Normal())
        model.t[i] = model.t[i-1] + r
        Libtask.produce(model.t[i])
    end
end

model = Model(4)
ttask = Libtask.TapedTask(model)

Libtask.consume(ttask) # -> 1

ttask2 = copy(ttask)

Libtask.consume(ttask)   # -> 1 + r1
Libtask.consume(ttask2) # -> 1 + r1 + r2 (!)

To get the expected result we need to add:

Libtask.tape_copy(model::Model) = deepcopy(model)

which is hidden somewhere (the comment itself is very helpful)

"""
tape_copy(x)
Function `tape_copy` is used to copy data while copying a TapedTask, the
default behavior is: 1. for `Array` and `Dict`, we do `deepcopy`; 2. for
other data types, we do not copy and share the data between tasks, i.e.,
`tape_copy(x) = x`. If one wants some kinds of data to be copied, or
deeply copied, one can add a method to this function.
"""
function tape_copy end

Maybe we could mention it somewhere in the readme since it's required to get AdvancedPS.PG running properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions