@inbounds macro scope, and @simd

152 views
Skip to first unread message

Ben Ward

unread,
Aug 26, 2016, 1:43:08 PM8/26/16
to julia-users
Hi, Just wondering, I have the following doube loop:

for site in 1:nsites
        votes[1] = votes[2] = votes[3] = votes[4] = votes[5] = 0
        for seq in 1:neqs
            nuc = mat[seq, site]
            votes[1] += iscompatible(nuc, DNA_A)
            votes[2] += iscompatible(nuc, DNA_C)
            votes[3] += iscompatible(nuc, DNA_G)
            votes[4] += iscompatible(nuc, DNA_T)
            votes[5] += iscompatible(nuc, DNA_Gap)
        end
    end

Say I add an @inbounds macro to the outer loop to eliminate bounds checks, will it's effects extend to setmatrix statements in the inner loop. Inspecting the expanded macro I believe it is the case, as an @inbounds is set to true, and then after the inner loop is popped. But I'm not 100% sure if I am correct that is indeed how it works:

quote  # REPL[63], line 2:

    begin 

        $(Expr(:inbounds, true))

        for site = 1:nsites # REPL[63], line 3:

            votes[1] = (votes[2] = (votes[3] = (votes[4] = (votes[5] = 0)))) # REPL[63], line 4:

            for seq = 1:neqs # REPL[63], line 5:

                nuc = mat[seq,site] # REPL[63], line 6:

                votes[1] += iscompatible(nuc,DNA_A) # REPL[63], line 7:

                votes[2] += iscompatible(nuc,DNA_C) # REPL[63], line 8:

                votes[3] += iscompatible(nuc,DNA_G) # REPL[63], line 9:

                votes[4] += iscompatible(nuc,DNA_T) # REPL[63], line 10:

                votes[5] += iscompatible(nuc,DNA_Gap)

            end

        end

        $(Expr(:inbounds, :pop))

    end

end


I'd also like someone's opinion. Will I benefit from @simd on the inner loop?

The function `iscompatible` is annotated with @inline, and has no branching:

@inline function iscompatible{T<:Nucleotide}(x::T, y::T)
    return compatbits(x) & compatbits(y) != 0
end

# Return the compatibility bits of `nt`.
@inline function compatbits(nt::Nucleotide)
    return reinterpret(UInt8, nt)
end

As per the assumptions of an simd loop I read in the docs, each iteration is independent, order does not matter.
I'd just like some advice as if I'm right this will be the first time I use a simd loop to speed up my loops. 

Thanks,
Ben.

Blake Johnson

unread,
Aug 27, 2016, 6:41:12 PM8/27/16
to julia-users
Yes, if you change the first line to

@inbounds for site in 1:nsites

Then that will declare everything inside that outer loop as being in bounds. If you'd like to make a more restricted declaration, than you should put it on specific lines inside the loop.

Chris Rackauckas

unread,
Aug 27, 2016, 8:16:52 PM8/27/16
to julia-users
Reply all
Reply to author
Forward
0 new messages