1
- using LinearAlgebra, SparseArrays, Random, SharedArrays
2
- import Dagger: DArray, chunks, domainchunks, treereduce_nd
3
- import Distributed: myid, procs
4
- import Statistics: mean, var, std
5
- import OnlineStats
6
-
7
1
@testset " treereduce_nd" begin
8
2
xs = rand (1 : 10 , 8 ,8 ,8 )
9
3
concats = [(x... )-> cat (x... , dims= n) for n in 1 : 3 ]
80
74
end
81
75
end
82
76
83
- function test_mapreduce (f, init_func; no_init= true , zero_init= zero,
84
- types= (Int32, Int64, Float32, Float64),
85
- cmp= isapprox)
86
- @testset " $T " for T in types
87
- X = init_func (Blocks (10 , 10 ), T, 100 , 100 )
88
- inits = ()
89
- if no_init
90
- inits = (inits... , nothing )
91
- end
92
- if zero_init != = nothing
93
- inits = (inits... , zero_init (T))
94
- end
95
- @testset " dims=$dims " for dims in (Colon (), 1 , 2 , (1 ,), (2 ,))
96
- @testset " init=$init " for init in inits
97
- if init === nothing
98
- if dims == Colon ()
99
- @test cmp (f (X; dims), f (collect (X); dims))
100
- else
101
- @test cmp (collect (f (X; dims)), f (collect (X); dims))
102
- end
103
- else
104
- if dims == Colon ()
105
- @test cmp (f (X; dims, init), f (collect (X); dims, init))
106
- else
107
- @test cmp (collect (f (X; dims, init)), f (collect (X); dims, init))
108
- end
109
- end
110
- end
111
- end
112
- end
113
- end
114
-
115
- # Base
116
- @testset " reduce" test_mapreduce ((X; dims, init= Base. _InitialValue ())-> reduce (+ , X; dims, init), ones)
117
- @testset " mapreduce" test_mapreduce ((X; dims, init= Base. _InitialValue ())-> mapreduce (x-> x+ 1 , + , X; dims, init), ones)
118
- @testset " sum" test_mapreduce (sum, ones)
119
- @testset " prod" test_mapreduce (prod, rand)
120
- @testset " minimum" test_mapreduce (minimum, rand)
121
- @testset " maximum" test_mapreduce (maximum, rand)
122
- @testset " extrema" test_mapreduce (extrema, rand; cmp= Base.:(== ), zero_init= T-> (zero (T), zero (T)))
123
-
124
- # Statistics
125
- @testset " mean" test_mapreduce (mean, rand; zero_init= nothing , types= (Float32, Float64))
126
- @testset " var" test_mapreduce (var, rand; zero_init= nothing , types= (Float32, Float64))
127
- @testset " std" test_mapreduce (std, rand; zero_init= nothing , types= (Float32, Float64))
128
-
129
77
@testset " broadcast" begin
130
78
X1 = rand (Blocks (10 ), 100 )
131
79
X2 = X1 .* 3.4
138
86
139
87
@testset " distributing an array" begin
140
88
function test_dist (X)
141
- X1 = distribute (X, Blocks (10 , 20 ))
89
+ X1 = Distribute ( Blocks (10 , 20 ), X )
142
90
Xc = fetch (X1)
143
91
@test Xc isa DArray{eltype (X),ndims (X)}
144
92
@test Xc == X
147
95
@test map (x-> size (x) == (10 , 20 ), domainchunks (Xc)) |> all
148
96
end
149
97
x = [1 2 ; 3 4 ]
150
- @test distribute (x, Blocks (1 ,1 )) == x
98
+ @test Distribute ( Blocks (1 ,1 ), x ) == x
151
99
test_dist (rand (100 , 100 ))
152
100
test_dist (sprand (100 , 100 , 0.1 ))
153
101
174
122
@testset " matrix-matrix multiply" begin
175
123
function test_mul (X)
176
124
tol = 1e-12
177
- X1 = distribute (X, Blocks (10 , 20 ))
125
+ X1 = Distribute ( Blocks (10 , 20 ), X )
178
126
@test_throws DimensionMismatch X1* X1
179
127
X2 = X1' * X1
180
128
X3 = X1* X1'
188
136
test_mul (rand (40 , 40 ))
189
137
190
138
x = rand (10 ,10 )
191
- X = distribute (x, Blocks (3 ,3 ))
139
+ X = Distribute ( Blocks (3 ,3 ), x )
192
140
y = rand (10 )
193
141
@test norm (collect (X* y) - x* y) < 1e-13
194
142
end
@@ -202,24 +150,24 @@ end
202
150
203
151
@testset " concat" begin
204
152
m = rand (75 ,75 )
205
- x = distribute (m, Blocks (10 ,20 ))
206
- y = distribute (m, Blocks (10 ,10 ))
153
+ x = Distribute ( Blocks (10 ,20 ), m )
154
+ y = Distribute ( Blocks (10 ,10 ), m )
207
155
@test hcat (m,m) == collect (hcat (x,x)) == collect (hcat (x,y))
208
156
@test vcat (m,m) == collect (vcat (x,x))
209
157
@test_throws DimensionMismatch vcat (x,y)
210
158
end
211
159
212
160
@testset " scale" begin
213
161
x = rand (10 ,10 )
214
- X = distribute (x, Blocks (3 ,3 ))
162
+ X = Distribute ( Blocks (3 ,3 ), x )
215
163
y = rand (10 )
216
164
217
165
@test Diagonal (y)* x == collect (Diagonal (y)* X)
218
166
end
219
167
220
168
@testset " Getindex" begin
221
169
function test_getindex (x)
222
- X = distribute (x, Blocks (3 ,3 ))
170
+ X = Distribute ( Blocks (3 ,3 ), x )
223
171
@test collect (X[3 : 8 , 2 : 7 ]) == x[3 : 8 , 2 : 7 ]
224
172
ragged_idx = [1 ,2 ,9 ,7 ,6 ,2 ,4 ,5 ]
225
173
@test collect (X[ragged_idx, 2 : 7 ]) == x[ragged_idx, 2 : 7 ]
248
196
249
197
250
198
@testset " cleanup" begin
251
- X = distribute ( rand (10 ,10 ), Blocks (10 ,10 ))
199
+ X = Distribute ( Blocks (10 ,10 ), rand (10 ,10 ))
252
200
@test collect (sin .(X)) == collect (sin .(X))
253
201
end
254
202
269
217
x= rand (10 ,10 )
270
218
y= copy (x)
271
219
y[3 : 8 , 2 : 7 ] .= 1.0
272
- X = distribute (x, Blocks (3 ,3 ))
220
+ X = Distribute ( Blocks (3 ,3 ), x )
273
221
@test collect (setindex (X,1.0 , 3 : 8 , 2 : 7 )) == y
274
222
@test collect (X) == x
275
223
end
292
240
@test collect (sort (y)) == x
293
241
294
242
x = ones (10 )
295
- y = distribute (x, Blocks (3 ))
243
+ y = Distribute ( Blocks (3 ), x )
296
244
@test_broken map (x-> length (collect (x)), sort (y). chunks) == [3 ,3 ,3 ,1 ]
297
245
end
298
246
0 commit comments