1
1
# Very simple Julia back-end which is just for testing the implementation and can be used as
2
2
# a reference implementation
3
3
4
-
5
- # # construction
6
-
7
4
struct JLArray{T, N} <: GPUArray{T, N}
8
5
data:: Array{T, N}
9
- size :: Dims{N}
6
+ dims :: Dims{N}
10
7
11
- function JLArray {T,N} (data:: Array{T, N} , size :: NTuple{N, Int } ) where {T,N}
12
- new (data, size )
8
+ function JLArray {T,N} (data:: Array{T, N} , dims :: Dims{N } ) where {T,N}
9
+ new (data, dims )
13
10
end
14
11
end
15
12
16
- JLArray (data:: AbstractArray{T, N} , size:: Dims{N} ) where {T,N} = JLArray {T,N} (data, size)
17
13
18
- (:: Type{<: JLArray{T}} )(x:: AbstractArray ) where T = JLArray (convert (Array{T}, x), size (x))
14
+ # # construction
15
+
16
+ # type and dimensionality specified, accepting dims as tuples of Ints
17
+ JLArray {T,N} (:: UndefInitializer , dims:: Dims{N} ) where {T,N} =
18
+ JLArray {T,N} (Array {T, N} (undef, dims), dims)
19
19
20
- function JLArray {T, N} (size:: NTuple{N, Integer} ) where {T, N}
21
- JLArray {T, N} (Array {T, N} (undef, size), size)
22
- end
20
+ # type and dimensionality specified, accepting dims as series of Ints
21
+ JLArray {T,N} (:: UndefInitializer , dims:: Integer... ) where {T,N} = JLArray {T,N} (undef, dims)
23
22
24
- struct JLBackend <: GPUBackend end
25
- backend (:: Type{<:JLArray} ) = JLBackend ()
23
+ # type but not dimensionality specified
24
+ JLArray {T} (:: UndefInitializer , dims:: Dims{N} ) where {T,N} = JLArray {T,N} (undef, dims)
25
+ JLArray {T} (:: UndefInitializer , dims:: Integer... ) where {T} =
26
+ JLArray {T} (undef, convert (Tuple{Vararg{Int}}, dims))
27
+
28
+ # empty vector constructor
29
+ JLArray {T,1} () where {T} = JLArray {T,1} (undef, 0 )
30
+
31
+
32
+ Base. similar (a:: JLArray{T,N} ) where {T,N} = JLArray {T,N} (undef, size (a))
33
+ Base. similar (a:: JLArray{T} , dims:: Base.Dims{N} ) where {T,N} = JLArray {T,N} (undef, dims)
34
+ Base. similar (a:: JLArray , :: Type{T} , dims:: Base.Dims{N} ) where {T,N} = JLArray {T,N} (undef, dims)
35
+
36
+
37
+ # # array interface
38
+
39
+ Base. elsize (:: Type{<:JLArray{T}} ) where {T} = sizeof (T)
40
+
41
+ Base. size (x:: JLArray ) = x. dims
42
+ Base. sizeof (x:: JLArray ) = Base. elsize (x) * length (x)
26
43
27
- # # getters
28
44
29
- Base . size (x :: JLArray ) = x . size
45
+ # # interop with other arrays
30
46
31
- Base. pointer (x:: JLArray ) = pointer (x. data)
47
+ JLArray {T,N} (x:: AbstractArray{S,N} ) where {T,N,S} =
48
+ JLArray {T,N} (convert (Array{T}, x), size (x))
32
49
50
+ # underspecified constructors
51
+ JLArray {T} (xs:: AbstractArray{S,N} ) where {T,N,S} = JLArray {T,N} (xs)
52
+ (:: Type{JLArray{T,N} where T} )(x:: AbstractArray{S,N} ) where {S,N} = JLArray {S,N} (x)
53
+ JLArray (A:: AbstractArray{T,N} ) where {T,N} = JLArray {T,N} (A)
33
54
34
- # # other
55
+ # idempotency
56
+ JLArray {T,N} (xs:: JLArray{T,N} ) where {T,N} = xs
57
+
58
+
59
+ # # conversions
60
+
61
+ Base. convert (:: Type{T} , x:: T ) where T <: JLArray = x
62
+
63
+
64
+ # # broadcast
65
+
66
+ BroadcastStyle (:: Type{<:JLArray} ) = ArrayStyle {JLArray} ()
67
+
68
+ function Base. similar (bc:: Broadcasted{ArrayStyle{JLArray}} , :: Type{T} ) where T
69
+ similar (JLArray{T}, axes (bc))
70
+ end
71
+
72
+
73
+ # # gpuarray interface
74
+
75
+ struct JLBackend <: GPUBackend end
76
+ backend (:: Type{<:JLArray} ) = JLBackend ()
35
77
36
78
"""
37
79
Thread group local memory
@@ -51,8 +93,6 @@ to_blocks(state, x) = x
51
93
# unpacks local memory for each block
52
94
to_blocks (state, x:: LocalMem ) = x. x[blockidx_x (state)]
53
95
54
- Base. similar (:: Type{<: JLArray} , :: Type{T} , size:: Base.Dims{N} ) where {T, N} = JLArray {T, N} (size)
55
-
56
96
unsafe_reinterpret (:: Type{T} , A:: JLArray , size:: Tuple ) where T =
57
97
reshape (reinterpret (T, A. data), size)
58
98
0 commit comments