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

at-batch iterating over more general iterators

Open ranocha opened this issue 4 years ago • 2 comments

I got another edge case for @batch:

julia> using Pkg; Pkg.status("CheapThreads")
      Status `/tmp/jl_es2knB/Project.toml`
  [b630d9fa] CheapThreads v0.2.0

julia> Threads.nthreads()
2

julia> using CheapThreads, Test

julia> function issue18!(dest)
           @assert length(dest) == 3
           @assert dest[1] == 1
           @batch for i in 2:3
               dest[i] = i
           end
           @test dest ≈ 1:3
       end
issue18! (generic function with 1 method)

julia> dest = ones(3)
3-element Vector{Float64}:
 1.0
 1.0
 1.0

julia> issue18!(dest)
Test Failed at REPL[7]:7
  Expression: dest ≈ 1:3
   Evaluated: [1.0, 1.0, 1.0] ≈ 1:3
ERROR: There was an error during testing

I would have expected the same output as from

julia> function issue18!(dest)
           @assert length(dest) == 3
           @assert dest[1] == 1
           Threads.@threads for i in 2:3
               dest[i] = i
           end
           @test dest ≈ 1:3
       end
issue18! (generic function with 1 method)

julia> issue18!(dest)
Test Passed

Another related issue: I noticed that @batch requires the iterator to be an OrdinalRange (or something similar implementing step). That's of course a good way for performance optimizations and therefore a reasonable restriction. Some syntactic sugar might be to also allow iterating over v::Vectors (or v::AbstractArrays, maybe restricted to ones using linear indexing) by reducing that to an iteration over eachindex(v). But that's also something I can easily work around in my applications.

ranocha avatar Apr 05 '21 09:04 ranocha

Fixed the example issue and added it to the tests.

But that's also something I can easily work around in my applications.

Or make a PR here.

chriselrod avatar Apr 05 '21 09:04 chriselrod

Great, thanks :+1:

ranocha avatar Apr 05 '21 10:04 ranocha