Skip to content

Cannot use @one_by_one in a nested loop #149

@CarloLucibello

Description

@CarloLucibello

At the moment, it is not possible to use @one_by_one in a loop nested within an outer @tasks loop.
In my use case, where I run MCMC chains in parallel, and collect samples after a burn-in phase, it would be convenient to have this feature.
The code I would like to work looks more or less like this

nsamples = 1000
@tasks for c in 1:nchains
    x = rand([-1, 1], N)
    # burn-in
    run_mcmc!(x; nsweeps=100)
    # collect samples
    while nsamples > 0
        run_mcmc!(x, ξ; nsweeps=10)
        @one_by_one begin
            nsamples -= 1                
            open(file, "a") do io
                writedlm(io, x', ',')
            end
        end
    end
end

for which I get the error

ERROR: LoadError: The @one_by_one macro may only be used inside of a @tasks block.```

Can this limitation be lifted?

Is using a ReentrantLock within the @tasks loop a valid workaround?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions