Polyester.jl icon indicating copy to clipboard operation
Polyester.jl copied to clipboard

Support for cfunction + closures on Apple M1 chip

Open JakobAsslaender opened this issue 3 years ago • 2 comments
trafficstars

Hi everyone,

it seems that the @batch macro in combination with closures does not on the arm64-apple-darwin21.2.0 platform (Apple silicon). Any chance this could be fixed?

Here is a MWE:

using Polyester

function test1()
    a = 5
    function test2()
        return a
    end
    @batch for _ = 1:100
        test2()
    end
    return nothing
end

test1()

which produces this error message:

ERROR: cfunction: closures are not supported on this platform
Stacktrace:

batch_closure at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

macro expansion at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

#_batch_no_reserve#5 at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

(::Polyester.var"#_batch_no_reserve##kw")(::NamedTuple{(:threadlocal,), Tuple{Val{false}}}, ::typeof(Polyester._batch_no_reserve), ::var"#9#11"{var"#test2#10"{Int64}}, ::Tuple{UInt8}, ::Tuple{UInt32}, ::Tuple{UInt8}, ::UInt64, ::UInt64, ::UInt64, ::Static.StaticInt{1}, ::Static.StaticInt{1}, ::Polyester.NoLoop, ::Polyester.CombineIndices) at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

#batch#7 at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

batch at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

macro expansion at [~/.julia/packages/Polyester/4Q0NM/src/closure.jl]()

test1() at [./Untitled-1]()

top-level scope at [Untitled-1]()

ERROR: cfunction: closures are not supported on this platform
Stacktrace:

batch_closure at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

macro expansion at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

#_batch_no_reserve#5 at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

(::Polyester.var"#_batch_no_reserve##kw")(::NamedTuple{(:threadlocal,), Tuple{Val{false}}}, ::typeof(Polyester._batch_no_reserve), ::var"#9#11"{var"#test2#10"{Int64}}, ::Tuple{UInt8}, ::Tuple{UInt32}, ::Tuple{UInt8}, ::UInt64, ::UInt64, ::UInt64, ::Static.StaticInt{1}, ::Static.StaticInt{1}, ::Polyester.NoLoop, ::Polyester.CombineIndices) at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

#batch#7 at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

batch at [~/.julia/packages/Polyester/4Q0NM/src/batch.jl]()

macro expansion at [~/.julia/packages/Polyester/4Q0NM/src/closure.jl]()

test1() at [./Untitled-1]()

top-level scope at [Untitled-1]()

running on this system/Julia version:

julia> versioninfo()
Julia Version 1.8.0-beta1
Commit 7b711ce699 (2022-02-23 15:09 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.2.0)
  CPU: 8 × Apple M1
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
  Threads: 4 on 4 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 4

Thanks a bunch for looking into this!

JakobAsslaender avatar Mar 11 '22 16:03 JakobAsslaender

I get this same error, but intermittent. If I re-run the function to compile it works sometimes. Like, literally do nothing else, just re-run 2 times in a row and it'll work the second time. Weird

kylebeggs avatar Apr 13 '22 14:04 kylebeggs

Yeah, this isn't great. The M1 is popular and ARM is probably going to only get more popular in the future.

I think this should be less frequent in Polyester 0.6.8, as it should now be avoiding CFunctions when it succeeded in hoisting everything that would've been captured into the closure out and turning them into function arguments. If you have examples of this error on 0.6.8+, a work around would perhaps be to strengthen this hoisting.

chriselrod avatar Apr 13 '22 15:04 chriselrod