Git Product home page Git Product logo

Comments (2)

chriselrod avatar chriselrod commented on July 16, 2024

I got

julia> function test()
       A = randn(16, 64)
       C = randn(64, 64)
       for a = 1:1000
           At_mul_A!(C, A)
       end
       end
test (generic function with 1 method)

julia> @time test()
  0.004525 seconds (3 allocations: 40.172 KiB)

julia> @time test()
  0.004688 seconds (3 allocations: 40.172 KiB)

julia> @time test()
  0.004536 seconds (3 allocations: 40.172 KiB)

julia> @time test()
  0.004673 seconds (3 allocations: 40.172 KiB)

julia> @time foreach(_->test(),1:1000)
  4.794809 seconds (5.07 k allocations: 39.368 MiB, 0.20% gc time, 0.09% compilation time)

julia> function test2()
       A = randn(64, 16)' # 16x64
       C = randn(64, 64)
       for a = 1:1000
           At_mul_A!(C, A)
       end
       end
test2 (generic function with 1 method)

julia> @time test2()
  0.001879 seconds (3 allocations: 40.172 KiB)

julia> malloc(): invalid size (unsorted)

[7231] signal (6.-6): Aborted
in expression starting at none:0

It'd be faster to use a different memory layout if possible, which you can see: test2 is over twice as fast as test.
That is, A_mul_At is faster than At_mul_A due to the better memory layout.
This surprises most people who only think about a matrix multiply as a bunch of dot products; i.e. some people naively suspect that setting the layout to make a dot product of a view as fast as possible would be best, but column-major A' * B is actually the worst of the four combinations, even A' * B' is better.
A' * A requires traversing memory much more quickly, increasing bandwdith requirements, and requires reductions at the end of the k loop, requiring extra FLOPs. None of the other 3 orientations need this.

Anyway, I got a segfault, too.

I tried replaces indices with axes, and didn't get a segfault.
It might be a bug in the optimizations it does for indices.

julia> using LoopVectorization

julia> function At_mul_A!(C, A)
          @turbo for n in axes(C, 2), m in axes(C, 1)
              Cmn = zero(eltype(C))
              for k in axes(A, 1)
                  Cmn += A[k,m] * A[k,n]
              end
              C[m,n] = Cmn
          end
       end
At_mul_A! (generic function with 1 method)

julia> function test()
       A = randn(16, 64)
       C = randn(64, 64)
       for a = 1:1000
           At_mul_A!(C, A)
       end
       end
test (generic function with 1 method)

julia> @time test()
  0.004558 seconds (3 allocations: 40.172 KiB)

julia> @time test()
  0.004533 seconds (3 allocations: 40.172 KiB)

julia> @time test()
  0.004505 seconds (3 allocations: 40.172 KiB)

julia> @time test()
  0.004553 seconds (3 allocations: 40.172 KiB)

julia> @time foreach(_->test(),1:1000)
  4.710555 seconds (12.46 k allocations: 39.802 MiB, 0.24% gc time, 0.08% compilation time)

julia> function test2()
       A = randn(64, 16)' # 16x64
       C = randn(64, 64)
       for a = 1:1000
           At_mul_A!(C, A)
       end
       end
test2 (generic function with 1 method)

julia> @time test2()
  0.002044 seconds (3 allocations: 40.172 KiB)

julia> @time test2()
  0.001877 seconds (3 allocations: 40.172 KiB)

julia> @time test2()
  0.001880 seconds (3 allocations: 40.172 KiB)

julia> @time test2()
  0.001868 seconds (3 allocations: 40.172 KiB)

julia> @time foreach(_->test2(),1:1000)
  2.011197 seconds (5.07 k allocations: 39.368 MiB, 0.45% gc time, 0.20% compilation time)

from loopvectorization.jl.

ojwoodford avatar ojwoodford commented on July 16, 2024

Thanks, @chriselrod . Especially for the tips on faster layouts. However, I also get the memory corruption using axes instead of indices.

from loopvectorization.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.