|
| 1 | +module GPUArraysCore |
| 2 | + |
| 3 | +using Adapt |
| 4 | + |
| 5 | + |
| 6 | +## essential types |
| 7 | + |
| 8 | +export AbstractGPUArray, AbstractGPUVector, AbstractGPUMatrix, AbstractGPUVecOrMat, |
| 9 | + WrappedGPUArray, AnyGPUArray |
| 10 | + |
| 11 | +""" |
| 12 | + AbstractGPUArray{T, N} <: DenseArray{T, N} |
| 13 | +
|
| 14 | +Supertype for `N`-dimensional GPU arrays (or array-like types) with elements of type `T`. |
| 15 | +Instances of this type are expected to live on the host, see [`AbstractDeviceArray`](@ref) |
| 16 | +for device-side objects. |
| 17 | +""" |
| 18 | +abstract type AbstractGPUArray{T, N} <: DenseArray{T, N} end |
| 19 | + |
| 20 | +const AbstractGPUVector{T} = AbstractGPUArray{T, 1} |
| 21 | +const AbstractGPUMatrix{T} = AbstractGPUArray{T, 2} |
| 22 | +const AbstractGPUVecOrMat{T} = Union{AbstractGPUArray{T, 1}, AbstractGPUArray{T, 2}} |
| 23 | + |
| 24 | +# convenience aliases for working with wrapped arrays |
| 25 | +const WrappedGPUArray{T,N} = WrappedArray{T,N,AbstractGPUArray,AbstractGPUArray{T,N}} |
| 26 | +const AnyGPUArray{T,N} = Union{AbstractGPUArray{T,N}, WrappedGPUArray{T,N}} |
| 27 | + |
| 28 | + |
| 29 | +## scalar iteration |
| 30 | + |
| 31 | +export allowscalar, @allowscalar, assertscalar |
| 32 | + |
| 33 | +@enum ScalarIndexing ScalarAllowed ScalarWarn ScalarWarned ScalarDisallowed |
| 34 | + |
| 35 | +# if the user explicitly calls allowscalar, use that setting for all new tasks |
| 36 | +# XXX: use context variables to inherit the parent task's setting, once available. |
| 37 | +const default_scalar_indexing = Ref{Union{Nothing,ScalarIndexing}}(nothing) |
| 38 | + |
| 39 | +""" |
| 40 | + allowscalar() do |
| 41 | + # code that can use scalar indexing |
| 42 | + end |
| 43 | +
|
| 44 | +Denote which operations can use scalar indexing. |
| 45 | +
|
| 46 | +See also: [`@allowscalar`](@ref). |
| 47 | +""" |
| 48 | +function allowscalar(f::Base.Callable) |
| 49 | + task_local_storage(f, :ScalarIndexing, ScalarAllowed) |
| 50 | +end |
| 51 | + |
| 52 | +function allowscalar(allow::Bool=true) |
| 53 | + if allow |
| 54 | + Base.depwarn("allowscalar([true]) is deprecated, use `allowscalar() do end` or `@allowscalar` to denote exactly which operations can use scalar operations.", :allowscalar) |
| 55 | + end |
| 56 | + setting = allow ? ScalarAllowed : ScalarDisallowed |
| 57 | + task_local_storage(:ScalarIndexing, setting) |
| 58 | + default_scalar_indexing[] = setting |
| 59 | + return |
| 60 | +end |
| 61 | + |
| 62 | +""" |
| 63 | + assertscalar(op::String) |
| 64 | +
|
| 65 | +Assert that a certain operation `op` performs scalar indexing. If this is not allowed, an |
| 66 | +error will be thrown ([`allowscalar`](@ref)). |
| 67 | +""" |
| 68 | +function assertscalar(op = "operation") |
| 69 | + val = get!(task_local_storage(), :ScalarIndexing) do |
| 70 | + something(default_scalar_indexing[], isinteractive() ? ScalarWarn : ScalarDisallowed) |
| 71 | + end |
| 72 | + desc = """Invocation of $op resulted in scalar indexing of a GPU array. |
| 73 | + This is typically caused by calling an iterating implementation of a method. |
| 74 | + Such implementations *do not* execute on the GPU, but very slowly on the CPU, |
| 75 | + and therefore are only permitted from the REPL for prototyping purposes. |
| 76 | + If you did intend to index this array, annotate the caller with @allowscalar.""" |
| 77 | + if val == ScalarDisallowed |
| 78 | + error("""Scalar indexing is disallowed. |
| 79 | + $desc""") |
| 80 | + elseif val == ScalarWarn |
| 81 | + @warn("""Performing scalar indexing on task $(current_task()). |
| 82 | + $desc""") |
| 83 | + task_local_storage(:ScalarIndexing, ScalarWarned) |
| 84 | + end |
| 85 | + return |
| 86 | +end |
| 87 | + |
| 88 | +""" |
| 89 | + @allowscalar() begin |
| 90 | + # code that can use scalar indexing |
| 91 | + end |
| 92 | +
|
| 93 | +Denote which operations can use scalar indexing. |
| 94 | +
|
| 95 | +See also: [`allowscalar`](@ref). |
| 96 | +""" |
| 97 | +macro allowscalar(ex) |
| 98 | + quote |
| 99 | + task_local_storage(:ScalarIndexing, ScalarAllowed) do |
| 100 | + $(esc(ex)) |
| 101 | + end |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | + |
| 106 | +end # module GPUArraysCore |
0 commit comments