Enumerate not suported for @threads

When trying to use @threads on a for loop with enumerate, an error is thrown

Threads.@threads for (idx, a) in enumerate(A)
    ... # Process
end

ERROR: LoadError: TaskFailedException:
MethodError: no method matching firstindex(::Base.Iterators.Enumerate{Array{Int64,1}})

Is there something I should be aware of, or a workaround? Or is this a bug in Julia?

3 Likes

Try replacing with collect(enumerate(A)) instead. Its just that @threads doesn’t work with generic iterables like the output of enumerate, it needs some concrete array-like thing which has a length and firstindex.

7 Likes

FYI, ThreadsX.foreach supports enumerate

ThreadsX.foreach(enumerate(A)) do (idx, a)
    ...  # Process
end
7 Likes