diff --git a/test/array.jl b/test/array/core.jl similarity index 74% rename from test/array.jl rename to test/array/core.jl index 01764161e..36618a823 100644 --- a/test/array.jl +++ b/test/array/core.jl @@ -1,9 +1,3 @@ -using LinearAlgebra, SparseArrays, Random, SharedArrays -import Dagger: DArray, chunks, domainchunks, treereduce_nd -import Distributed: myid, procs -import Statistics: mean, var, std -import OnlineStats - @testset "treereduce_nd" begin xs = rand(1:10, 8,8,8) concats = [(x...)->cat(x..., dims=n) for n in 1:3] @@ -80,52 +74,6 @@ end end end -function test_mapreduce(f, init_func; no_init=true, zero_init=zero, - types=(Int32, Int64, Float32, Float64), - cmp=isapprox) - @testset "$T" for T in types - X = init_func(Blocks(10, 10), T, 100, 100) - inits = () - if no_init - inits = (inits..., nothing) - end - if zero_init !== nothing - inits = (inits..., zero_init(T)) - end - @testset "dims=$dims" for dims in (Colon(), 1, 2, (1,), (2,)) - @testset "init=$init" for init in inits - if init === nothing - if dims == Colon() - @test cmp(f(X; dims), f(collect(X); dims)) - else - @test cmp(collect(f(X; dims)), f(collect(X); dims)) - end - else - if dims == Colon() - @test cmp(f(X; dims, init), f(collect(X); dims, init)) - else - @test cmp(collect(f(X; dims, init)), f(collect(X); dims, init)) - end - end - end - end - end -end - -# Base -@testset "reduce" test_mapreduce((X; dims, init=Base._InitialValue())->reduce(+, X; dims, init), ones) -@testset "mapreduce" test_mapreduce((X; dims, init=Base._InitialValue())->mapreduce(x->x+1, +, X; dims, init), ones) -@testset "sum" test_mapreduce(sum, ones) -@testset "prod" test_mapreduce(prod, rand) -@testset "minimum" test_mapreduce(minimum, rand) -@testset "maximum" test_mapreduce(maximum, rand) -@testset "extrema" test_mapreduce(extrema, rand; cmp=Base.:(==), zero_init=T->(zero(T), zero(T))) - -# Statistics -@testset "mean" test_mapreduce(mean, rand; zero_init=nothing, types=(Float32, Float64)) -@testset "var" test_mapreduce(var, rand; zero_init=nothing, types=(Float32, Float64)) -@testset "std" test_mapreduce(std, rand; zero_init=nothing, types=(Float32, Float64)) - @testset "broadcast" begin X1 = rand(Blocks(10), 100) X2 = X1 .* 3.4 @@ -138,7 +86,7 @@ end @testset "distributing an array" begin function test_dist(X) - X1 = distribute(X, Blocks(10, 20)) + X1 = Distribute(Blocks(10, 20), X) Xc = fetch(X1) @test Xc isa DArray{eltype(X),ndims(X)} @test Xc == X @@ -147,7 +95,7 @@ end @test map(x->size(x) == (10, 20), domainchunks(Xc)) |> all end x = [1 2; 3 4] - @test distribute(x, Blocks(1,1)) == x + @test Distribute(Blocks(1,1), x) == x test_dist(rand(100, 100)) test_dist(sprand(100, 100, 0.1)) @@ -174,7 +122,7 @@ end @testset "matrix-matrix multiply" begin function test_mul(X) tol = 1e-12 - X1 = distribute(X, Blocks(10, 20)) + X1 = Distribute(Blocks(10, 20), X) @test_throws DimensionMismatch X1*X1 X2 = X1'*X1 X3 = X1*X1' @@ -188,7 +136,7 @@ end test_mul(rand(40, 40)) x = rand(10,10) - X = distribute(x, Blocks(3,3)) + X = Distribute(Blocks(3,3), x) y = rand(10) @test norm(collect(X*y) - x*y) < 1e-13 end @@ -202,8 +150,8 @@ end @testset "concat" begin m = rand(75,75) - x = distribute(m, Blocks(10,20)) - y = distribute(m, Blocks(10,10)) + x = Distribute(Blocks(10,20), m) + y = Distribute(Blocks(10,10), m) @test hcat(m,m) == collect(hcat(x,x)) == collect(hcat(x,y)) @test vcat(m,m) == collect(vcat(x,x)) @test_throws DimensionMismatch vcat(x,y) @@ -211,7 +159,7 @@ end @testset "scale" begin x = rand(10,10) - X = distribute(x, Blocks(3,3)) + X = Distribute(Blocks(3,3), x) y = rand(10) @test Diagonal(y)*x == collect(Diagonal(y)*X) @@ -219,7 +167,7 @@ end @testset "Getindex" begin function test_getindex(x) - X = distribute(x, Blocks(3,3)) + X = Distribute(Blocks(3,3), x) @test collect(X[3:8, 2:7]) == x[3:8, 2:7] ragged_idx = [1,2,9,7,6,2,4,5] @test collect(X[ragged_idx, 2:7]) == x[ragged_idx, 2:7] @@ -248,7 +196,7 @@ end @testset "cleanup" begin - X = distribute(rand(10,10), Blocks(10,10)) + X = Distribute(Blocks(10,10), rand(10,10)) @test collect(sin.(X)) == collect(sin.(X)) end @@ -269,7 +217,7 @@ end x=rand(10,10) y=copy(x) y[3:8, 2:7] .= 1.0 - X = distribute(x, Blocks(3,3)) + X = Distribute(Blocks(3,3), x) @test collect(setindex(X,1.0, 3:8, 2:7)) == y @test collect(X) == x end @@ -292,7 +240,7 @@ end @test collect(sort(y)) == x x = ones(10) - y = distribute(x, Blocks(3)) + y = Distribute(Blocks(3), x) @test_broken map(x->length(collect(x)), sort(y).chunks) == [3,3,3,1] end diff --git a/test/linalg.jl b/test/array/linalg.jl similarity index 99% rename from test/linalg.jl rename to test/array/linalg.jl index c66065e63..e8b0be13e 100644 --- a/test/linalg.jl +++ b/test/array/linalg.jl @@ -1,5 +1,3 @@ -using LinearAlgebra - @testset "Linear Algebra" begin @testset "GEMM: $T" for T in (Float32, Float64, ComplexF32, ComplexF64) A = rand(T, 128, 128) diff --git a/test/array/mapreduce.jl b/test/array/mapreduce.jl new file mode 100644 index 000000000..c7b207a28 --- /dev/null +++ b/test/array/mapreduce.jl @@ -0,0 +1,45 @@ +function test_mapreduce(f, init_func; no_init=true, zero_init=zero, + types=(Int32, Int64, Float32, Float64), + cmp=isapprox) + @testset "$T" for T in types + X = init_func(Blocks(10, 10), T, 100, 100) + inits = () + if no_init + inits = (inits..., nothing) + end + if zero_init !== nothing + inits = (inits..., zero_init(T)) + end + @testset "dims=$dims" for dims in (Colon(), 1, 2, (1,), (2,)) + @testset "init=$init" for init in inits + if init === nothing + if dims == Colon() + @test cmp(f(X; dims), f(collect(X); dims)) + else + @test cmp(collect(f(X; dims)), f(collect(X); dims)) + end + else + if dims == Colon() + @test cmp(f(X; dims, init), f(collect(X); dims, init)) + else + @test cmp(collect(f(X; dims, init)), f(collect(X); dims, init)) + end + end + end + end + end +end + +# Base +@testset "reduce" test_mapreduce((X; dims, init=Base._InitialValue())->reduce(+, X; dims, init), ones) +@testset "mapreduce" test_mapreduce((X; dims, init=Base._InitialValue())->mapreduce(x->x+1, +, X; dims, init), ones) +@testset "sum" test_mapreduce(sum, ones) +@testset "prod" test_mapreduce(prod, rand) +@testset "minimum" test_mapreduce(minimum, rand) +@testset "maximum" test_mapreduce(maximum, rand) +@testset "extrema" test_mapreduce(extrema, rand; cmp=Base.:(==), zero_init=T->(zero(T), zero(T))) + +# Statistics +@testset "mean" test_mapreduce(mean, rand; zero_init=nothing, types=(Float32, Float64)) +@testset "var" test_mapreduce(var, rand; zero_init=nothing, types=(Float32, Float64)) +@testset "std" test_mapreduce(std, rand; zero_init=nothing, types=(Float32, Float64)) diff --git a/test/imports.jl b/test/imports.jl new file mode 100644 index 000000000..dad3d02b2 --- /dev/null +++ b/test/imports.jl @@ -0,0 +1,5 @@ +using LinearAlgebra, SparseArrays, Random, SharedArrays +import Dagger: DArray, chunks, domainchunks, treereduce_nd +import Distributed: myid, procs +import Statistics: mean, var, std +import OnlineStats diff --git a/test/runtests.jl b/test/runtests.jl index 9dd3910b0..b82e69c73 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,8 +11,9 @@ tests = [ ("Task Queues", "task-queues.jl"), ("Datadeps", "datadeps.jl"), ("Domain Utilities", "domain.jl"), - ("Array", "array.jl"), - ("Linear Algebra", "linalg.jl"), + ("Array - Core", "array/core.jl"), + ("Array - MapReduce", "array/mapreduce.jl"), + ("Array - LinearAlgebra", "array/linalg.jl"), ("Caching", "cache.jl"), ("Disk Caching", "diskcaching.jl"), ("File IO", "file-io.jl"), @@ -70,6 +71,7 @@ end using Distributed addprocs(3) +include("imports.jl") include("util.jl") include("fakeproc.jl")