Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow scalar iteration in the REPL on 1.9+. #450

Merged
merged 4 commits into from
Jun 21, 2023
Merged

Conversation

maleadt
Copy link
Member

@maleadt maleadt commented Feb 6, 2023

Thanks to JuliaLang/julia#48400, we can now detect the REPL task and tweak how scalar indexing is treated. Specifically, I think we should allow scalar iteration in the REPL's frontend task, which is only going to be used to render objects (a common source of scalar iteration). Current GPUArrays already tried to do this by checking isinteractive(), but since that may be a false positive, we were still warning about scalar iteration.

Old behavior, with JLArrays' output functions disabled (and show hence triggering scalar iteration):

julia> jl([1])
1-element JLArray{Int64, 1}:
┌ Warning: Performing scalar indexing on task Task (runnable) @0x00007f0e401b4650.
│ Invocation of getindex resulted in scalar indexing of a GPU array.
│ This is typically caused by calling an iterating implementation of a method.
│ Such implementations *do not* execute on the GPU, but very slowly on the CPU,
│ and therefore should be avoided.
│
│ If you want to allow scalar iteration, use `allowscalar` or `@allowscalar`
│ to enable scalar iteration globally or for the operations in question.
└ @ GPUArraysCore ~/Julia/pkg/GPUArrays/lib/GPUArraysCore/src/GPUArraysCore.jl:85
 1

# trigger scalar iteration on a non-REPL task
julia> jl([1])[1]
┌ Warning: Performing scalar indexing on task Task (runnable) @0x00007f10049fc010.
│ Invocation of getindex resulted in scalar indexing of a GPU array.
│ This is typically caused by calling an iterating implementation of a method.
│ Such implementations *do not* execute on the GPU, but very slowly on the CPU,
│ and therefore should be avoided.
│
│ If you want to allow scalar iteration, use `allowscalar` or `@allowscalar`
│ to enable scalar iteration globally or for the operations in question.
└ @ GPUArraysCore ~/Julia/pkg/GPUArrays/lib/GPUArraysCore/src/GPUArraysCore.jl:85
1

# do that again (to show that the warning is only emitted once)
julia> jl([1])[1]
1

New behavior:

julia> jl([1])
1-element JLArray{Int64, 1}:
 1

julia> jl([1])[1]
ERROR: Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore should be avoided.

If you want to allow scalar iteration, use `allowscalar` or `@allowscalar`
to enable scalar iteration globally or for the operations in question.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] assertscalar(op::String)
   @ GPUArraysCore ~/Julia/pkg/GPUArrays/lib/GPUArraysCore/src/GPUArraysCore.jl:82
 [3] getindex(xs::JLArray{Int64, 1}, I::Int64)
   @ GPUArrays ~/Julia/pkg/GPUArrays/src/host/indexing.jl:9
 [4] top-level scope
   @ REPL[3]:1

Fixes #446

@maleadt maleadt merged commit c628dda into master Jun 21, 2023
@maleadt maleadt deleted the tb/repl_allowscalar branch June 21, 2023 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow scalar iteration in REPL tasks?
1 participant