Skip to content

Commit 5ca04f0

Browse files
authored
Move core functionality to GPUArraysCore. (#411)
1 parent 855cc97 commit 5ca04f0

File tree

8 files changed

+145
-101
lines changed

8 files changed

+145
-101
lines changed

Manifest.toml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
2828
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
2929
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
3030

31+
[[GPUArraysCore]]
32+
deps = ["Adapt"]
33+
path = "lib/GPUArraysCore"
34+
uuid = "46192b85-c4d5-4398-a991-12ede77f4527"
35+
version = "0.1.0"
36+
3137
[[InteractiveUtils]]
3238
deps = ["Markdown"]
3339
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
@@ -46,9 +52,9 @@ version = "4.7.1"
4652

4753
[[LLVMExtra_jll]]
4854
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
49-
git-tree-sha1 = "62115afed394c016c2d3096c5b85c407b48be96b"
55+
git-tree-sha1 = "67cc5406b15bd04ff72a45f628bec61d36078908"
5056
uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
51-
version = "0.0.13+1"
57+
version = "0.0.13+3"
5258

5359
[[LibCURL]]
5460
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
@@ -112,6 +118,11 @@ uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
112118
deps = ["Serialization"]
113119
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
114120

121+
[[Reexport]]
122+
git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b"
123+
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
124+
version = "1.2.2"
125+
115126
[[SHA]]
116127
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
117128

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ version = "8.3.2"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
7+
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
78
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
89
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1011
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
12+
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1113
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1214
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1315

lib/GPUArraysCore/Manifest.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
[[Adapt]]
4+
deps = ["LinearAlgebra"]
5+
git-tree-sha1 = "af92965fb30777147966f58acb05da51c5616b5f"
6+
uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
7+
version = "3.3.3"
8+
9+
[[Libdl]]
10+
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
11+
12+
[[LinearAlgebra]]
13+
deps = ["Libdl"]
14+
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

lib/GPUArraysCore/Project.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name = "GPUArraysCore"
2+
uuid = "46192b85-c4d5-4398-a991-12ede77f4527"
3+
authors = ["Tim Besard <tim.besard@gmail.com>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

src/GPUArrays.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ using Base.Cartesian
1111
using Adapt
1212
using LLVM.Interop
1313

14+
using Reexport
15+
@reexport using GPUArraysCore
16+
1417
# device functionality
1518
include("device/execution.jl")
1619
## executed on-device

src/host/abstractarray.jl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
# core definition of the AbstractGPUArray type
22

3-
export AbstractGPUArray
4-
5-
"""
6-
AbstractGPUArray{T, N} <: DenseArray{T, N}
7-
8-
Supertype for `N`-dimensional GPU arrays (or array-like types) with elements of type `T`.
9-
Instances of this type are expected to live on the host, see [`AbstractDeviceArray`](@ref)
10-
for device-side objects.
11-
"""
12-
abstract type AbstractGPUArray{T, N} <: DenseArray{T, N} end
13-
14-
const AbstractGPUVector{T} = AbstractGPUArray{T, 1}
15-
const AbstractGPUMatrix{T} = AbstractGPUArray{T, 2}
16-
const AbstractGPUVecOrMat{T} = Union{AbstractGPUArray{T, 1}, AbstractGPUArray{T, 2}}
17-
18-
19-
# convenience aliases for working with wrapped arrays
20-
21-
const WrappedGPUArray{T,N} = WrappedArray{T,N,AbstractGPUArray,AbstractGPUArray{T,N}}
22-
23-
const AnyGPUArray{T,N} = Union{AbstractGPUArray{T,N}, WrappedGPUArray{T,N}}
24-
253

264
# input/output
275

src/host/indexing.jl

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,5 @@
11
# host-level indexing
22

3-
export allowscalar, @allowscalar, assertscalar
4-
5-
6-
# mechanism to disallow scalar operations
7-
8-
@enum ScalarIndexing ScalarAllowed ScalarWarn ScalarWarned ScalarDisallowed
9-
10-
# if the user explicitly calls allowscalar, use that setting for all new tasks
11-
# XXX: use context variables to inherit the parent task's setting, once available.
12-
const default_scalar_indexing = Ref{Union{Nothing,ScalarIndexing}}(nothing)
13-
14-
"""
15-
allowscalar() do
16-
# code that can use scalar indexing
17-
end
18-
19-
Denote which operations can use scalar indexing.
20-
21-
See also: [`@allowscalar`](@ref).
22-
"""
23-
function allowscalar(f::Base.Callable)
24-
task_local_storage(f, :ScalarIndexing, ScalarAllowed)
25-
end
26-
27-
function allowscalar(allow::Bool=true)
28-
if allow
29-
Base.depwarn("allowscalar([true]) is deprecated, use `allowscalar() do end` or `@allowscalar` to denote exactly which operations can use scalar operations.", :allowscalar)
30-
end
31-
setting = allow ? ScalarAllowed : ScalarDisallowed
32-
task_local_storage(:ScalarIndexing, setting)
33-
default_scalar_indexing[] = setting
34-
return
35-
end
36-
37-
"""
38-
assertscalar(op::String)
39-
40-
Assert that a certain operation `op` performs scalar indexing. If this is not allowed, an
41-
error will be thrown ([`allowscalar`](@ref)).
42-
"""
43-
function assertscalar(op = "operation")
44-
val = get!(task_local_storage(), :ScalarIndexing) do
45-
something(default_scalar_indexing[], isinteractive() ? ScalarWarn : ScalarDisallowed)
46-
end
47-
desc = """Invocation of $op resulted in scalar indexing of a GPU array.
48-
This is typically caused by calling an iterating implementation of a method.
49-
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
50-
and therefore are only permitted from the REPL for prototyping purposes.
51-
If you did intend to index this array, annotate the caller with @allowscalar."""
52-
if val == ScalarDisallowed
53-
error("""Scalar indexing is disallowed.
54-
$desc""")
55-
elseif val == ScalarWarn
56-
@warn("""Performing scalar indexing on task $(current_task()).
57-
$desc""")
58-
task_local_storage(:ScalarIndexing, ScalarWarned)
59-
end
60-
return
61-
end
62-
63-
"""
64-
@allowscalar() begin
65-
# code that can use scalar indexing
66-
end
67-
68-
Denote which operations can use scalar indexing.
69-
70-
See also: [`allowscalar`](@ref).
71-
"""
72-
macro allowscalar(ex)
73-
quote
74-
task_local_storage(:ScalarIndexing, ScalarAllowed) do
75-
$(esc(ex))
76-
end
77-
end
78-
end
79-
803

814
# basic indexing with integers
825

0 commit comments

Comments
 (0)