-
Notifications
You must be signed in to change notification settings - Fork 3
/
runtests.jl
210 lines (165 loc) · 6.94 KB
/
runtests.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using ArrayMeta
using Base.Test
import ArrayMeta: indicesinvolved
@testset "utilities" begin
@test indicesinvolved(:(A[i,j,k])) == [:A=>Any[:i,:j,:k]]
@test indicesinvolved(:(A[i,j,k]+B[x,y,z])) == [:A=>Any[:i,:j,:k], :B=>[:x,:y,:z]]
@test indicesinvolved(:(A[i,j,k] |> f)) == [:A=>Any[:i,:j,:k]]
end
import ArrayMeta: Indexing,Map,Reduce,IterSym,IterConst, arraytype, ConstArg
@testset "Indexing" begin
itr = Indexing([1], (IterSym{:i}(),))
@test eltype(typeof(itr)) == Int64
@test arraytype(typeof(itr)) == Array{Int64,1}
end
@testset "Map" begin
itr1 = Indexing([1], (IterSym{:i}()))
itr2 = Indexing([1.], (IterSym{:j}()))
map1 = Map(*, (itr1, itr2))
map2 = Map(/, (ConstArg(1), itr1))
@test eltype(typeof(map1)) == Float64
@test eltype(typeof(map2)) == Float64
@test arraytype(typeof(map1)) <: Array
end
@testset "Reduce" begin
itr1 = Indexing(rand(Int,10), (IterSym{:i}(),))
@test eltype(Reduce(IterSym{:i}(), push!, itr1, Int[])) == Array{Int,1}
end
import ArrayMeta: @lower, ArrayOp
@testset "Lower" begin
A = rand(2,2); B = rand(2,2); C = rand(2,2);
i, j, k = [IterSym{x}() for x in [:i,:j,:k]]
# map
@test @lower(A[i,j] = B[i,j]) == ArrayOp(Indexing(A, (i, j)), Indexing(B, (i, j)))
# transpose
@test @lower(A[i,j] = B[j,i]) == ArrayOp(Indexing(A, (i, j)), Indexing(B, (j, i)))
# reduced over i:
@test @lower(A[j] = B[j,i]) == ArrayOp(Indexing(A, (j,)), Reduce(i, +, Indexing(B, (j, i))))
# reduced over i, output is reducedim
@test @lower(A[1,j] = B[i,j]) == ArrayOp(Indexing(A, (IterConst{Int}(1), j)), Reduce(i, +, Indexing(B, (i, j))))
# reduce both dimensions, use * to reduce i and + to reduce j
@test @lower(A[1,1] = B[i,j], [i=>*,j=>+]) == ArrayOp(Indexing(A, (IterConst{Int}(1), IterConst{Int}(1))),
Reduce(j, +, Reduce(i, *, Indexing(B, (i, j)))))
end
import ArrayMeta: index_spaces
@testset "indexspaces" begin
i,j,k=IterSym{:i}(), IterSym{:j}(), IterSym{:k}()
itr = Indexing(rand(10,10), (i,j))
@test (index_spaces(:X, typeof(itr))|>string ==
Dict{Any,Any}(:i=>Any[(Array{Float64, 2}, 1, :(X.array))],
:j=>Any[(Array{Float64, 2}, 2, :(X.array))]) |> string)
a = Indexing(rand(10,10), (i,k))
b = Indexing(rand(10,10), (k,j))
#equality problems
#@test index_spaces(:X, typeof(Map(*, (a, b)))) ==
# Dict{Any, Any}(:i => Any[(Array{Float64,2}, 1, :(X.Xs[1]))],
# :k => Any[(Array{Float64,2}, 2, :(X.Xs[1])),
# (Array{Float64,2}, 1, :(X.arrays[2]))],
# :j => Any[(Array{Float64,2}, 2, :(X.arrays[2]))],
# )
end
import MacroTools: striplines
import ArrayMeta: kernel_expr
@testset "kernel_expr" begin
@testset "Indexing" begin
X = rand(2,2);
testtype(x) = typeof(x.rhs)
@test kernel_expr(:X, Array{Float64, 2}, testtype(@lower(X[i,j,k] = X[i,j,k])))|>string == :(X.array[i,j,k])|>string
@test kernel_expr(:X, Array{Float64, 2}, testtype(@lower(X[i,j,1] = X[i,j,1])))|>string == :(X.array[i,j,X.idx[3].val])|>string
end
@testset "Map" begin
X = rand(2,2);
Y = rand(2,2);
testtype(x) = typeof(x.rhs)
@test kernel_expr(:X,Array{Float64,2}, testtype(@lower(X[i,j,k] = -Y[i,j,k])))|>string == :(X.f(X.arrays[1].array[i,j,k]))|>string
@test kernel_expr(:X,Array{Float64,2}, testtype(@lower(X[i,j,k] = X[i,k,j]-Y[i,j,k])))|>string == :(X.f(X.arrays[1].array[i,k,j], X.arrays[2].array[i,j,k]))|>string
end
@testset "Reduce" begin
X = rand(2,2);
Y = rand(2,2);
testtype(x) = typeof(x.rhs)
tex = quote
let tmp = start(1:size(X.array.arrays[1].array, 3))
if done(1:size(X.array.arrays[1].array, 3), tmp)
acc = X.empty
else
(k, tmp) = next(1:size(X.array.arrays[1].array, 3), tmp)
acc = X.array.f(X.array.arrays[1].array[i, j, k])
end
while !(done(1:size(X.array.arrays[1].array, 3), tmp))
(k, tmp) = next(1:size(X.array.arrays[1].array, 3), tmp)
acc = X.f(acc, X.array.f(X.array.arrays[1].array[i, j, k]))
end
acc
end
end|>striplines
@test kernel_expr(:X, typeof(X), testtype(@lower(X[i,j] = -Y[i,j,k])))|>striplines|>string == string(tex)
end
end
using Dagger
import ArrayMeta: @arrayop
@testset "@arrayop" begin
X = convert(Array, reshape(1:12, 4,3))
Y = ones(3,4)
# copy
@test @arrayop(_[i,j] := X[i,j]) == X
# transpose
@test @arrayop(_[i,j] := X[j,i]) == X'
# elementwise 1-arg
@test @arrayop(_[i,j] := -X[i,j]) == -X
# elementwise 2-args
@test @arrayop(_[i,j] := X[i,j] + Y[j,i]) == X + Y'
# elementwise with const
#@test @arrayop(_[] := 2 * X[i,j])[] == sum(2.*X)
# reduce default (+)
@test @arrayop(_[] := X[i,j])[] == sum(X)
# reduce with function
@test @arrayop(_[] := X[i,j], [i=>*, j=>*])[] == prod(X)
# reducedim default (+)
@test @arrayop(_[1, j] := X[i,j]) == sum(X, 1)
@test @arrayop(_[i, 1] := X[i,j]) == sum(X, 2)
# reducedim with function
@test @arrayop(_[1, j] := X[i,j], (i=>*,)) == prod(X, 1)
# broadcast
y = [1, 2, 3, 4]
@test @arrayop(_[i, j] := X[i, j] + y[i]) == X .+ y
y = [1 2 3]
@test @arrayop(_[i, j] := X[i, j] + y[j]) == X .+ y
# matmul
@test @arrayop(_[i, j] := X[i,k] * Y[k,j]) == X*Y
end
Base.:(==)(a::ArrayMeta.DArray, b::Array) = gather(a) == b
@testset "@arrayop - Dagger" begin
X = convert(Array, reshape(1:16, 4,4))
dX = compute(Distribute(Blocks(2,2), X))
Y = ones(4,4)
dY = compute(compute(Distribute(Blocks(2,2), Y))')
# copy
@test @arrayop(_[i,j] := dX[i,j]) == X
# transpose
@test @arrayop(_[i,j] := dX[j,i]) == X'
# elementwise 1-arg
@test @arrayop(_[i,j] := -dX[i,j]) == -X
# elementwise 2-args
@test @arrayop(_[i,j] := dX[i,j] + dY[j,i]) == X + Y'
# elementwise with const
#@test @arrayop(_[] := 2 * X[i,j])[] == sum(2.*X)
# reduce default (+)
@test gather(@arrayop(_[1,1] := dX[i,j])) |> first == sum(X)
# reduce with function
@test gather(@arrayop(_[1,1] := dX[i,j], [i=>*, j=>*])) |> first == prod(X)
# reducedim default (+)
@test @arrayop(_[1, j] := dX[i,j]) == sum(X, 1)
@test @arrayop(_[i, 1] := dX[i,j]) == sum(X, 2)
# reducedim with function
@test @arrayop(_[1, j] := dX[i,j], (i=>*,)) == prod(X, 1)
# broadcast
y = [1, 2, 3, 4]
dy = compute(Distribute(Blocks(2), y))
@test @arrayop(_[i, j] := dX[i, j] + dy[i]) == X .+ y
y = [1 2 3 4]
dy = compute(Distribute(Blocks(1,2), y))
@test @arrayop(_[i, j] := dX[i, j] + dy[j]) == X .+ y
# matmul
@test @arrayop(_[i, j] := dX[i,k] * dY[k,j]) == X*Y
end