Code Monkey home page Code Monkey logo

Comments (3)

christiangnrd avatar christiangnrd commented on September 28, 2024

I can reproduce on M2 Max Julia 1.10 and 1.11.

Take between 350 and 23000 iterations to reproduce using the code below.

count = 0
for _ in 1:50000
    for typ in [Float16, Float32]
        function kernel(a::MtlDeviceArray{T}, b::MtlDeviceArray{T}) where {T}
            pos = thread_position_in_threadgroup_2d()

            tg_a = MtlThreadGroupArray(T, (8, 8))
            tg_a[pos.x, pos.y] = a[pos.x, pos.y]
            sg_a = simdgroup_load(tg_a)

            tg_b = MtlThreadGroupArray(T, (8, 8))
            simdgroup_store(sg_a, tg_b)
            b[pos.x, pos.y] = tg_b[pos.x, pos.y]

            return
        end

        a = MtlArray(rand(typ, 8, 8))
        b = MtlArray(zeros(typ, 8, 8))
        @metal threads=(8, 8) kernel(a, b)
        if Array(a) != Array(b)
            @test Array(a) == Array(b)
        end
    end
    count += 1
end

from metal.jl.

christiangnrd avatar christiangnrd commented on September 28, 2024

This didn't fail for 1000000 iterations just by adding threadgroup_barrier(Metal.MemoryFlagThreadGroup) as shown below. Is this just a fluke? I still don't fully understand what barriers do so I don't know if this actually fixes the problem or just hides it.

count = 0
for _ in 1:50000
    for typ in [Float16, Float32]
        function kernel(a::MtlDeviceArray{T}, b::MtlDeviceArray{T}) where {T}
            pos = thread_position_in_threadgroup_2d()

            tg_a = MtlThreadGroupArray(T, (8, 8))
            tg_a[pos.x, pos.y] = a[pos.x, pos.y]
            threadgroup_barrier(Metal.MemoryFlagThreadGroup)
            sg_a = simdgroup_load(tg_a)

            tg_b = MtlThreadGroupArray(T, (8, 8))
            simdgroup_store(sg_a, tg_b)
            b[pos.x, pos.y] = tg_b[pos.x, pos.y]

            return
        end

        a = MtlArray(rand(typ, 8, 8))
        b = MtlArray(zeros(typ, 8, 8))
        @metal threads=(8, 8) kernel(a, b)
        if Array(a) != Array(b)
            @test Array(a) == Array(b)
        end
    end
    count += 1
end

from metal.jl.

maleadt avatar maleadt commented on September 28, 2024

Yeah, that does seem correct. I guess the simdgroup_load could result in a different thread reading the threadgroup array, so we need a memory fence for the threagroup memory (aka. threadgroup_barrier(Metal.MemoryFlagThreadGroup)) to ensure the correct memory is read by the SIMD operation afterwards.

Possibly we need another fence after the simdgroup_store, since we're not directly having the wavefront store to device memory, but go through the threadgroup array first. I'll add one to be sure.

from metal.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.