Skip to content

Commit

Permalink
allowscalar
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Jan 21, 2025
1 parent 1886884 commit dbb5387
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
27 changes: 15 additions & 12 deletions src/scalar.jl
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
# Manual control over scalar indexing
const ALLOW_SCALAR = Ref{Bool}(true)
const ALLOWSCALAR = Ref{Bool}(true)

"""
allow_scalar(x::Bool)
allowscalar(x::Bool)
Specify if a disk array can do scalar indexing, (with all `Int` arguments).
Setting `allow_scalar(false)` can help identify the cause of poor performance.
Setting `allowscalar(false)` can help identify the cause of poor performance.
"""
allow_scalar(x::Bool) = ALLOW_SCALAR[] = x
allowscalar(x::Bool) = ALLOWSCALAR[] = x

"""
can_scalar()
canscalar()
Check if DiskArrays is set to allow scalar indexing, with [`allow_scalar`](@ref).
Check if DiskArrays is set to allow scalar indexing, with [`allowscalar`](@ref).
Returns a `Bool`.
"""
can_scalar() = ALLOW_SCALAR[]
canscalar() = ALLOWSCALAR[]

function _scalar_error()
return error(
"Scalar indexing with `Int` is very slow, and currently is disallowed. Run DiskArrays.allow_scalar(true) to allow",
)
end
@deprecate allow_scalar allowscalar
@deprecate can_scalar canscalar

# Checks if an index is scalar at all, and then if scalar indexing is allowed.
# Syntax as for `checkbounds`.
checkscalar(::Type{Bool}, I::Tuple) = checkscalar(Bool, I...)
checkscalar(::Type{Bool}, I...) = !all(map(i -> i isa Int, I)) || can_scalar()
checkscalar(I::Tuple) = checkscalar(I...)
checkscalar(I...) = checkscalar(Bool, I...) || _scalar_error()

function _scalar_error()
return error(
"Scalar indexing with `Int` is very slow, and currently is disallowed. Run DiskArrays.allowscalar(true) to allow",
)
end
12 changes: 6 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ if VERSION >= v"1.9.0"
Aqua.test_deps_compat(DiskArrays)
end

@testset "allow_scalar" begin
DiskArrays.allow_scalar(false)
@testset "allowscalar" begin
DiskArrays.allowscalar(false)
@test DiskArrays.can_scalar() == false
@test DiskArrays.checkscalar(Bool, 1, 2, 3) == false
@test DiskArrays.checkscalar(Bool, 1, 2:5, :) == true
DiskArrays.allow_scalar(true)
DiskArrays.allowscalar(true)
@test DiskArrays.can_scalar() == true
@test DiskArrays.checkscalar(Bool, 1, 2, 3) == true
@test DiskArrays.checkscalar(Bool, :, 2:5, 3) == true
Expand Down Expand Up @@ -72,11 +72,11 @@ function test_getindex(a)
# Test that readblock was called exactly onces for every getindex
@test a[2:2:4, 1:2:5] == [2 10 18; 4 12 20]
@test a[[1, 3, 4], [1, 3], 1] == [1 9; 3 11; 4 12]
@testset "allow_scalar" begin
DiskArrays.allow_scalar(false)
@testset "allowscalar" begin
DiskArrays.allowscalar(false)
@test_throws ErrorException a[2, 3, 1]
@test_throws ErrorException a[5]
DiskArrays.allow_scalar(true)
DiskArrays.allowscalar(true)
@test a[2, 3, 1] == 10
@test a[5] == 5
end
Expand Down

0 comments on commit dbb5387

Please sign in to comment.