Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

should become an atomic int when we care about multithreading #229

Open
github-actions bot opened this issue Mar 6, 2025 · 0 comments
Open

should become an atomic int when we care about multithreading #229

github-actions bot opened this issue Mar 6, 2025 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Mar 6, 2025

The struct def for constructing a new Boolean variable f with the given probability

so you can assign f = Flip(0.6, "x") --> f is a new probabilistic Boolean variable

p = f.prob

p = if p isa AbstractFloat round(p, digits=2) else p end

if isnothing(f.name)

print(io, "$(typeof(f))($(f.global_id),$(p))")

else

print(io, "$(typeof(f))($(f.global_id),$(p),$(f.name))")

end

end

(is returned from this function?)

# TODO should become an atomic int when we care about multithreading

const AnyBool = Union{Dist{Bool}, Bool}

# TODO should become an atomic int when we care about multithreading
global_flip_id::Int64 = one(Int64)
    # number field on 'sampling branch'

# Compilation into wbf
#   The struct def for constructing a new Boolean variable f with the given probability
#       so you can assign f = Flip(0.6, "x") --> f is a new probabilistic Boolean variable 
mutable struct Flip <: Dist{Bool}
    const global_id::Int
    prob
    const name
    ## NEW ## - note that bit_index 0 = MSB
    bit_index::Union{Nothing, Int}
    ordering::Int
    ## End NEW ##
    
    Flip(prob, name, bit_index) = begin
        if prob isa Real
            @assert !isone(prob) "Use `true` for deterministic flips"
            @assert !iszero(prob) "Use `false` for deterministic flips"
            @assert isnan(prob) || 0 < prob < 1 "Probabilities are between 0 and 1 (or undefined as NaN)"
        end
        global global_flip_id
        id = global_flip_id+=1
        new(id, prob, name, bit_index, id)
    end
end

# Modified SHOW to include orderings
function Base.show(io::IO, f::Flip)
    p = f.prob
    p = if p isa AbstractFloat round(p, digits=2) else p end
    if isnothing(f.name)
        print(io, "$(typeof(f))($(f.global_id),$(p), ordering=$(f.ordering)), bit_index=$(f.bit_index))")
    else
        print(io, "$(typeof(f))($(f.global_id),$(p),$(f.name), ordering=$(f.ordering)), bit_index=$(f.bit_index))")
    end
end

# function Base.show(io::IO, f::Flip)
#     p = f.prob
#     p = if p isa AbstractFloat round(p, digits=2) else p end
#     if isnothing(f.name)
#         print(io, "$(typeof(f))($(f.global_id),$(p))")
#     else
#         print(io, "$(typeof(f))($(f.global_id),$(p),$(f.name))")
#     end
# end

"Create a Bernoulli random variable with the given probability (a coin flip)"
#   C-FLIP (coin flip compilation)
#   (is returned from this function?)
function flip(prob = NaN16; name = nothing, bit_index = nothing)
    if prob isa Real
        iszero(prob) && return false
        isone(prob) && return true
    end
    Flip(prob, name, bit_index)
end

"Set the probability of a flip that has not been assigned a probability yet"
@github-actions github-actions bot added the todo label Mar 6, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants